aboutsummaryrefslogtreecommitdiff
path: root/lib/fetchGitData.js
diff options
context:
space:
mode:
authorChristophe Porteneuve <[email protected]>2013-11-08 19:31:19 +0100
committerChristophe Porteneuve <[email protected]>2013-11-08 19:35:30 +0100
commita49eaa7f533bb7c3338d7864e3d118d43ede861e (patch)
tree88da9a5a295d4dcd7bb0f0a30c27da48036639fb /lib/fetchGitData.js
parent36c62aff7424b12ca9e84370afc65ecfbe73e632 (diff)
downloadnode-coveralls-a49eaa7f533bb7c3338d7864e3d118d43ede861e.tar.xz
node-coveralls-a49eaa7f533bb7c3338d7864e3d118d43ede861e.zip
Fix step 3: code cleanup + faster/nimbler use of Git to obtain data (rev-parse and cat-file instead of multiple log calls)
Diffstat (limited to 'lib/fetchGitData.js')
-rw-r--r--lib/fetchGitData.js78
1 files changed, 28 insertions, 50 deletions
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)