-
Notifications
You must be signed in to change notification settings - Fork 48
/
git_adapter.js
81 lines (70 loc) · 2.59 KB
/
git_adapter.js
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
/*
* code-forensics
* Copyright (C) 2016-2017 Silvio Montanari
* Distributed under the GNU General Public License v3.0
* see http://www.gnu.org/licenses/gpl.html
*/
var csvString = require('csv-string'),
StringDecoder = require('string_decoder').StringDecoder,
logger = require('../../log').Logger,
command = require('../../command');
var GIT_COMMANDS_ARGS = {
gitlog_analysis: ['log', '--all', '--numstat', '--date=short', '--no-renames', '--pretty=format:--%h--%ad--%an'],
gitlog_messages: ['log', '--date=short', '--pretty=format:%s'],
gitlog_revisions: ['log', '--date=iso-strict', '--pretty=format:%h,%ad'],
git_show: ['show']
};
command.Command.definitions.addDefinition('git', {
cmd: 'git',
args: [],
installCheck: function() {
this.verifyExecutable('git', 'Cannot find the git commmand.');
}
});
module.exports = function(repository) {
var rootDir = repository.rootPath;
command.Command.ensure('git');
var decoder = new StringDecoder();
var timePeriodArguments = function(timePeriod) {
var isoPeriod = timePeriod.toISOFormat();
return ['--after=' + isoPeriod.startDate, '--before=' + isoPeriod.endDate];
};
var logMessageWithTimePeriod = function(msg, timePeriod) {
var displayPeriod = timePeriod.toDisplayFormat();
logger.info(msg, ' from ' + displayPeriod.startDate + ' to ' + displayPeriod.endDate);
};
this.revisions = function(filepath, timePeriod) {
var gitOutput = command.run('git',
GIT_COMMANDS_ARGS.gitlog_revisions
.concat(timePeriodArguments(timePeriod))
.concat([filepath]),
{ cwd: rootDir });
var output = decoder.write(gitOutput).trim();
if (output.length > 0) {
return csvString.parse(output, ',').map(function(row) {
return { revisionId: row[0], date: row[1] };
});
}
return [];
};
this.showRevisionStream = function(revisionId, filepath) {
logger.info('Fetching git revision ', revisionId);
return command.stream('git',
GIT_COMMANDS_ARGS.git_show.concat([revisionId + ':' + filepath]),
{ cwd: rootDir });
};
this.logStream = function(timePeriod) {
logMessageWithTimePeriod('Fetching git log', timePeriod);
return command.stream('git',
GIT_COMMANDS_ARGS.gitlog_analysis
.concat(timePeriodArguments(timePeriod)),
{ cwd: rootDir });
};
this.commitMessagesStream = function(timePeriod) {
logMessageWithTimePeriod('Fetching git messages', timePeriod);
return command.stream('git',
GIT_COMMANDS_ARGS.gitlog_messages
.concat(timePeriodArguments(timePeriod)),
{ cwd: rootDir });
};
};