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