Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sub-directory): add -s, --sub-directory option #32

Closed
wants to merge 11 commits into from
25 changes: 16 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,19 @@ $ changelog -h

Generate a changelog from git commits.


Options:

-h, --help output usage information
-V, --version output the version number
-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
-V, --version output the version number
-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
-s, --sub-directory <path> specify a path to be passed into git log
-h, --help output usage information

```

Expand All @@ -87,6 +89,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:
Expand Down
7 changes: 5 additions & 2 deletions bin/generate
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@
'use strict';

var Bluebird = require('bluebird');
var Path = require('path');

var CLI = require('../lib/cli');
var Changelog = require('../lib');
var File = require('../lib/file');

CLI.parse(process.argv);

var filePath = Path.join(CLI.subDirectory, CLI.file);
Copy link
Contributor

@robinjoseph08 robinjoseph08 Feb 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I'm not sure if we can join the paths together here since some people might be passing in - for stdout. We might need to pass CLI.subDirectory through to the two functions. We also should probably be checking for - explicitly in File.readIfExists, but you don't have to do that in this PR.


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);
Expand Down
3 changes: 2 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ module.exports = CLI
.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');
.option('-u, --repo-url [url]', 'specify the repo URL for commit links, defaults to checking the package.json')
.option('-s, --sub-directory <path>', 'specify a path to be passed into git log', '.');
4 changes: 3 additions & 1 deletion lib/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
14 changes: 14 additions & 0 deletions test/git.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,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();
});
});

});

});