Skip to content

Commit 46a57e7

Browse files
authored
fix: accept worktree base alias (#480)
1 parent faf6807 commit 46a57e7

2 files changed

Lines changed: 36 additions & 10 deletions

File tree

inc/Cli/Commands/WorkspaceCommand.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,9 +1992,12 @@ private function renderGitOperationResult( string $operation, array $result, arr
19921992
* [--from=<ref>]
19931993
* : Base ref when creating a branch on add (default origin/HEAD).
19941994
*
1995+
* [--base=<ref>]
1996+
* : Alias for `--from=<ref>`.
1997+
*
19951998
* [--base-branch=<branch>]
19961999
* : Convenience alias for branch-shaped bases. `--base-branch=main`
1997-
* maps to `--from=origin/main`. Use `--from` for exact refs.
2000+
* maps to `--from=origin/main`. Use `--from` or `--base` for exact refs.
19982001
*
19992002
* [--skip-context-injection]
20002003
* : Skip injecting the originating site's agent context into a new
@@ -2323,17 +2326,22 @@ public function worktree( array $args, array $assoc_args ): void {
23232326
switch ( $operation ) {
23242327
case 'add':
23252328
if ( empty($args[1]) || empty($args[2]) ) {
2326-
WP_CLI::error('Usage: worktree add <repo> <branch> [--from=<ref>|--base-branch=<branch>] [--skip-context-injection] [--skip-bootstrap] [--allow-stale] [--rebase-base] [--force]');
2329+
WP_CLI::error('Usage: worktree add <repo> <branch> [--from=<ref>|--base=<ref>|--base-branch=<branch>] [--skip-context-injection] [--skip-bootstrap] [--allow-stale] [--rebase-base] [--force]');
23272330
return;
23282331
}
23292332
$input['repo'] = $args[1];
23302333
$input['branch'] = $args[2];
2331-
if ( ! empty($assoc_args['from']) && ! empty($assoc_args['base-branch']) ) {
2332-
WP_CLI::error('Use either --from=<ref> or --base-branch=<branch>, not both.');
2334+
$exact_base = $assoc_args['from'] ?? $assoc_args['base'] ?? '';
2335+
if ( ! empty($assoc_args['from']) && ! empty($assoc_args['base']) ) {
2336+
WP_CLI::error('Use either --from=<ref> or --base=<ref>, not both.');
2337+
return;
2338+
}
2339+
if ( ! empty($exact_base) && ! empty($assoc_args['base-branch']) ) {
2340+
WP_CLI::error('Use either --from=<ref>/--base=<ref> or --base-branch=<branch>, not both.');
23332341
return;
23342342
}
2335-
if ( ! empty($assoc_args['from']) ) {
2336-
$input['from'] = (string) $assoc_args['from'];
2343+
if ( ! empty($exact_base) ) {
2344+
$input['from'] = (string) $exact_base;
23372345
} elseif ( ! empty($assoc_args['base-branch']) ) {
23382346
$input['from'] = self::base_branch_to_ref( (string) $assoc_args['base-branch']);
23392347
}

tests/smoke-worktree-base-branch-cli.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Smoke test for the workspace worktree --base-branch CLI alias.
3+
* Smoke test for workspace worktree base-ref CLI aliases.
44
*
55
* php tests/smoke-worktree-base-branch-cli.php
66
*
@@ -144,18 +144,36 @@ public function execute( array $input ): array
144144
);
145145
datamachine_code_assert('upstream/develop' === $ability->last_input['from'], 'remote ref preserved');
146146

147-
echo "\n[4] --from wins by rejecting ambiguous input\n";
147+
echo "\n[4] --base aliases exact --from refs\n";
148+
$command->worktree(
149+
array( 'add', 'data-machine', 'feat/test' ),
150+
array( 'base' => 'origin/main' )
151+
);
152+
datamachine_code_assert('origin/main' === $ability->last_input['from'], '--base forwards exact refs as from');
153+
154+
echo "\n[5] --from and --base reject ambiguous exact-base input\n";
155+
try {
156+
$command->worktree(
157+
array( 'add', 'data-machine', 'feat/test' ),
158+
array( 'from' => 'origin/main', 'base' => 'upstream/develop' )
159+
);
160+
datamachine_code_assert(false, 'ambiguous exact-base flags should throw');
161+
} catch ( RuntimeException $e ) {
162+
datamachine_code_assert(str_contains($e->getMessage(), 'not both'), 'ambiguous exact-base flags fail clearly');
163+
}
164+
165+
echo "\n[6] exact base flags reject ambiguous base-branch input\n";
148166
try {
149167
$command->worktree(
150168
array( 'add', 'data-machine', 'feat/test' ),
151-
array( 'from' => 'origin/main', 'base-branch' => 'develop' )
169+
array( 'base' => 'origin/main', 'base-branch' => 'develop' )
152170
);
153171
datamachine_code_assert(false, 'ambiguous flags should throw');
154172
} catch ( RuntimeException $e ) {
155173
datamachine_code_assert(str_contains($e->getMessage(), 'not both'), 'ambiguous flags fail clearly');
156174
}
157175

158-
echo "\n[5] --force is forwarded for add and JSON output keeps disk budget\n";
176+
echo "\n[7] --force is forwarded for add and JSON output keeps disk budget\n";
159177
\WP_CLI::reset_logs();
160178
$command->worktree(
161179
array( 'add', 'data-machine', 'feat/test' ),

0 commit comments

Comments
 (0)