[Pkg-javascript-commits] [node-coveralls] 103/332: Fix step 1: for dev-local uses, properly detect local Git branch and commit

Bastien Roucariès rouca at moszumanska.debian.org
Thu Nov 9 13:53:46 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-coveralls.

commit 335918f28d6cbbac2efbc25990e97549dbd96aee
Author: Christophe Porteneuve <tdd at tddsworld.com>
Date:   Fri Nov 8 19:05:52 2013 +0100

    Fix step 1: for dev-local uses, properly detect local Git branch and commit
---
 lib/detectLocalGit.js | 30 ++++++++++++++++++++++++++++++
 lib/getOptions.js     |  8 ++++++++
 2 files changed, 38 insertions(+)

diff --git a/lib/detectLocalGit.js b/lib/detectLocalGit.js
new file mode 100644
index 0000000..2d0fba0
--- /dev/null
+++ b/lib/detectLocalGit.js
@@ -0,0 +1,30 @@
+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 = strip(fs.readFileSync(path.join(dir, '.git', 'HEAD'), 'utf-8'));
+  var branch = (head.match(REGEX_BRANCH) || [])[1];
+  if (!branch)
+    return { git_commit: head };
+
+  var commit = strip(fs.readFileSync(path.join(dir, '.git', 'refs', 'heads', branch), 'utf-8'));
+  return { git_commit: commit, git_branch: branch };
+};
+
+function strip(str) {
+  return (str || '').replace(/^\s+|\s+$/g, '');
+}
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;

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-coveralls.git



More information about the Pkg-javascript-commits mailing list