From 844e0b07e04754c8185d9d88523c8afc236de02a Mon Sep 17 00:00:00 2001 From: Pierre Vanduynslager Date: Fri, 1 Nov 2019 16:40:04 -0400 Subject: [PATCH] fix: remove hack to workaround GitHub Rebase & Merge --- lib/branches/get-tags.js | 2 +- lib/git.js | 17 ++--------------- test/branches/get-tags.test.js | 27 +-------------------------- test/git.test.js | 8 ++++---- test/helpers/git-utils.js | 13 ------------- 5 files changed, 8 insertions(+), 59 deletions(-) diff --git a/lib/branches/get-tags.js b/lib/branches/get-tags.js index 194f25de00..d732d1a920 100644 --- a/lib/branches/get-tags.js +++ b/lib/branches/get-tags.js @@ -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, [] diff --git a/lib/git.js b/lib/git.js index 099ae110c0..c776804c98 100644 --- a/lib/git.js +++ b/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'); @@ -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; } @@ -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; } @@ -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); } diff --git a/test/branches/get-tags.test.js b/test/branches/get-tags.test.js index 0ebe140dc9..78195dd733 100644 --- a/test/branches/get-tags.test.js +++ b/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(); @@ -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}], - }, - ]); -}); diff --git a/test/git.test.js b/test/git.test.js index d804966d81..813844f3f6 100644 --- a/test/git.test.js +++ b/test/git.test.js @@ -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 => { diff --git a/test/helpers/git-utils.js b/test/helpers/git-utils.js index 375e003516..2019d561b8 100644 --- a/test/helpers/git-utils.js +++ b/test/helpers/git-utils.js @@ -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 - ); -}