1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
#!/usr/bin/env node
var Promise = require('bluebird');
var git = require('nodegit');
var program = require('commander');
var _ = require('lodash');
var moment = require('moment');
var exec = Promise.promisify(require('child_process').exec);
var DATE_FORMAT = 'YYYY-MM-DD';
var config = {
// Maximum time diff between 2 subsequent commits in minutes which are
// counted to be in the same coding "session"
maxCommitDiffInMinutes: 2 * 60,
// How many minutes should be added for the first commit of coding session
firstCommitAdditionInMinutes: 2 * 60,
// Include commits since time x
since: 'always'
};
function main() {
parseArgs();
config = mergeDefaultsWithArgs(config);
config.since = parseSinceDate(config.since);
getCommits('.').then(function(commits) {
var commitsByEmail = _.groupBy(commits, function(commit) {
return commit.author.email || 'unknown';
});
var authorWorks = _.map(commitsByEmail, function(authorCommits, authorEmail) {
return {
email: authorEmail,
name: authorCommits[0].author.name,
hours: estimateHours(_.pluck(authorCommits, 'date')),
commits: authorCommits.length
};
});
// XXX: This relies on the implementation detail that json is printed
// in the same order as the keys were added. This is anyway just for
// 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');
});
var totalHours = _.reduce(sortedWork, function(sum, authorWork) {
return sum + authorWork.hours;
}, 0);
sortedWork.total = {
hours: totalHours,
commits: commits.length
};
console.log(JSON.stringify(sortedWork, undefined, 2));
}).catch(function(e) {
console.error(e.stack);
});
}
function parseArgs() {
function int(val) {
return parseInt(val, 10);
}
program
.version(require('../package.json').version)
.usage('[options]')
.option(
'-d, --max-commit-diff [max-commit-diff]',
'maximum difference in minutes between commits counted to one' +
' session. Default: ' + config.maxCommitDiffInMinutes,
int
)
.option(
'-a, --first-commit-add [first-commit-add]',
'how many minutes first commit of session should add to total.' +
' Default: ' + config.firstCommitAdditionInMinutes,
int
)
.option(
'-s, --since [since-certain-date]',
'Analyze data since certain date.' +
' [always|yesterday|today|lastweek|thisweek|yyyy-mm-dd] Default: ' + config.since,
String
);
program.on('--help', function() {
console.log(' Examples:');
console.log('');
console.log(' - Estimate hours of project');
console.log('');
console.log(' $ git hours');
console.log('');
console.log(' - Estimate hours in repository where developers commit' +
' more seldom: they might have 4h(240min) pause between commits');
console.log('');
console.log(' $ git hours --max-commit-diff 240');
console.log('');
console.log(' - Estimate hours in repository where developer works 5' +
' hours before first commit in day');
console.log('');
console.log(' $ git hours --first-commit-add 300');
console.log('');
console.log(' - Estimate hours work in repository since yesterday');
console.log('');
console.log(' $ git hours --since yesterday');
console.log('');
console.log(' - Estimate hours work in repository since 2015-01-31');
console.log('');
console.log(' $ git hours --since 2015-01-31');
console.log('');
console.log(' For more details, visit https://github.com/kimmobrunfeldt/git-hours');
console.log('');
});
program.parse(process.argv);
}
function parseSinceDate(since) {
switch (since) {
case 'today':
return moment().startOf('day');
case 'yesterday':
return moment().startOf('day').subtract(1, 'day');
case 'thisweek':
return moment().startOf('week');
case 'lastweek':
return moment().startOf('week').subtract(1, 'week');
case 'always':
return 'always';
default:
// XXX: Moment tries to parse anything, results might be weird
return moment(since, DATE_FORMAT);
}
}
function mergeDefaultsWithArgs(conf) {
return {
range: program.range,
maxCommitDiffInMinutes: program.maxCommitDiff || conf.maxCommitDiffInMinutes,
firstCommitAdditionInMinutes: program.firstCommitAdd || conf.firstCommitAdditionInMinutes,
since: program.since || conf.since
};
}
// Estimates spent working hours based on commit dates
function estimateHours(dates) {
if (dates.length < 2) {
return 0;
}
// Oldest commit first, newest last
var sortedDates = dates.sort(function(a, b) {
return a - b;
});
var allButLast = _.take(sortedDates, sortedDates.length - 1);
var totalHours = _.reduce(allButLast, function(hours, date, index) {
var nextDate = sortedDates[index + 1];
var diffInMinutes = (nextDate - date) / 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 Math.round(totalHours);
}
// Promisify nodegit's API of getting all commits in repository
function getCommits(gitPath) {
return git.Repository.open(gitPath)
.then(function(repo) {
var branchNames = getBranchNames(gitPath);
return Promise.map(branchNames, function(branchName) {
return getBranchLatestCommit(repo, branchName);
})
.map(function(branchLatestCommit) {
return getBranchCommits(branchLatestCommit);
})
.reduce(function(allCommits, branchCommits) {
_.each(branchCommits, function(commit) {
allCommits.push(commit);
});
return allCommits;
}, [])
.then(function(commits) {
// Multiple branches might share commits, so take unique
var uniqueCommits = _.uniq(commits, function(item, key, a) {
return item.sha;
});
return uniqueCommits;
});
});
}
function getBranchNames(gitPath) {
var cmd = "git branch --no-color | awk -F ' +' '! /\\(no branch\\)/ {print $2}'";
return new Promise(function(resolve, reject) {
exec(cmd, {cwd: gitPath}, function(err, stdout, stderr) {
if (err) {
reject(err);
}
resolve(stdout
.split('\n')
.filter(function(e) { return e; }) // Remove empty
.map(function(str) { return str.trim(); }) // Trim whitespace
);
});
});
}
function getBranchLatestCommit(repo, branchName) {
return repo.getBranch(branchName).then(function(reference) {
return repo.getBranchCommit(reference.name());
});
}
function getBranchCommits(branchLatestCommit) {
return new Promise(function(resolve, reject) {
var history = branchLatestCommit.history();
var commits = [];
history.on('commit', function(commit) {
var author = null;
if (!_.isNull(commit.author())) {
author = {
name: commit.author().name(),
email: commit.author().email()
};
}
var commitData = {
sha: commit.sha(),
date: commit.date(),
message: commit.message(),
author: author
};
var sinceAlways = config.since === 'always' || !config.since;
if (sinceAlways || moment(commitData.date.toISOString()).isAfter(config.since)) {
commits.push(commitData);
}
});
history.on('end', function() {
resolve(commits);
});
history.on('error', function(err) {
reject(err);
});
// Start emitting events.
history.start();
});
}
main();
|