-
Notifications
You must be signed in to change notification settings - Fork 16
/
git.js
544 lines (477 loc) · 15.7 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
/**
* @license jspm-git
* Copyright (c) 2014-2016 Tauren Mills, contributors
* jspm endpoint for Git Repositories
* (based on Guy Bedford's github endpoint for jspm https://github.com/jspm/github)
* License: MIT
*/
'use strict';
var path = require('path');
var Promise = require('rsvp').Promise;
var rimraf = require('rimraf');
var ncp = require('ncp').ncp;
var mkdirp = require('mkdirp');
var fs = require('fs');
var temp = require('temp');
var semver = require('semver');
var urljoin = require('url-join');
var which = require('which');
var asp = require('rsvp').denodeify;
var validUrl = require('valid-url');
var execGit = require('./exec-git');
var logging = false;
var logMsg = function(msg) {
if (logging) {
console.log(new Date() + ': ' + msg);
}
};
var createBaseUrl = function(baseurl, auth) {
if (validUrl.isUri(baseurl)) {
var baseWithAuth = baseurl;
if (auth) {
var authStr = encodeURIComponent(auth.username) + ':' + encodeURIComponent(auth.password) + '@';
var parts = baseurl.match(/^((http[s]?|ftp|ssh):\/*)(.*)/);
baseWithAuth = parts[1] + authStr + parts[3];
}
return baseWithAuth;
} else {
return null;
}
};
var createGitUrl = function(baseurl, repo, reposuffix, auth) {
if (validUrl.isUri(baseurl)) {
var baseWithAuth = createBaseUrl(baseurl, auth);
return urljoin(baseWithAuth, repo + reposuffix);
} else {
// Assume that baseurl is scp-like formated path i.e. [[user@]host]
return baseurl + ':' + repo + reposuffix;
}
};
function encodeCredentials(auth) {
if (auth) {
// avoid storing passwords as plain text in config
return new Buffer(encodeURIComponent(auth.username || '') + ':' + encodeURIComponent(auth.password || '')).toString('base64');
} else {
return null;
}
}
function decodeCredentials(str) {
if (typeof str === 'string' && str !== '') {
var auth = new Buffer(str, 'base64').toString('ascii').split(':');
return {
username: decodeURIComponent(auth[0]),
password: decodeURIComponent(auth[1])
};
} else {
return null;
}
}
var getGitVersion = function() {
return new Promise(function(resolve, reject) {
execGit(['--version'], null, function(err, stdout, stderr) {
var versionArr;
if (err) {
logMsg('Error while reading our the Git version: ' + stderr);
reject(stderr);
} else {
versionArr = stdout.match(/\d+.\d+.\d+/);
if (versionArr.length === 1) {
resolve(versionArr[0]);
} else {
logMsg('Error while parsing the Git version');
reject();
}
}
});
});
};
var cloneGitRepo = function(repoDir, branch, url, execOpt, shallowclone) {
return getGitVersion().
then(function(gitVersion) {
return new Promise(function(resolve, reject) {
var command, gitLegacyMode;
// Detect if we need to run in git legacy mode
gitLegacyMode = semver.lt(gitVersion, '1.7.10');
command = ['clone'];
if (shallowclone) {
command.push('--depth', '1');
}
// Parameters --single-branch and -b are only supported from Git version 1.7.10 or greater
if (!gitLegacyMode) {
command.push('-b', branch, '--single-branch');
}
command.push(url, repoDir);
execGit(command, execOpt, function(err, stdout, stderr) {
if (err) {
var error = new Error(stderr.toString().replace(url, ''));
error.hideStack = true;
error.retriable = true;
logMsg('Error while cloning the git repository: ' + error);
rimraf(repoDir, function() {
reject(error);
});
} else {
if (gitLegacyMode) {
execGit(['checkout', branch], {cwd: repoDir}, function(err, stdout, stderr) {
if (err) {
logMsg('Error while checking out the git branch: ' + stderr);
rimraf(repoDir, function() {
reject(stderr);
});
} else {
resolve();
}
});
} else {
resolve();
}
}
});
});
});
};
var exportGitRepo = function(repoDir, branch, url, execOpt, shallowclone) {
return cloneGitRepo(repoDir, branch, url, execOpt, shallowclone).
then(function() {
return new Promise(function(resolve, reject) {
// Remove the .git folder
rimraf(path.join(repoDir, '.git'), function(err) {
if (err) {
logMsg('Error while removing the .git folder: ' + err);
reject(err);
} else {
resolve();
}
});
});
});
};
var readPackageJSON = function(repoDir) {
return new Promise(function(resolve, reject) {
fs.readFile(path.join(repoDir, 'package.json'), 'utf8', function (err, data) {
if (err) {
resolve({});
} else {
try {
resolve(JSON.parse(data));
} catch (err) {
logMsg('Error while parsing package.json: ' + err);
reject(err);
}
}
});
});
};
var moveRepoToOutDir = function(repoDir, outDir) {
return new Promise(function(resolve, reject) {
mkdirp(outDir, function(err) {
if (err) {
return reject(err);
}
ncp(repoDir, outDir, {'stopOnErr': true}, function(err) {
if (err) {
logMsg('Error while moving repo to outDir: ' + err);
// Make sure the the outDir gets removed
rimraf(outDir, function() {
reject(err);
});
} else {
rimraf(repoDir, function() {
resolve();
});
}
});
});
});
};
var createTempDir = function() {
return new Promise(function(resolve, reject) {
temp.mkdir('jspm-git-', function(err, tempDirPath) {
if (err) {
reject();
} else {
resolve(tempDirPath);
}
});
});
};
var GitLocation = function(options) {
logging = options.log === false ? false : true;
// ensure git is installed
try {
which.sync('git');
} catch(ex) {
throw 'Git not installed. Please make sure that it\'s installed on your system.';
}
if (!semver.satisfies(options.apiVersion + '.0', '^1.0')) {
throw 'Current jspm-git version isn\'t compatible to the jspm Endpoint API v' + options.apiVersion + '\n' +
'Please update or install a compatible version of jspm-git.';
}
this.maxRepoSize = (options.maxRepoSize || 0) * 1024 * 1024;
this.execOpt = {
cwd: options.tmpDir,
timeout: (options.timeout || 0) * 1000,
killSignal: 'SIGKILL',
maxBuffer: this.maxRepoSize || 2 * 1024 * 1024
};
if (typeof options.version !== 'string') {
options.version = '1.0';
}
if (typeof options.reposuffix !== 'string') {
options.reposuffix = '.git';
}
if (typeof options.shallowclone !== 'boolean') {
options.shallowclone = options.shallowclone !== 'false';
}
this.options = options;
if (options.auth) {
this.auth = decodeCredentials(options.auth);
}
};
// static configuration function
GitLocation.configure = function(config, ui) {
config.version = '1.0';
return Promise.resolve(ui.input('Enter the base URL of your git server', config.baseurl))
.then(function(baseurl) {
if (!baseurl || baseurl === '' || !validUrl.isUri(baseurl)) {
return Promise.reject('Invalid base URL was entered');
}
config.baseurl = baseurl;
}).then(function() {
return Promise.resolve(ui.confirm('Set advanced configurations?', false))
.then(function(advancedconfig) {
if (advancedconfig) {
return Promise.resolve()
.then(function() {
return Promise.resolve()
.then(function() {
var authEnabled = config.auth !== null && config.auth !== undefined;
if (!authEnabled) {
return ui.confirm('Enable authentication?', authEnabled);
} else {
return ui.confirm('Disable authentication?', false)
.then(function(disable) {
if (disable) {
return false;
} else {
return ui.confirm('Update authentication?', false)
.then(function(update) {
return update || null;
});
}
});
}
})
.then(function(authentication) {
if (authentication) {
// TRUE - Set or update authentication
var auth = decodeCredentials(config.auth) || {};
return Promise.resolve()
.then(function() {
return ui.input('Username', auth.username);
})
.then(function(username) {
auth.username = username;
return ui.input('Password', null, true);
})
.then(function(password) {
auth.password = password;
return encodeCredentials(auth);
});
} else if (authentication !== null){
// FALSE - Delete authentication
return null;
} else {
// NULL - Keep current authentication
return config.auth;
}
})
.then(function(auth) {
if (auth) {
config.auth = auth;
} else {
delete config.auth;
}
});
})
.then(function() {
var isRepoSuffixUnset = config.reposuffix === undefined || config.reposuffix === null;
return Promise.resolve(ui.confirm('Would you like to use the default git repository suffix (.git)?', isRepoSuffixUnset));
})
.then(function(usedefaultsuffix) {
if(usedefaultsuffix) {
// Leave the reposuffix config empty in order to use the default configuration of the endpoint
delete config.reposuffix;
return;
}
return Promise.resolve(ui.confirm('Would you like to set an empty git repository suffix?', false))
.then(function(setemptysuffix) {
if (setemptysuffix) {
// Set an empty repository suffix
config.reposuffix = '';
return;
}
return Promise.resolve(ui.input('Enter the git repository suffix', config.reposuffix || '.git'))
.then(function(reposuffix) {
config.reposuffix = reposuffix;
});
});
})
.then(function() {
return Promise.resolve(ui.confirm('Disable shallow git clones?', false))
.then(function(shallowclone) {
if (shallowclone) {
return Promise.resolve(ui.confirm('Disabling shallow git clones might have severe impact on the git clone performance and you might run into jspm install timeouts. Are you sure you want to disable it?', false))
.then(function(confirm) {
if (confirm) {
config.shallowclone = !shallowclone;
}
});
}
});
});
}
});
})
.then(function() {
return config;
});
};
GitLocation.prototype = {
parse: function(name) {
var parts = name.split('/');
var packageName = parts.splice(0, 2).join('/');
return {
package: packageName,
path: parts.join('/')
};
},
// return values
// { versions: { versionhash } }
// { notfound: true }
lookup: function(repo) {
var execOpt = this.execOpt, versions, self = this;
return new Promise(function(resolve, reject) {
var url = createGitUrl(self.options.baseurl, repo, self.options.reposuffix, self.auth);
execGit(['ls-remote', url, 'refs/tags/*', 'refs/heads/*'], execOpt, function(err, stdout, stderr) {
if (err) {
if (err.toString().indexOf('not found') === -1) {
// dont show plain text passwords in error
var error = new Error(stderr.toString().replace(url, ''));
error.hideStack = true;
error.retriable = true;
reject(error);
} else {
resolve({ notfound: true });
}
}
versions = {};
var refs = stdout.split('\n');
for (var i = 0; i < refs.length; i++) {
if (!refs[i]) {
continue;
}
var hash = refs[i].substr(0, refs[i].indexOf('\t'));
var refName = refs[i].substr(hash.length + 1);
var version;
var versionObj = { hash: hash, meta: {} };
if (refName.substr(0, 11) === 'refs/heads/') {
version = refName.substr(11);
versionObj.stable = false;
} else if (refName.substr(0, 10) === 'refs/tags/') {
if (refName.substr(refName.length - 3, 3) === '^{}') {
version = refName.substr(10, refName.length - 13);
} else {
version = refName.substr(10);
}
if (version.substr(0, 1) === 'v' && semver.valid(version.substr(1))) {
version = version.substr(1);
// note when we remove a "v" which versions we need to add it back to
// to work out the tag version again
versionObj.meta.vPrefix = true;
}
}
versions[version] = versionObj;
}
resolve({ versions: versions });
});
});
},
// always an exact version
// assumed that this is run after getVersions so the repo exists
download: function(repo, version, hash, meta, outDir) {
var url, tempRepoDir, packageJSONData, self = this;
if (meta.vPrefix) {
version = 'v' + version;
}
// Automatically track and cleanup files at exit
temp.track();
url = createGitUrl(self.options.baseurl, repo, self.options.reposuffix, self.auth);
return createTempDir().then(function(tempDir) {
tempRepoDir = tempDir;
return exportGitRepo(tempRepoDir, version, url, self.execOpt, self.options.shallowclone);
}).then(function() {
return readPackageJSON(tempRepoDir);
}).then(function(data) {
packageJSONData = data;
return moveRepoToOutDir(tempRepoDir, outDir);
}).then(function() {
return packageJSONData;
});
},
// check if the main entry point exists. If not, try the bower.json main.
build: function(pjson, dir) {
var main = pjson.main || '';
var libDir = pjson.directories && (pjson.directories.dist || pjson.directories.lib) || '.';
// convert to windows-style paths if necessary
main = path.normalize(main);
libDir = path.normalize(libDir);
if (main.indexOf('!') !== -1) {
return;
}
function checkMain(main, libDir) {
if (!main) {
return Promise.resolve(false);
}
if (main.substr(main.length - 3, 3) === '.js') {
main = main.substr(0, main.length - 3);
}
return new Promise(function(resolve) {
fs.exists(path.resolve(dir, libDir || '.', main) + '.js', function(exists) {
resolve(exists);
});
});
}
return checkMain(main, libDir)
.then(function(hasMain) {
if (hasMain) {
return;
}
return asp(fs.readFile)(path.resolve(dir, 'bower.json'))
.then(function(bowerJson) {
try {
bowerJson = JSON.parse(bowerJson);
} catch(e) {
return;
}
main = bowerJson.main || '';
if (main instanceof Array) {
main = main[0];
}
return checkMain(main);
}, function() {})
.then(function(hasBowerMain) {
if (!hasBowerMain) {
return;
}
pjson.main = main;
});
});
},
// sync function to clean up any tmp files / store save caches etc
dispose: function() {
// temp folder that has been created during download will be automaically
// removed at exit due the call of temp.track();
}
};
module.exports = GitLocation;