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/fetchGitData.js | |
| 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/fetchGitData.js')
| -rw-r--r-- | lib/fetchGitData.js | 111 |
1 files changed, 111 insertions, 0 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; |
