feat: move npm workaround for missing gitHead to the npm plugin
This commit is contained in:
+22
-27
@@ -1,8 +1,8 @@
|
||||
const gitLogParser = require('git-log-parser');
|
||||
const getStream = require('get-stream');
|
||||
const debug = require('debug')('semantic-release:get-commits');
|
||||
const {unshallow} = require('./git');
|
||||
const getVersionHead = require('./get-version-head');
|
||||
const SemanticReleaseError = require('@semantic-release/error');
|
||||
const {unshallow, gitCommitTag, gitTagHead, isCommitInHistory} = require('./git');
|
||||
|
||||
/**
|
||||
* Commit message.
|
||||
@@ -44,22 +44,28 @@ const getVersionHead = require('./get-version-head');
|
||||
* @return {Promise<Result>} The list of commits on the branch `branch` since the last release and the updated lastRelease with the gitHead used to retrieve the commits.
|
||||
*
|
||||
* @throws {SemanticReleaseError} with code `ENOTINHISTORY` if `lastRelease.gitHead` or the commit sha derived from `config.lastRelease.version` is not in the direct history of `branch`.
|
||||
* @throws {SemanticReleaseError} with code `ENOGITHEAD` if `lastRelease.gitHead` is undefined and no commit sha can be found for the `config.lastRelease.version`.
|
||||
*/
|
||||
module.exports = async ({version, gitHead} = {}, branch, logger) => {
|
||||
let gitTag;
|
||||
if (gitHead || version) {
|
||||
try {
|
||||
({gitHead, gitTag} = await getVersionHead(gitHead, version, branch));
|
||||
} catch (err) {
|
||||
if (err.code === 'ENOTINHISTORY') {
|
||||
logger.error(notInHistoryMessage(err.gitHead, branch, version));
|
||||
} else {
|
||||
logger.error(noGitHeadMessage(branch, version));
|
||||
}
|
||||
throw err;
|
||||
if (gitHead) {
|
||||
// If gitHead doesn't exists in release branch
|
||||
if (!await isCommitInHistory(gitHead)) {
|
||||
// Unshallow the repository
|
||||
await unshallow();
|
||||
}
|
||||
logger.log('Retrieving commits since %s, corresponding to version %s', gitHead, version);
|
||||
// If gitHead still doesn't exists in release branch
|
||||
if (!await isCommitInHistory(gitHead)) {
|
||||
// Try to find the commit corresponding to the version, using got tags
|
||||
const tagHead = (await gitTagHead(`v${version}`)) || (await gitTagHead(version));
|
||||
|
||||
// If tagHead doesn't exists in release branch
|
||||
if (!tagHead || !await isCommitInHistory(tagHead)) {
|
||||
// Then the commit corresponding to the version cannot be found in the bracnh hsitory
|
||||
logger.error(notInHistoryMessage(gitHead, branch, version));
|
||||
throw new SemanticReleaseError('Commit not in history', 'ENOTINHISTORY');
|
||||
}
|
||||
gitHead = tagHead;
|
||||
}
|
||||
debug('Use gitHead: %s', gitHead);
|
||||
} else {
|
||||
logger.log('No previous release found, retrieving all commits');
|
||||
// If there is no gitHead nor a version, there is no previous release. Unshallow the repo in order to retrieve all commits
|
||||
@@ -76,20 +82,9 @@ module.exports = async ({version, gitHead} = {}, branch, logger) => {
|
||||
);
|
||||
logger.log('Found %s commits since last release', commits.length);
|
||||
debug('Parsed commits: %o', commits);
|
||||
return {commits, lastRelease: {version, gitHead, gitTag}};
|
||||
return {commits, lastRelease: {version, gitHead, gitTag: await gitCommitTag(gitHead)}};
|
||||
};
|
||||
|
||||
function noGitHeadMessage(branch, version) {
|
||||
return `The commit the last release of this package was derived from cannot be determined from the release metadata nor 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 creating a tag for the version "${version}" on the commit corresponding to this release:
|
||||
$ git tag -f v${version} <commit sha1 corresponding to last release>
|
||||
$ git push -f --tags origin ${branch}
|
||||
`;
|
||||
}
|
||||
|
||||
function notInHistoryMessage(gitHead, branch, version) {
|
||||
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.
|
||||
|
||||
Reference in New Issue
Block a user