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

Adds a filter for per-branch statistics #30

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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

Expand Down
35 changes: 27 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ var config = {

// Include commits since time x
since: 'always',
until: 'always'
until: 'always',
branch: null
};

function main() {
Expand All @@ -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';
});
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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 "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 @@ -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
};
}

Expand Down Expand Up @@ -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) {
Expand Down