Commit | Line | Data |
---|---|---|
6130de47 TO |
1 | #!/usr/bin/env php |
2 | <?php | |
3 | ||
4 | // This is the minimalist denialist implementation that doesn't check it's | |
5 | // pre-conditions and will screw up if you don't know what you're doing. | |
6 | ||
7 | /** | |
8 | * Manage the current working directory as a stack. | |
9 | */ | |
10 | class DirStack { | |
11 | protected $dirs; | |
12 | ||
13 | function __construct($dirs = array()) { | |
14 | $this->dirs = $dirs; | |
15 | } | |
16 | ||
17 | function push($dir) { | |
18 | $this->dirs[] = getcwd(); | |
19 | if (!chdir($dir)) { | |
20 | throw new Exception("Failed to chdir($dir)"); | |
21 | } | |
22 | } | |
23 | ||
24 | function pop() { | |
25 | $oldDir = array_pop($this->dirs); | |
26 | chdir($oldDir); | |
27 | } | |
28 | } | |
29 | ||
74d6f80e TO |
30 | /** |
31 | * FIXME: Why am I doing this? Can't we get a proper build-system for little | |
32 | * CLI tools -- and then use prepackaged libraries? | |
33 | */ | |
34 | class PullRequest { | |
35 | ||
36 | /** | |
37 | * Given a link to a pull-request, determine which local repo | |
38 | * it applies to and fetch any metadata. | |
39 | * | |
40 | * @param string $url | |
41 | * @param array $repos list of locally known repos | |
42 | * @return PullRequest|NULL | |
43 | */ | |
44 | public static function get($url, $repos) { | |
45 | foreach ($repos as $repo => $relPath) { | |
f8e9731a | 46 | if (preg_match("/^https:\/\/github.com\/(.*)\/(civicrm-{$repo})\/pull\/([0-9]+)(|\/commits|\/files)$/", $url, $matches)) { |
74d6f80e TO |
47 | list ($full, $githubUser, $githubRepo, $githubPr) = $matches; |
48 | ||
49 | $pr = new PullRequest(); | |
50 | $pr->repo = $repo; | |
51 | $pr->data = HttpClient::getJson("https://api.github.com/repos/$githubUser/$githubRepo/pulls/$githubPr"); | |
52 | if (empty($pr->data)) { | |
53 | return NULL; | |
54 | } | |
55 | ||
56 | return $pr; | |
57 | } | |
58 | } | |
59 | return NULL; | |
60 | } | |
61 | ||
62 | /** | |
63 | * @var string local repo name e.g. "core", "drupal" | |
64 | */ | |
65 | public $repo; | |
66 | ||
67 | protected $data; | |
68 | ||
69 | public function getNumber() { | |
70 | return $this->data->number; | |
71 | } | |
72 | ||
73 | /** | |
74 | * @return string name of the branch on the requestor's repo | |
75 | */ | |
76 | public function getRequestorBranch() { | |
77 | return $this->data->head->ref; | |
78 | } | |
79 | ||
80 | /** | |
81 | * @return string URL of the requestor's repo | |
82 | */ | |
83 | public function getRequestorRepoUrl() { | |
84 | return $this->data->head->repo->git_url; | |
85 | } | |
86 | } | |
87 | ||
6130de47 TO |
88 | class Givi { |
89 | ||
90 | /** | |
749e432e | 91 | * @var string 'checkout', 'begin', 'help', etc |
6130de47 TO |
92 | */ |
93 | protected $action; | |
94 | ||
95 | /** | |
96 | * @var string | |
97 | */ | |
98 | protected $baseBranch; | |
99 | ||
100 | /** | |
101 | * @var array ($repoName => $gitRef) | |
102 | */ | |
103 | protected $branches; | |
104 | ||
105 | /** | |
106 | * @var string | |
107 | */ | |
108 | protected $civiRoot = '.'; | |
109 | ||
110 | /** | |
111 | * @var int | |
112 | */ | |
113 | protected $drupalVersion = 7; | |
114 | ||
115 | /** | |
116 | * @var bool | |
117 | */ | |
118 | protected $dryRun = FALSE; | |
119 | ||
120 | /** | |
121 | * @var bool | |
122 | */ | |
123 | protected $fetch = FALSE; | |
124 | ||
74d6f80e TO |
125 | /** |
126 | * @var bool | |
127 | */ | |
128 | protected $force = FALSE; | |
129 | ||
6130de47 TO |
130 | /** |
131 | * @var bool | |
132 | */ | |
133 | protected $rebase = FALSE; | |
134 | ||
8d64bbe0 TO |
135 | /** |
136 | * @var string, the word 'all' or comma-delimited list of repo names | |
137 | */ | |
138 | protected $repoFilter = 'all'; | |
139 | ||
6130de47 TO |
140 | /** |
141 | * @var array ($repoName => $relPath) | |
142 | */ | |
143 | protected $repos; | |
144 | ||
4384f1bb TO |
145 | /** |
146 | * @var bool | |
147 | */ | |
148 | protected $useGencode = FALSE; | |
149 | ||
150 | /** | |
151 | * @var bool | |
152 | */ | |
153 | protected $useSetup = FALSE; | |
154 | ||
6130de47 TO |
155 | /** |
156 | * @var array, non-hyphenated arguments after the basedir | |
157 | */ | |
158 | protected $arguments; | |
159 | ||
160 | /** | |
161 | * @var string, the name of this program | |
162 | */ | |
163 | protected $program; | |
164 | ||
165 | /** | |
166 | * @var DirStack | |
167 | */ | |
168 | protected $dirStack; | |
169 | ||
170 | function __construct() { | |
171 | $this->dirStack = new DirStack(); | |
172 | $this->repos = array( | |
173 | 'core' => '.', | |
6130de47 | 174 | 'drupal' => 'drupal', |
4842290b TO |
175 | 'joomla' => 'joomla', |
176 | 'packages' => 'packages', | |
6130de47 TO |
177 | 'wordpress' => 'WordPress', |
178 | ); | |
179 | } | |
180 | ||
181 | function main($args) { | |
182 | if (!$this->parseOptions($args)) { | |
183 | printf("Error parsing arguments\n"); | |
184 | $this->doHelp(); | |
185 | return FALSE; | |
186 | } | |
187 | ||
188 | // All operations relative to civiRoot | |
189 | $this->dirStack->push($this->civiRoot); | |
190 | ||
191 | // Filter branch list based on what repos actually exist | |
192 | foreach (array_keys($this->repos) as $repo) { | |
193 | if (!is_dir($this->repos[$repo])) { | |
194 | unset($this->repos[$repo]); | |
195 | } | |
196 | } | |
197 | if (!isset($this->repos['core']) || !isset($this->repos['packages'])) { | |
198 | return $this->returnError("Root appears to be invalid -- missing too many repos. Try --root=<dir>\n"); | |
199 | } | |
200 | ||
8d64bbe0 TO |
201 | $this->repos = $this->filterRepos($this->repoFilter, $this->repos); |
202 | ||
6130de47 TO |
203 | // Run the action |
204 | switch ($this->action) { | |
749e432e | 205 | case 'checkout': |
6130de47 TO |
206 | call_user_func_array(array($this, 'doCheckoutAll'), $this->arguments); |
207 | break; | |
749e432e | 208 | case 'fetch': |
6130de47 TO |
209 | call_user_func_array(array($this, 'doFetchAll'), $this->arguments); |
210 | break; | |
749e432e | 211 | case 'status': |
6130de47 TO |
212 | call_user_func_array(array($this, 'doStatusAll'), $this->arguments); |
213 | break; | |
214 | case 'begin': | |
215 | call_user_func_array(array($this, 'doBegin'), $this->arguments); | |
216 | break; | |
217 | case 'resume': | |
218 | call_user_func_array(array($this, 'doResume'), $this->arguments); | |
219 | break; | |
74d6f80e TO |
220 | case 'review': |
221 | call_user_func_array(array($this, 'doReview'), $this->arguments); | |
222 | break; | |
8d64bbe0 TO |
223 | //case 'merge-forward': |
224 | // call_user_func_array(array($this, 'doMergeForward'), $this->arguments); | |
225 | // break; | |
226 | case 'push': | |
227 | call_user_func_array(array($this, 'doPush'), $this->arguments); | |
228 | break; | |
6130de47 | 229 | case 'help': |
03da1773 | 230 | case '': |
6130de47 TO |
231 | $this->doHelp(); |
232 | break; | |
233 | default: | |
234 | return $this->returnError("unrecognized action: {$this->action}\n"); | |
235 | } | |
236 | ||
4384f1bb TO |
237 | if ($this->useSetup) { |
238 | $this->run('core', $this->civiRoot . '/bin', 'bash', 'setup.sh'); | |
239 | } | |
240 | elseif ($this->useGencode) { | |
241 | $this->run('core', $this->civiRoot . '/xml', 'php', 'GenCode.php'); | |
242 | } | |
243 | ||
6130de47 TO |
244 | $this->dirStack->pop(); |
245 | } | |
246 | ||
247 | /** | |
248 | * @param $args | |
249 | * @return bool | |
250 | */ | |
251 | function parseOptions($args) { | |
252 | $this->branches = array(); | |
253 | $this->arguments = array(); | |
254 | ||
255 | foreach ($args as $arg) { | |
256 | if ($arg == '--fetch') { | |
257 | $this->fetch = TRUE; | |
258 | } | |
259 | elseif ($arg == '--rebase') { | |
260 | $this->rebase = TRUE; | |
261 | } | |
262 | elseif ($arg == '--dry-run' || $arg == '-n') { | |
263 | $this->dryRun = TRUE; | |
264 | } | |
74d6f80e TO |
265 | elseif ($arg == '--force' || $arg == '-f') { |
266 | $this->force = TRUE; | |
267 | } | |
4384f1bb TO |
268 | elseif ($arg == '--gencode') { |
269 | $this->useGencode = TRUE; | |
270 | } | |
271 | elseif ($arg == '--setup') { | |
272 | $this->useSetup = TRUE; | |
273 | } | |
6130de47 TO |
274 | elseif (preg_match('/^--d([678])/', $arg, $matches)) { |
275 | $this->drupalVersion = $matches[1]; | |
276 | } | |
277 | elseif (preg_match('/^--root=(.*)/', $arg, $matches)) { | |
278 | $this->civiRoot = $matches[1]; | |
279 | } | |
8d64bbe0 TO |
280 | elseif (preg_match('/^--repos=(.*)/', $arg, $matches)) { |
281 | $this->repoFilter = $matches[1]; | |
282 | } | |
6130de47 TO |
283 | elseif (preg_match('/^--(core|packages|joomla|drupal|wordpress)=(.*)/', $arg, $matches)) { |
284 | $this->branches[$matches[1]] = $matches[2]; | |
285 | } | |
286 | elseif (preg_match('/^-/', $arg)) { | |
287 | printf("unrecognized argument: %s\n", $arg); | |
288 | return FALSE; | |
289 | } | |
290 | else { | |
291 | $this->arguments[] = $arg; | |
292 | } | |
293 | } | |
294 | ||
295 | $this->program = @array_shift($this->arguments); | |
296 | $this->action = @array_shift($this->arguments); | |
74d6f80e | 297 | |
6130de47 TO |
298 | return TRUE; |
299 | } | |
300 | ||
301 | function doHelp() { | |
302 | $program = basename($this->program); | |
303 | echo "Givi - Coordinate git checkouts across CiviCRM repositories\n"; | |
729ccb4d TO |
304 | echo "Scenario:\n"; |
305 | echo " You have cloned and forked the CiviCRM repos. Each of the repos has two\n"; | |
306 | echo " remotes (origin + upstream). When working on a new PR, you generally want\n"; | |
307 | echo " to checkout official code (eg upstream/master) in all repos, but 1-2 repos\n"; | |
308 | echo " should use a custom branch (which tracks upstream/master).\n"; | |
6130de47 | 309 | echo "Usage:\n"; |
749e432e TO |
310 | echo " $program [options] checkout <branch>\n"; |
311 | echo " $program [options] fetch\n"; | |
312 | echo " $program [options] status\n"; | |
6130de47 TO |
313 | echo " $program [options] begin <base-branch> [--core=<new-branch>|--drupal=<new-branch>|...] \n"; |
314 | echo " $program [options] resume [--rebase] <base-branch> [--core=<custom-branch>|--drupal=<custom-branch>|...] \n"; | |
74d6f80e | 315 | echo " $program [options] review <base-branch> <pr-url-1> <pr-url-2>...\n"; |
8d64bbe0 TO |
316 | #echo " $program [options] merge-forward <maintenace-branch> <development-branch>\n"; |
317 | #echo " $program [options] push <remote> <branch>[:<branch>]\n"; | |
6130de47 | 318 | echo "Actions:\n"; |
749e432e TO |
319 | echo " checkout: Checkout same branch name on all repos\n"; |
320 | echo " fetch: Fetch remote changes on all repos\n"; | |
321 | echo " status: Display status on all repos\n"; | |
6130de47 TO |
322 | echo " begin: Begin work on a new branch on some repo (and use base-branch for all others)\n"; |
323 | echo " resume: Resume work on an existing branch on some repo (and use base-branch for all others)\n"; | |
74d6f80e | 324 | echo " review: Test work provided by someone else's pull-request. (If each repo has related PRs, then you can link to each of them.)\n"; |
8d64bbe0 TO |
325 | #echo " merge-forward: On each repo, merge changes from maintenance branch to development branch\n"; |
326 | #echo " push: On each repo, push a branch to a remote (Note: only intended for use with merge-forward)\n"; | |
6130de47 | 327 | echo "Common options:\n"; |
749e432e | 328 | echo " --dry-run: Don't do anything; only print commands that would be run\n"; |
6130de47 TO |
329 | echo " --d6: Specify that Drupal branches should use 6.x-* prefixes\n"; |
330 | echo " --d7: Specify that Drupal branches should use 7.x-* prefixes (default)\n"; | |
74d6f80e | 331 | echo " -f: When switching branches, proceed even if the index or the working tree differs from HEAD. This is used to throw away local changes.\n"; |
6130de47 | 332 | echo " --fetch: Fetch the latest code before creating, updating, or checking-out anything\n"; |
8d64bbe0 | 333 | echo " --repos=X: Restrict operations to the listed repos (comma-delimited list) (default: all)"; |
6130de47 | 334 | echo " --root=X: Specify CiviCRM root directory (default: .)\n"; |
4384f1bb TO |
335 | echo " --gencode: Run xml/GenCode after checking out code\n"; |
336 | echo " --setup: Run bin/setup.sh (incl xml/GenCode) after checking out code\n"; | |
6130de47 TO |
337 | echo "Special options:\n"; |
338 | echo " --core=X: Specify the branch to use on the core repository\n"; | |
339 | echo " --packages=X: Specify the branch to use on the packages repository\n"; | |
340 | echo " --drupal=X: Specify the branch to use on the drupal repository\n"; | |
341 | echo " --joomla=X: Specify the branch to use on the joomla repository\n"; | |
342 | echo " --wordpress=X: Specify the branch to use on the wordpress repository\n"; | |
343 | echo " --rebase: Perform a rebase before starting work\n"; | |
ab79d84c TO |
344 | echo "Known repositories:\n"; |
345 | foreach ($this->repos as $repo => $relPath) { | |
346 | printf(" %-12s: %s\n", $repo, realpath($this->civiRoot . DIRECTORY_SEPARATOR . $relPath)); | |
347 | } | |
6130de47 TO |
348 | echo "When using 'begin' or 'resume' with a remote base-branch, most repositories\n"; |
349 | echo "will have a detached HEAD. Only repos with an explicit branch will be real,\n"; | |
350 | echo "local branches.\n"; | |
351 | } | |
352 | ||
353 | function doCheckoutAll($baseBranch = NULL) { | |
354 | if (!$baseBranch) { | |
355 | return $this->returnError("Missing <branch>\n"); | |
356 | } | |
357 | $branches = $this->resolveBranches($baseBranch, $this->branches); | |
358 | if ($this->fetch) { | |
359 | $this->doFetchAll(); | |
360 | } | |
361 | ||
362 | foreach ($this->repos as $repo => $relPath) { | |
363 | $filteredBranch = $this->filterBranchName($repo, $branches[$repo]); | |
74d6f80e | 364 | $this->run($repo, $relPath, 'git', 'checkout', $filteredBranch, $this->force ? '-f' : NULL); |
6130de47 TO |
365 | } |
366 | return TRUE; | |
367 | } | |
368 | ||
369 | function doStatusAll() { | |
370 | foreach ($this->repos as $repo => $relPath) { | |
8d64bbe0 | 371 | $this->run($repo, $relPath, 'git', 'status'); |
6130de47 TO |
372 | } |
373 | return TRUE; | |
374 | } | |
375 | ||
376 | function doBegin($baseBranch = NULL) { | |
377 | if (!$baseBranch) { | |
378 | return $this->returnError("Missing <base-branch>\n"); | |
379 | } | |
380 | if (empty($this->branches)) { | |
381 | return $this->returnError("Must specify a custom branch for at least one repository.\n"); | |
382 | } | |
383 | $branches = $this->resolveBranches($baseBranch, $this->branches); | |
384 | if ($this->fetch) { | |
385 | $this->doFetchAll(); | |
386 | } | |
387 | ||
388 | foreach ($this->repos as $repo => $relPath) { | |
389 | $filteredBranch = $this->filterBranchName($repo, $branches[$repo]); | |
390 | $filteredBaseBranch = $this->filterBranchName($repo, $baseBranch); | |
9476bf6f | 391 | |
6130de47 | 392 | if ($filteredBranch == $filteredBaseBranch) { |
74d6f80e | 393 | $this->run($repo, $relPath, 'git', 'checkout', $filteredBranch, $this->force ? '-f' : NULL); |
6130de47 TO |
394 | } |
395 | else { | |
74d6f80e | 396 | $this->run($repo, $relPath, 'git', 'checkout', '-b', $filteredBranch, $filteredBaseBranch, $this->force ? '-f' : NULL); |
6130de47 TO |
397 | } |
398 | } | |
74d6f80e TO |
399 | |
400 | return TRUE; | |
6130de47 TO |
401 | } |
402 | ||
403 | function doResume($baseBranch = NULL) { | |
404 | if (!$baseBranch) { | |
405 | return $this->returnError("Missing <base-branch>\n"); | |
406 | } | |
407 | if (empty($this->branches)) { | |
408 | return $this->returnError("Must specify a custom branch for at least one repository.\n"); | |
409 | } | |
410 | $branches = $this->resolveBranches($baseBranch, $this->branches); | |
411 | if ($this->fetch) { | |
412 | $this->doFetchAll(); | |
413 | } | |
414 | ||
415 | foreach ($this->repos as $repo => $relPath) { | |
9476bf6f TO |
416 | $filteredBranch = $this->filterBranchName($repo, $branches[$repo]); |
417 | $filteredBaseBranch = $this->filterBranchName($repo, $baseBranch); | |
418 | ||
74d6f80e | 419 | $this->run($repo, $relPath, 'git', 'checkout', $filteredBranch, $this->force ? '-f' : NULL); |
9476bf6f TO |
420 | if ($filteredBranch != $filteredBaseBranch && $this->rebase) { |
421 | list ($baseRemoteRepo, $baseRemoteBranch) = $this->parseBranchRepo($filteredBaseBranch); | |
8d64bbe0 | 422 | $this->run($repo, $relPath, 'git', 'pull', '--rebase', $baseRemoteRepo, $baseRemoteBranch); |
6130de47 TO |
423 | } |
424 | } | |
74d6f80e TO |
425 | |
426 | return TRUE; | |
427 | } | |
428 | ||
429 | function doReview($baseBranch = NULL) { | |
430 | if (! $this->doCheckoutAll($baseBranch)) { | |
431 | return FALSE; | |
432 | } | |
433 | ||
434 | $args = func_get_args(); | |
435 | array_shift($args); // $baseBranch | |
436 | ||
437 | $pullRequests = array(); | |
438 | foreach ($args as $prUrl) { | |
439 | $pullRequest = PullRequest::get($prUrl, $this->repos); | |
440 | if ($pullRequest) { | |
441 | $pullRequests[] = $pullRequest; | |
442 | } else { | |
443 | return $this->returnError("Invalid pull-request URL: $prUrl"); | |
444 | } | |
445 | } | |
446 | ||
447 | foreach ($pullRequests as $pullRequest) { | |
448 | $repo = $pullRequest->repo; | |
449 | $branchName = 'pull-request-' . $pullRequest->getNumber(); | |
450 | if ($this->hasLocalBranch($repo, $branchName)) { | |
451 | $this->run($repo, $this->repos[$repo], 'git', 'branch', '-D', $branchName); | |
452 | } | |
453 | $this->run($repo, $this->repos[$repo], 'git', 'checkout', '-b', $branchName); ## based on whatever was chosen by doCheckoutAll() | |
454 | $this->run($repo, $this->repos[$repo], 'git', 'pull', $pullRequest->getRequestorRepoUrl(), $pullRequest->getRequestorBranch()); | |
455 | } | |
456 | ||
457 | return TRUE; | |
6130de47 TO |
458 | } |
459 | ||
8d64bbe0 TO |
460 | /* |
461 | ||
462 | If we want merge-forward changes to be subject to PR process, then this | |
463 | should useful. Currently using a simpler process based on | |
464 | toosl/scripts/merge-forward | |
465 | ||
466 | function doMergeForward($maintBranch, $devBranch) { | |
467 | if (!$maintBranch) { | |
468 | return $this->returnError("Missing <maintenace-base-branch>\n"); | |
469 | } | |
470 | if (!$devBranch) { | |
471 | return $this->returnError("Missing <development-base-branch>\n"); | |
472 | } | |
473 | list ($maintBranchRepo, $maintBranchName) = $this->parseBranchRepo($maintBranch); | |
474 | list ($devBranchRepo, $devBranchName) = $this->parseBranchRepo($devBranch); | |
475 | ||
476 | $newBranchRepo = $devBranchRepo; | |
477 | $newBranchName = $maintBranchName . '-' . $devBranchName . '-' . date('Y-m-d-H-i-s'); | |
478 | ||
479 | if ($this->fetch) { | |
480 | $this->doFetchAll(); | |
481 | } | |
482 | ||
483 | foreach ($this->repos as $repo => $relPath) { | |
484 | $filteredMaintBranch = $this->filterBranchName($repo, $maintBranch); | |
485 | $filteredDevBranch = $this->filterBranchName($repo, $devBranch); | |
486 | $filteredNewBranchName = $this->filterBranchName($repo, $newBranchName); | |
487 | ||
488 | $this->run($repo, $relPath, 'git', 'checkout', '-b', $filteredNewBranchName, $filteredDevBranch); | |
489 | $this->run($repo, $relPath, 'git', 'merge', $filteredMaintBranch); | |
490 | } | |
491 | } | |
492 | */ | |
493 | ||
494 | function doPush($newBranchRepo, $newBranchNames) { | |
495 | if (!$newBranchRepo) { | |
496 | return $this->returnError("Missing <remote>\n"); | |
497 | } | |
498 | if (!$newBranchNames) { | |
499 | return $this->returnError("Missing <branch>[:<branch>]\n"); | |
500 | } | |
501 | if (FALSE !== strpos($newBranchNames, ':')) { | |
502 | list ($newBranchFromName,$newBranchToName) = explode(':', $newBranchNames); | |
503 | foreach ($this->repos as $repo => $relPath) { | |
504 | $filteredFromName = $this->filterBranchName($repo, $newBranchFromName); | |
505 | $filteredToName = $this->filterBranchName($repo, $newBranchToName); | |
506 | ||
507 | $this->run($repo, $relPath, 'git', 'push', $newBranchRepo, $filteredFromName . ':' . $filteredToName); | |
508 | } | |
509 | } else { | |
510 | foreach ($this->repos as $repo => $relPath) { | |
511 | $filteredName = $this->filterBranchName($repo, $newBranchNames); | |
512 | $this->run($repo, $relPath, 'git', 'push', $newBranchRepo, $filteredName); | |
513 | } | |
514 | } | |
515 | ||
74d6f80e TO |
516 | return TRUE; |
517 | } | |
518 | ||
519 | /** | |
520 | * Determine if a branch exists locally | |
521 | * | |
522 | * @param string $repo | |
523 | * @param string $name branch name | |
524 | * @return bool | |
525 | */ | |
526 | function hasLocalBranch($repo, $name) { | |
527 | $path = $this->repos[$repo] . '/.git/refs/heads/' . $name; | |
528 | return file_exists($path); | |
8d64bbe0 TO |
529 | } |
530 | ||
6130de47 TO |
531 | /** |
532 | * Given a ref name, determine the repo and branch | |
533 | * | |
534 | * FIXME: only supports $refs like "foo" (implicit origin) or "myremote/foo" | |
535 | * | |
536 | * @param $ref | |
537 | * @return array | |
538 | */ | |
539 | function parseBranchRepo($ref, $defaultRemote = 'origin') { | |
540 | $parts = explode('/', $ref); | |
541 | if (count($parts) == 1) { | |
542 | return array($defaultRemote, $parts[1]); | |
543 | } | |
544 | elseif (count($parts) == 2) { | |
545 | return $parts; | |
546 | } | |
547 | else { | |
548 | throw new Exception("Failed to parse branch name ($ref)"); | |
549 | } | |
550 | } | |
551 | ||
552 | /** | |
553 | * Run a command | |
554 | * | |
555 | * Any items after $command will be escaped and added to $command | |
556 | * | |
557 | * @param string $runDir | |
558 | * @param string $command | |
559 | * @return string | |
560 | */ | |
8d64bbe0 | 561 | function run($repoName, $runDir, $command) { |
6130de47 TO |
562 | $this->dirStack->push($runDir); |
563 | ||
564 | $args = func_get_args(); | |
565 | array_shift($args); | |
566 | array_shift($args); | |
8d64bbe0 | 567 | array_shift($args); |
6130de47 | 568 | foreach ($args as $arg) { |
74d6f80e TO |
569 | if ($arg !== NULL) { |
570 | $command .= ' ' . escapeshellarg($arg); | |
571 | } | |
6130de47 | 572 | } |
8d64bbe0 | 573 | printf("\n\n\nRUN [%s]: %s\n", $repoName, $command); |
6130de47 TO |
574 | if ($this->dryRun) { |
575 | $r = NULL; | |
576 | } else { | |
577 | $r = system($command); | |
578 | } | |
579 | ||
580 | $this->dirStack->pop(); | |
581 | return $r; | |
582 | } | |
583 | ||
584 | function doFetchAll() { | |
585 | foreach ($this->repos as $repo => $relPath) { | |
8d64bbe0 | 586 | $this->run($repo, $relPath, 'git', 'fetch', '--all'); |
6130de47 TO |
587 | } |
588 | } | |
589 | ||
590 | /** | |
591 | * @param string $default branch to use by default | |
592 | * @return array ($repoName => $gitRef) | |
593 | */ | |
594 | function resolveBranches($default, $overrides) { | |
595 | $branches = $overrides; | |
596 | foreach ($this->repos as $repo => $relPath) { | |
597 | if (!isset($branches[$repo])) { | |
598 | $branches[$repo] = $default; | |
599 | } | |
600 | } | |
601 | return $branches; | |
602 | } | |
603 | ||
604 | function filterBranchName($repoName, $branchName) { | |
8d64bbe0 TO |
605 | if ($branchName == '') { |
606 | return ''; | |
607 | } | |
6130de47 TO |
608 | if ($repoName == 'drupal') { |
609 | $parts = explode('/', $branchName); | |
610 | $last = $this->drupalVersion . '.x-' . array_pop($parts); | |
611 | array_push($parts, $last); | |
612 | return implode('/', $parts); | |
613 | } | |
614 | return $branchName; | |
615 | } | |
616 | ||
8d64bbe0 TO |
617 | /** |
618 | * @param string $filter e.g. "all" or "repo1,repo2" | |
619 | * @param array $repos ($repoName => $repoDir) | |
620 | * @return array ($repoName => $repoDir) | |
621 | */ | |
622 | function filterRepos($filter, $repos) { | |
623 | if ($filter == 'all') { | |
624 | return $repos; | |
625 | } | |
626 | ||
627 | $inclRepos = explode(',', $filter); | |
628 | $unknowns = array_diff($inclRepos, array_keys($repos)); | |
629 | if (!empty($unknowns)) { | |
630 | throw new Exception("Unknown Repos: " . implode(',', $unknowns)); | |
631 | } | |
632 | $unwanted = array_diff(array_keys($repos), $inclRepos); | |
633 | foreach ($unwanted as $repo) { | |
634 | unset($repos[$repo]); | |
635 | } | |
636 | return $repos; | |
637 | } | |
638 | ||
6130de47 | 639 | function returnError($message) { |
74d6f80e | 640 | echo "\nERROR: ", $message, "\n\n"; |
6130de47 TO |
641 | $this->doHelp(); |
642 | return FALSE; | |
643 | } | |
644 | } | |
645 | ||
74d6f80e TO |
646 | class HttpClient { |
647 | static function download($url, $file) { | |
648 | // PHP native client is unreliable PITA for HTTPS | |
649 | if (exec("which wget")) { | |
650 | self::run('wget', '-q', '-O', $file, $url); | |
651 | } elseif (exec("which curl")) { | |
652 | self::run('curl', '-o', $file, $url); | |
653 | } | |
654 | ||
655 | // FIXME: really detect errors | |
656 | return TRUE; | |
657 | } | |
658 | ||
659 | static function getJson($url) { | |
660 | $file = tempnam(sys_get_temp_dir(), 'givi-json-'); | |
661 | HttpClient::download($url, $file); | |
662 | $data = json_decode(file_get_contents($file)); | |
663 | unlink($file); | |
664 | return $data; | |
665 | } | |
666 | ||
667 | /** | |
668 | * Run a command | |
669 | * | |
670 | * Any items after $command will be escaped and added to $command | |
671 | * | |
672 | * @param string $runDir | |
673 | * @param string $command | |
674 | * @return string | |
675 | */ | |
676 | static function run($command) { | |
677 | $args = func_get_args(); | |
678 | array_shift($args); | |
679 | foreach ($args as $arg) { | |
680 | $command .= ' ' . escapeshellarg($arg); | |
681 | } | |
682 | printf("\n\n\nRUN: %s\n", $command); | |
683 | $r = system($command); | |
684 | ||
685 | return $r; | |
686 | } | |
687 | } | |
688 | ||
6130de47 TO |
689 | $givi = new Givi(); |
690 | $givi->main($argv); |