diff --git a/README.md b/README.md index 19b89d6..5858c0c 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,7 @@ $ changelog -h -x, --exclude 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 + -s, --sub-directory specify a path to be passed into git log ``` It's possible to create a `./CHANGELOG.md` file for a specific commit range: @@ -107,6 +108,11 @@ The way that I would recommend using this module would be the way it's being use "release:patch": "changelog -p && git add CHANGELOG.md && git commit -m 'updated CHANGELOG.md' && npm version patch && git push origin && git push origin --tags", ``` +### Sub-directory + +Setting up the `--sub-directory` flag will run the `git log` command under the specified path. +When combined with the `--file` flag, the input/output file would be ***relative to the sub-directory*** specified in the previous flag. + ## Testing To run the test suite, just clone the repository and run the following: diff --git a/bin/generate b/bin/generate index 78fe7b5..c122138 100755 --- a/bin/generate +++ b/bin/generate @@ -2,6 +2,7 @@ 'use strict'; var Bluebird = require('bluebird'); +var Path = require('path'); var CLI = require('../lib/cli'); var Changelog = require('../lib'); @@ -9,12 +10,14 @@ var File = require('../lib/file'); CLI.parse(process.argv); +var filePath = CLI.file !== '-' ? Path.join(CLI.subDirectory, CLI.file) : CLI.file; + return Bluebird.all([ Changelog.generate(CLI), - File.readIfExists(CLI.file) + File.readIfExists(filePath) ]) .spread(function (newLogs, oldLogs) { - return File.writeToFile(CLI.file, newLogs + oldLogs); + return File.writeToFile(filePath, newLogs + oldLogs); }) .catch(function (err) { console.error(err); diff --git a/lib/cli.js b/lib/cli.js index cbb4216..aa5f561 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -17,4 +17,5 @@ module.exports = CLI .option('-t, --tag ', 'generate from specific tag or range (e.g. v1.2.3 or v1.2.3..v1.2.4)') .option('-x, --exclude ', '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'); + .option('-u, --repo-url [url]', 'specify the repo URL for commit links, defaults to checking the package.json') + .option('-s, --sub-directory ', 'specify a path to be passed into git log', '.'); diff --git a/lib/git.js b/lib/git.js index 83785f7..73b468b 100644 --- a/lib/git.js +++ b/lib/git.js @@ -33,8 +33,10 @@ exports.getCommits = function (options) { revisions = tag ? tag + '..HEAD' : ''; } + var gitLogCommand = 'git log -E --format=' + FORMAT + ' ' + revisions + ' -- ' + options.subDirectory; + return CP.execAsync( - 'git log -E --format=' + FORMAT + ' ' + revisions, + gitLogCommand, { maxBuffer: Number.MAX_SAFE_INTEGER } diff --git a/test/git.test.js b/test/git.test.js index b743c56..a92373e 100644 --- a/test/git.test.js +++ b/test/git.test.js @@ -126,6 +126,20 @@ describe('git', function () { }); }); + it('uses subDirectory for filtering git log command when `-s` / `--sub-directory` option was used', function () { + Sinon.stub(CP, 'execAsync') + .onFirstCall().returns(Bluebird.resolve('1.2.3.4')) + .onSecondCall().returns(Bluebird.resolve(VALID_COMMITS)); + + var subDirectory = 'subdirectory'; + + return Git.getCommits({ subDirectory: subDirectory }) + .then(function () { + CP.execAsync.secondCall.calledWithMatch(new RegExp('-- ' + subDirectory + '$')); + CP.execAsync.restore(); + }); + }); + }); });