Skip to content

Commit

Permalink
fix: verify is branch is up to date by comparing remote and local HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdlg committed Dec 10, 2019
1 parent 9a1af4d commit a8747c4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 57 deletions.
38 changes: 4 additions & 34 deletions lib/git.js
Expand Up @@ -69,33 +69,6 @@ async function getBranches(repositoryUrl, execaOpts) {
.filter(Boolean);
}

/**
* 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 {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, execaOpts) {
if (!(await isRefExists(branch, execaOpts))) {
return false;
}

try {
await execa('git', ['merge-base', '--is-ancestor', ref, branch], execaOpts);
return true;
} catch (error) {
if (error.exitCode === 1) {
return false;
}

debug(error);
throw error;
}
}

/**
* Verify if the `ref` exits
*
Expand Down Expand Up @@ -310,12 +283,10 @@ async function verifyBranchName(branch, execaOpts) {
* @return {Boolean} `true` is the HEAD of the current local branch is the same as the HEAD of the remote branch, falsy otherwise.
*/
async function isBranchUpToDate(repositoryUrl, branch, execaOpts) {
const {stdout: remoteHead} = await execa('git', ['ls-remote', '--heads', repositoryUrl, branch], execaOpts);
try {
return await isRefInHistory(remoteHead.match(/^(?<ref>\w+)?/)[1], branch, execaOpts);
} catch (error) {
debug(error);
}
return (
(await getGitHead(execaOpts)) ===
(await execa('git', ['ls-remote', '--heads', repositoryUrl, branch], execaOpts)).stdout.match(/^(?<ref>\w+)?/)[1]
);
}

/**
Expand Down Expand Up @@ -355,7 +326,6 @@ module.exports = {
getTags,
getCommits,
getBranches,
isRefInHistory,
isRefExists,
fetch,
fetchNotes,
Expand Down
31 changes: 8 additions & 23 deletions test/git.test.js
Expand Up @@ -2,7 +2,6 @@ import test from 'ava';
import tempy from 'tempy';
import {
getTagHead,
isRefInHistory,
isRefExists,
fetch,
getGitHead,
Expand Down Expand Up @@ -125,23 +124,6 @@ test('Fetch all tags on a repository with a detached head from branch', async t
t.deepEqual((await getTags('master', {cwd})).sort(), ['v1.0.0', 'v1.0.1', 'v1.1.0', 'v2.0.0'].sort());
});

test('Verify if the commit `sha` is in the direct history of the current branch', async t => {
// Create a git repository, set the current working directory at the root of the repo
const {cwd} = await gitRepo();
// Add commits to the master branch
const commits = await gitCommits(['First'], {cwd});
// Create the new branch 'other-branch' from master
await gitCheckout('other-branch', true, {cwd});
// Add commits to the 'other-branch' branch
const otherCommits = await gitCommits(['Second'], {cwd});
await gitCheckout('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 => {
// Create a git repository, set the current working directory at the root of the repo
const {cwd} = await gitRepo();
Expand Down Expand Up @@ -299,13 +281,16 @@ test('Return falsy if repository is not up to date', async t => {
t.falsy(await isBranchUpToDate(repositoryUrl, 'master', {cwd}));
});

test('Return "true" if local repository is ahead', async t => {
const {cwd, repositoryUrl} = await gitRepo(true);
await gitCommits(['First'], {cwd});
await gitPush(repositoryUrl, 'master', {cwd});
test('Return falsy if detached head repository is not up to date', async t => {
let {cwd, repositoryUrl} = await gitRepo();

const [commit] = await gitCommits(['First'], {cwd});
await gitCommits(['Second'], {cwd});
await gitPush(repositoryUrl, 'master', {cwd});
cwd = await gitDetachedHead(repositoryUrl, commit.hash);
await fetch(repositoryUrl, 'master', 'master', {cwd});

t.true(await isBranchUpToDate(repositoryUrl, 'master', {cwd}));
t.falsy(await isBranchUpToDate(repositoryUrl, 'master', {cwd}));
});

test('Get a commit note', async t => {
Expand Down

0 comments on commit a8747c4

Please sign in to comment.