Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions lib/detectLocalGit.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
var execSync = require('child_process').execSync;
var fs = require('fs');
var path = require('path');

// branch naming only has a few excluded characters, see git-check-ref-format(1)
var REGEX_BRANCH = /^ref: refs\/heads\/([^?*\[\\~^:]+)$/;

module.exports = function detectLocalGit() {
var dir = process.cwd(), gitDir;
while (path.resolve('/') !== dir) {
Expand All @@ -18,11 +16,10 @@ module.exports = function detectLocalGit() {
if (path.resolve('/') === dir)
return;

var head = fs.readFileSync(path.join(dir, '.git', 'HEAD'), 'utf-8').trim();
var branch = (head.match(REGEX_BRANCH) || [])[1];
var commit = execSync('git rev-parse HEAD', {cwd: dir, encoding: 'utf8'}).trim();
var branch = execSync('git rev-parse --abbrev-ref HEAD', {cwd: dir, encoding: 'utf8'}).trim();
if (!branch)
return { git_commit: head };
return { git_commit: commit };

var commit = fs.readFileSync(path.join(dir, '.git', 'refs', 'heads', branch), 'utf-8').trim();
return { git_commit: commit, git_branch: branch };
};