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