diff --git a/README.md b/README.md index 55c46841d..d83368a3a 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,14 @@ Type: `array` All git semver tags found in the repository. You can't overwrite this value. +##### previousTag + +Type: `string` Default: previous tag or the first commit hash if no previous tag. + +##### currentTag + +Type: `string` Default: current tag or `'v'` + version if no current tag. + #### gitRawCommitsOpts See the [git-raw-commits](https://github.com/stevemao/git-raw-commits) docs. There are some defaults: diff --git a/index.js b/index.js index 9e1b7114f..84b5fc049 100644 --- a/index.js +++ b/index.js @@ -122,7 +122,7 @@ function conventinalChangelog(options, context, gitRawCommitsOpts, parserOpts, w } } - context.gitSemverTags = tagsObj.value; + var gitSemverTags = context.gitSemverTags = tagsObj.value; if (tagsObj.state === 'fulfilled') { tag = tagsObj.value[options.releaseCount - 1]; } @@ -174,8 +174,30 @@ function conventinalChangelog(options, context, gitRawCommitsOpts, parserOpts, w }, parserOpts); - writerOpts = _.assign( - preset.writerOpts || {}, { + writerOpts = _.assign({ + finalizeContext: function(context, writerOpts, commits, keyCommit) { + if ((!context.currentTag || !context.previousTag) && keyCommit) { + var match = /tag:\s*(.+?)[,\)]/gi.exec(keyCommit.gitTags); + var currentTag = context.currentTag = context.currentTag || match ? match[1] : null; + var index = gitSemverTags.indexOf(currentTag); + var previousTag = context.previousTag = gitSemverTags[index + 1]; + + if (!previousTag) { + if (options.append) { + context.previousTag = context.previousTag || commits[0].hash; + } else { + context.previousTag = context.previousTag || commits[commits.length - 1].hash; + } + } + } else { + context.previousTag = context.previousTag || gitSemverTags[0]; + context.currentTag = context.currentTag || 'v' + context.version; + } + + return context; + } + }, + preset.writerOpts, { reverse: options.append }, writerOpts diff --git a/package.json b/package.json index 71f38ba93..a8f41e360 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,7 @@ "chai": "^3.0.0", "concat-stream": "^1.4.10", "coveralls": "^2.11.2", + "git-tails": "^1.0.0", "istanbul": "^0.3.15", "jscs": "^2.0.0", "jshint": "^2.8.0", diff --git a/test/test.js b/test/test.js index f2ffcf827..4c2eecf93 100644 --- a/test/test.js +++ b/test/test.js @@ -1,6 +1,7 @@ 'use strict'; var conventionalChangelog = require('../'); var expect = require('chai').expect; +var gitTails = require('git-tails'); var shell = require('shelljs'); var through = require('through2'); var writeFileSync = require('fs').writeFileSync; @@ -331,7 +332,73 @@ describe('conventionalChangelog', function() { expect(i).to.equal(3); done(); })); + }); + + describe('finalizeContext', function() { + var tail; + + before(function(done) { + shell.exec('git tag -d v0.1.0'); + gitTails(function(err, data) { + tail = data[data.length - 1].substring(0, 7); + done(); + }); + }); + + it('should make `context.previousTag` default to a previous version of generated log (prepend)', function(done) { + var i = 0; + conventionalChangelog({ + releaseCount: 0 + }, { + version: '3.0.0' + }, {}, {}, { + mainTemplate: '{{previousTag}}...{{currentTag}}' + }) + .pipe(through(function(chunk, enc, cb) { + chunk = chunk.toString(); + + if (i === 0) { + expect(chunk).to.equal('v2.0.0...v3.0.0'); + } else if (i === 1) { + expect(chunk).to.equal(tail + '...v2.0.0'); + } + + i++; + cb(); + }, function() { + expect(i).to.equal(2); + done(); + })); + }); + + it('should make `context.previousTag` default to a previous version of generated log (append)', function(done) { + var i = 0; + + conventionalChangelog({ + releaseCount: 0, + append: true + }, { + version: '3.0.0' + }, {}, {}, { + mainTemplate: '{{previousTag}}...{{currentTag}}' + }) + .pipe(through(function(chunk, enc, cb) { + chunk = chunk.toString(); + + if (i === 0) { + expect(chunk).to.equal(tail + '...v2.0.0'); + } else if (i === 1) { + expect(chunk).to.equal('v2.0.0...v3.0.0'); + } + + i++; + cb(); + }, function() { + expect(i).to.equal(2); + done(); + })); + }); }); it('should warn if preset is not found', function(done) {