Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite local git detection using git command #132

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 };
};