Skip to content
This repository has been archived by the owner on Feb 5, 2018. It is now read-only.

Commit

Permalink
fix(windows): use execFile for executing git
Browse files Browse the repository at this point in the history
Fix problems with variable expansion and escaping by passing
arguments straight to git with execFile instead of going through
a shell to execute it.

Closes #11
  • Loading branch information
Tapppi authored and stevemao committed Jun 27, 2016
1 parent 595e805 commit 9ae06df
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
27 changes: 13 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
var dargs = require('dargs');
var exec = require('child_process').exec;
var execFile = require('child_process').execFile;
var split = require('split2');
var stream = require('stream');
var template = require('lodash.template');
Expand All @@ -15,29 +15,28 @@ function gitRawCommits(options) {
options.from = options.from || '';
options.to = options.to || 'HEAD';

var gitFormat = template('--format=<%= format %>%n' +
'------------------------ >8 ------------------------'
)(options);
var gitFromTo = template('<%- from ? [from, to].join("..") : to %>')(options);

var args = dargs(options, {
excludes: ['from', 'to', 'format']
});

var cmd = template(
'git log --format="<%= format %>%n------------------------ >8 ------------------------" ' +
'"<%- from ? [from, to].join("..") : to %>" '
)(options) + args.join(' ');

if (process.platform === 'win32') {
// Git format strings have percent signs in them
// On windows percent signs need to be doubled to escape them,
// so that they aren't used for variable expansion
cmd.replace(/%/g, '%%');
}
args = [
'log',
gitFormat,
gitFromTo
].concat(args);

if (options.debug) {
options.debug('Your git-log command is:\n' + cmd);
options.debug('Your git-log command is:\ngit ' + args.join(' '));
}

var isError = false;

var child = exec(cmd, {
var child = execFile('git', args, {
maxBuffer: Infinity
});

Expand Down
27 changes: 26 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,33 @@ it('should show your git-log command', function(done) {
gitRawCommits({
format: 'what%n%B',
debug: function(cmd) {
expect(cmd).to.equal('Your git-log command is:\ngit log --format="what%n%B%n------------------------ >8 ------------------------" "HEAD" ');
expect(cmd).to.include('Your git-log command is:\ngit log --format');
done();
}
});
});

it('should prevent variable expansion on windows', function(done) {
var i = 0;

gitRawCommits({
format: '%%cd%n%B'
})
.pipe(through(function(chunk, enc, cb) {
chunk = chunk.toString();

if (i === 0) {
expect(chunk).to.equal('%cd\nThird commit\n\n');
} else if (i === 1) {
expect(chunk).to.equal('%cd\nSecond commit\n\n');
} else {
expect(chunk).to.equal('%cd\nFirst commit\n\n');
}

i++;
cb();
}, function() {
expect(i).to.equal(3);
done();
}));
});

0 comments on commit 9ae06df

Please sign in to comment.