-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
76 lines (57 loc) · 1.59 KB
/
index.js
File metadata and controls
76 lines (57 loc) · 1.59 KB
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
'use strict';
var streamCommits = require('./lib/parser');
var gitSpawnedStream = require('git-spawned-stream');
function addFlag(args, flag, value) {
if (!value || !flag) {
return args.length;
}
if (Array.isArray(value)) {
return value.map(function addItem(item) {
return addFlag(args, flag, item);
}).pop();
}
if (value === true) {
return args.push(flag);
}
return args.push(flag + '=' + value);
}
function searchType(opts) {
switch (opts.searchIn) {
case 'author':
case 'authors':
return '--author';
case 'committer':
case 'committers':
return '--committer';
default:
return '--grep';
}
}
function escapeTerm(term) {
return term.replace(/'/g, '').replace(/"/g, '');
}
function streamHistory(repoPath, ops) {
var opts = ops || {};
var args = ['rev-list', '--header'];
addFlag(args, '--max-count', opts.limit);
addFlag(args, '--skip', opts.skip);
addFlag(args, '--since', opts.since || opts.after);
addFlag(args, '--until', opts.until || opts.before);
if (opts.searchTerm) {
var terms = [].concat(opts.searchTerm).map(escapeTerm);
var searchIn = searchType(opts);
var isRegex = Boolean(opts.regex);
addFlag(args, '--extended-regexp', isRegex);
addFlag(args, '--fixed-strings', !isRegex);
addFlag(args, '--regexp-ignore-case', true);
addFlag(args, searchIn, terms);
}
var posArgs = [].concat(
opts.rev || 'HEAD',
'--',
opts.path || opts.file || []
);
Array.prototype.push.apply(args, posArgs);
return streamCommits(gitSpawnedStream(repoPath, args));
}
module.exports = streamHistory;