diff --git a/index.js b/index.js index fc3d578..f3d9090 100755 --- a/index.js +++ b/index.js @@ -1,28 +1,47 @@ var exec = require('child_process').exec function _command (cmd, cb) { - exec(cmd, { cwd: __dirname }, function (err, stdout, stderr) { - cb(stdout.split('\n').join('')) + exec(cmd, { cwd: __dirname, maxBuffer: 1024 * 1024 }, function (err, stdout, stderr) { + if (err !== null) { + console.log('[NODE-GIT-REV] exec error: ' + err); + cb(null); + } + cb(stdout.slice(0, -1)); }) } -module.exports = { - short : function (cb) { +module.exports = { + short : function (cb) { _command('git rev-parse --short HEAD', cb) } - , long : function (cb) { + , long : function (cb) { _command('git rev-parse HEAD', cb) } - , branch : function (cb) { + , branch : function (cb) { _command('git rev-parse --abbrev-ref HEAD', cb) } - , tag : function (cb) { + , tag : function (cb) { _command('git describe --always --tag --abbrev=0', cb) } - , log : function (cb) { - _command('git log --no-color --pretty=format:\'[ "%H", "%s", "%cr", "%an" ],\' --abbrev-commit', function (str) { - str = str.substr(0, str.length-1) - cb(JSON.parse('[' + str + ']')) - }) - } + , log : function (cb, limit) { + var cmd_string = 'git log --no-color --pretty=format:\"%H | %cr | %an | %s\" --abbrev-commit'; + if(limit != null && !isNaN(limit)) { + cmd_string += ' -'+limit; + } + _command(cmd_string, function (str) { + var logs = []; + if ( str !== null ) { + (str.split('\n')).forEach(function(line, line_idx) { + var parsed_line = line.split(' | '); + logs.push([ + parsed_line[0], + parsed_line[3], + parsed_line[1], + parsed_line[2] + ]); + }); + }; + cb(logs); + }); + } } diff --git a/package.json b/package.json index 386cba6..ef3a07e 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name" : "git-rev", - "version" : "0.2.1", + "version" : "0.2.3", "description" : "get the current git commit hash, tag or branch in node", "main" : "index.js", "bin" : {},