Skip to content

Commit

Permalink
Add git.requireCommits feature to prevent empty releases (resolves #…
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed May 3, 2019
1 parent a85b6df commit d27587f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions conf/release-it.json
Expand Up @@ -9,6 +9,7 @@
"changelog": "git log --pretty=format:\"* %s (%h)\" ${latestTag}...HEAD",
"requireCleanWorkingDir": true,
"requireUpstream": true,
"requireCommits": false,
"addUntrackedFiles": false,
"commit": true,
"commitMessage": "Release ${version}",
Expand Down
7 changes: 7 additions & 0 deletions lib/errors.js
Expand Up @@ -57,6 +57,12 @@ class GitUpstreamError extends ReleaseItError {
}
}

class GitNoCommitsError extends ReleaseItError {
constructor() {
super('There are no commits since the latest tag.');
}
}

class GitNetworkError extends ReleaseItError {
constructor(err, remoteUrl) {
super(`Unable to fetch from ${remoteUrl}${EOL}${err.message}`);
Expand Down Expand Up @@ -94,6 +100,7 @@ module.exports = {
GitRemoteUrlError,
GitCleanWorkingDirError,
GitUpstreamError,
GitNoCommitsError,
GitCommitError,
GitNetworkError,
TokenError,
Expand Down
9 changes: 8 additions & 1 deletion lib/plugin/git/Git.js
Expand Up @@ -2,7 +2,7 @@ const _ = require('lodash');
const findUp = require('find-up');
const GitBase = require('../GitBase');
const prompts = require('./prompts');
const { GitCleanWorkingDirError, GitUpstreamError, GitCommitError } = require('../../errors');
const { GitCleanWorkingDirError, GitUpstreamError, GitNoCommitsError, GitCommitError } = require('../../errors');

const noop = Promise.resolve();
const invalidPushRepoRe = /^\S+@/;
Expand All @@ -26,6 +26,9 @@ class Git extends GitBase {
if (this.options.requireUpstream && !(await this.hasUpstreamBranch())) {
throw new GitUpstreamError();
}
if (this.options.requireCommits && (await this.getCommitsSinceLatestTag()) === 0) {
throw new GitNoCommitsError();
}
}

async beforeRelease() {
Expand Down Expand Up @@ -69,6 +72,10 @@ class Git extends GitBase {
return this.exec('git diff-index --quiet HEAD --', { options }).then(() => true, () => false);
}

getCommitsSinceLatestTag() {
return this.exec('git rev-list $(git describe --tags --abbrev=0)..HEAD --count', { options }).then(Number);
}

stage(file) {
if (!file || !file.length) return noop;
const files = _.castArray(file).join(' ');
Expand Down

0 comments on commit d27587f

Please sign in to comment.