diff options
| author | Gabe Hayes <[email protected]> | 2013-07-26 13:01:07 -0700 |
|---|---|---|
| committer | Gabe Hayes <[email protected]> | 2013-07-26 13:01:07 -0700 |
| commit | bc464dc1d8c9a0ecfac4aefb8f12dd24347b5732 (patch) | |
| tree | 639210261cc511c045b9d02af84c1ca1aecd64e1 /lib | |
| parent | 87a316a80997593d25ef8b3a12bfa9b82b42ba67 (diff) | |
| download | node-coveralls-bc464dc1d8c9a0ecfac4aefb8f12dd24347b5732.tar.xz node-coveralls-bc464dc1d8c9a0ecfac4aefb8f12dd24347b5732.zip | |
fetch git data from command line git
- added exec-sync package to execute git commands
- if a proper git hash is not passed, falls back to default values
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/fetchGitData.js | 111 | ||||
| -rw-r--r-- | lib/getOptions.js | 22 |
2 files changed, 117 insertions, 16 deletions
diff --git a/lib/fetchGitData.js b/lib/fetchGitData.js new file mode 100644 index 0000000..5f3e582 --- /dev/null +++ b/lib/fetchGitData.js @@ -0,0 +1,111 @@ +var exec = require("exec-sync"); + +var fetchGitData = function(git) { + + var i, + execGit = true, + head = { + "author_name": { + "format": "'%aN'", + "default": "Unknown Author" + }, + "author_email": { + "format": "'%ae'", + "default": "" + }, + "committer_name": { + "format": "'%cN'", + "default": "Unknown Committer" + }, + "committer_email": { + "format": "'%ce'", + "default" :"" + }, + "message": { + "format": "'%s'", + "default": "Unknown Commit Message" + } + }, + remotes = {}; + + 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 + }); + } + } + } + + //-- Set required properties of git if they weren"t provided + if (!git.hasOwnProperty("head")) { + git.head = {}; + } + if (!git.hasOwnProperty("branch")) { + git.branch = ""; + } + if (!git.hasOwnProperty("remotes")) { + git.remotes = []; + } + + //-- Assert the property types + if ("object" !== typeof git.head) { + git.head = {}; + } + if ("string" !== typeof git.branch) { + git.branch = ""; + } + if (!git.remotes.hasOwnProperty("length")) { + git.remotes = []; + } + + //-- Use git? + try { + exec("git log -1 " + git.head.id + " --pretty=format:'%H'"); + } catch (e) { + execGit = false; + } + + //-- Head + for (i in head) { + if (!git.head.hasOwnProperty(i)) { + if (execGit) { + git.head[i] = exec("git log -1 " + git.head.id + " --pretty=format:" + head[i].format); + } else { + git.head[i] = head[i].default; + } + } + } + + if (execGit) { + + //-- Branch + if ("" === git.branch.length) { + git.branch = exec("git branch").split("\n")[0].replace(/^\*\ /, "").trim(); + } + + //-- Remotes + if (0 !== git.remotes.length) { + for (i in git.remotes) { + saveRemote(git.remotes[i].name, git.remotes[i].url, false); + } + } + exec("git remote -v").split("\n").forEach(function(remote) { + remote = remote.split(/\s/); + saveRemote(remote[0], remote[1]); + }); + + } + + return git; + +}; + +module.exports = fetchGitData; diff --git a/lib/getOptions.js b/lib/getOptions.js index 973b82b..deb1072 100644 --- a/lib/getOptions.js +++ b/lib/getOptions.js @@ -2,6 +2,7 @@ var fs = require('fs'); var path = require('path'); var yaml = require('yaml'); var logger = require('./logger')(); +var git = require('./fetchGitData'); var getOptions = function(){ var options = {}; @@ -41,23 +42,12 @@ var getOptions = function(){ } if (git_commit){ - options.git = { - "head": { - "id": git_commit, - "author_name": "Unknown Author", - "author_email": "", - "committer_name": "Unknown Committer", - "committer_email": "", - "message": "Unknown Commit Message" + options.git = git({ + head: { + id: git_commit }, - "branch": git_branch /*, - "remotes": [ - { - "name": "origin", - "url": "[email protected]:lemurheavy/coveralls-ruby.git" - } - ]*/ - }; + branch: git_branch + }); } options.run_at = process.env.COVERALLS_RUN_AT || JSON.stringify(new Date()).slice(1, -1); |
