1010 'origin:: ' ,
1111 'tag:: ' ,
1212 'calculate-splits ' ,
13+ 'publish ' ,
1314 'help ' ,
1415]);
1516
1617if (isset ($ options ['help ' ])) {
1718 fwrite (STDOUT , <<<'HELP'
1819Usage:
19- php .github/scripts/monorepo-split-plan.php [--config=.github/monorepo-split.json] [--base=<git-ref>] [--head=<git-ref>] [--origin=<git-ref>] [--tag=<tag>] [--calculate-splits]
20+ php .github/scripts/monorepo-split-plan.php [--config=.github/monorepo-split.json] [--base=<git-ref>] [--head=<git-ref>] [--origin=<git-ref>] [--tag=<tag>] [--calculate-splits] [--publish]
2021
2122Validates the monorepo split map and prints the read-only repository split plan.
2223This is a dry-run planner only. By default it prints splitsh-lite commands but
2324does not run them. With --calculate-splits it runs splitsh-lite and prints split
24- SHAs, but still never pushes.
25+ SHAs, but still never pushes. With --publish it calculates split SHAs and pushes
26+ branch or tag refs to configured active read-only repositories.
2527
2628HELP);
2729 exit (0 );
3537$ origin = isset ($ options ['origin ' ]) ? (string ) $ options ['origin ' ] : 'HEAD ' ;
3638$ tag = isset ($ options ['tag ' ]) ? (string ) $ options ['tag ' ] : null ;
3739$ calculateSplits = isset ($ options ['calculate-splits ' ]);
40+ $ publish = isset ($ options ['publish ' ]);
41+ if ($ publish ) {
42+ $ calculateSplits = true ;
43+ }
3844
3945$ errors = [];
4046$ warnings = [];
156162 $ errors [] = 'splitsh-lite is required when --calculate-splits is provided. ' ;
157163}
158164
165+ if ($ publish && !commandExists ('git ' )) {
166+ $ errors [] = 'git is required when --publish is provided. ' ;
167+ }
168+
159169if (null !== $ base || null !== $ head ) {
160170 if (null === $ base || null === $ head ) {
161171 $ errors [] = 'Both --base and --head are required when checking changed paths. ' ;
165175}
166176
167177if ([] !== $ errors ) {
168- printSummary ($ configFile , $ packages , $ statusCounts , $ warnings , $ changedFiles , [], [], $ origin , $ tag , $ root , $ calculateSplits , $ errors );
178+ printSummary ($ configFile , $ packages , $ statusCounts , $ warnings , $ changedFiles , [], [], $ origin , $ tag , $ root , $ calculateSplits , $ publish , $ errors );
169179 fail ($ errors );
170180}
171181
172182[$ plannedPackages , $ skippedPackages ] = planPackages ($ packages , $ changedFiles , null !== $ tag );
173183$ splitErrors = [];
174- printSummary ($ configFile , $ packages , $ statusCounts , $ warnings , $ changedFiles , $ plannedPackages , $ skippedPackages , $ origin , $ tag , $ root , $ calculateSplits , $ splitErrors );
184+ printSummary ($ configFile , $ packages , $ statusCounts , $ warnings , $ changedFiles , $ plannedPackages , $ skippedPackages , $ origin , $ tag , $ root , $ calculateSplits , $ publish , $ splitErrors );
175185
176186if ([] !== $ splitErrors ) {
177187 fail ($ splitErrors );
@@ -319,9 +329,9 @@ function packageChanged(string $path, array $changedFiles): bool
319329 * @param list<array<string, mixed>> $plannedPackages
320330 * @param list<array{name: string, reason: string}> $skippedPackages
321331 */
322- function printSummary (string $ configFile , array $ packages , array $ statusCounts , array $ warnings , array $ changedFiles , array $ plannedPackages , array $ skippedPackages , string $ origin , ?string $ tag , string $ root , bool $ calculateSplits , array &$ splitErrors ): void
332+ function printSummary (string $ configFile , array $ packages , array $ statusCounts , array $ warnings , array $ changedFiles , array $ plannedPackages , array $ skippedPackages , string $ origin , ?string $ tag , string $ root , bool $ calculateSplits , bool $ publish , array &$ splitErrors ): void
323333{
324- fwrite (STDOUT , 'Monorepo split dry-run plan ' . PHP_EOL );
334+ fwrite (STDOUT , $ publish ? ' Monorepo split publish plan ' . PHP_EOL : 'Monorepo split dry-run plan ' . PHP_EOL );
325335 fwrite (STDOUT , 'Config: ' . $ configFile . PHP_EOL );
326336 fwrite (STDOUT , 'Split origin: ' . $ origin . PHP_EOL );
327337 if (null !== $ tag ) {
@@ -350,7 +360,7 @@ function printSummary(string $configFile, array $packages, array $statusCounts,
350360 fwrite (STDOUT , PHP_EOL . 'Changed files considered: none, planning every active package ' . PHP_EOL );
351361 }
352362
353- fwrite (STDOUT , PHP_EOL . sprintf ('Would split/publish: %d package(s) ' , count ($ plannedPackages )) . PHP_EOL );
363+ fwrite (STDOUT , PHP_EOL . sprintf ('%s split/publish: %d package(s)' , $ publish ? ' Will ' : ' Would ' , count ($ plannedPackages )) . PHP_EOL );
354364 foreach ($ plannedPackages as $ package ) {
355365 $ line = sprintf (
356366 ' - %s | path=%s | repository=%s | branch=%s ' ,
@@ -371,6 +381,9 @@ function printSummary(string $configFile, array $packages, array $statusCounts,
371381 $ sha = splitshSha ((string ) $ package ['path ' ], $ origin , $ root , $ splitErrors );
372382 if (null !== $ sha ) {
373383 fwrite (STDOUT , ' sha: ' . $ sha . PHP_EOL );
384+ if ($ publish ) {
385+ publishSplit ($ package , $ sha , $ tag , $ splitErrors );
386+ }
374387 }
375388 }
376389 }
@@ -389,6 +402,12 @@ function printSummary(string $configFile, array $packages, array $statusCounts,
389402 }
390403 }
391404
405+ if ($ publish ) {
406+ fwrite (STDOUT , PHP_EOL . 'Publish mode enabled. Planned split refs were pushed when no errors occurred. ' . PHP_EOL );
407+
408+ return ;
409+ }
410+
392411 if ($ calculateSplits ) {
393412 fwrite (STDOUT , PHP_EOL . 'Dry run only. splitsh-lite calculated split SHAs, but nothing was pushed. ' . PHP_EOL );
394413
@@ -428,6 +447,73 @@ function splitshSha(string $path, string $origin, string $root, array &$errors):
428447 return $ sha ;
429448}
430449
450+ /**
451+ * @param array<string, mixed> $package
452+ */
453+ function publishSplit (array $ package , string $ sha , ?string $ tag , array &$ errors ): void
454+ {
455+ $ name = (string ) $ package ['name ' ];
456+ $ repository = (string ) $ package ['repository ' ];
457+
458+ if (null !== $ tag ) {
459+ publishTag ($ name , $ repository , $ sha , $ tag , $ errors );
460+
461+ return ;
462+ }
463+
464+ $ branch = (string ) $ package ['branch ' ];
465+ $ refspec = sprintf ('%s:refs/heads/%s ' , $ sha , $ branch );
466+ $ command = sprintf ('git push %s %s 2>&1 ' , escapeshellarg ($ repository ), escapeshellarg ($ refspec ));
467+ exec ($ command , $ output , $ exitCode );
468+ if (0 !== $ exitCode ) {
469+ $ errors [] = sprintf ('%s branch publish failed: %s ' , $ name , implode (PHP_EOL , $ output ));
470+
471+ return ;
472+ }
473+
474+ fwrite (STDOUT , sprintf (' pushed: %s -> %s:%s ' , $ sha , $ repository , $ branch ) . PHP_EOL );
475+ }
476+
477+ function publishTag (string $ name , string $ repository , string $ sha , string $ tag , array &$ errors ): void
478+ {
479+ $ remoteTagRef = 'refs/tags/ ' . $ tag ;
480+ $ lookupCommand = sprintf (
481+ 'git ls-remote --tags --refs %s %s 2>&1 ' ,
482+ escapeshellarg ($ repository ),
483+ escapeshellarg ($ remoteTagRef ),
484+ );
485+ exec ($ lookupCommand , $ lookupOutput , $ lookupExitCode );
486+ if (0 !== $ lookupExitCode ) {
487+ $ errors [] = sprintf ('%s tag lookup failed for %s: %s ' , $ name , $ tag , implode (PHP_EOL , $ lookupOutput ));
488+
489+ return ;
490+ }
491+
492+ if ([] !== $ lookupOutput ) {
493+ $ remoteSha = strtok (trim ($ lookupOutput [0 ]), " \t" );
494+ if ($ remoteSha === $ sha ) {
495+ fwrite (STDOUT , sprintf (' tag exists: %s -> %s:%s ' , $ sha , $ repository , $ tag ) . PHP_EOL );
496+
497+ return ;
498+ }
499+
500+ $ errors [] = sprintf ('%s tag %s already exists on %s at %s, expected %s ' , $ name , $ tag , $ repository , $ remoteSha , $ sha );
501+
502+ return ;
503+ }
504+
505+ $ refspec = sprintf ('%s:%s ' , $ sha , $ remoteTagRef );
506+ $ pushCommand = sprintf ('git push %s %s 2>&1 ' , escapeshellarg ($ repository ), escapeshellarg ($ refspec ));
507+ exec ($ pushCommand , $ pushOutput , $ pushExitCode );
508+ if (0 !== $ pushExitCode ) {
509+ $ errors [] = sprintf ('%s tag publish failed for %s: %s ' , $ name , $ tag , implode (PHP_EOL , $ pushOutput ));
510+
511+ return ;
512+ }
513+
514+ fwrite (STDOUT , sprintf (' pushed tag: %s -> %s:%s ' , $ sha , $ repository , $ tag ) . PHP_EOL );
515+ }
516+
431517function commandExists (string $ command ): bool
432518{
433519 exec (sprintf ('command -v %s >/dev/null 2>&1 ' , escapeshellarg ($ command )), $ output , $ exitCode );
0 commit comments