-
Notifications
You must be signed in to change notification settings - Fork 37
/
git.js
331 lines (280 loc) · 8.75 KB
/
git.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/**
* Lifted and tweaked from semantic-release because we follow how they test their internals.
* https://github.com/semantic-release/semantic-release/blob/master/test/helpers/git-utils.js
*/
const { check } = require("blork");
const tempy = require("tempy");
const execa = require("execa");
const fileUrl = require("file-url");
const gitLogParser = require("git-log-parser");
const { array: getStreamArray } = require("get-stream");
const env = {};
/**
* @typedef {Object} Commit
* @property {string} branch The commit branch.
* @property {string} hash The commit hash.
* @property {string} message The commit message.
*/
// Init.
/**
* Create a Git repository.
* _Created in a temp folder._
*
* @param {string} branch="master" The branch to initialize the repository to.
* @return {Promise<string>} Promise that resolves to string pointing to the CWD for the created Git repository.
*/
function gitInit(branch = "master") {
// Check params.
check(branch, "branch: kebab");
// Init Git in a temp directory.
const cwd = tempy.directory();
execa.sync("git", ["init", "-b", "master"], { cwd, env });
execa.sync("git", ["checkout", "-b", branch], { cwd, env });
// Disable GPG signing for commits.
gitConfig(cwd, "commit.gpgsign", false);
// Set user profile.
gitConfig(cwd, "user.name", "Test");
gitConfig(cwd, "user.email", "test@example.com");
// Return directory.
return cwd;
}
/**
* Create a remote Git repository.
* _Created in a temp folder._
*
* @return {Promise<string>} Promise that resolves to string URL of the remote origin.
*/
function gitInitRemote() {
// Init bare Git repository in a temp directory.
const cwd = tempy.directory();
execa.sync("git", ["init", "--bare"], { cwd, env });
// Turn remote path into a file URL.
const url = fileUrl(cwd);
// Return URL for remote.
return url;
}
/**
* Create a remote Git repository and set it as the origin for a Git repository.
* _Created in a temp folder._
*
* @param {string} cwd The cwd to create and set the origin for.
* @param {string} releaseBranch="null" Optional branch to be added in case of prerelease is activated for a branch.
* @return {Promise<string>} Promise that resolves to string URL of the of the remote origin.
*/
function gitInitOrigin(cwd, releaseBranch = null) {
// Check params.
check(cwd, "cwd: absolute");
// Turn remote path into a file URL.
const url = gitInitRemote();
// Set origin on local repo.
execa.sync("git", ["remote", "add", "origin", url], { cwd, env });
// Set up a release branch. Return to master afterwards.
if (releaseBranch) {
execa.sync("git", ["checkout", "-b", releaseBranch], { cwd, env });
execa.sync("git", ["checkout", "master"], { cwd, env });
}
execa.sync("git", ["push", "--all", "origin"], { cwd, env });
// Return URL for remote.
return url;
}
// Add.
/**
* Add files to staged commit in a Git repository.
*
* @param {string} cwd The cwd to create and set the origin for.
* @param {string} file="." The file to add, defaulting to "." (all files).
* @return {Promise<void>} Promise that resolves when done.
*/
function gitAdd(cwd, file = ".") {
// Check params.
check(cwd, "cwd: absolute");
// Await command.
execa.sync("git", ["add", file], { cwd, env });
}
// Commits.
/**
* Create commit on a Git repository.
* _Allows empty commits without any files added._
*
* @param {string} cwd The CWD of the Git repository.
* @param {string} message Commit message.
* @returns {Promise<string>} Promise that resolves to the SHA for the commit.
*/
function gitCommit(cwd, message) {
// Check params.
check(cwd, "cwd: absolute");
check(message, "message: string+");
// Await the command.
execa.sync("git", ["commit", "-m", message, "--no-gpg-sign"], { cwd, env });
// Return HEAD SHA.
return gitGetHead(cwd);
}
/**
* `git add .` followed by `git commit`
* _Allows empty commits without any files added._
*
* @param {string} cwd The CWD of the Git repository.
* @param {string} message Commit message.
* @returns {Promise<string>} Promise that resolves to the SHA for the commit.
*/
function gitCommitAll(cwd, message) {
// Check params.
check(cwd, "cwd: absolute");
check(message, "message: string+");
// Await command.
gitAdd(cwd);
// Await command and return the SHA hash.
return gitCommit(cwd, message);
}
// Push.
/**
* Push to a remote Git repository.
*
* @param {string} cwd The CWD of the Git repository.
* @param {string} remote The remote repository URL or name.
* @param {string} branch The branch to push.
* @returns {Promise<void>} Promise that resolves when done.
* @throws {Error} if the push failed.
*/
function gitPush(cwd, remote = "origin", branch = "master") {
// Check params.
check(cwd, "cwd: absolute");
check(remote, "remote: string");
check(branch, "branch: lower");
// Await command.
execa.sync("git", ["push", "--tags", remote, `HEAD:${branch}`], { cwd, env });
}
// Branches.
/**
* Create a branch in a local Git repository.
*
* @param {string} cwd The CWD of the Git repository.
* @param {string} branch Branch name to create.
* @returns {Promise<void>} Promise that resolves when done.
*/
function gitBranch(cwd, branch) {
// Check params.
check(cwd, "cwd: absolute");
check(branch, "branch: lower");
// Await command.
execa.sync("git", ["branch", branch], { cwd, env });
}
/**
* Checkout a branch in a local Git repository.
*
* @param {string} cwd The CWD of the Git repository.
* @param {string} branch Branch name to checkout.
* @returns {Promise<void>} Promise that resolves when done.
*/
function gitCheckout(cwd, branch) {
// Check params.
check(cwd, "cwd: absolute");
check(branch, "branch: lower");
// Await command.
execa.sync("git", ["checkout", branch], { cwd, env });
}
// Hashes.
/**
* Get the current HEAD SHA in a local Git repository.
*
* @param {string} cwd The CWD of the Git repository.
* @return {Promise<string>} Promise that resolves to the SHA of the head commit.
*/
function gitGetHead(cwd) {
// Check params.
check(cwd, "cwd: absolute");
// Await command and return HEAD SHA.
return execa.sync("git", ["rev-parse", "HEAD"], { cwd, env }).stdout;
}
// Tags.
/**
* Create a tag on the HEAD commit in a local Git repository.
*
* @param {string} cwd The CWD of the Git repository.
* @param {string} tagName The tag name to create.
* @param {string} hash=false SHA for the commit on which to create the tag. If falsy the tag is created on the latest commit.
* @returns {Promise<void>} Promise that resolves when done.
*/
function gitTag(cwd, tagName, hash = undefined) {
// Check params.
check(cwd, "cwd: absolute");
check(tagName, "tagName: string+");
check(hash, "hash: alphanumeric{40}?");
// Run command.
execa.sync("git", hash ? ["tag", "-f", tagName, hash] : ["tag", tagName], { cwd, env });
}
/**
* Get the tag associated with a commit SHA.
*
* @param {string} cwd The CWD of the Git repository.
* @param {string} hash The commit SHA for which to retrieve the associated tag.
* @return {Promise<string>} The tag associated with the SHA in parameter or `null`.
*/
function gitGetTags(cwd, hash) {
// Check params.
check(cwd, "cwd: absolute");
check(hash, "hash: alphanumeric{40}");
// Run command.
return execa.sync("git", ["describe", "--tags", "--exact-match", hash], { cwd, env }).stdout;
}
/**
* Get the first commit SHA tagged `tagName` in a local Git repository.
*
* @param {string} cwd The CWD of the Git repository.
* @param {string} tagName Tag name for which to retrieve the commit sha.
* @return {Promise<string>} Promise that resolves to the SHA of the first commit associated with `tagName`.
*/
function gitGetTagHash(cwd, tagName) {
// Check params.
check(cwd, "cwd: absolute");
check(tagName, "tagName: string+");
// Run command.
return execa.sync("git", ["rev-list", "-1", tagName], { cwd, env }).stdout;
}
// Configs.
/**
* Add a Git config setting.
*
* @param {string} cwd The CWD of the Git repository.
* @param {string} name Config name.
* @param {any} value Config value.
* @returns {Promise<void>} Promise that resolves when done.
*/
function gitConfig(cwd, name, value) {
// Check params.
check(cwd, "cwd: absolute");
check(name, "name: string+");
// Run command.
execa.sync("git", ["config", "--add", name, value], { cwd, env });
}
/**
* Get a Git config setting.
*
* @param {string} cwd The CWD of the Git repository.
* @param {string} name Config name.
* @returns {Promise<void>} Promise that resolves when done.
*/
function gitGetConfig(cwd, name) {
// Check params.
check(cwd, "cwd: absolute");
check(name, "name: string+");
// Run command.
execa.sync("git", ["config", name], { cwd, env }).stdout;
}
// Exports.
module.exports = {
gitInit,
gitInitRemote,
gitInitOrigin,
gitAdd,
gitCommit,
gitCommitAll,
gitPush,
gitCheckout,
gitGetHead,
gitGetTags,
gitTag,
gitGetTagHash,
gitConfig,
gitGetConfig,
};