summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRuslan Gainutdinov <ruslan.gainutdinov@futurice.com>2018-09-12 10:25:10 +0300
committerRuslan Gainutdinov <ruslan.gainutdinov@futurice.com>2018-09-12 10:26:56 +0300
commit826d8617b683202794161a59723769589597c665 (patch)
tree4ad557676d041e2e72d8cb01f86078a100f7217a /src
parentfb76ed35e9893f713f5ab6001e0334f947c5f1ab (diff)
downloadgit-hours-826d8617b683202794161a59723769589597c665.zip
git-hours-826d8617b683202794161a59723769589597c665.tar.gz
git-hours-826d8617b683202794161a59723769589597c665.tar.bz2
Added options: different git path, exclude merge requests, email aliases. Upgraded nodegit to 0.22
Diffstat (limited to 'src')
-rwxr-xr-xsrc/index.js89
1 files changed, 82 insertions, 7 deletions
diff --git a/src/index.js b/src/index.js
index ab6b671..7a3141d 100755
--- a/src/index.js
+++ b/src/index.js
@@ -19,7 +19,18 @@ var config = {
// Include commits since time x
since: 'always',
- until: 'always'
+ until: 'always',
+
+ // Include merge requests
+ mergeRequest: true,
+
+ // Git repo
+ gitPath: '.',
+
+ // Aliases of emails for grouping the same activity as one person
+ emailAliases: {
+ "linus@torvalds.com": "linus@linux.com"
+ }
};
function main() {
@@ -30,10 +41,29 @@ function main() {
config.since = parseSinceDate(config.since);
config.until = parseUntilDate(config.until);
- getCommits('.').then(function(commits) {
+ // Poor man`s multiple args support
+ // https://github.com/tj/commander.js/issues/531
+ for (var i = 0; i < process.argv.length; i++) {
+ var k = process.argv[i];
+ var n = i <= process.argv.length - 1 ? process.argv[i + 1] : undefined;
+ if (k == "-e" || k == "--email") {
+ parseEmailAlias(n);
+ } else
+ if (k.startsWith("--email=")) {
+ n = k.substring(k.indexOf("=") + 1)
+ parseEmailAlias(n);
+ }
+ }
+
+ getCommits(config.gitPath).then(function(commits) {
var commitsByEmail = _.groupBy(commits, function(commit) {
- return commit.author.email || 'unknown';
+ var email = commit.author.email || 'unknown'
+ if (config.emailAliases !== undefined && config.emailAliases[email] !== undefined) {
+ email = config.emailAliases[email]
+ }
+ return email;
});
+
var authorWorks = _.map(commitsByEmail, function(authorCommits, authorEmail) {
return {
email: authorEmail,
@@ -48,6 +78,7 @@ function main() {
// making the output easier to read, so it doesn't matter if it
// isn't sorted in some cases.
var sortedWork = {};
+
_.each(_.sortBy(authorWorks, 'hours'), function(authorWork) {
sortedWork[authorWork.email] = _.omit(authorWork, 'email');
});
@@ -55,6 +86,7 @@ function main() {
var totalHours = _.reduce(sortedWork, function(sum, authorWork) {
return sum + authorWork.hours;
}, 0);
+
sortedWork.total = {
hours: totalHours,
commits: commits.length
@@ -101,10 +133,28 @@ function parseArgs() {
String
)
.option(
+ '-e, --email [emailOther=emailMain]',
+ 'Group person by email address.' +
+ ' Default: none',
+ String
+ )
+ .option(
'-u, --until [until-certain-date]',
'Analyze data until certain date.' +
' [always|yesterday|today|lastweek|thisweek|yyyy-mm-dd] Default: ' + config.until,
String
+ )
+ .option(
+ '-m, --merge-request [false|true]',
+ 'Include merge requests into calculation. ' +
+ ' Default: ' + config.mergeRequest,
+ String
+ )
+ .option(
+ '-p, --path [git-repo]',
+ 'Git repository to analyze.' +
+ ' Default: ' + config.gitPath,
+ String
);
program.on('--help', function() {
@@ -156,21 +206,39 @@ function parseInputDate(inputDate) {
return moment(inputDate, DATE_FORMAT);
}
}
+
function parseSinceDate(since) {
return parseInputDate(since);
}
+
function parseUntilDate(until) {
return parseInputDate(until);
}
-function mergeDefaultsWithArgs(conf) {
+function parseEmailAlias(alias) {
+ if (alias.indexOf("=") > 0) {
+ var email = alias.substring(0, alias.indexOf("=")).trim();
+ var alias = alias.substring(alias.indexOf("=") + 1).trim();
+ // console.warn("Adding alias " + email + " -> " + alias);
+ if (config.emailAliases === undefined) {
+ config.emailAliases = {}
+ }
+ config.emailAliases[email] = alias
+ } else {
+ console.error("ERROR: Invalid alias: " + alias);
+ }
+}
+function mergeDefaultsWithArgs(conf) {
+ console.log(program.mergeRequest !== undefined ? !!program.mergeRequest : conf.mergeRequest)
return {
range: program.range,
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,
+ gitPath: program.path || conf.gitPath,
+ mergeRequest: program.mergeRequest !== undefined ? (program.mergeRequest == "true") : conf.mergeRequest
};
}
@@ -210,7 +278,7 @@ function getCommits(gitPath) {
return git.Repository.open(gitPath)
.then(function(repo) {
var allReferences = getAllReferences(repo);
-
+
return Promise.filter(allReferences, function(reference) {
return reference.match(/refs\/heads\/.*/);
})
@@ -233,7 +301,14 @@ function getCommits(gitPath) {
return item.sha;
});
- return uniqueCommits;
+ return uniqueCommits.filter(function (commit) {
+ // Exclude all commits starting with "Merge ..."
+ if (!config.mergeRequest && commit.message.startsWith("Merge ")) {
+ return false;
+ } else {
+ return true;
+ }
+ });
});
});
}