|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | + |
| 4 | +/* |
| 5 | + * This file is part of Gitonomy. |
| 6 | + * |
| 7 | + * (c) Alexandre Salomé <alexandre.salome@gmail.com> |
| 8 | + * (c) Julien DIDIER <genzo.wm@gmail.com> |
| 9 | + * |
| 10 | + * This source file is subject to the MIT license that is bundled |
| 11 | + * with this source code in the file LICENSE. |
| 12 | + */ |
| 13 | + |
| 14 | +require dirname(__DIR__, 2).'/vendor/autoload.php'; |
| 15 | + |
| 16 | +use Symfony\Component\Console\Application; |
| 17 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 18 | +use Symfony\Component\Console\Command\Command; |
| 19 | +use Symfony\Component\Console\Input\InputArgument; |
| 20 | +use Symfony\Component\Console\Input\InputInterface; |
| 21 | +use Symfony\Component\Console\Output\OutputInterface; |
| 22 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 23 | +use Symfony\Component\Process\Process; |
| 24 | + |
| 25 | +const BUNDLE_PATH = __DIR__.'/foobar.bundle'; |
| 26 | +const REFS_PATH = __DIR__.'/bundle-refs.txt'; |
| 27 | + |
| 28 | +function git(array $args, ?string $cwd = null): Process |
| 29 | +{ |
| 30 | + $process = new Process(['git', ...$args], $cwd); |
| 31 | + $process->mustRun(); |
| 32 | + |
| 33 | + return $process; |
| 34 | +} |
| 35 | + |
| 36 | +function bundleRefs(): array |
| 37 | +{ |
| 38 | + return array_values(array_filter(explode("\n", trim(file_get_contents(REFS_PATH))))); |
| 39 | +} |
| 40 | + |
| 41 | +#[AsCommand(name: 'extract', description: 'Clone the fixture bundle to a working directory, ready for new commits, branches or tags')] |
| 42 | +final class ExtractCommand extends Command |
| 43 | +{ |
| 44 | + protected function configure(): void |
| 45 | + { |
| 46 | + $this->addArgument('path', InputArgument::OPTIONAL, 'Where to clone the fixture', sys_get_temp_dir().'/foobar-fixture-'.bin2hex(random_bytes(4))); |
| 47 | + } |
| 48 | + |
| 49 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 50 | + { |
| 51 | + $io = new SymfonyStyle($input, $output); |
| 52 | + $dest = $input->getArgument('path'); |
| 53 | + |
| 54 | + if (file_exists($dest)) { |
| 55 | + $io->error(sprintf('Destination "%s" already exists.', $dest)); |
| 56 | + |
| 57 | + return Command::FAILURE; |
| 58 | + } |
| 59 | + |
| 60 | + git(['clone', '--quiet', BUNDLE_PATH, $dest]); |
| 61 | + |
| 62 | + $localBranches = explode("\n", trim(git(['branch', '--format=%(refname:short)'], $dest)->getOutput())); |
| 63 | + |
| 64 | + // Full ref names, not `--format=%(refname:short)`: git shortens the symbolic |
| 65 | + // refs/remotes/origin/HEAD pointer to a bare "origin" on some git versions, which |
| 66 | + // would otherwise be mistaken for a real branch called "origin". |
| 67 | + $refs = trim(git(['for-each-ref', '--format=%(refname)', 'refs/remotes/origin'], $dest)->getOutput()); |
| 68 | + foreach (explode("\n", $refs) as $ref) { |
| 69 | + if ('' === $ref) { |
| 70 | + continue; |
| 71 | + } |
| 72 | + $local = preg_replace('#^refs/remotes/origin/#', '', $ref); |
| 73 | + if ('HEAD' === $local || in_array($local, $localBranches, true)) { |
| 74 | + // Symbolic HEAD pointer, or already checked out as the clone's default branch. |
| 75 | + continue; |
| 76 | + } |
| 77 | + git(['branch', '--track', $local, "origin/{$local}"], $dest); |
| 78 | + } |
| 79 | + |
| 80 | + $io->success('Fixture extracted.'); |
| 81 | + $io->writeln([ |
| 82 | + sprintf('Path: %s', $dest), |
| 83 | + '', |
| 84 | + 'Make your changes there (commits, branches, tags), then run:', |
| 85 | + sprintf(' %s build %s', $_SERVER['argv'][0], $dest), |
| 86 | + ]); |
| 87 | + |
| 88 | + return Command::SUCCESS; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +#[AsCommand(name: 'build', description: 'Rebuild tests/fixtures/foobar.bundle from a working directory produced by "extract"')] |
| 93 | +final class BuildCommand extends Command |
| 94 | +{ |
| 95 | + protected function configure(): void |
| 96 | + { |
| 97 | + $this->addArgument('path', InputArgument::REQUIRED, 'The working directory produced by "extract"'); |
| 98 | + } |
| 99 | + |
| 100 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 101 | + { |
| 102 | + $io = new SymfonyStyle($input, $output); |
| 103 | + $src = $input->getArgument('path'); |
| 104 | + |
| 105 | + if (!is_dir($src)) { |
| 106 | + $io->error(sprintf('Source "%s" does not exist. Run "extract" first.', $src)); |
| 107 | + |
| 108 | + return Command::FAILURE; |
| 109 | + } |
| 110 | + |
| 111 | + $tmpBundle = tempnam(sys_get_temp_dir(), 'foobar_bundle_'); |
| 112 | + git(['bundle', 'create', $tmpBundle, ...bundleRefs()], $src); |
| 113 | + git(['bundle', 'verify', $tmpBundle]); |
| 114 | + |
| 115 | + copy($tmpBundle, BUNDLE_PATH); |
| 116 | + unlink($tmpBundle); |
| 117 | + |
| 118 | + $io->success('Bundle rebuilt.'); |
| 119 | + $io->writeln([ |
| 120 | + sprintf('Path: %s', BUNDLE_PATH), |
| 121 | + '', |
| 122 | + sprintf('If you added a ref not in %s, update that file first and re-run build.', REFS_PATH), |
| 123 | + 'Otherwise, update the commit constants in AbstractTestCase if needed, then run:', |
| 124 | + ' tests/fixtures/verify-bundle.sh', |
| 125 | + ]); |
| 126 | + |
| 127 | + return Command::SUCCESS; |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +$app = new Application('gitlib fixture bundle tool'); |
| 132 | +$app->addCommands([new ExtractCommand(), new BuildCommand()]); |
| 133 | +exit($app->run()); |
0 commit comments