diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/detectLocalGit.js | 26 | ||||
| -rw-r--r-- | lib/fetchGitData.js | 135 | ||||
| -rw-r--r-- | lib/getOptions.js | 8 | ||||
| -rw-r--r-- | lib/handleInput.js | 6 |
4 files changed, 98 insertions, 77 deletions
diff --git a/lib/detectLocalGit.js b/lib/detectLocalGit.js new file mode 100644 index 0000000..4fd8d1b --- /dev/null +++ b/lib/detectLocalGit.js @@ -0,0 +1,26 @@ +var fs = require('fs'); +var path = require('path'); + +var REGEX_BRANCH = /^ref: refs\/heads\/(\w+)$/; + +module.exports = function detectLocalGit(knownCommit, knownBranch) { + var dir = process.cwd(), gitDir; + while ('/' !== dir) { + gitDir = path.join(dir, '.git'); + if (fs.existsSync(path.join(gitDir, 'HEAD'))) + break; + + dir = path.dirname(dir); + } + + if ('/' === dir) + return; + + var head = fs.readFileSync(path.join(dir, '.git', 'HEAD'), 'utf-8').trim(); + var branch = (head.match(REGEX_BRANCH) || [])[1]; + if (!branch) + return { git_commit: head }; + + var commit = fs.readFileSync(path.join(dir, '.git', 'refs', 'heads', branch), 'utf-8').trim(); + return { git_commit: commit, git_branch: branch }; +}; 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; diff --git a/lib/getOptions.js b/lib/getOptions.js index 35187b4..abd2f2e 100644 --- a/lib/getOptions.js +++ b/lib/getOptions.js @@ -43,6 +43,14 @@ var getBaseOptions = function(cb){ options.service_job_id = process.env.COVERALLS_SERVICE_JOB_ID; } + if (!git_commit || !git_branch) { + var data = require('./detectLocalGit')(git_commit, git_branch); + if (data) { + git_commit = git_commit || data.git_commit; + git_branch = git_branch || data.git_branch; + } + } + // try to get the repo token as an environment variable if (process.env.COVERALLS_REPO_TOKEN) { options.repo_token = process.env.COVERALLS_REPO_TOKEN; diff --git a/lib/handleInput.js b/lib/handleInput.js index 1435c1c..807e31d 100644 --- a/lib/handleInput.js +++ b/lib/handleInput.js @@ -1,9 +1,10 @@ var index = require('../index'); var logger = require('./logger')(); -var handleInput = function(input){ +function handleInput(input) { logger.debug(input); var options = index.getOptions(function(err, options){ + if (err){ logger.error("error from getOptions"); throw err; @@ -28,7 +29,6 @@ var handleInput = function(input){ }); }); }); - -}; +} module.exports = handleInput; |
