Skip to content

Commit

Permalink
Merge GuyPaddock:feature/per-branch-filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruslan Gainutdinov committed Sep 12, 2018
2 parents 305f88b + 1d4117a commit 20669d0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ Help
-u, --until [until-certain-date] Analyze data until certain date. [always|yesterday|today|lastweek|thisweek|yyyy-mm-dd] Default: always
-m, --merge-request [false|true] Include merge requests into calculation. Default: true
-p, --path [git-repo] Git repository to analyze. Default: .
-b, --branch [branch-name] Analyze only data on the specified branch. Default: all branches

Examples:

Expand All @@ -126,6 +127,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

Expand Down
38 changes: 29 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ var config = {
// Aliases of emails for grouping the same activity as one person
emailAliases: {
"linus@torvalds.com": "linus@linux.com"
}
},

branch: null
};

function main() {
Expand All @@ -55,7 +57,7 @@ function main() {
}
}

getCommits(config.gitPath).then(function(commits) {
getCommits(config.gitPath, config.branch).then(function(commits) {
var commitsByEmail = _.groupBy(commits, function(commit) {
var email = commit.author.email || 'unknown'
if (config.emailAliases !== undefined && config.emailAliases[email] !== undefined) {
Expand Down Expand Up @@ -155,6 +157,11 @@ function parseArgs() {
'Git repository to analyze.' +
' Default: ' + config.gitPath,
String
)
.option(
'-b, --branch [branch-name]',
'Analyze only data on the specified branch. Default: ' + config.branch,
String
);

program.on('--help', function() {
Expand Down Expand Up @@ -182,6 +189,10 @@ function parseArgs() {
console.log('');
console.log(' $ git hours --since 2015-01-31');
console.log('');
console.log(' - Estimate hours work in repository on the "master" branch');
console.log('');
console.log(' $ git hours --branch master');
console.log('');
console.log(' For more details, visit /~https://github.com/kimmobrunfeldt/git-hours');
console.log('');
});
Expand Down Expand Up @@ -237,7 +248,8 @@ function mergeDefaultsWithArgs(conf) {
since: program.since || conf.since,
until: program.until || conf.until,
gitPath: program.path || conf.gitPath,
mergeRequest: program.mergeRequest !== undefined ? (program.mergeRequest == "true") : conf.mergeRequest
mergeRequest: program.mergeRequest !== undefined ? (program.mergeRequest == "true") : conf.mergeRequest,
branch: program.branch || conf.branch
};
}

Expand Down Expand Up @@ -273,15 +285,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) {
Expand Down

0 comments on commit 20669d0

Please sign in to comment.