Skip to content

Commit

Permalink
feat(git): generate from specific tag (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
ffflorian authored and robinjoseph08 committed Sep 19, 2017
1 parent b20b37c commit cadb4e2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ $ changelog -h
-p, --patch create a patch changelog
-m, --minor create a minor changelog
-M, --major create a major changelog
-t, --tag <range> generate from specific tag or range (e.g. v1.2.3 or v1.2.3..v1.2.4)
-x, --exclude <types> exclude selected commit types (comma separated)
-f, --file [file] file to write to, defaults to ./CHANGELOG.md, use - for stdout
-u, --repo-url [url] specify the repo URL for commit links, defaults to checking the package.json
Expand Down
1 change: 1 addition & 0 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = CLI
.option('-p, --patch', 'create a patch changelog')
.option('-m, --minor', 'create a minor changelog')
.option('-M, --major', 'create a major changelog')
.option('-t, --tag <range>', 'generate from specific tag or range (e.g. v1.2.3 or v1.2.3..v1.2.4)')
.option('-x, --exclude <types>', 'exclude selected commit types (comma separated)', list)
.option('-f, --file [file]', 'file to write to, defaults to ./CHANGELOG.md, use - for stdout', './CHANGELOG.md')
.option('-u, --repo-url [url]', 'specify the repo URL for commit links, defaults to checking the package.json');
15 changes: 13 additions & 2 deletions lib/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,24 @@ var FORMAT = '%H%n%s%n%b%n' + SEPARATOR;
*/
exports.getCommits = function (options) {
options = options || {};
return CP.execAsync('git describe --tags --abbrev=0')
return new Bluebird(function (resolve) {
if (options.tag) {
return resolve(options.tag);
}
return resolve(CP.execAsync('git describe --tags --abbrev=0'));
})
.catch(function () {
return '';
})
.then(function (tag) {
tag = tag.toString().trim();
var revisions = tag ? tag + '..HEAD' : '';
var revisions;

if (tag.indexOf('..') !== -1) {
revisions = tag;
} else {
revisions = tag ? tag + '..HEAD' : '';
}

return CP.execAsync('git log -E --format=' + FORMAT + ' ' + revisions);
})
Expand Down
15 changes: 14 additions & 1 deletion test/git.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,20 @@ describe('git', function () {
});
});

it('errs if there are no commits yet', function () {
it('uses custom revision range if `-t` / `--tag` option was used', function () {
Sinon.stub(CP, 'execAsync')
.onFirstCall().returns(Bluebird.resolve(VALID_COMMITS));

var tagRange = 'abcdef01..23456789';

return Git.getCommits({ tag: tagRange })
.then(function () {
CP.execAsync.firstCall.calledWithMatch(tagRange);
CP.execAsync.restore();
});
});

it('errors if there are no commits yet', function () {
Sinon.stub(CP, 'execAsync')
.onFirstCall().returns(Bluebird.resolve('v1.2.3'))
.onSecondCall().returns(Bluebird.reject());
Expand Down

0 comments on commit cadb4e2

Please sign in to comment.