diff options
| author | Gregg Caines <[email protected]> | 2013-11-15 13:08:29 -0800 |
|---|---|---|
| committer | Gregg Caines <[email protected]> | 2013-11-15 13:08:29 -0800 |
| commit | 097ce4e175e701efb95fd8aeaeb81a9d929d90ba (patch) | |
| tree | 97bf43b178f0f21b168c10474afbae7627ecc652 /lib/detectLocalGit.js | |
| parent | d52c62b04c5c11fd931ca967f09621d03c330f88 (diff) | |
| parent | a639b7dcb280f3b1ab6c8357fb3c8b9057543e79 (diff) | |
| download | node-coveralls-097ce4e175e701efb95fd8aeaeb81a9d929d90ba.tar.xz node-coveralls-097ce4e175e701efb95fd8aeaeb81a9d929d90ba.zip | |
Merge pull request #25 from tdd/master
Fix direct dev use on a local Git repo + improve Git metadata fetching
Diffstat (limited to 'lib/detectLocalGit.js')
| -rw-r--r-- | lib/detectLocalGit.js | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/lib/detectLocalGit.js b/lib/detectLocalGit.js new file mode 100644 index 0000000..4fd8d1b --- /dev/null +++ b/lib/detectLocalGit.js @@ -0,0 +1,26 @@ +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 = fs.readFileSync(path.join(dir, '.git', 'HEAD'), 'utf-8').trim(); + var branch = (head.match(REGEX_BRANCH) || [])[1]; + if (!branch) + return { git_commit: head }; + + var commit = fs.readFileSync(path.join(dir, '.git', 'refs', 'heads', branch), 'utf-8').trim(); + return { git_commit: commit, git_branch: branch }; +}; |
