-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
81 lines (68 loc) · 2.19 KB
/
index.js
File metadata and controls
81 lines (68 loc) · 2.19 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
77
78
79
80
81
import execa from 'execa';
import isGit from 'is-git-repository';
import { platform } from 'os';
import makepath from 'path';
import pathIsAbsolute from 'path-is-absolute';
const cwd = process.cwd();
const getCommitRange = (options = {}) => {
const {
path,
from,
to,
include,
includeMerges = true,
short,
type,
} = options;
const commits = [];
const thisFrom = from || '';
const thisTo = to || '';
// cannot use `inclue || true`
// because if you set include to false
// it will automatically change to true
const thisInclude = include === false ? include : true;
const thisShort = short === true ? short : false;
let thisPath = path || cwd;
let getCommits;
thisPath = pathIsAbsolute(thisPath) ? thisPath : makepath.join(cwd, thisPath);
if (!isGit(thisPath)) {
return [];
}
const format = type === 'text' ? 'B' : 'H';
const mergeFlag = includeMerges ? '' : '--no-merges';
try {
let commitRangeExec;
if (platform() === 'win32') {
if (!thisFrom) {
commitRangeExec = `pushd ${thisPath} & git --no-pager log --format=format:"%${format}{{gitCommitRangeEnd}}" ${mergeFlag}`;
} else {
commitRangeExec = `pushd ${thisPath} & git --no-pager log ${thisFrom}...${thisTo} --format=format:"%${format}{{gitCommitRangeEnd}}" ${mergeFlag}`;
}
} else {
if (!thisFrom) { // eslint-disable-line
commitRangeExec = `(cd ${thisPath} ; git --no-pager log --format=format:"%${format}{{gitCommitRangeEnd}}" ${mergeFlag})`;
} else {
commitRangeExec = `(cd ${thisPath} ; git --no-pager log ${thisFrom}...${thisTo} --format=format:"%${format}{{gitCommitRangeEnd}}" ${mergeFlag})`;
}
}
getCommits = execa.shellSync(commitRangeExec).stdout;
getCommits = getCommits
.split('{{gitCommitRangeEnd}}')
.map((s) => s.trim())
.filter(Boolean);
getCommits.forEach((commithash) => {
if (thisShort) {
return commits.push(commithash.substring(0, 7));
}
return commits.push(commithash);
});
if (!thisInclude) {
commits.shift();
commits.pop();
}
return commits;
} catch (err) {
return [];
}
};
export default getCommitRange;