Skip to content

Commit

Permalink
feat: make exit 0 possible when commits required but missing (#984)
Browse files Browse the repository at this point in the history
* feat: make exit 0 possible when commits required but missing

* fix: removed dummy code

---------

Co-authored-by: Lars Kappert <lars@webpro.nl>
  • Loading branch information
b12k and webpro committed Mar 1, 2023
1 parent 81a7d69 commit 7076fd3
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion bin/release-it.js
Expand Up @@ -38,5 +38,5 @@ const options = parseCliArguments([].slice.call(process.argv, 2));
updater({ pkg: pkg }).notify();
release(options).then(
() => process.exit(0),
() => process.exit(1)
({ code }) => process.exit(Number.isInteger(code) ? code : 1)
);
1 change: 1 addition & 0 deletions config/release-it.json
Expand Up @@ -6,6 +6,7 @@
"requireBranch": false,
"requireUpstream": true,
"requireCommits": false,
"requireCommitsFail": true,
"commitsPath": "",
"addUntrackedFiles": false,
"commit": true,
Expand Down
2 changes: 1 addition & 1 deletion lib/plugin/git/Git.js
Expand Up @@ -47,7 +47,7 @@ class Git extends GitBase {
throw e(`No upstream configured for current branch.${EOL}Please set an upstream branch.`, docs);
}
if (this.options.requireCommits && (await this.getCommitsSinceLatestTag(this.options.commitsPath)) === 0) {
throw e(`There are no commits since the latest tag.`, docs);
throw e(`There are no commits since the latest tag.`, docs, this.options.requireCommitsFail);
}
}

Expand Down
6 changes: 5 additions & 1 deletion lib/util.js
Expand Up @@ -89,7 +89,11 @@ const parseVersion = raw => {
};
};

const e = (message, docs) => new Error(docs ? `${message}${EOL}Documentation: ${docs}${EOL}` : message);
const e = (message, docs, fail = true) => {
const error = new Error(docs ? `${message}${EOL}Documentation: ${docs}${EOL}` : message);
error.code = fail ? 1 : 0;
return error;
};

export {
getSystemInfo,
Expand Down
14 changes: 14 additions & 0 deletions test/git.init.js
Expand Up @@ -72,6 +72,20 @@ test.serial('should not throw if there are commits', async t => {
await t.notThrowsAsync(gitClient.init());
});

test.serial('should fail (exit code 1) if there are no commits', async t => {
const options = { git: { requireCommits: true } };
const gitClient = factory(Git, { options });
sh.exec('git tag 1.0.0');
await t.throwsAsync(gitClient.init(), { code: 1 });
});

test.serial('should not fail (exit code 0) if there are no commits', async t => {
const options = { git: { requireCommits: true, requireCommitsFail: false } };
const gitClient = factory(Git, { options });
sh.exec('git tag 1.0.0');
await t.throwsAsync(gitClient.init(), { code: 0 });
});

test.serial('should throw if there are no commits in specified path', async t => {
const options = { git: { requireCommits: true, commitsPath: 'dir' } };
const gitClient = factory(Git, { options });
Expand Down

0 comments on commit 7076fd3

Please sign in to comment.