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 | |
| 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
| -rw-r--r-- | lib/fetchGitData.js | 111 | ||||
| -rw-r--r-- | lib/getOptions.js | 22 | ||||
| -rw-r--r-- | package.json | 3 | ||||
| -rw-r--r-- | test/getOptions.js | 6 |
4 files changed, 123 insertions, 19 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); diff --git a/package.json b/package.json index d262d8b..76b0a4b 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,8 @@ "yaml": "0.2.3", "request": "2.16.2", "lcov-parse": "0.0.4", - "log-driver": "1.2.1" + "log-driver": "1.2.1", + "exec-sync": "~0.1.6" }, "devDependencies": { "sinon-restore": "1.0.0", diff --git a/test/getOptions.js b/test/getOptions.js index 501eb51..ba3b7f0 100644 --- a/test/getOptions.js +++ b/test/getOptions.js @@ -57,7 +57,8 @@ describe("getOptions", function(){ committer_name: 'Unknown Committer', committer_email: '', message: 'Unknown Commit Message' }, - branch: 'master' }); + branch: 'master', + remotes: [] }); }); it ("should set service_name and service_job_id if it's running on circleci", function(){ process.env.CIRCLECI = true; @@ -74,6 +75,7 @@ describe("getOptions", function(){ committer_name: 'Unknown Committer', committer_email: '', message: 'Unknown Commit Message' }, - branch: 'master' }); + branch: 'master', + remotes: [] }); }); }); |
