From 77cca390bc4efc447215f425d53618f11d02f85a Mon Sep 17 00:00:00 2001 From: Guy Paddock Date: Thu, 9 Feb 2017 20:07:14 -0500 Subject: [PATCH 1/2] Adds a filter for per-branch statistics --- src/index.js | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/src/index.js b/src/index.js index ab6b671..2c8fb2b 100755 --- a/src/index.js +++ b/src/index.js @@ -19,7 +19,8 @@ var config = { // Include commits since time x since: 'always', - until: 'always' + until: 'always', + branch: null }; function main() { @@ -30,7 +31,7 @@ function main() { config.since = parseSinceDate(config.since); config.until = parseUntilDate(config.until); - getCommits('.').then(function(commits) { + getCommits('.', config.branch).then(function(commits) { var commitsByEmail = _.groupBy(commits, function(commit) { return commit.author.email || 'unknown'; }); @@ -105,6 +106,11 @@ function parseArgs() { 'Analyze data until certain date.' + ' [always|yesterday|today|lastweek|thisweek|yyyy-mm-dd] Default: ' + config.until, String + ) + .option( + '-b, --branch [branch name]', + 'Analyze only data on the specified branch. Default: ' + config.branch, + String ); program.on('--help', function() { @@ -132,6 +138,10 @@ function parseArgs() { console.log(''); console.log(' $ git hours --since 2015-01-31'); console.log(''); + console.log(' - Estimate hours work in repository on the "develop" branch'); + console.log(''); + console.log(' $ git hours --branch develop'); + console.log(''); console.log(' For more details, visit /~https://github.com/kimmobrunfeldt/git-hours'); console.log(''); }); @@ -170,7 +180,8 @@ function mergeDefaultsWithArgs(conf) { maxCommitDiffInMinutes: program.maxCommitDiff || conf.maxCommitDiffInMinutes, firstCommitAdditionInMinutes: program.firstCommitAdd || conf.firstCommitAdditionInMinutes, since: program.since || conf.since, - until: program.until || conf.until + until: program.until || conf.until, + branch: program.branch || conf.branch }; } @@ -206,15 +217,23 @@ function estimateHours(dates) { } // Promisify nodegit's API of getting all commits in repository -function getCommits(gitPath) { +function getCommits(gitPath, branch) { return git.Repository.open(gitPath) .then(function(repo) { var allReferences = getAllReferences(repo); - return Promise.filter(allReferences, function(reference) { - return reference.match(/refs\/heads\/.*/); - }) - .map(function(branchName) { + if (branch) { + filterPromise = Promise.filter(allReferences, function(reference) { + return (reference == ('refs/heads/' + branch)); + }); + } + else { + filterPromise = Promise.filter(allReferences, function(reference) { + return reference.match(/refs\/heads\/.*/); + }); + } + + return filterPromise.map(function(branchName) { return getBranchLatestCommit(repo, branchName); }) .map(function(branchLatestCommit) { From 1d4117af7a61329c09eb1e04edef99ed0bec49ec Mon Sep 17 00:00:00 2001 From: Guy Elsmore-Paddock Date: Mon, 31 Jul 2017 14:49:33 -0400 Subject: [PATCH 2/2] Clean-up documentation and examples for new arg --- README.md | 5 +++++ src/index.js | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 64412dc..64a8d7b 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,7 @@ Help -d, --max-commit-diff [max-commit-diff] maximum difference in minutes between commits counted to one session. Default: 120 -a, --first-commit-add [first-commit-add] how many minutes first commit of session should add to total. Default: 120 -s, --since [since-certain-date] Analyze data since certain date. [always|yesterday|tonight|lastweek|yyyy-mm-dd] Default: always' + -b, --branch [branch-name] Analyze only data on the specified branch. Default: all branches Examples: @@ -122,6 +123,10 @@ Help - Estimate hours work in repository since 2015-01-31 $ git hours --since 2015-01-31 + + - Estimate hours work in repository on the "master" branch + + $ git hours --branch master For more details, visit /~https://github.com/kimmobrunfeldt/git-hours diff --git a/src/index.js b/src/index.js index 2c8fb2b..524d60e 100755 --- a/src/index.js +++ b/src/index.js @@ -108,7 +108,7 @@ function parseArgs() { String ) .option( - '-b, --branch [branch name]', + '-b, --branch [branch-name]', 'Analyze only data on the specified branch. Default: ' + config.branch, String ); @@ -138,9 +138,9 @@ function parseArgs() { console.log(''); console.log(' $ git hours --since 2015-01-31'); console.log(''); - console.log(' - Estimate hours work in repository on the "develop" branch'); + console.log(' - Estimate hours work in repository on the "master" branch'); console.log(''); - console.log(' $ git hours --branch develop'); + console.log(' $ git hours --branch master'); console.log(''); console.log(' For more details, visit /~https://github.com/kimmobrunfeldt/git-hours'); console.log('');