/
GitUtilities.js
118 lines (99 loc) · 3.24 KB
/
GitUtilities.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
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
import ChildProcessUtilities from "./ChildProcessUtilities";
import logger from "./logger";
import escapeArgs from "command-join";
export default class GitUtilities {
@logger.logifySync()
static isDetachedHead(opts) {
const branchName = GitUtilities.getCurrentBranch(opts);
return branchName === "HEAD";
}
@logger.logifySync()
static isInitialized(opts) {
try {
// we only want the return code, so ignore stdout/stderr
ChildProcessUtilities.execSync("git rev-parse", Object.assign({}, opts, {
stdio: "ignore"
}));
return true;
} catch (err) {
return false;
}
}
@logger.logifySync()
static addFile(file, opts) {
ChildProcessUtilities.execSync("git add " + escapeArgs(file), opts);
}
@logger.logifySync()
static commit(message, opts) {
// Use echo to allow multi\nline strings.
ChildProcessUtilities.execSync("git commit -m \"$(echo \"" + message + "\")\"", opts);
}
@logger.logifySync()
static addTag(tag, opts) {
ChildProcessUtilities.execSync("git tag -a " + tag + " -m \"" + tag + "\"", opts);
}
@logger.logifySync()
static removeTag(tag, opts) {
ChildProcessUtilities.execSync("git tag -d " + tag, opts);
}
@logger.logifySync()
static hasTags(opts) {
return !!ChildProcessUtilities.execSync("git tag", opts);
}
@logger.logifySync()
static getLastTaggedCommit(opts) {
return ChildProcessUtilities.execSync("git rev-list --tags --max-count=1", opts);
}
@logger.logifySync()
static getLastTaggedCommitInBranch(opts) {
const tagName = GitUtilities.getLastTag(opts);
return ChildProcessUtilities.execSync("git rev-list -n 1 " + tagName, opts);
}
@logger.logifySync()
static getFirstCommit(opts) {
return ChildProcessUtilities.execSync("git rev-list --max-parents=0 HEAD", opts);
}
@logger.logifySync()
static pushWithTags(remote, tags, opts) {
const branch = GitUtilities.getCurrentBranch(opts);
ChildProcessUtilities.execSync(`git push ${remote} ${branch}`, opts);
ChildProcessUtilities.execSync(`git push ${remote} ${tags.join(" ")}`, opts);
}
@logger.logifySync()
static getLastTag(opts) {
return ChildProcessUtilities.execSync("git describe --tags --abbrev=0", opts);
}
@logger.logifySync()
static describeTag(commit, opts) {
return ChildProcessUtilities.execSync("git describe --tags " + commit, opts);
}
@logger.logifySync()
static diffSinceIn(since, location, opts) {
return ChildProcessUtilities.execSync(`git diff --name-only ${since} -- ${escapeArgs(location)}`, opts);
}
@logger.logifySync()
static getCurrentBranch(opts) {
return ChildProcessUtilities.execSync("git rev-parse --abbrev-ref HEAD", opts);
}
@logger.logifySync()
static getCurrentSHA(opts) {
return ChildProcessUtilities.execSync("git rev-parse HEAD", opts);
}
@logger.logifySync()
static checkoutChanges(changes, opts) {
ChildProcessUtilities.execSync("git checkout -- " + changes, opts);
}
@logger.logifySync()
static init(opts) {
return ChildProcessUtilities.execSync("git init", opts);
}
@logger.logifySync()
static hasCommit(opts) {
try {
ChildProcessUtilities.execSync("git log", opts);
return true;
} catch (e) {
return false;
}
}
}