diff options
author | Petar Šegina <psegina@ymail.com> | 2016-02-08 03:05:20 +0100 |
---|---|---|
committer | Petar Šegina <psegina@ymail.com> | 2016-02-08 03:10:43 +0100 |
commit | 30fb8ed7ee5c18b6132f452782209db2309f904c (patch) | |
tree | 36f39d222f97bebc18a9a804171f630769684445 /src | |
parent | d8fd60d5407ae3d293fa49a00fda70caa00ce506 (diff) | |
download | git-hours-30fb8ed7ee5c18b6132f452782209db2309f904c.zip git-hours-30fb8ed7ee5c18b6132f452782209db2309f904c.tar.gz git-hours-30fb8ed7ee5c18b6132f452782209db2309f904c.tar.bz2 |
Use nodegit to fetch branch names
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.
Diffstat (limited to 'src')
-rwxr-xr-x | src/index.js | 25 |
1 files changed, 7 insertions, 18 deletions
diff --git a/src/index.js b/src/index.js index 54e26a5..9018fb9 100755 --- a/src/index.js +++ b/src/index.js @@ -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'; @@ -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) { @@ -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) { |