aboutsummaryrefslogtreecommitdiff
path: root/lib/detectLocalGit.js
diff options
context:
space:
mode:
authorNick Merwin <[email protected]>2019-11-20 17:05:48 -0800
committerGitHub <[email protected]>2019-11-20 17:05:48 -0800
commit336123fe05e7f80d79fc8522b4ff21a60fffe77c (patch)
tree97e5b9343471fc73ddf4ffb20cd71ca58367d6c6 /lib/detectLocalGit.js
parenteba01a29c69f28ffd7f50556492a43b5ba2486d7 (diff)
downloadnode-coveralls-336123fe05e7f80d79fc8522b4ff21a60fffe77c.tar.xz
node-coveralls-336123fe05e7f80d79fc8522b4ff21a60fffe77c.zip
ES6
* remove unused variables * move `require`s at the top * check for prototype built-ins * run in strict mode * always check for error values in tests * make style consistent * mark Node.js 6 as the minimum supported version in package.json * Use the arrow return syntax * logger.js: use `Boolean` to make the intention clearer. * Use the rest params instead of arguments. * test/getOptions.js: beautify git data. * Update logger.js Remove `hasDebugEnvVariable` function; it's just a `process.env` check * Create .jshintrc
Diffstat (limited to 'lib/detectLocalGit.js')
-rw-r--r--lib/detectLocalGit.js68
1 files changed, 40 insertions, 28 deletions
diff --git a/lib/detectLocalGit.js b/lib/detectLocalGit.js
index 46a9e67..89133ff 100644
--- a/lib/detectLocalGit.js
+++ b/lib/detectLocalGit.js
@@ -1,47 +1,59 @@
-var fs = require('fs');
-var path = require('path');
+'use strict';
+
+const fs = require('fs');
+const path = require('path');
// branch naming only has a few excluded characters, see git-check-ref-format(1)
-var REGEX_BRANCH = /^ref: refs\/heads\/([^?*\[\\~^:]+)$/;
+const REGEX_BRANCH = /^ref: refs\/heads\/([^?*[\\~^:]+)$/;
+
+function detectLocalGit() {
+ let dir = process.cwd();
+ let gitDir;
-module.exports = function detectLocalGit() {
- var dir = process.cwd(), gitDir;
while (path.resolve('/') !== dir) {
gitDir = path.join(dir, '.git');
- var existsSync = fs.existsSync || path.existsSync;
- if (existsSync(path.join(gitDir, 'HEAD')))
+ const existsSync = fs.existsSync || path.existsSync;
+ if (existsSync(path.join(gitDir, 'HEAD'))) {
break;
+ }
dir = path.dirname(dir);
}
- if (path.resolve('/') === dir)
+ if (path.resolve('/') === dir) {
return;
+ }
- var head = fs.readFileSync(path.join(dir, '.git', 'HEAD'), 'utf-8').trim();
- var branch = (head.match(REGEX_BRANCH) || [])[1];
- if (!branch)
+ const head = fs.readFileSync(path.join(dir, '.git', 'HEAD'), 'utf-8').trim();
+ const branch = (head.match(REGEX_BRANCH) || [])[1];
+ if (!branch) {
return { git_commit: head };
+ }
- var commit = _parseCommitHashFromRef(dir, branch);
+ const commit = _parseCommitHashFromRef(dir, branch);
- return { git_commit: commit, git_branch: branch };
-};
+ return {
+ git_commit: commit,
+ git_branch: branch
+ };
+}
function _parseCommitHashFromRef(dir, branch) {
- var ref = path.join(dir, '.git', 'refs', 'heads', branch);
- if (fs.existsSync(ref)) {
- return fs.readFileSync(ref, 'utf-8').trim();
- } else {
- // ref does not exist; get it from packed-refs
- var commit = '';
- var packedRefs = path.join(dir, '.git', 'packed-refs');
- var packedRefsText = fs.readFileSync(packedRefs, 'utf-8');
- packedRefsText.split('\n').forEach(function (line) {
- if (line.match('refs/heads/'+branch)) {
- commit = line.split(' ')[0];
- }
- });
- return commit;
+ const ref = path.join(dir, '.git', 'refs', 'heads', branch);
+ if (fs.existsSync(ref)) {
+ return fs.readFileSync(ref, 'utf-8').trim();
+ }
+
+ // ref does not exist; get it from packed-refs
+ let commit = '';
+ const packedRefs = path.join(dir, '.git', 'packed-refs');
+ const packedRefsText = fs.readFileSync(packedRefs, 'utf-8');
+ packedRefsText.split('\n').forEach(line => {
+ if (line.match(`refs/heads/${branch}`)) {
+ commit = line.split(' ')[0];
}
+ });
+ return commit;
}
+
+module.exports = detectLocalGit;