-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
78 lines (65 loc) · 2.95 KB
/
index.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
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 taggedGitCommits = ({ path, lookBehind, local, remote } = {}) => {
let getCommits;
let thisPath = path || cwd;
thisPath = pathIsAbsolute(thisPath) ? thisPath : makepath.join(cwd, thisPath);
const thisLookBehind = lookBehind || 1;
const thisLocal = local === undefined ? true : local;
const thisRemote = remote || 'origin';
const taggedCommits = [];
if (!isGit(thisPath)) {
return [];
}
try {
let getTaggedCommitsExec;
let getCommitsExec;
if (thisLocal) {
if (platform() === 'win32') {
getCommitsExec = `pushd ${thisPath} & git show-ref`;
getTaggedCommitsExec = `pushd ${thisPath} & git for-each-ref --format="%(*committerdate:raw)%(committerdate:raw) %(refname) %(*objectname) %(objectname)" refs/tags --sort=-v:refname | awk "{ print $4, $3; }"`;
} else {
getCommitsExec = `(cd ${thisPath} ; git show-ref)`;
getTaggedCommitsExec = `(cd ${thisPath} ; git for-each-ref --format='%(*committerdate:raw)%(committerdate:raw) %(refname) %(*objectname) %(objectname)' refs/tags --sort=-v:refname | awk '{ print $4, $3; }')`;
}
getCommits = execa.shellSync(getCommitsExec).stdout;
} else {
if (platform() === 'win32') {
getCommitsExec = `pushd ${thisPath} & git ls-remote ${thisRemote}`;
getTaggedCommitsExec = `pushd ${thisPath} & git for-each-ref --format="%(*committerdate:raw)%(committerdate:raw) %(refname) %(*objectname) %(objectname)" refs/tags --sort=-v:refname --merged=${thisRemote} | awk "{ print $4, $3; }"`;
} else {
getCommitsExec = `(cd ${thisPath} ; git ls-remote)`;
getTaggedCommitsExec = `(cd ${thisPath} ; git for-each-ref --format='%(*committerdate:raw)%(committerdate:raw) %(refname) %(*objectname) %(objectname)' refs/tags --sort=-v:refname --merged=${thisRemote} | awk '{ print $4, $3; }')`;
}
getCommits = execa.shellSync(getCommitsExec).stdout;
}
if (getCommits.includes('refs/tags')) {
const getTaggedCommits = execa.shellSync(getTaggedCommitsExec).stdout;
let taggedCommitsSimple = getTaggedCommits.split('\n');
taggedCommitsSimple = taggedCommitsSimple.reverse();
taggedCommitsSimple = taggedCommitsSimple.slice(-Math.abs(thisLookBehind));
taggedCommitsSimple.forEach((taggedCommit) => {
const commit = taggedCommit.split(' ')[0];
const shortCommit = commit.substring(0, 7);
const refsTags = taggedCommit.split(' ')[1];
const version = refsTags.split('refs/tags/')[1];
taggedCommits.push({
commit,
shortCommit,
hash: commit,
shortHash: shortCommit,
version,
refsTags,
});
});
}
return taggedCommits;
} catch (e) {
return [];
}
};
export default taggedGitCommits;