-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
git.js
511 lines (442 loc) · 14.3 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
/* @flow */
import invariant from 'invariant';
import {StringDecoder} from 'string_decoder';
import tarFs from 'tar-fs';
import tarStream from 'tar-stream';
import url from 'url';
import {createWriteStream} from 'fs';
import type Config from '../config.js';
import type {Reporter} from '../reporters/index.js';
import type {ResolvedSha, GitRefResolvingInterface, GitRefs} from './git/git-ref-resolver.js';
import {MessageError, ProcessSpawnError} from '../errors.js';
import {spawn as spawnGit} from './git/git-spawn.js';
import {resolveVersion, isCommitSha, parseRefs} from './git/git-ref-resolver.js';
import * as crypto from './crypto.js';
import * as fs from './fs.js';
import map from './map.js';
import {removePrefix} from './misc.js';
const GIT_PROTOCOL_PREFIX = 'git+';
const SSH_PROTOCOL = 'ssh:';
const SCP_PATH_PREFIX = '/:';
const FILE_PROTOCOL = 'file:';
const GIT_VALID_REF_LINE_REGEXP = /^([a-fA-F0-9]+|ref)/;
const validRef = line => {
return GIT_VALID_REF_LINE_REGEXP.exec(line);
};
type GitUrl = {
protocol: string, // parsed from URL
hostname: ?string,
repository: string, // git-specific "URL"
};
const supportsArchiveCache: {[key: string]: boolean} = map({
'github.com': false, // not support, doubt they will ever support it
});
const handleSpawnError = err => {
if (err instanceof ProcessSpawnError) {
throw err;
}
};
const SHORTHAND_SERVICES: {[key: string]: url.parse} = map({
'github:': parsedUrl => ({
...parsedUrl,
slashes: true,
auth: 'git',
protocol: SSH_PROTOCOL,
host: 'github.com',
hostname: 'github.com',
pathname: `/${parsedUrl.hostname}${parsedUrl.pathname}`,
}),
'bitbucket:': parsedUrl => ({
...parsedUrl,
slashes: true,
auth: 'git',
protocol: SSH_PROTOCOL,
host: 'bitbucket.com',
hostname: 'bitbucket.com',
pathname: `/${parsedUrl.hostname}${parsedUrl.pathname}`,
}),
});
export default class Git implements GitRefResolvingInterface {
constructor(config: Config, gitUrl: GitUrl, hash: string) {
this.supportsArchive = false;
this.fetched = false;
this.config = config;
this.reporter = config.reporter;
this.hash = hash;
this.ref = hash;
this.gitUrl = gitUrl;
this.cwd = this.config.getTemp(crypto.hash(this.gitUrl.repository));
}
supportsArchive: boolean;
fetched: boolean;
config: Config;
reporter: Reporter;
hash: string;
ref: string;
cwd: string;
gitUrl: GitUrl;
/**
* npm URLs contain a 'git+' scheme prefix, which is not understood by git.
* git "URLs" also allow an alternative scp-like syntax, so they're not standard URLs.
*/
static npmUrlToGitUrl(npmUrl: string): GitUrl {
npmUrl = removePrefix(npmUrl, GIT_PROTOCOL_PREFIX);
let parsed = url.parse(npmUrl);
const expander = parsed.protocol && SHORTHAND_SERVICES[parsed.protocol];
if (expander) {
parsed = expander(parsed);
}
// Special case in npm, where ssh:// prefix is stripped to pass scp-like syntax
// which in git works as remote path only if there are no slashes before ':'.
// See #3146.
if (
parsed.protocol === SSH_PROTOCOL &&
parsed.hostname &&
parsed.path &&
parsed.path.startsWith(SCP_PATH_PREFIX) &&
parsed.port === null
) {
const auth = parsed.auth ? parsed.auth + '@' : '';
const pathname = parsed.path.slice(SCP_PATH_PREFIX.length);
return {
hostname: parsed.hostname,
protocol: parsed.protocol,
repository: `${auth}${parsed.hostname}:${pathname}`,
};
}
// git local repos are specified as `git+file:` and a filesystem path, not a url.
let repository;
if (parsed.protocol === FILE_PROTOCOL) {
repository = parsed.path;
} else {
repository = url.format({...parsed, hash: ''});
}
return {
hostname: parsed.hostname || null,
protocol: parsed.protocol || FILE_PROTOCOL,
repository: repository || '',
};
}
/**
* Check if the host specified in the input `gitUrl` has archive capability.
*/
static async hasArchiveCapability(ref: GitUrl): Promise<boolean> {
const hostname = ref.hostname;
if (ref.protocol !== 'ssh:' || hostname == null) {
return false;
}
if (hostname in supportsArchiveCache) {
return supportsArchiveCache[hostname];
}
try {
await spawnGit(['archive', `--remote=${ref.repository}`, 'HEAD', Date.now() + '']);
throw new Error();
} catch (err) {
handleSpawnError(err);
const supports = err.message.indexOf('did not match any files') >= 0;
return (supportsArchiveCache[hostname] = supports);
}
}
/**
* Check if the input `target` is a 5-40 character hex commit hash.
*/
static async repoExists(ref: GitUrl): Promise<boolean> {
const isLocal = ref.protocol === FILE_PROTOCOL;
try {
if (isLocal) {
await spawnGit(['show-ref', '-t'], {cwd: ref.repository});
} else {
await spawnGit(['ls-remote', '-t', ref.repository]);
}
return true;
} catch (err) {
handleSpawnError(err);
return false;
}
}
static replaceProtocol(ref: GitUrl, protocol: string): GitUrl {
return {
hostname: ref.hostname,
protocol,
repository: ref.repository.replace(/^(?:git|http):/, protocol),
};
}
/**
* Attempt to upgrade insecure protocols to secure protocol
*/
static async secureGitUrl(ref: GitUrl, hash: string, reporter: Reporter): Promise<GitUrl> {
if (isCommitSha(hash)) {
// this is cryptographically secure
return ref;
}
if (ref.protocol === 'git:') {
const secureUrl = Git.replaceProtocol(ref, 'https:');
if (await Git.repoExists(secureUrl)) {
return secureUrl;
} else {
reporter.warn(reporter.lang('downloadGitWithoutCommit', ref.repository));
return ref;
}
}
if (ref.protocol === 'http:') {
const secureRef = Git.replaceProtocol(ref, 'https:');
if (await Git.repoExists(secureRef)) {
return secureRef;
} else {
reporter.warn(reporter.lang('downloadHTTPWithoutCommit', ref.repository));
return ref;
}
}
return ref;
}
/**
* Archive a repo to destination
*/
archive(dest: string): Promise<string> {
if (this.supportsArchive) {
return this._archiveViaRemoteArchive(dest);
} else {
return this._archiveViaLocalFetched(dest);
}
}
async _archiveViaRemoteArchive(dest: string): Promise<string> {
const hashStream = new crypto.HashStream();
await spawnGit(['archive', `--remote=${this.gitUrl.repository}`, this.ref], {
process(proc, resolve, reject, done) {
const writeStream = createWriteStream(dest);
proc.on('error', reject);
writeStream.on('error', reject);
writeStream.on('end', done);
writeStream.on('open', function() {
proc.stdout.pipe(hashStream).pipe(writeStream);
});
writeStream.once('finish', done);
},
});
return hashStream.getHash();
}
async _archiveViaLocalFetched(dest: string): Promise<string> {
const hashStream = new crypto.HashStream();
await spawnGit(['archive', this.hash], {
cwd: this.cwd,
process(proc, resolve, reject, done) {
const writeStream = createWriteStream(dest);
proc.on('error', reject);
writeStream.on('error', reject);
writeStream.on('open', function() {
proc.stdout.pipe(hashStream).pipe(writeStream);
});
writeStream.once('finish', done);
},
});
return hashStream.getHash();
}
/**
* Clone a repo to the input `dest`. Use `git archive` if it's available, otherwise fall
* back to `git clone`.
*/
clone(dest: string): Promise<void> {
if (this.supportsArchive) {
return this._cloneViaRemoteArchive(dest);
} else {
return this._cloneViaLocalFetched(dest);
}
}
async _cloneViaRemoteArchive(dest: string): Promise<void> {
await spawnGit(['archive', `--remote=${this.gitUrl.repository}`, this.ref], {
process(proc, update, reject, done) {
const extractor = tarFs.extract(dest, {
dmode: 0o555, // all dirs should be readable
fmode: 0o444, // all files should be readable
});
extractor.on('error', reject);
extractor.on('finish', done);
proc.stdout.pipe(extractor);
proc.on('error', reject);
},
});
}
async _cloneViaLocalFetched(dest: string): Promise<void> {
await spawnGit(['archive', this.hash], {
cwd: this.cwd,
process(proc, resolve, reject, done) {
const extractor = tarFs.extract(dest, {
dmode: 0o555, // all dirs should be readable
fmode: 0o444, // all files should be readable
});
extractor.on('error', reject);
extractor.on('finish', done);
proc.stdout.pipe(extractor);
},
});
}
/**
* Clone this repo.
*/
fetch(): Promise<void> {
const {gitUrl, cwd} = this;
return fs.lockQueue.push(gitUrl.repository, async () => {
if (await fs.exists(cwd)) {
await spawnGit(['fetch', '--tags'], {cwd});
await spawnGit(['pull'], {cwd});
} else {
await spawnGit(['clone', gitUrl.repository, cwd]);
}
this.fetched = true;
});
}
/**
* Fetch the file by cloning the repo and reading it.
*/
getFile(filename: string): Promise<string | false> {
if (this.supportsArchive) {
return this._getFileFromArchive(filename);
} else {
return this._getFileFromClone(filename);
}
}
async _getFileFromArchive(filename: string): Promise<string | false> {
try {
return await spawnGit(['archive', `--remote=${this.gitUrl.repository}`, this.ref, filename], {
process(proc, update, reject, done) {
const parser = tarStream.extract();
parser.on('error', reject);
parser.on('finish', done);
parser.on('entry', (header, stream, next) => {
const decoder = new StringDecoder('utf8');
let fileContent = '';
stream.on('data', buffer => {
fileContent += decoder.write(buffer);
});
stream.on('end', () => {
const remaining: string = decoder.end();
update(fileContent + remaining);
next();
});
stream.resume();
});
proc.stdout.pipe(parser);
},
});
} catch (err) {
if (err.message.indexOf('did not match any files') >= 0) {
return false;
} else {
throw err;
}
}
}
async _getFileFromClone(filename: string): Promise<string | false> {
invariant(this.fetched, 'Repo not fetched');
try {
return await spawnGit(['show', `${this.hash}:${filename}`], {
cwd: this.cwd,
});
} catch (err) {
handleSpawnError(err);
// file doesn't exist
return false;
}
}
/**
* Initialize the repo, find a secure url to use and
* set the ref to match an input `target`.
*/
async init(): Promise<string> {
this.gitUrl = await Git.secureGitUrl(this.gitUrl, this.hash, this.reporter);
await this.setRefRemote();
// check capabilities
if (this.ref !== '' && (await Git.hasArchiveCapability(this.gitUrl))) {
this.supportsArchive = true;
} else {
await this.fetch();
}
return this.hash;
}
async setRefRemote(): Promise<string> {
const isLocal = this.gitUrl.protocol === FILE_PROTOCOL;
let stdout;
if (isLocal) {
stdout = await spawnGit(['show-ref', '--tags', '--heads'], {cwd: this.gitUrl.repository});
} else {
stdout = await spawnGit(['ls-remote', '--tags', '--heads', this.gitUrl.repository]);
}
const refs = parseRefs(stdout);
return this.setRef(refs);
}
setRefHosted(hostedRefsList: string): Promise<string> {
const refs = parseRefs(hostedRefsList);
return this.setRef(refs);
}
/**
* Resolves the default branch of a remote repository (not always "master")
*/
async resolveDefaultBranch(): Promise<ResolvedSha> {
const isLocal = this.gitUrl.protocol === FILE_PROTOCOL;
try {
let stdout;
if (isLocal) {
stdout = await spawnGit(['show-ref', 'HEAD'], {cwd: this.gitUrl.repository});
const refs = parseRefs(stdout);
const sha = refs.values().next().value;
if (sha) {
return {sha, ref: undefined};
} else {
throw new Error('Unable to find SHA for git HEAD');
}
} else {
stdout = await spawnGit(['ls-remote', '--symref', this.gitUrl.repository, 'HEAD']);
const lines = stdout.split('\n').filter(validRef);
const [, ref] = lines[0].split(/\s+/);
const [sha] = lines[1].split(/\s+/);
return {sha, ref};
}
} catch (err) {
handleSpawnError(err);
// older versions of git don't support "--symref"
const stdout = await spawnGit(['ls-remote', this.gitUrl.repository, 'HEAD']);
const lines = stdout.split('\n').filter(validRef);
const [sha] = lines[0].split(/\s+/);
return {sha, ref: undefined};
}
}
/**
* Resolve a git commit to it's 40-chars format and ensure it exists in the repository
* We need to use the 40-chars format to avoid multiple folders in the cache
*/
async resolveCommit(shaToResolve: string): Promise<?ResolvedSha> {
try {
await this.fetch();
const revListArgs = ['rev-list', '-n', '1', '--no-abbrev-commit', '--format=oneline', shaToResolve];
const stdout = await spawnGit(revListArgs, {cwd: this.cwd});
const [sha] = stdout.split(/\s+/);
return {sha, ref: undefined};
} catch (err) {
handleSpawnError(err);
// assuming commit not found, let's try something else
return null;
}
}
/**
* Resolves the input hash / ref / semver range to a valid commit sha
* If possible also resolves the sha to a valid ref in order to use "git archive"
*/
async setRef(refs: GitRefs): Promise<string> {
// get commit ref
const {hash: version} = this;
const resolvedResult = await resolveVersion({
config: this.config,
git: this,
version,
refs,
});
if (!resolvedResult) {
throw new MessageError(
this.reporter.lang('couldntFindMatch', version, Array.from(refs.keys()).join(','), this.gitUrl.repository),
);
}
this.hash = resolvedResult.sha;
this.ref = resolvedResult.ref || '';
return this.hash;
}
}