From a49eaa7f533bb7c3338d7864e3d118d43ede861e Mon Sep 17 00:00:00 2001 From: Christophe Porteneuve Date: Fri, 8 Nov 2013 19:31:19 +0100 Subject: Fix step 3: code cleanup + faster/nimbler use of Git to obtain data (rev-parse and cat-file instead of multiple log calls) --- lib/fetchGitData.js | 78 +++++++++++++++++++---------------------------------- 1 file changed, 28 insertions(+), 50 deletions(-) (limited to 'lib/fetchGitData.js') diff --git a/lib/fetchGitData.js b/lib/fetchGitData.js index f75ca82..1633bd5 100644 --- a/lib/fetchGitData.js +++ b/lib/fetchGitData.js @@ -1,37 +1,19 @@ var exec = require('child_process').exec; var logger = require('./logger')(); -var fetchGitData = function(git, cb) { +function fetchGitData(git, cb) { if (!cb){ throw new Error("fetchGitData requires a callback"); } - var i; - var execGit = true; - var head = { - "author_name": { - "format": "'%aN'", - }, - "author_email": { - "format": "'%ae'", - }, - "committer_name": { - "format": "'%cN'", - }, - "committer_email": { - "format": "'%ce'", - }, - "message": { - "format": "'%s'", - } - }; - //-- Malformed/undefined git object if ('undefined' === typeof git) { return cb(new Error('No options passed')); - } else if (!git.hasOwnProperty('head')) { + } + if (!git.hasOwnProperty('head')) { return cb(new Error('You must provide the head')); - } else if (!git.head.hasOwnProperty('id')) { + } + if (!git.head.hasOwnProperty('id')) { return cb(new Error('You must provide the head.id')); } @@ -52,7 +34,7 @@ var fetchGitData = function(git, cb) { } //-- Use git? - exec("git log -1 " + git.head.id + " --pretty=format:'%H'", function(err, response){ + exec("git rev-parse --verify " + git.head.id, function(err, response){ if (err){ // git is not available... git.head.author_name = git.head.author_name || "Unknown Author"; @@ -63,32 +45,7 @@ var fetchGitData = function(git, cb) { return cb(null, git); } - //-- Head - var commands = []; - var fields = []; - for (var field in head) { - fields.push(field); - var command = "git log -1 " + git.head.id + " --pretty=format:" + head[field].format; - commands.push(command); - } - var i = 0; - var remaining = commands.length; - commands.forEach(function(command){ - var field = fields[i]; - i++; - exec(command, function(err, response){ - if (err) return cb(err); - git.head[field] = response; - remaining--; - if (remaining === 0){ - if (git.branch) { - fetchRemotes(git, cb); - } else { - fetchBranch(git, cb); - } - } - }); - }); + fetchHeadDetails(git, cb); }); } @@ -102,6 +59,27 @@ function fetchBranch(git, cb) { }); } +var REGEX_COMMIT_DETAILS = /\nauthor (.+?) <(.+?)>.+\ncommitter (.+?) <(.+?)>.+\n\n(.*)/m; + +function fetchHeadDetails(git, cb) { + exec('git cat-file -p ' + git.head.id, function(err, response) { + if (err) + return cb(err); + + var items = response.match(REGEX_COMMIT_DETAILS).slice(1); + var fields = ['author_name', 'author_email', 'committer_name', 'committer_email', 'message']; + fields.forEach(function(field, index) { + git.head[field] = items[index]; + }); + + if (git.branch) { + fetchRemotes(git, cb); + } else { + fetchBranch(git, cb); + } + }); +} + function fetchRemotes(git, cb) { exec("git remote -v", function(err, remotes){ if (err) -- cgit v1.2.3