diff options
| author | Christophe Porteneuve <[email protected]> | 2013-11-08 19:05:52 +0100 |
|---|---|---|
| committer | Christophe Porteneuve <[email protected]> | 2013-11-08 19:35:19 +0100 |
| commit | 335918f28d6cbbac2efbc25990e97549dbd96aee (patch) | |
| tree | fffc8a187ee63decd9264edd06bc504b5f0450b6 /lib/detectLocalGit.js | |
| parent | d52c62b04c5c11fd931ca967f09621d03c330f88 (diff) | |
| download | node-coveralls-335918f28d6cbbac2efbc25990e97549dbd96aee.tar.xz node-coveralls-335918f28d6cbbac2efbc25990e97549dbd96aee.zip | |
Fix step 1: for dev-local uses, properly detect local Git branch and commit
Diffstat (limited to 'lib/detectLocalGit.js')
| -rw-r--r-- | lib/detectLocalGit.js | 30 |
1 files changed, 30 insertions, 0 deletions
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, ''); +} |
