-
-
Notifications
You must be signed in to change notification settings - Fork 313
/
git.js
622 lines (548 loc) · 17.1 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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
const { GitExecutor } = require('./lib/runners/git-executor');
const { SimpleGitApi } = require('./lib/simple-git-api');
const { Scheduler } = require('./lib/runners/scheduler');
const { configurationErrorTask } = require('./lib/tasks/task');
const {
asArray,
filterArray,
filterPrimitives,
filterString,
filterStringOrStringArray,
filterType,
getTrailingOptions,
trailingFunctionArgument,
trailingOptionsArgument,
} = require('./lib/utils');
const { applyPatchTask } = require('./lib/tasks/apply-patch');
const {
branchTask,
branchLocalTask,
deleteBranchesTask,
deleteBranchTask,
} = require('./lib/tasks/branch');
const { checkIgnoreTask } = require('./lib/tasks/check-ignore');
const { checkIsRepoTask } = require('./lib/tasks/check-is-repo');
const { cloneTask, cloneMirrorTask } = require('./lib/tasks/clone');
const { cleanWithOptionsTask, isCleanOptionsArray } = require('./lib/tasks/clean');
const { diffSummaryTask } = require('./lib/tasks/diff');
const { fetchTask } = require('./lib/tasks/fetch');
const { moveTask } = require('./lib/tasks/move');
const { pullTask } = require('./lib/tasks/pull');
const { pushTagsTask } = require('./lib/tasks/push');
const {
addRemoteTask,
getRemotesTask,
listRemotesTask,
remoteTask,
removeRemoteTask,
} = require('./lib/tasks/remote');
const { getResetMode, resetTask } = require('./lib/tasks/reset');
const { stashListTask } = require('./lib/tasks/stash-list');
const {
addSubModuleTask,
initSubModuleTask,
subModuleTask,
updateSubModuleTask,
} = require('./lib/tasks/sub-module');
const { addAnnotatedTagTask, addTagTask, tagListTask } = require('./lib/tasks/tag');
const { straightThroughBufferTask, straightThroughStringTask } = require('./lib/tasks/task');
function Git(options, plugins) {
this._plugins = plugins;
this._executor = new GitExecutor(
options.baseDir,
new Scheduler(options.maxConcurrentProcesses),
plugins
);
this._trimmed = options.trimmed;
}
(Git.prototype = Object.create(SimpleGitApi.prototype)).constructor = Git;
/**
* Sets the path to a custom git binary, should either be `git` when there is an installation of git available on
* the system path, or a fully qualified path to the executable.
*/
Git.prototype.customBinary = function (command) {
this._plugins.reconfigure('binary', command);
return this;
};
/**
* Sets an environment variable for the spawned child process, either supply both a name and value as strings or
* a single object to entirely replace the current environment variables.
*
* @param {string|Object} name
* @param {string} [value]
* @returns {Git}
*/
Git.prototype.env = function (name, value) {
if (arguments.length === 1 && typeof name === 'object') {
this._executor.env = name;
} else {
(this._executor.env = this._executor.env || {})[name] = value;
}
return this;
};
/**
* List the stash(s) of the local repo
*/
Git.prototype.stashList = function (options) {
return this._runTask(
stashListTask(
trailingOptionsArgument(arguments) || {},
(filterArray(options) && options) || []
),
trailingFunctionArgument(arguments)
);
};
function createCloneTask(api, task, repoPath, localPath) {
if (typeof repoPath !== 'string') {
return configurationErrorTask(`git.${api}() requires a string 'repoPath'`);
}
return task(repoPath, filterType(localPath, filterString), getTrailingOptions(arguments));
}
/**
* Clone a git repo
*/
Git.prototype.clone = function () {
return this._runTask(
createCloneTask('clone', cloneTask, ...arguments),
trailingFunctionArgument(arguments)
);
};
/**
* Mirror a git repo
*/
Git.prototype.mirror = function () {
return this._runTask(
createCloneTask('mirror', cloneMirrorTask, ...arguments),
trailingFunctionArgument(arguments)
);
};
/**
* Moves one or more files to a new destination.
*
* @see https://git-scm.com/docs/git-mv
*
* @param {string|string[]} from
* @param {string} to
*/
Git.prototype.mv = function (from, to) {
return this._runTask(moveTask(from, to), trailingFunctionArgument(arguments));
};
/**
* Internally uses pull and tags to get the list of tags then checks out the latest tag.
*
* @param {Function} [then]
*/
Git.prototype.checkoutLatestTag = function (then) {
var git = this;
return this.pull(function () {
git.tags(function (err, tags) {
git.checkout(tags.latest, then);
});
});
};
/**
* Pull the updated contents of the current repo
*/
Git.prototype.pull = function (remote, branch, options, then) {
return this._runTask(
pullTask(
filterType(remote, filterString),
filterType(branch, filterString),
getTrailingOptions(arguments)
),
trailingFunctionArgument(arguments)
);
};
/**
* Fetch the updated contents of the current repo.
*
* @example
* .fetch('upstream', 'master') // fetches from master on remote named upstream
* .fetch(function () {}) // runs fetch against default remote and branch and calls function
*
* @param {string} [remote]
* @param {string} [branch]
*/
Git.prototype.fetch = function (remote, branch) {
return this._runTask(
fetchTask(
filterType(remote, filterString),
filterType(branch, filterString),
getTrailingOptions(arguments)
),
trailingFunctionArgument(arguments)
);
};
/**
* Disables/enables the use of the console for printing warnings and errors, by default messages are not shown in
* a production environment.
*
* @param {boolean} silence
* @returns {Git}
*/
Git.prototype.silent = function (silence) {
console.warn(
'simple-git deprecation notice: git.silent: logging should be configured using the `debug` library / `DEBUG` environment variable, this will be an error in version 3'
);
return this;
};
/**
* List all tags. When using git 2.7.0 or above, include an options object with `"--sort": "property-name"` to
* sort the tags by that property instead of using the default semantic versioning sort.
*
* Note, supplying this option when it is not supported by your Git version will cause the operation to fail.
*
* @param {Object} [options]
* @param {Function} [then]
*/
Git.prototype.tags = function (options, then) {
return this._runTask(
tagListTask(getTrailingOptions(arguments)),
trailingFunctionArgument(arguments)
);
};
/**
* Rebases the current working copy. Options can be supplied either as an array of string parameters
* to be sent to the `git rebase` command, or a standard options object.
*/
Git.prototype.rebase = function () {
return this._runTask(
straightThroughStringTask(['rebase', ...getTrailingOptions(arguments)]),
trailingFunctionArgument(arguments)
);
};
/**
* Reset a repo
*/
Git.prototype.reset = function (mode) {
return this._runTask(
resetTask(getResetMode(mode), getTrailingOptions(arguments)),
trailingFunctionArgument(arguments)
);
};
/**
* Revert one or more commits in the local working copy
*/
Git.prototype.revert = function (commit) {
const next = trailingFunctionArgument(arguments);
if (typeof commit !== 'string') {
return this._runTask(configurationErrorTask('Commit must be a string'), next);
}
return this._runTask(
straightThroughStringTask(['revert', ...getTrailingOptions(arguments, 0, true), commit]),
next
);
};
/**
* Add a lightweight tag to the head of the current branch
*/
Git.prototype.addTag = function (name) {
const task =
typeof name === 'string'
? addTagTask(name)
: configurationErrorTask('Git.addTag requires a tag name');
return this._runTask(task, trailingFunctionArgument(arguments));
};
/**
* Add an annotated tag to the head of the current branch
*/
Git.prototype.addAnnotatedTag = function (tagName, tagMessage) {
return this._runTask(
addAnnotatedTagTask(tagName, tagMessage),
trailingFunctionArgument(arguments)
);
};
/**
* Delete a local branch
*/
Git.prototype.deleteLocalBranch = function (branchName, forceDelete, then) {
return this._runTask(
deleteBranchTask(branchName, typeof forceDelete === 'boolean' ? forceDelete : false),
trailingFunctionArgument(arguments)
);
};
/**
* Delete one or more local branches
*/
Git.prototype.deleteLocalBranches = function (branchNames, forceDelete, then) {
return this._runTask(
deleteBranchesTask(branchNames, typeof forceDelete === 'boolean' ? forceDelete : false),
trailingFunctionArgument(arguments)
);
};
/**
* List all branches
*
* @param {Object | string[]} [options]
* @param {Function} [then]
*/
Git.prototype.branch = function (options, then) {
return this._runTask(
branchTask(getTrailingOptions(arguments)),
trailingFunctionArgument(arguments)
);
};
/**
* Return list of local branches
*
* @param {Function} [then]
*/
Git.prototype.branchLocal = function (then) {
return this._runTask(branchLocalTask(), trailingFunctionArgument(arguments));
};
/**
* Executes any command against the git binary.
*/
Git.prototype.raw = function (commands) {
const createRestCommands = !Array.isArray(commands);
const command = [].slice.call(createRestCommands ? arguments : commands, 0);
for (let i = 0; i < command.length && createRestCommands; i++) {
if (!filterPrimitives(command[i])) {
command.splice(i, command.length - i);
break;
}
}
command.push(...getTrailingOptions(arguments, 0, true));
var next = trailingFunctionArgument(arguments);
if (!command.length) {
return this._runTask(
configurationErrorTask('Raw: must supply one or more command to execute'),
next
);
}
return this._runTask(straightThroughStringTask(command, this._trimmed), next);
};
Git.prototype.submoduleAdd = function (repo, path, then) {
return this._runTask(addSubModuleTask(repo, path), trailingFunctionArgument(arguments));
};
Git.prototype.submoduleUpdate = function (args, then) {
return this._runTask(
updateSubModuleTask(getTrailingOptions(arguments, true)),
trailingFunctionArgument(arguments)
);
};
Git.prototype.submoduleInit = function (args, then) {
return this._runTask(
initSubModuleTask(getTrailingOptions(arguments, true)),
trailingFunctionArgument(arguments)
);
};
Git.prototype.subModule = function (options, then) {
return this._runTask(
subModuleTask(getTrailingOptions(arguments)),
trailingFunctionArgument(arguments)
);
};
Git.prototype.listRemote = function () {
return this._runTask(
listRemotesTask(getTrailingOptions(arguments)),
trailingFunctionArgument(arguments)
);
};
/**
* Adds a remote to the list of remotes.
*/
Git.prototype.addRemote = function (remoteName, remoteRepo, then) {
return this._runTask(
addRemoteTask(remoteName, remoteRepo, getTrailingOptions(arguments)),
trailingFunctionArgument(arguments)
);
};
/**
* Removes an entry by name from the list of remotes.
*/
Git.prototype.removeRemote = function (remoteName, then) {
return this._runTask(removeRemoteTask(remoteName), trailingFunctionArgument(arguments));
};
/**
* Gets the currently available remotes, setting the optional verbose argument to true includes additional
* detail on the remotes themselves.
*/
Git.prototype.getRemotes = function (verbose, then) {
return this._runTask(getRemotesTask(verbose === true), trailingFunctionArgument(arguments));
};
/**
* Call any `git remote` function with arguments passed as an array of strings.
*
* @param {string[]} options
* @param {Function} [then]
*/
Git.prototype.remote = function (options, then) {
return this._runTask(
remoteTask(getTrailingOptions(arguments)),
trailingFunctionArgument(arguments)
);
};
/**
* Call any `git tag` function with arguments passed as an array of strings.
*
* @param {string[]} options
* @param {Function} [then]
*/
Git.prototype.tag = function (options, then) {
const command = getTrailingOptions(arguments);
if (command[0] !== 'tag') {
command.unshift('tag');
}
return this._runTask(straightThroughStringTask(command), trailingFunctionArgument(arguments));
};
/**
* Updates repository server info
*
* @param {Function} [then]
*/
Git.prototype.updateServerInfo = function (then) {
return this._runTask(
straightThroughStringTask(['update-server-info']),
trailingFunctionArgument(arguments)
);
};
/**
* Pushes the current tag changes to a remote which can be either a URL or named remote. When not specified uses the
* default configured remote spec.
*
* @param {string} [remote]
* @param {Function} [then]
*/
Git.prototype.pushTags = function (remote, then) {
const task = pushTagsTask(
{ remote: filterType(remote, filterString) },
getTrailingOptions(arguments)
);
return this._runTask(task, trailingFunctionArgument(arguments));
};
/**
* Removes the named files from source control.
*/
Git.prototype.rm = function (files) {
return this._runTask(
straightThroughStringTask(['rm', '-f', ...asArray(files)]),
trailingFunctionArgument(arguments)
);
};
/**
* Removes the named files from source control but keeps them on disk rather than deleting them entirely. To
* completely remove the files, use `rm`.
*
* @param {string|string[]} files
*/
Git.prototype.rmKeepLocal = function (files) {
return this._runTask(
straightThroughStringTask(['rm', '--cached', ...asArray(files)]),
trailingFunctionArgument(arguments)
);
};
/**
* Returns a list of objects in a tree based on commit hash. Passing in an object hash returns the object's content,
* size, and type.
*
* Passing "-p" will instruct cat-file to determine the object type, and display its formatted contents.
*
* @param {string[]} [options]
* @param {Function} [then]
*/
Git.prototype.catFile = function (options, then) {
return this._catFile('utf-8', arguments);
};
Git.prototype.binaryCatFile = function () {
return this._catFile('buffer', arguments);
};
Git.prototype._catFile = function (format, args) {
var handler = trailingFunctionArgument(args);
var command = ['cat-file'];
var options = args[0];
if (typeof options === 'string') {
return this._runTask(
configurationErrorTask('Git.catFile: options must be supplied as an array of strings'),
handler
);
}
if (Array.isArray(options)) {
command.push.apply(command, options);
}
const task =
format === 'buffer' ? straightThroughBufferTask(command) : straightThroughStringTask(command);
return this._runTask(task, handler);
};
Git.prototype.diff = function (options, then) {
const task = filterString(options)
? configurationErrorTask(
'git.diff: supplying options as a single string is no longer supported, switch to an array of strings'
)
: straightThroughStringTask(['diff', ...getTrailingOptions(arguments)]);
return this._runTask(task, trailingFunctionArgument(arguments));
};
Git.prototype.diffSummary = function () {
return this._runTask(
diffSummaryTask(getTrailingOptions(arguments, 1)),
trailingFunctionArgument(arguments)
);
};
Git.prototype.applyPatch = function (patches) {
const task = !filterStringOrStringArray(patches)
? configurationErrorTask(
`git.applyPatch requires one or more string patches as the first argument`
)
: applyPatchTask(asArray(patches), getTrailingOptions([].slice.call(arguments, 1)));
return this._runTask(task, trailingFunctionArgument(arguments));
};
Git.prototype.revparse = function () {
const commands = ['rev-parse', ...getTrailingOptions(arguments, true)];
return this._runTask(
straightThroughStringTask(commands, true),
trailingFunctionArgument(arguments)
);
};
/**
*/
Git.prototype.clean = function (mode, options, then) {
const usingCleanOptionsArray = isCleanOptionsArray(mode);
const cleanMode =
(usingCleanOptionsArray && mode.join('')) || filterType(mode, filterString) || '';
const customArgs = getTrailingOptions([].slice.call(arguments, usingCleanOptionsArray ? 1 : 0));
return this._runTask(
cleanWithOptionsTask(cleanMode, customArgs),
trailingFunctionArgument(arguments)
);
};
Git.prototype.exec = function (then) {
const task = {
commands: [],
format: 'utf-8',
parser() {
if (typeof then === 'function') {
then();
}
},
};
return this._runTask(task);
};
/**
* Clears the queue of pending commands and returns the wrapper instance for chaining.
*
* @returns {Git}
*/
Git.prototype.clearQueue = function () {
// TODO:
// this._executor.clear();
return this;
};
/**
* Check if a pathname or pathnames are excluded by .gitignore
*
* @param {string|string[]} pathnames
* @param {Function} [then]
*/
Git.prototype.checkIgnore = function (pathnames, then) {
return this._runTask(
checkIgnoreTask(asArray(filterType(pathnames, filterStringOrStringArray, []))),
trailingFunctionArgument(arguments)
);
};
Git.prototype.checkIsRepo = function (checkType, then) {
return this._runTask(
checkIsRepoTask(filterType(checkType, filterString)),
trailingFunctionArgument(arguments)
);
};
module.exports = Git;