Skip to content

Commit

Permalink
Use nodegit to fetch branch names
Browse files Browse the repository at this point in the history
This should be more robust than using scripts and it also works when the
repository is in detached HEAD state.

The repository references are explicitly filtered to return only local
branches.

This fixes #18.
  • Loading branch information
mrPjer committed Feb 8, 2016
1 parent d8fd60d commit 30fb8ed
Showing 1 changed file with 7 additions and 18 deletions.
25 changes: 7 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ var git = require('nodegit');
var program = require('commander');
var _ = require('lodash');
var moment = require('moment');
var exec = Promise.promisify(require('child_process').exec);

var DATE_FORMAT = 'YYYY-MM-DD';

Expand Down Expand Up @@ -183,9 +182,12 @@ function estimateHours(dates) {
function getCommits(gitPath) {
return git.Repository.open(gitPath)
.then(function(repo) {
var branchNames = getBranchNames(gitPath);
var allReferences = getAllReferences(repo);

return Promise.map(branchNames, function(branchName) {
return Promise.filter(allReferences, function(reference) {
return reference.match(/refs\/heads\/.*/);
})
.map(function(branchName) {
return getBranchLatestCommit(repo, branchName);
})
.map(function(branchLatestCommit) {
Expand All @@ -209,21 +211,8 @@ function getCommits(gitPath) {
});
}

function getBranchNames(gitPath) {
var cmd = "git branch --no-color | awk -F ' +' '! /\\(no branch\\)/ {print $2}'";
return new Promise(function(resolve, reject) {
exec(cmd, {cwd: gitPath}, function(err, stdout, stderr) {
if (err) {
reject(err);
}

resolve(stdout
.split('\n')
.filter(function(e) { return e; }) // Remove empty
.map(function(str) { return str.trim(); }) // Trim whitespace
);
});
});
function getAllReferences(repo) {
return repo.getReferenceNames(git.Reference.TYPE.LISTALL);
}

function getBranchLatestCommit(repo, branchName) {
Expand Down

0 comments on commit 30fb8ed

Please sign in to comment.