This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
/
git.ts
578 lines (480 loc) · 18.3 KB
/
git.ts
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
/**
* This file is part of the vscode-deploy-reloaded distribution.
* Copyright (c) Marcel Joachim Kloubert.
*
* vscode-deploy-reloaded is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, version 3.
*
* vscode-deploy-reloaded is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Most of the code has been taken from 'vscode-gitlens':
* https://github.com/eamodio/vscode-gitlens
*
* LICENSE:
* https://github.com/eamodio/vscode-gitlens/blob/master/LICENSE
*/
import * as ChildProcess from 'child_process';
import * as deploy_helpers from './helpers';
import * as deploy_scm from './scm';
import * as Enumerable from 'node-enumerable';
const MergeDeep = require('merge-deep');
import * as Moment from 'moment';
import * as Path from 'path';
interface Executable {
cmd: string;
args: string[];
}
/**
* Stores the data of a git executable.
*/
export interface GitExecutable {
/**
* The path to the executable.
*/
readonly path: string;
/**
* The version.
*/
readonly version: string;
}
const COMMITS_PER_PAGE = 25;
const IS_WINDOWS = process.platform === 'win32';
/**
* A git client.
*/
export class GitClient implements deploy_scm.SourceControlClient {
/**
* Initializes a new instance of that class.
*
* @param {GitExecutable} executable The data of the executable.
* @param {string} [cwd] The optional working directory.
*/
constructor(public readonly executable: GitExecutable,
cwd?: string) {
this.cwd = deploy_helpers.toStringSafe(cwd);
if (deploy_helpers.isEmptyString(this.cwd)) {
this.cwd = undefined;
}
}
/** @inheritdoc */
public async branches() : Promise<deploy_scm.Branch[]> {
const ME = this;
const BRANCHES: deploy_scm.Branch[] = [];
const RESULT = deploy_helpers.toStringSafe(
await this.exec([ 'branch', '-v' ])
).trim();
// split lines
const LINES = RESULT.split("\n");
LINES.forEach(l => {
const SEP = l.indexOf(' ', 2);
const NAME = l.substr(2, SEP - 2).trim();
BRANCHES.push({
client: ME,
commitCount: async function(skip?: number) {
skip = parseInt(deploy_helpers.toStringSafe(skip).trim());
if (isNaN(skip)) {
skip = 0;
}
if (skip < 1) {
skip = 0;
}
return parseInt(
deploy_helpers.toStringSafe(
await ME.exec([ "rev-list", "--count", "--skip=" + skip, "HEAD" ]),
)
);
},
commits: async function(page?: number, skip?: number) {
page = parseInt(
deploy_helpers.toStringSafe(page).trim()
);
if (isNaN(page)) {
page = 0;
}
if (page < 1) {
page = 1;
}
skip = parseInt(
deploy_helpers.toStringSafe(skip).trim()
);
if (isNaN(skip)) {
skip = 0;
}
if (skip < 1) {
skip = 0;
}
const ITEMS_TO_SKIP = (page - 1) * COMMITS_PER_PAGE + skip;
const BRANCH: deploy_scm.Branch = this;
const LOADED_COMMITS: deploy_scm.Commit[] = [];
const TOTAL_COUNT: number = await this.commitCount();
const COMMITS_RESULT = await ME.exec([
'log',
'--pretty=%H %aI %s',
'--skip=' + ITEMS_TO_SKIP,
'-' + COMMITS_PER_PAGE
]);
const LINES = COMMITS_RESULT.split("\n").map(l => {
return l.trim();
}).filter(l => '' !== l);
LINES.forEach((l, i) => {
const HASH = l.substr(0, 39);
const DATE = l.substr(41, 25);
const SUBJECT = l.substr(67).trim();
LOADED_COMMITS.push({
branch: BRANCH,
changes: async function() {
const COMMIT: deploy_scm.Commit = this;
const LOADED_CHANGES: deploy_scm.FileChange[] = [];
const CHANGES_RESULT = await ME.exec([
'log',
COMMIT.id,
'--pretty=',
'--name-status',
'-1'
]);
const LINES = CHANGES_RESULT.split("\n").map(l => {
return l.trim();
}).filter(l => '' !== l);
LINES.forEach(l => {
let changeType: deploy_scm.FileChangeType;
let pushNewItems = () => {
LOADED_CHANGES.push({
commit: COMMIT,
file: l.substr(2).trim(),
type: changeType,
});
};
switch (deploy_helpers.normalizeString(l.substr(0, 1))) {
case 'a':
changeType = deploy_scm.FileChangeType.Added;
break;
case 'd':
changeType = deploy_scm.FileChangeType.Deleted;
break;
case 'm':
changeType = deploy_scm.FileChangeType.Modified;
break;
default:
pushNewItems = null; //TODO: implement later
break;
}
if (pushNewItems) {
pushNewItems();
}
});
return LOADED_CHANGES;
},
date: deploy_helpers.asUTC( Moment(DATE) ),
id: HASH,
index: TOTAL_COUNT - ITEMS_TO_SKIP - i - 1,
subject: SUBJECT,
});
});
return LOADED_COMMITS;
},
id: NAME,
});
});
const MAX_BRANCH_LEN = Enumerable.from( BRANCHES )
.select(b => b.id.length)
.max();
if (!deploy_helpers.isSymbol(MAX_BRANCH_LEN)) {
const START_AT = MAX_BRANCH_LEN + 3;
BRANCHES.forEach((b, i) => {
const L = LINES[i];
(<any>b).lastCommit = {
id: L.substr(START_AT, 7),
subject: L.substr(START_AT + 7).trim(),
};
});
}
return BRANCHES;
}
/** @inheritdoc */
public async changes(): Promise<deploy_scm.UncommitedFileChange[]> {
const CHANGED_FILES: deploy_scm.UncommitedFileChange[] = [];
const LINES = (await this.exec([
'status', '-s'
])).split("\n").filter(l => {
return !deploy_helpers.isEmptyString(l);
});
for (const L of LINES) {
const GIT_TYPE = L.substr(0, 3).toLowerCase().trim();
const GIT_FILE = L.substr(3);
let changeType: deploy_scm.FileChangeType;
let pushNewItems = () => {
CHANGED_FILES.push({
file: GIT_FILE,
type: changeType,
});
};
switch (GIT_TYPE) {
case 'a':
changeType = deploy_scm.FileChangeType.Added;
break;
case 'd':
changeType = deploy_scm.FileChangeType.Deleted;
break;
case 'm':
changeType = deploy_scm.FileChangeType.Modified;
break;
case 'r':
pushNewItems = () => {
const SEP = GIT_FILE.indexOf(' -> ');
if (SEP > -1) {
CHANGED_FILES.push({
file: GIT_FILE.substr(0, SEP),
type: deploy_scm.FileChangeType.Deleted,
});
CHANGED_FILES.push({
file: GIT_FILE.substr(SEP + 4),
type: deploy_scm.FileChangeType.Added,
});
}
};
break;
default:
pushNewItems = null; //TODO: implement later
break;
}
if (pushNewItems) {
pushNewItems();
}
}
return CHANGED_FILES;
}
/** @inheritdoc */
public readonly cwd: string;
/**
* Executes the Git client and returns the stdout.
*
* @param {any[]} args Arguments for the execution.
* @param {ChildProcess.ExecFileOptions} [opts] Custom options.
*
* @return {Promise<string>} The promise with the standard output.
*/
public async exec(args: any[], opts?: ChildProcess.ExecFileOptions): Promise<string> {
return (await this.execFile(
args, opts,
)).stdOut;
}
/**
* Executes the Git client.
*
* @param {any[]} args Arguments for the execution.
* @param {ChildProcess.ExecFileOptions} [opts] Custom options.
*
* @return {Promise<deploy_helpers.ExecResult>} The promise with the result.
*/
public async execFile(args: any[], opts?: ChildProcess.ExecFileOptions): Promise<deploy_helpers.ExecResult> {
const DEFAULT_OPTS: ChildProcess.ExecFileOptions = {
cwd: this.cwd,
};
return await deploy_helpers.execFile(
this.executable.path,
args,
MergeDeep(DEFAULT_OPTS, opts),
);
}
/**
* Detects the version of the underlying Git client.
*
* @return {Promise<string>} The promise with the version.
*/
public async version() {
return parseVersion(
deploy_helpers.toStringSafe(
await this.exec([ '--version' ])
)
).trim();
}
}
async function findExecutable(exe: string, args: string[]): Promise<Executable> {
// POSIX can just execute scripts directly, no need for silly goosery
if (!IS_WINDOWS) {
return {
cmd: await runDownPath(exe),
args: args
};
}
if (!(await deploy_helpers.exists(exe))) {
// NB: When you write something like `surf-client ... -- surf-build` on Windows,
// a shell would normally convert that to surf-build.cmd, but since it's passed
// in as an argument, it doesn't happen
const POSSIBLE_EXTENSIONS = ['.exe', '.bat', '.cmd', '.ps1'];
for (const EXT of POSSIBLE_EXTENSIONS) {
const FULL_PATH = await runDownPath(`${exe}${EXT}`);
if (await deploy_helpers.exists(FULL_PATH)) {
return await findExecutable(FULL_PATH, args);
}
}
}
if (exe.match(/\.ps1$/i)) { // PowerShell
const CMD = Path.join(process.env.SYSTEMROOT!,
'System32', 'WindowsPowerShell', 'v1.0', 'PowerShell.exe');
const PS_ARGS = [ '-ExecutionPolicy', 'Unrestricted', '-NoLogo', '-NonInteractive', '-File', exe ];
return {
cmd: CMD,
args: PS_ARGS.concat(args),
};
}
if (exe.match(/\.(bat|cmd)$/i)) { // Windows batch?
const CMD = Path.join(process.env.SYSTEMROOT!, 'System32', 'cmd.exe');
const CMD_ARGS = ['/C', exe, ...args];
return {
cmd: CMD,
args: CMD_ARGS,
};
}
if (exe.match(/\.(js)$/i)) { // NodeJS?
const CMD = process.execPath;
const NODE_ARGS = [exe];
return {
cmd: CMD,
args: NODE_ARGS.concat(args)
};
}
return {
cmd: exe,
args: args
};
}
async function findGitDarwin(): Promise<GitExecutable> {
let path = await runCommand('which', ['git']);
path = path.replace(/^\s+|\s+$/g, '');
if (path !== '/usr/bin/git') {
return findSpecificGit(path);
}
try {
await runCommand('xcode-select', ['-p']);
return findSpecificGit(path);
}
catch (ex) {
if (2 === ex.code) {
throw new Error('Unable to find git');
}
return findSpecificGit(path);
}
}
async function findGitPath(path: string): Promise<GitExecutable | false> {
path = deploy_helpers.toStringSafe(path);
if (deploy_helpers.isEmptyString(path)) {
path = 'git'; // default
}
try {
return await findSpecificGit(path);
}
catch (e) {
}
// fallback: platform specific
try {
switch (process.platform) {
case 'darwin':
return await findGitDarwin();
case 'win32':
return await findGitWin32();
}
}
catch (e) {
}
return false;
}
async function findSpecificGit(path: string): Promise<GitExecutable> {
const VERSION = await runCommand(path, [ '--version' ]);
// If needed, let's update our path to avoid the search on every command
if (deploy_helpers.isEmptyString(path) || path === 'git') {
path = (await findExecutable(path, [ '--version' ])).cmd;
}
return {
path,
version: parseVersion(VERSION.trim()),
};
}
function findGitWin32(): Promise<GitExecutable> {
return findSystemGitWin32(process.env['ProgramW6432']!)
.then(null, () => findSystemGitWin32(process.env['ProgramFiles(x86)']!))
.then(null, () => findSystemGitWin32(process.env['ProgramFiles']!))
.then(null, () => findSpecificGit('git'));
}
async function findSystemGitWin32(basePath: string): Promise<GitExecutable> {
if (deploy_helpers.isEmptyString(basePath)) {
throw new Error('Unable to find git');
}
return await findSpecificGit(Path.join(basePath,
'Git', 'cmd', 'git.exe'));
}
function parseVersion(raw: string): string {
return raw.replace(/^git version /, '');
}
/**
* Normalizes a git hash.
*
* @param {string} hash The input value.
* @param {TDefault} [defaultValue] The custom default value.
*
* @return {string|TDefault} The normalized hash or the default value.
*/
export function normalizeGitHash<TDefault = null>(hash: string, defaultValue: TDefault = <any>null): string | TDefault {
hash = deploy_helpers.normalizeString(hash);
if (hash.length > 7) {
hash = hash.substr(0, 7).trim();
}
if (hash.length < 1) {
return defaultValue;
}
return hash;
}
async function runCommand(command: string, args: any[]) {
return (await deploy_helpers.execFile(command, args)).stdOut;
}
async function runDownPath(exe: string): Promise<string> {
// NB: Windows won't search PATH looking for executables in spawn like
// Posix does
// Files with any directory path don't get this applied
if (exe.match(/[\\\/]/)) {
return exe;
}
const TARGET = Path.join('.', exe);
try {
if (await deploy_helpers.stat(TARGET)) {
return TARGET;
}
}
catch (e) { }
const HAYSTACK = process.env.PATH!.split(IS_WINDOWS ? ';' : ':');
for (const P of HAYSTACK) {
const NEEDLE = Path.join(P, exe);
try {
if (await deploy_helpers.stat(NEEDLE)) {
return NEEDLE;
}
}
catch (e) { }
}
return exe;
}
/**
* Tries to find the path of the Git executable.
*
* @param {string} [path] The optional specific path where to search first.
*
* @return {GitExecutable|false} The promise with the executable or (false) if not found.
*/
export async function tryFindGitPath(path?: string): Promise<GitExecutable | false> {
let git = await findGitPath(path);
if (false !== git) {
git = {
path: Path.resolve(git.path),
version: git.version,
};
}
return git;
}