Merge pull request #3112 from freeform/CRM-14568
[civicrm-core.git] / bin / givi
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
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) {
46 if (preg_match("/^https:\/\/github.com\/(.*)\/(civicrm-{$repo})\/pull\/([0-9]+)(|\/commits|\/files)$/", $url, $matches)) {
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
88 class Givi {
89
90 /**
91 * @var string 'checkout', 'begin', 'help', etc
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
125 /**
126 * @var bool
127 */
128 protected $force = FALSE;
129
130 /**
131 * @var bool
132 */
133 protected $rebase = FALSE;
134
135 /**
136 * @var string, the word 'all' or comma-delimited list of repo names
137 */
138 protected $repoFilter = 'all';
139
140 /**
141 * @var array ($repoName => $relPath)
142 */
143 protected $repos;
144
145 /**
146 * @var bool
147 */
148 protected $useGencode = FALSE;
149
150 /**
151 * @var bool
152 */
153 protected $useSetup = FALSE;
154
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' => '.',
174 'drupal' => 'drupal',
175 'joomla' => 'joomla',
176 'packages' => 'packages',
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
201 $this->repos = $this->filterRepos($this->repoFilter, $this->repos);
202
203 // Run the action
204 switch ($this->action) {
205 case 'checkout':
206 call_user_func_array(array($this, 'doCheckoutAll'), $this->arguments);
207 break;
208 case 'fetch':
209 call_user_func_array(array($this, 'doFetchAll'), $this->arguments);
210 break;
211 case 'status':
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;
220 case 'review':
221 call_user_func_array(array($this, 'doReview'), $this->arguments);
222 break;
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;
229 case 'help':
230 case '':
231 $this->doHelp();
232 break;
233 default:
234 return $this->returnError("unrecognized action: {$this->action}\n");
235 }
236
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
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 }
265 elseif ($arg == '--force' || $arg == '-f') {
266 $this->force = TRUE;
267 }
268 elseif ($arg == '--gencode') {
269 $this->useGencode = TRUE;
270 }
271 elseif ($arg == '--setup') {
272 $this->useSetup = TRUE;
273 }
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 }
280 elseif (preg_match('/^--repos=(.*)/', $arg, $matches)) {
281 $this->repoFilter = $matches[1];
282 }
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);
297
298 return TRUE;
299 }
300
301 function doHelp() {
302 $program = basename($this->program);
303 echo "Givi - Coordinate git checkouts across CiviCRM repositories\n";
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";
309 echo "Usage:\n";
310 echo " $program [options] checkout <branch>\n";
311 echo " $program [options] fetch\n";
312 echo " $program [options] status\n";
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";
315 echo " $program [options] review <base-branch> <pr-url-1> <pr-url-2>...\n";
316 #echo " $program [options] merge-forward <maintenace-branch> <development-branch>\n";
317 #echo " $program [options] push <remote> <branch>[:<branch>]\n";
318 echo "Actions:\n";
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";
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";
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";
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";
327 echo "Common options:\n";
328 echo " --dry-run: Don't do anything; only print commands that would be run\n";
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";
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";
332 echo " --fetch: Fetch the latest code before creating, updating, or checking-out anything\n";
333 echo " --repos=X: Restrict operations to the listed repos (comma-delimited list) (default: all)";
334 echo " --root=X: Specify CiviCRM root directory (default: .)\n";
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";
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";
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 }
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]);
364 $this->run($repo, $relPath, 'git', 'checkout', $filteredBranch, $this->force ? '-f' : NULL);
365 }
366 return TRUE;
367 }
368
369 function doStatusAll() {
370 foreach ($this->repos as $repo => $relPath) {
371 $this->run($repo, $relPath, 'git', 'status');
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);
391
392 if ($filteredBranch == $filteredBaseBranch) {
393 $this->run($repo, $relPath, 'git', 'checkout', $filteredBranch, $this->force ? '-f' : NULL);
394 }
395 else {
396 $this->run($repo, $relPath, 'git', 'checkout', '-b', $filteredBranch, $filteredBaseBranch, $this->force ? '-f' : NULL);
397 }
398 }
399
400 return TRUE;
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) {
416 $filteredBranch = $this->filterBranchName($repo, $branches[$repo]);
417 $filteredBaseBranch = $this->filterBranchName($repo, $baseBranch);
418
419 $this->run($repo, $relPath, 'git', 'checkout', $filteredBranch, $this->force ? '-f' : NULL);
420 if ($filteredBranch != $filteredBaseBranch && $this->rebase) {
421 list ($baseRemoteRepo, $baseRemoteBranch) = $this->parseBranchRepo($filteredBaseBranch);
422 $this->run($repo, $relPath, 'git', 'pull', '--rebase', $baseRemoteRepo, $baseRemoteBranch);
423 }
424 }
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;
458 }
459
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
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);
529 }
530
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 * @param string $defaultRemote
538 *
539 * @throws Exception
540 * @return array
541 */
542 function parseBranchRepo($ref, $defaultRemote = 'origin') {
543 $parts = explode('/', $ref);
544 if (count($parts) == 1) {
545 return array($defaultRemote, $parts[1]);
546 }
547 elseif (count($parts) == 2) {
548 return $parts;
549 }
550 else {
551 throw new Exception("Failed to parse branch name ($ref)");
552 }
553 }
554
555 /**
556 * Run a command
557 *
558 * Any items after $command will be escaped and added to $command
559 *
560 * @param $repoName
561 * @param string $runDir
562 * @param string $command
563 *
564 * @return string
565 */
566 function run($repoName, $runDir, $command) {
567 $this->dirStack->push($runDir);
568
569 $args = func_get_args();
570 array_shift($args);
571 array_shift($args);
572 array_shift($args);
573 foreach ($args as $arg) {
574 if ($arg !== NULL) {
575 $command .= ' ' . escapeshellarg($arg);
576 }
577 }
578 printf("\n\n\nRUN [%s]: %s\n", $repoName, $command);
579 if ($this->dryRun) {
580 $r = NULL;
581 } else {
582 $r = system($command);
583 }
584
585 $this->dirStack->pop();
586 return $r;
587 }
588
589 function doFetchAll() {
590 foreach ($this->repos as $repo => $relPath) {
591 $this->run($repo, $relPath, 'git', 'fetch', '--all');
592 }
593 }
594
595 /**
596 * @param string $default branch to use by default
597 * @param $overrides
598 *
599 * @return array ($repoName => $gitRef)
600 */
601 function resolveBranches($default, $overrides) {
602 $branches = $overrides;
603 foreach ($this->repos as $repo => $relPath) {
604 if (!isset($branches[$repo])) {
605 $branches[$repo] = $default;
606 }
607 }
608 return $branches;
609 }
610
611 function filterBranchName($repoName, $branchName) {
612 if ($branchName == '') {
613 return '';
614 }
615 if ($repoName == 'drupal') {
616 $parts = explode('/', $branchName);
617 $last = $this->drupalVersion . '.x-' . array_pop($parts);
618 array_push($parts, $last);
619 return implode('/', $parts);
620 }
621 return $branchName;
622 }
623
624 /**
625 * @param string $filter e.g. "all" or "repo1,repo2"
626 * @param array $repos ($repoName => $repoDir)
627 *
628 * @throws Exception
629 * @return array ($repoName => $repoDir)
630 */
631 function filterRepos($filter, $repos) {
632 if ($filter == 'all') {
633 return $repos;
634 }
635
636 $inclRepos = explode(',', $filter);
637 $unknowns = array_diff($inclRepos, array_keys($repos));
638 if (!empty($unknowns)) {
639 throw new Exception("Unknown Repos: " . implode(',', $unknowns));
640 }
641 $unwanted = array_diff(array_keys($repos), $inclRepos);
642 foreach ($unwanted as $repo) {
643 unset($repos[$repo]);
644 }
645 return $repos;
646 }
647
648 function returnError($message) {
649 echo "\nERROR: ", $message, "\n\n";
650 $this->doHelp();
651 return FALSE;
652 }
653 }
654
655 class HttpClient {
656 static function download($url, $file) {
657 // PHP native client is unreliable PITA for HTTPS
658 if (exec("which wget")) {
659 self::run('wget', '-q', '-O', $file, $url);
660 } elseif (exec("which curl")) {
661 self::run('curl', '-o', $file, $url);
662 }
663
664 // FIXME: really detect errors
665 return TRUE;
666 }
667
668 static function getJson($url) {
669 $file = tempnam(sys_get_temp_dir(), 'givi-json-');
670 HttpClient::download($url, $file);
671 $data = json_decode(file_get_contents($file));
672 unlink($file);
673 return $data;
674 }
675
676 /**
677 * Run a command
678 *
679 * Any items after $command will be escaped and added to $command
680 *
681 * @param string $command
682 *
683 * @internal param string $runDir
684 * @return string
685 */
686 static function run($command) {
687 $args = func_get_args();
688 array_shift($args);
689 foreach ($args as $arg) {
690 $command .= ' ' . escapeshellarg($arg);
691 }
692 printf("\n\n\nRUN: %s\n", $command);
693 $r = system($command);
694
695 return $r;
696 }
697 }
698
699 $givi = new Givi();
700 $givi->main($argv);