diff --git a/index.js b/index.js index 9dff8cd..90e059f 100644 --- a/index.js +++ b/index.js @@ -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'); @@ -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 }); diff --git a/test.js b/test.js index 658c52f..71906ce 100644 --- a/test.js +++ b/test.js @@ -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(); + })); +});