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