Skip to content

Commit

Permalink
fix: remove hack to workaround GitHub Rebase & Merge
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdlg committed Nov 1, 2019
1 parent 1edae67 commit 844e0b0
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 59 deletions.
2 changes: 1 addition & 1 deletion lib/branches/get-tags.js
Expand Up @@ -25,7 +25,7 @@ module.exports = async ({cwd, env, options: {tagFormat}}, branches) => {
const branchTags = await pReduce(
tags,
async (tags, {gitTag, ...rest}) =>
(await isRefInHistory(gitTag, branch.name, true, {cwd, env}))
(await isRefInHistory(gitTag, branch.name, {cwd, env}))
? [...tags, {...rest, gitTag, gitHead: await getTagHead(gitTag, {cwd, env})}]
: tags,
[]
Expand Down
17 changes: 2 additions & 15 deletions lib/git.js
@@ -1,4 +1,3 @@
const {matches, pick, memoize} = require('lodash');
const gitLogParser = require('git-log-parser');
const getStream = require('get-stream');
const execa = require('execa');
Expand Down Expand Up @@ -70,21 +69,16 @@ async function getBranches(repositoryUrl, execaOpts) {
.filter(Boolean);
}

const getBranchCommits = memoize((branch, execaOpts) =>
getStream.array(gitLogParser.parse({_: branch}, {cwd: execaOpts.cwd, env: {...process.env, ...execaOpts.env}}))
);

/**
* Verify if the `ref` is in the direct history of a given branch.
*
* @param {String} ref The reference to look for.
* @param {String} branch The branch for which to check if the `ref` is in history.
* @param {Boolean} findRebasedTags Weither consider in history tags associated with a commit that was rebased to another branch (i.e. GitHub Rebase and Merge feature).
* @param {Object} [execaOpts] Options to pass to `execa`.
*
* @return {Boolean} `true` if the reference is in the history of the current branch, falsy otherwise.
*/
async function isRefInHistory(ref, branch, findRebasedTags, execaOpts) {
async function isRefInHistory(ref, branch, execaOpts) {
if (!(await isRefExists(branch, execaOpts))) {
return false;
}
Expand All @@ -94,13 +88,6 @@ async function isRefInHistory(ref, branch, findRebasedTags, execaOpts) {
return true;
} catch (error) {
if (error.exitCode === 1) {
if (findRebasedTags) {
const [tagCommit] = await getStream.array(
gitLogParser.parse({_: ref, n: '1'}, {cwd: execaOpts.cwd, env: {...process.env, ...execaOpts.env}})
);
return (await getBranchCommits(branch, execaOpts)).some(matches(pick(tagCommit, ['message', 'author'])));
}

return false;
}

Expand Down Expand Up @@ -288,7 +275,7 @@ async function verifyBranchName(branch, execaOpts) {
async function isBranchUpToDate(repositoryUrl, branch, execaOpts) {
const {stdout: remoteHead} = await execa('git', ['ls-remote', '--heads', repositoryUrl, branch], execaOpts);
try {
return await isRefInHistory(remoteHead.match(/^(\w+)?/)[1], branch, false, execaOpts);
return await isRefInHistory(remoteHead.match(/^(\w+)?/)[1], branch, execaOpts);
} catch (error) {
debug(error);
}
Expand Down
27 changes: 1 addition & 26 deletions test/branches/get-tags.test.js
@@ -1,6 +1,6 @@
import test from 'ava';
import getTags from '../../lib/branches/get-tags';
import {gitRepo, gitCommits, gitTagVersion, gitCheckout, merge, changeAuthor} from '../helpers/git-utils';
import {gitRepo, gitCommits, gitTagVersion, gitCheckout} from '../helpers/git-utils';

test('Get the valid tags', async t => {
const {cwd} = await gitRepo();
Expand Down Expand Up @@ -175,28 +175,3 @@ test('Get the highest valid tag corresponding to the "tagFormat"', async t => {
{name: 'master', tags: [{gitTag: '3.0.0-bar.2', version: '3.0.0', channel: undefined, gitHead: commits[0].hash}]},
]);
});

test('Get the tag on branch where commits have been rebased', async t => {
const {cwd} = await gitRepo();
const commits = await gitCommits(['First'], {cwd});
await gitCheckout('next', true, {cwd});
commits.push(...(await gitCommits(['Second/n/n/commit body'], {cwd})));
await gitTagVersion('v1.0.0@next', undefined, {cwd});
await gitCheckout('master', false, {cwd});
await merge('next', {cwd});
// Simulate GitHub "Rebase and Merge" by changing the committer info, which will result in a new commit sha and losing the tag
await changeAuthor(commits[1].hash, {cwd});

const result = await getTags({cwd, options: {tagFormat: `v\${version}`}}, [{name: 'master'}, {name: 'next'}]);

t.deepEqual(result, [
{
name: 'master',
tags: [{gitTag: 'v1.0.0@next', version: '1.0.0', channel: 'next', gitHead: commits[1].hash}],
},
{
name: 'next',
tags: [{gitTag: 'v1.0.0@next', version: '1.0.0', channel: 'next', gitHead: commits[1].hash}],
},
]);
});
8 changes: 4 additions & 4 deletions test/git.test.js
Expand Up @@ -105,10 +105,10 @@ test('Verify if the commit `sha` is in the direct history of the current branch'
const otherCommits = await gitCommits(['Second'], {cwd});
await gitCheckout('master', false, {cwd});

t.true(await isRefInHistory(commits[0].hash, 'master', false, {cwd}));
t.falsy(await isRefInHistory(otherCommits[0].hash, 'master', false, {cwd}));
t.falsy(await isRefInHistory(otherCommits[0].hash, 'missing-branch', false, {cwd}));
await t.throwsAsync(isRefInHistory('non-existant-sha', 'master', false, {cwd}));
t.true(await isRefInHistory(commits[0].hash, 'master', {cwd}));
t.falsy(await isRefInHistory(otherCommits[0].hash, 'master', {cwd}));
t.falsy(await isRefInHistory(otherCommits[0].hash, 'missing-branch', {cwd}));
await t.throwsAsync(isRefInHistory('non-existant-sha', 'master', {cwd}));
});

test('Verify if a branch exists', async t => {
Expand Down
13 changes: 0 additions & 13 deletions test/helpers/git-utils.js
Expand Up @@ -256,16 +256,3 @@ export async function mergeFf(ref, execaOpts) {
export async function rebase(ref, execaOpts) {
await execa('git', ['rebase', ref], execaOpts);
}

export async function changeAuthor(sha, execaOpts) {
await execa(
'git',
[
'filter-branch',
'-f',
'--env-filter',
`if [[ "$GIT_COMMIT" = "${sha}" ]]; then export GIT_COMMITTER_NAME="New Author" GIT_COMMITTER_EMAIL="author@test.com"; fi`,
],
execaOpts
);
}

0 comments on commit 844e0b0

Please sign in to comment.