This repository has been archived by the owner on Mar 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
232 lines (214 loc) · 5.84 KB
/
index.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
/**
* @license
* Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
* The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also
* subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/
// jshint node: true
'use strict';
var async = require('async');
var chalk = require('chalk');
var fs = require('fs');
var nopt = require('nopt');
var path = require('path');
var spawn = require('child_process').spawn;
var EOL = require('os').EOL;
// default to https
var ACCESS = 'https://';
// default to 10 concurrent checkous
var JOBS = 10;
// githubname::checkoutname
var SPLIT = '::';
// global array of clone/update failures
// report these at the end
var failed = [];
var options = nopt(
{
'branch': String,
'ssh': Boolean,
'jobs': Number,
'help': Boolean,
'quiet': Boolean
},
{
'-b': ['--branch'],
'-s': ['--ssh'],
'-j': ['--jobs'],
'-?': ['--help'],
'-h': ['--help'],
'-q': ['--quiet']
}
);
var configFiles = options.argv.remain;
if (!configFiles.length || options.help) {
console.log([
'bigstraw: get all the repos, fast',
'',
'Usage:',
' bigstraw [OPTIONS] <json file>*',
'',
'Options:',
' --branch <name>, -b <name>: Force a certain branch to checkout (Default: GitHub default branch)',
' --ssh, -s: Use ssh keys for push/pull access',
' --jobs #, -j #: Number of concurrent git operations (Default ' + JOBS + ')',
' --quiet, -q: Only print errors and success messages',
' --help, -h: Print this message'
].join(EOL));
process.exit();
}
if (options.jobs > 0) {
JOBS = options.jobs;
}
if (options.ssh) {
ACCESS = 'ssh://git@';
}
function githubToCheckout(repo) {
var parts = repo.split(SPLIT);
if (parts.length === 1) {
parts[1] = parts[0];
}
// [githubName, checkoutName]
return parts;
}
function cloneOrUpdate(repo, callback) {
fs.exists(repo.to, function(result) {
if (result) {
update(repo, callback);
} else {
clone(repo, callback);
}
});
}
function gitWrapper(repo, args, cwd, callback) {
var git = spawn('git', args, {
cwd: cwd,
stdout: 'ignore'
});
var operation = args[0];
// print a nice status message "=== pull foo ==="
if (!options.quiet) {
var sides = chalk.blue('===');
console.log(sides, chalk.blue.bold(operation), chalk.bold(path.basename(repo.to, '.git')), sides);
}
var errData = [];
git.stderr.on('data', function(data) {
errData.push(String(data));
});
git.on('close', function(code) {
if (code !== 0) {
failed.push({
operation: operation,
reason: errData.join(EOL),
repo: repo
});
}
callback();
});
}
function update(repo, callback) {
var ops = [
async.apply(gitWrapper, repo, ['pull', '--rebase'], repo.to),
// if .gitmodules exists, then try to update submodules
function(callback) {
fs.exists(path.join(repo.to, '.gitmodules'), function(exists) {
if (exists) {
gitWrapper(repo, ['submodule', 'update', '--init', '--recursive'], repo.to, callback);
} else {
callback();
}
});
}
];
var branch = repo.branch || options.branch;
if (branch) {
ops.unshift(async.apply(gitWrapper, repo, ['checkout', branch], repo.to));
}
async.series(ops, callback);
}
function clone(repo, callback) {
var args = [
'clone',
// don't hang on asking passwords
'-c',
'core.askpass=true',
'--recurse',
ACCESS + 'github.com/' + repo.from,
path.basename(repo.to)
];
var branch = repo.branch || options.branch;
if (branch) {
args.push('-b');
args.push(branch);
}
gitWrapper(repo, args, path.dirname(repo.to), callback);
}
async.waterfall([
// read and parse JSON configs from commandline
function(callback) {
var fn = async.seq(
fs.readFile, function(data, callback) {
try {
callback(null, JSON.parse(data));
} catch (e) {
callback(e, null);
}
});
async.map(configFiles, fn, callback);
},
// flatten configs to an array of repos
function(configs, callback) {
var repos = [];
var folders = [];
configs = configs.reduce(function(a, b) {
return a.concat(b);
});
configs.forEach(function(conf) {
conf.repos.forEach(function(r) {
folders[conf.dir] = 1;
var repoNames = githubToCheckout(r);
repos.push({
from: conf.org + '/' + repoNames[0] + '.git',
to: path.join(conf.dir, repoNames[1]),
branch: conf.branch
});
});
});
callback(null, repos, folders);
},
// create output folders
function(repos, folders, callback) {
async.each(Object.keys(folders), function(folder, cb) {
fs.mkdir(folder, function() {
cb();
});
});
callback(null, repos);
},
// clone or update repos
function(repos, callback) {
async.eachLimit(repos, JOBS, cloneOrUpdate, function() {
// report a nice error log
if (failed.length) {
console.log(chalk.bold.red('FAILED REPOS'));
failed.forEach(function(fail) {
console.log(chalk.bold('Repo: '), fail.repo.from);
console.log(chalk.bold('Folder: '), fail.repo.to);
console.log(chalk.bold('Operation: '), fail.operation);
console.log(chalk.bold('Reason: '), fail.reason);
});
callback('FAILED SYNC');
} else {
console.log(chalk.bold.green('OK'));
callback();
}
});
}
], function(err) {
if (err) {
console.log(chalk.bold.red(String(err)));
process.exit(1);
}
});