summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKimmo Brunfeldt <kimmobrunfeldt@gmail.com>2014-07-06 21:21:00 +0300
committerKimmo Brunfeldt <kimmobrunfeldt@gmail.com>2014-07-06 21:21:00 +0300
commit39b66d4f88c926a831345eaf1add4d4d058da0e7 (patch)
tree935744f71a0ca3e5cb2e21b217ff2233332e0a64
parent34d4ede8d05bdc746d97efe824d1c00dd563deeb (diff)
downloadgit-hours-39b66d4f88c926a831345eaf1add4d4d058da0e7.zip
git-hours-39b66d4f88c926a831345eaf1add4d4d058da0e7.tar.gz
git-hours-39b66d4f88c926a831345eaf1add4d4d058da0e7.tar.bz2
Add logic for counting hours spent on git repository
-rw-r--r--index.js38
1 files changed, 33 insertions, 5 deletions
diff --git a/index.js b/index.js
index 08caa68..c7a6b03 100644
--- a/index.js
+++ b/index.js
@@ -3,13 +3,21 @@ var git = require('nodegit');
var moment = require('moment');
var _ = require('lodash');
+var config = {
+ // Maximum time diff between 2 subsequent commits in minutes which are
+ // counted to be in the same coding "session"
+ maxCommitDiffInMinutes: 240,
+
+ // How many minutes should be added for the first commit of coding session
+ firstCommitAdditionInMinutes: 60
+}
+
function main() {
commits('.').then(function(commits) {
- console.log(commits);
-
var work = {
total: {
- hours: estimateHours(_.pluck(commits, 'date'))
+ hours: estimateHours(_.pluck(commits, 'date')),
+ commits: commits.length
}
};
@@ -21,8 +29,28 @@ function main() {
// Estimates spent working hours based on commit dates
function estimateHours(dates) {
- var sortedDates = dates.map(moment).sort();
- console.log(sortedDates);
+ if (dates.length < 2) {
+ return 0;
+ }
+
+ // Oldest commit first, newest last
+ var sortedDates = dates.sort().reverse();
+ var hours = _.reduce(dates, function(hours, date, index) {
+ var previousDate = dates[index - 1];
+ var diffInMinutes = (date - previousDate) / 1000 / 60;
+
+ // Check if commits are counted to be in same coding session
+ if (diffInMinutes < config.maxCommitDiffInMinutes) {
+ return hours + (diffInMinutes / 60);
+ }
+
+ // The date difference is too big to be inside single coding session
+ // The work of first commit of a session cannot be seen in git history,
+ // so we make a blunt estimate of it
+ return hours + (config.firstCommitAdditionInMinutes / 60);
+ }, 0);
+
+ return hours;
}