Skip to content

Commit

Permalink
Rewrite local git detection using git command
Browse files Browse the repository at this point in the history
  • Loading branch information
dlackty committed Jul 7, 2016
1 parent 57db719 commit d7a6eb1
Showing 1 changed file with 4 additions and 7 deletions.
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 };
};

0 comments on commit d7a6eb1

Please sign in to comment.