|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * Syncs the target branch with upstream and merges in the given source |
| 7 | + * branch, as done at several points of the release process. |
| 8 | + * |
| 9 | + * Usage: php admin/sync-release-branches.php <target> <source> [--push] |
| 10 | + */ |
| 11 | +function run_command(string $command): void |
| 12 | +{ |
| 13 | + echo sprintf("$ %s\n", $command); |
| 14 | + system($command, $exitCode); |
| 15 | + |
| 16 | + if ($exitCode !== 0) { |
| 17 | + exit($exitCode); |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +function merge_branch(string $ref): void |
| 22 | +{ |
| 23 | + echo sprintf("$ git merge %s\n", $ref); |
| 24 | + system(sprintf('git merge %s', escapeshellarg($ref)), $exitCode); |
| 25 | + |
| 26 | + if ($exitCode !== 0) { |
| 27 | + echo sprintf("\nMerging %s failed. Resolve the conflicts and run \"git merge --continue\",\n", $ref); |
| 28 | + echo "then push with \"git push upstream HEAD\".\n"; |
| 29 | + |
| 30 | + exit(1); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +chdir(__DIR__ . '/..'); |
| 35 | + |
| 36 | +$args = array_slice($argv, 1); |
| 37 | +$options = array_values(array_filter($args, static fn (string $arg): bool => str_starts_with($arg, '--'))); |
| 38 | +$params = array_values(array_diff($args, $options)); |
| 39 | + |
| 40 | +$isBranch = static fn (string $name): bool => preg_match('/\A(?:develop|master|\d+\.\d+)\z/', $name) === 1; |
| 41 | + |
| 42 | +if ( |
| 43 | + count($params) !== 2 |
| 44 | + || ! $isBranch($params[0]) |
| 45 | + || ! $isBranch($params[1]) |
| 46 | + || $params[0] === $params[1] |
| 47 | + || array_diff($options, ['--push']) !== [] |
| 48 | +) { |
| 49 | + echo sprintf("Usage: php %s <target> <source> [--push]\n", $argv[0]); |
| 50 | + echo sprintf("E.g.,: php %s 4.8 develop --push # merges develop into 4.8\n", $argv[0]); |
| 51 | + echo sprintf(" php %s develop master --push # merges master into develop\n", $argv[0]); |
| 52 | + echo "Checks out <target>, merges upstream/<target> and then upstream/<source> into it.\n"; |
| 53 | + echo "Without --push, the result is left unpushed for review.\n"; |
| 54 | + |
| 55 | + exit(1); |
| 56 | +} |
| 57 | + |
| 58 | +[$target, $source] = $params; |
| 59 | +$push = in_array('--push', $options, true); |
| 60 | + |
| 61 | +exec('git remote get-url upstream 2>&1', $remoteOutput, $exitCode); |
| 62 | + |
| 63 | +if ($exitCode !== 0) { |
| 64 | + echo "The \"upstream\" remote is not configured.\n"; |
| 65 | + |
| 66 | + exit(1); |
| 67 | +} |
| 68 | + |
| 69 | +exec('git status --porcelain 2>&1', $statusOutput, $exitCode); |
| 70 | + |
| 71 | +if ($exitCode !== 0 || $statusOutput !== []) { |
| 72 | + echo "The working tree is not clean. Commit or stash your changes first.\n"; |
| 73 | + |
| 74 | + exit(1); |
| 75 | +} |
| 76 | + |
| 77 | +run_command('git fetch upstream'); |
| 78 | + |
| 79 | +exec(sprintf('git rev-parse --verify --quiet refs/heads/%s 2>&1', escapeshellarg($target)), $verifyOutput, $exitCode); |
| 80 | + |
| 81 | +if ($exitCode === 0) { |
| 82 | + run_command(sprintf('git switch %s', escapeshellarg($target))); |
| 83 | +} else { |
| 84 | + run_command(sprintf('git switch -c %s upstream/%s', escapeshellarg($target), escapeshellarg($target))); |
| 85 | +} |
| 86 | + |
| 87 | +merge_branch("upstream/{$target}"); |
| 88 | +merge_branch("upstream/{$source}"); |
| 89 | + |
| 90 | +if ($push) { |
| 91 | + run_command('git push upstream HEAD'); |
| 92 | + |
| 93 | + echo sprintf("Merged upstream/%s into %s and pushed to upstream.\n", $source, $target); |
| 94 | +} else { |
| 95 | + echo sprintf("Merged upstream/%s into %s. Review the result, then run \"git push upstream HEAD\".\n", $source, $target); |
| 96 | +} |
0 commit comments