aboutsummaryrefslogtreecommitdiff
path: root/lib/fetchGitData.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/fetchGitData.js')
-rw-r--r--lib/fetchGitData.js135
1 files changed, 61 insertions, 74 deletions
diff --git a/lib/fetchGitData.js b/lib/fetchGitData.js
index a77b4a8..1633bd5 100644
--- a/lib/fetchGitData.js
+++ b/lib/fetchGitData.js
@@ -1,55 +1,20 @@
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'",
- }
- };
- var remotes = {};
-
//-- 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')) {
- return cb(new Error('You must provide the head.id'));
}
-
- function saveRemote(name, url, push) {
- var key = name + "-" + url;
- if ("undefined" === typeof push || "boolean" !== typeof push) {
- push = true;
- }
- if (!remotes.hasOwnProperty(key)) {
- remotes[key] = true;
- if (push) {
- git.remotes.push({
- "name": name,
- "url": url
- });
- }
- }
+ if (!git.head.hasOwnProperty('id')) {
+ return cb(new Error('You must provide the head.id'));
}
//-- Set required properties of git if they weren"t provided
@@ -69,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";
@@ -80,42 +45,64 @@ 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);
+ fetchHeadDetails(git, cb);
+ });
+}
+
+function fetchBranch(git, cb) {
+ exec("git branch", function(err, branches){
+ if (err)
+ return cb(err);
+
+ git.branch = (branches.match(/^\* (\w+)/) || [])[1];
+ fetchRemotes(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);
}
- 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){
- //-- Branch
- exec("git branch", function(err, branches){
- if (err) return cb(err);
- git.branch = branches.split("\n")[0].replace(/^\*\ /, "").trim();
- exec("git remote -v", function(err, remotes){
- if (err) return cb(err);
- remotes.split("\n").forEach(function(remote) {
- remote = remote.split(/\s/);
- saveRemote(remote[0], remote[1]);
- });
- return cb(null, git);
- });
- });
- }
- });
+ });
+}
+
+function fetchRemotes(git, cb) {
+ exec("git remote -v", function(err, remotes){
+ if (err)
+ return cb(err);
+
+ var processed = {};
+ remotes.split("\n").forEach(function(remote) {
+ if (!/\s\(push\)$/.test(remote))
+ return;
+ remote = remote.split(/\s+/);
+ saveRemote(processed, git, remote[0], remote[1]);
});
+ cb(null, git);
});
-};
+}
+
+function saveRemote(processed, git, name, url) {
+ var key = name + "-" + url;
+ if (processed.hasOwnProperty(key))
+ return;
+ processed[key] = true;
+ git.remotes.push({ name: name, url: url });
+}
module.exports = fetchGitData;