Skip to content

Commit

Permalink
added the possibility to use a function as message in git.commit (#181)
Browse files Browse the repository at this point in the history
* added the possibility to use a function as message in git.commit

* remove unneccesary check
  • Loading branch information
Quantumplation authored and stephenlacy committed May 3, 2018
1 parent fc3ca0d commit 35aa99b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ gulp.task('commit', function(){
.pipe(git.commit('initial commit'));
});

// Run git commit with a computed commit message
gulp.task('commit', function(){
let newVersion;
function computeNewVersion() { newVersion = /* ... */ }
return gulp.src('./git-test/*')
.pipe(computeNewVersion())
.pipe(git.commit(() => `Bumps to version ${newVersion}`));
});

// Run git commit with options
gulp.task('commit', function(){
return gulp.src('./git-test/*')
Expand Down
5 changes: 5 additions & 0 deletions examples/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ gulp.task('commit', function() {
.pipe(git.commit('initial commit'));
});

gulp.task('commitDelayed', function() {
gulp.src('./*')
.pipe(git.commit(function () { return 'commiting at exactly ' + new Date().toISOString(); }));
});

// Commit files with arguments
gulp.task('commitopts', function() {
gulp.src('./*')
Expand Down
6 changes: 5 additions & 1 deletion lib/commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var path = require('path');

module.exports = function(message, opt) {
if (!opt) opt = {};
if (!message || message.length === 0) {
if (!message) {
if (opt.args.indexOf('--amend') === -1 && opt.disableMessageRequirement !== true) {
throw new Error('gulp-git: Commit message is required git.commit("commit message") or --amend arg must be given');
}
Expand All @@ -36,6 +36,10 @@ module.exports = function(message, opt) {
var flush = function(cb) {
var writeStdin = false;
var cmd = 'git commit ';
// Allow delayed execution to determine the message
if (typeof message === 'function') {
message = message();
}

if (message && opt.args.indexOf('--amend') === -1) {

Expand Down
18 changes: 18 additions & 0 deletions test/commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,22 @@ module.exports = function(git, util) {
gitS.end();
});
});

it('should allow functions for git message', function(done) {
var fakeFile = util.testFiles[0];
var opt = {cwd: './test/repo/'};
var msg = 'bogus commit message';
var gitS = git.commit(function() { return msg; }, opt);
msg = 'initial commit';
gitS.once('finish', function() {
setTimeout(function() {
fs.readFileSync(util.testCommit)
.toString('utf8')
.should.match(/initial commit/);
done();
}, 100);
});
gitS.write(fakeFile);
gitS.end();
});
};

0 comments on commit 35aa99b

Please sign in to comment.