Skip to content

Commit

Permalink
feat: Retrieve version gitHead from git tags and unshallow the repo i…
Browse files Browse the repository at this point in the history
…s necessary

Add several fix and improvement in the identification of the last release gitHead:
- If there is no last release, unshallow the repo in order to retrieve all existing commits
- If git head is not present in last release, try to retrieve it from git tag with format 'v<version>' or '<version>'
- If the git head cannot be found in commit history, unshallow the repo and try again
- Throw a ENOGITHEAD error if the gitHead for the last release cannot be found in the npm metadata nor in the git tags, preventing to make release based on the all the commits in the repo as before
- Add integration test for the scenario with a packed repo from which `npm republish` fails to read the git head

Fix #447, Fix #393, Fix #280, Fix #276
  • Loading branch information
pvdlg committed Sep 30, 2017
1 parent a58d12d commit e06f6d7
Show file tree
Hide file tree
Showing 5 changed files with 390 additions and 62 deletions.
112 changes: 74 additions & 38 deletions src/lib/get-commits.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,92 @@
const execa = require('execa');
const log = require('npmlog');
const SemanticReleaseError = require('@semantic-release/error');
const getVersionHead = require('./get-version-head');

module.exports = async ({lastRelease, options}) => {
let stdout;
if (lastRelease.gitHead) {
/**
* Commit message.
*
* @typedef {Object} Commit
* @property {string} hash The commit hash.
* @property {string} message The commit message.
*/

/**
* Retrieve the list of commits on the current branch since the last released version, or all the commits of the current branch if there is no last released version.
*
* The commit correspoding to the last released version is determined as follow:
* - Use `lastRelease.gitHead` is defined and present in `config.options.branch` history.
* - Search for a tag named `v<version>` or `<version>` and it's associated commit sha if present in `config.options.branch` history.
*
* If a commit corresponding to the last released is not found, unshallow the repository (as most CI create a shallow clone with limited number of commits and no tags) and try again.
*
* @param {Object} config
* @param {Object} config.lastRelease The lastRelease object obtained from the getLastRelease plugin.
* @param {string} [config.lastRelease.version] The version number of the last release.
* @param {string} [config.lastRelease.gitHead] The commit sha used to make the last release.
* @param {Object} config.options The semantic-relese options.
* @param {string} config.options.branch The branch to release from.
*
* @return {Promise<Array<Commit>>} The list of commits on the branch `config.options.branch` since the last release.
*
* @throws {SemanticReleaseError} with code `ENOTINHISTORY` if `config.lastRelease.gitHead` or the commit sha derived from `config.lastRelease.version` is not in the direct history of `config.options.branch`.
* @throws {SemanticReleaseError} with code `ENOGITHEAD` if `config.lastRelease.gitHead` is undefined and no commit sha can be found for the `config.lastRelease.version`.
*/
module.exports = async ({lastRelease: {version, gitHead}, options: {branch}}) => {
if (gitHead || version) {
try {
({stdout} = await execa('git', ['branch', '--no-color', '--contains', lastRelease.gitHead]));
gitHead = await getVersionHead(version, branch, gitHead);
} catch (err) {
throw notInHistoryError(lastRelease.gitHead, options.branch);
// Unshallow the repository if the gitHead cannot be found and the branch for the last release version
await execa('git', ['fetch', '--unshallow', '--tags'], {reject: false});
}
const branches = stdout
.split('\n')
.map(branch => branch.replace('*', '').trim())
.filter(branch => !!branch);

if (!branches.includes(options.branch)) {
throw notInHistoryError(lastRelease.gitHead, options.branch, branches);
// Try to find the gitHead on the branch again with an unshallowed repository
try {
gitHead = await getVersionHead(version, branch, gitHead);
} catch (err) {
if (err.code === 'ENOTINHISTORY') {
log.error('commits', notInHistoryMessage(gitHead, branch, version, err.branches));
} else if (err.code === 'ENOGITHEAD') {
log.error('commits', noGitHeadMessage());
}
throw err;
}
} else {
// If there is no gitHead nor a version, there is no previous release. Unshallow the repo in order to retrieve all commits
await execa('git', ['fetch', '--unshallow', '--tags'], {reject: false});
}

try {
({stdout} = await execa('git', [
return (await execa('git', [
'log',
'--format=%H==SPLIT==%B==END==',
`${lastRelease.gitHead ? lastRelease.gitHead + '..' : ''}HEAD`,
]));
'--format=format:%H==SPLIT==%B==END==',
`${gitHead ? gitHead + '..' : ''}HEAD`,
])).stdout
.split('==END==')
.filter(raw => !!raw.trim())
.map(raw => {
const [hash, message] = raw.trim().split('==SPLIT==');
return {hash, message};
});
} catch (err) {
return [];
}

return String(stdout)
.split('==END==')
.filter(raw => !!raw.trim())
.map(raw => {
const [hash, message] = raw.trim().split('==SPLIT==');
return {hash, message};
});
};

function notInHistoryError(gitHead, branch, branches) {
log.error(
'commits',
`
The commit the last release of this package was derived from is not in the direct history of the "${branch}" branch.
This means semantic-release can not extract the commits between now and then.
This is usually caused by force pushing, releasing from an unrelated branch, or using an already existing package name.
You can recover from this error by publishing manually or restoring the commit "${gitHead}".
${branches && branches.length
? `\nHere is a list of branches that still contain the commit in question: \n * ${branches.join('\n * ')}`
: ''}
`
);
return new SemanticReleaseError('Commit not in history', 'ENOTINHISTORY');
function noGitHeadMessage(version) {
return `The commit the last release of this package was derived from cannot be determined from the release metadata not from the repository tags.
This means semantic-release can not extract the commits between now and then.
This is usually caused by releasing from outside the repository directory or with innaccessible git metadata.
You can recover from this error by publishing manually.`;
}

function notInHistoryMessage(gitHead, branch, version, branches) {
return `The commit the last release of this package was derived from is not in the direct history of the "${branch}" branch.
This means semantic-release can not extract the commits between now and then.
This is usually caused by force pushing, releasing from an unrelated branch, or using an already existing package name.
You can recover from this error by publishing manually or restoring the commit "${gitHead}".
${branches && branches.length
? `Here is a list of branches that still contain the commit in question: \n * ${branches.join('\n * ')}`
: ''}`;
}
67 changes: 67 additions & 0 deletions src/lib/get-version-head.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const SemanticReleaseError = require('@semantic-release/error');
const execa = require('execa');

/**
* Get the commit sha for a given tag.
*
* @param {string} tagName Tag name for which to retrieve the commit sha.
*
* @return {string} The commit sha of the tag in parameter or `null`.
*/
async function gitTagHead(tagName) {
try {
return (await execa('git', ['rev-list', '-1', '--tags', tagName])).stdout;
} catch (err) {
return null;
}
}

/**
* Get the list of branches that contains the given commit.
*
* @param {string} sha The sha of the commit to look for.
*
* @return {Array<string>} The list of branches that contains the commit sha in parameter.
*/
async function getCommitBranches(sha) {
try {
return (await execa('git', ['branch', '--no-color', '--contains', sha])).stdout
.split('\n')
.map(branch => branch.replace('*', '').trim())
.filter(branch => !!branch);
} catch (err) {
return [];
}
}

/**
* Get the commit sha for a given version, if it is contained in the given branch.
*
* @param {string} version The version corresponding to the commit sha to look for. Used to search in git tags.
* @param {string} branch The branch that must have the commit in its direct history.
* @param {string} gitHead The commit sha to verify.
*
* @return {Promise<string>} A Promise that resolves to `gitHead` if defined and if present in branch direct history or the commit sha corresponding to `version`.
*
* @throws {SemanticReleaseError} with code `ENOTINHISTORY` if `gitHead` or the commit sha dereived from `version` is not in the direct history of `branch`. The Error will have a `branches` attributes with the list of branches containing the commit.
* @throws {SemanticReleaseError} with code `ENOGITHEAD` if `gitHead` is undefined and no commit sha can be found for the `version`.
*/
module.exports = async (version, branch, gitHead) => {
if (!gitHead && version) {
// Look for the version tag only if no gitHead exists
gitHead = (await gitTagHead(`v${version}`)) || (await gitTagHead(version));
}

if (gitHead) {
// Retrieve the branches containing the gitHead and verify one of them is the branch in param
const branches = await getCommitBranches(gitHead);
if (!branches.includes(branch)) {
const error = new SemanticReleaseError('Commit not in history', 'ENOTINHISTORY');
error.branches = branches;
throw error;
}
} else {
throw new SemanticReleaseError('There is no commit associated with last release', 'ENOGITHEAD');
}
return gitHead;
};
124 changes: 116 additions & 8 deletions test/get-commits.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import {gitRepo, gitCommits, gitCheckout} from './helpers/git-utils';
import {gitRepo, gitCommits, gitCheckout, gitTagVersion, gitShallowClone, gitTags, gitLog} from './helpers/git-utils';
import proxyquire from 'proxyquire';
import {stub} from 'sinon';
import SemanticReleaseError from '@semantic-release/error';
Expand Down Expand Up @@ -30,15 +30,37 @@ test.serial('Get all commits when there is no last release', async t => {
// Retrieve the commits with the commits module
const result = await getCommits({lastRelease: {}, options: {branch: 'master'}});

// The commits created and and retrieved by the module are identical
// Verify the commits created and retrieved by the module are identical
t.is(result.length, 2);
t.is(result[0].hash.substring(0, 7), commits[0].hash);
t.is(result[0].message, commits[0].message);
t.is(result[1].hash.substring(0, 7), commits[1].hash);
t.is(result[1].message, commits[1].message);
});

test.serial('Get all commits since lastRelease gitHead', async t => {
test.serial('Get all commits when there is no last release, including the ones not in the shallow clone', async t => {
// Create a git repository, set the current working directory at the root of the repo
const repo = await gitRepo();
// Add commits to the master branch
const commits = await gitCommits(['fix: First fix', 'feat: Second feature']);
// Create a shallow clone with only 1 commit
await gitShallowClone(repo);

// Verify the shallow clone contains only one commit
t.is((await gitLog()).length, 1);

// Retrieve the commits with the commits module
const result = await getCommits({lastRelease: {}, options: {branch: 'master'}});

// Verify the commits created and retrieved by the module are identical
t.is(result.length, 2);
t.is(result[0].hash.substring(0, 7), commits[0].hash);
t.is(result[0].message, commits[0].message);
t.is(result[1].hash.substring(0, 7), commits[1].hash);
t.is(result[1].message, commits[1].message);
});

test.serial('Get all commits since gitHead (from lastRelease)', async t => {
// Create a git repository, set the current working directory at the root of the repo
await gitRepo();
// Add commits to the master branch
Expand All @@ -49,7 +71,76 @@ test.serial('Get all commits since lastRelease gitHead', async t => {
lastRelease: {gitHead: commits[commits.length - 1].hash},
options: {branch: 'master'},
});
// The commits created and retrieved by the module are identical

// Verify the commits created and retrieved by the module are identical
t.is(result.length, 2);
t.is(result[0].hash.substring(0, 7), commits[0].hash);
t.is(result[0].message, commits[0].message);
t.is(result[1].hash.substring(0, 7), commits[1].hash);
t.is(result[1].message, commits[1].message);
});

test.serial('Get all commits since gitHead (from tag) ', async t => {
// Create a git repository, set the current working directory at the root of the repo
await gitRepo();
// Add commits to the master branch
let commits = await gitCommits(['fix: First fix']);
// Create the tag corresponding to version 1.0.0
await gitTagVersion('1.0.0');
// Add new commits to the master branch
commits = (await gitCommits(['feat: Second feature', 'feat: Third feature'])).concat(commits);

// Retrieve the commits with the commits module
const result = await getCommits({lastRelease: {version: `1.0.0`}, options: {branch: 'master'}});

// Verify the commits created and retrieved by the module are identical
t.is(result.length, 2);
t.is(result[0].hash.substring(0, 7), commits[0].hash);
t.is(result[0].message, commits[0].message);
t.is(result[1].hash.substring(0, 7), commits[1].hash);
t.is(result[1].message, commits[1].message);
});

test.serial('Get all commits since gitHead (from tag formatted like v<version>) ', async t => {
// Create a git repository, set the current working directory at the root of the repo
await gitRepo();
// Add commits to the master branch
let commits = await gitCommits(['fix: First fix']);
// Create the tag corresponding to version 1.0.0
await gitTagVersion('v1.0.0');
// Add new commits to the master branch
commits = (await gitCommits(['feat: Second feature', 'feat: Third feature'])).concat(commits);

// Retrieve the commits with the commits module
const result = await getCommits({lastRelease: {version: `1.0.0`}, options: {branch: 'master'}});

// Verify the commits created and retrieved by the module are identical
t.is(result.length, 2);
t.is(result[0].hash.substring(0, 7), commits[0].hash);
t.is(result[0].message, commits[0].message);
t.is(result[1].hash.substring(0, 7), commits[1].hash);
t.is(result[1].message, commits[1].message);
});

test.serial('Get all commits since gitHead from tag, when tags are mising from the shallow clone', async t => {
// Create a git repository, set the current working directory at the root of the repo
const repo = await gitRepo();
// Add commits to the master branch
let commits = await gitCommits(['fix: First fix']);
// Create the tag corresponding to version 1.0.0
await gitTagVersion('v1.0.0');
// Add new commits to the master branch
commits = (await gitCommits(['feat: Second feature', 'feat: Third feature'])).concat(commits);
// Create a shallow clone with only 1 commit and no tags
await gitShallowClone(repo);

// Verify the shallow clone does not contains any tags
t.is((await gitTags()).length, 0);

// Retrieve the commits with the commits module
const result = await getCommits({lastRelease: {version: `1.0.0`}, options: {branch: 'master'}});

// Verify the commits created and retrieved by the module are identical
t.is(result.length, 2);
t.is(result[0].hash.substring(0, 7), commits[0].hash);
t.is(result[0].message, commits[0].message);
Expand Down Expand Up @@ -81,6 +172,25 @@ test.serial('Return empty array if lastRelease.gitHead is the last commit', asyn
t.deepEqual(result, []);
});

test.serial('Throws ENOGITHEAD error if the gitHead of the last release cannot be found', async t => {
// Create a git repository, set the current working directory at the root of the repo
await gitRepo();
// Add commits to the master branch
await gitCommits(['fix: First fix', 'feat: Second feature']);

// Retrieve the commits with the commits module
const error = await t.throws(getCommits({lastRelease: {version: '1.0.0'}, options: {branch: 'master'}}));

// Verify error code and message
t.is(error.code, 'ENOGITHEAD');
t.true(error instanceof SemanticReleaseError);
// Verify the log function has been called with a message explaining the error
t.regex(
errorLog.firstCall.args[1],
/The commit the last release of this package was derived from cannot be determined from the release metadata not from the repository tags/
);
});

test.serial('Throws ENOTINHISTORY error if gitHead is not in history', async t => {
// Create a git repository, set the current working directory at the root of the repo
await gitRepo();
Expand All @@ -93,7 +203,6 @@ test.serial('Throws ENOTINHISTORY error if gitHead is not in history', async t =
// Verify error code and message
t.is(error.code, 'ENOTINHISTORY');
t.true(error instanceof SemanticReleaseError);

// Verify the log function has been called with a message mentionning the branch
t.regex(errorLog.firstCall.args[1], /history of the "master" branch/);
// Verify the log function has been called with a message mentionning the missing gitHead
Expand All @@ -106,11 +215,11 @@ test.serial('Throws ENOTINHISTORY error if gitHead is not in branch history but
// Add commits to the master branch
await gitCommits(['First', 'Second']);
// Create the new branch 'other-branch' from master
await gitCheckout('other-branch', true);
await gitCheckout('other-branch');
// Add commits to the 'other-branch' branch
const commitsBranch = await gitCommits(['Third', 'Fourth']);
// Create the new branch 'another-branch' from 'other-branch'
await gitCheckout('another-branch', true);
await gitCheckout('another-branch');

// Retrieve the commits with the commits module
const error = await t.throws(
Expand All @@ -120,7 +229,6 @@ test.serial('Throws ENOTINHISTORY error if gitHead is not in branch history but
// Verify error code and message
t.is(error.code, 'ENOTINHISTORY');
t.true(error instanceof SemanticReleaseError);

// Verify the log function has been called with a message mentionning the branch
t.regex(errorLog.firstCall.args[1], /history of the "master" branch/);
// Verify the log function has been called with a message mentionning the missing gitHead
Expand Down

0 comments on commit e06f6d7

Please sign in to comment.