Skip to content

Commit c11413a

Browse files
authored
Merge pull request #673 from Extra-Chill/fix/issue-672-local-primary-worktree
Fix local primary worktree routing
2 parents f54667b + f8ba766 commit c11413a

2 files changed

Lines changed: 46 additions & 3 deletions

File tree

inc/Abilities/WorkspaceAbilities.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3418,6 +3418,21 @@ public static function worktreeAdd( array $input ): array|\WP_Error {
34183418
$task['task_ref'] = (string) $input['task_ref'];
34193419
}
34203420

3421+
$workspace = new Workspace();
3422+
if ( RemoteWorkspaceBackend::should_handle() && self::hasLocalPrimaryCheckout($workspace, (string) ( $input['repo'] ?? '' )) ) {
3423+
return $workspace->worktree_add(
3424+
$input['repo'] ?? '',
3425+
$input['branch'] ?? '',
3426+
$input['from'] ?? null,
3427+
$inject_context,
3428+
$bootstrap,
3429+
$allow_stale,
3430+
$rebase_base,
3431+
$force,
3432+
$task
3433+
);
3434+
}
3435+
34213436
if ( RemoteWorkspaceBackend::should_handle() ) {
34223437
$result = ( new RemoteWorkspaceBackend() )->worktree_add(
34233438
$input['repo'] ?? '',
@@ -3429,7 +3444,6 @@ public static function worktreeAdd( array $input ): array|\WP_Error {
34293444
}
34303445
}
34313446

3432-
$workspace = new Workspace();
34333447
return $workspace->worktree_add(
34343448
$input['repo'] ?? '',
34353449
$input['branch'] ?? '',
@@ -3443,6 +3457,23 @@ public static function worktreeAdd( array $input ): array|\WP_Error {
34433457
);
34443458
}
34453459

3460+
/**
3461+
* Whether a repo argument resolves to an editable local primary checkout.
3462+
*/
3463+
private static function hasLocalPrimaryCheckout( Workspace $workspace, string $repo ): bool {
3464+
if ( '' === trim($repo) ) {
3465+
return false;
3466+
}
3467+
3468+
$result = $workspace->show_repo($repo);
3469+
if ( is_wp_error($result) ) {
3470+
return false;
3471+
}
3472+
3473+
$path = (string) ( $result['path'] ?? '' );
3474+
return '' !== $path && ! str_starts_with($path, 'github://') && ! str_contains(basename($path), '@');
3475+
}
3476+
34463477
/**
34473478
* Whether a remote-backend miss should be retried against local workspace discovery.
34483479
*/

tests/smoke-workspace-local-fallback.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class RemoteWorkspaceBackend
2222
{
2323
public static array $show_input = array();
2424
public static array $worktree_input = array();
25+
public static bool $worktree_returns_remote_success = false;
2526

2627
public static function should_handle(): bool
2728
{
@@ -37,9 +38,17 @@ public function show( string $handle ): \WP_Error
3738
);
3839
}
3940

40-
public function worktree_add( string $repo_name, string $branch, ?string $from = null ): \WP_Error
41+
public function worktree_add( string $repo_name, string $branch, ?string $from = null ): array|\WP_Error
4142
{
4243
self::$worktree_input = compact('repo_name', 'branch', 'from');
44+
if ( self::$worktree_returns_remote_success ) {
45+
return array(
46+
'success' => true,
47+
'handle' => $repo_name . '@' . str_replace('/', '-', $branch),
48+
'path' => 'github://Automattic/' . $repo_name . '#' . $branch,
49+
);
50+
}
51+
4352
return new \WP_Error(
4453
'remote_workspace_repo_not_found',
4554
'Remote workspace repository "wpcom-codebox" is not registered. Call workspace_clone first.'
@@ -156,6 +165,8 @@ function is_wp_error( $value ): bool
156165
$assert('local show fallback attempted', 'wpcom-codebox' === ( \DataMachineCode\Workspace\Workspace::$show_input['handle'] ?? '' ));
157166
$assert('show returns local listed primary', is_array($show) && '/Users/chubes/Developer/wpcom-codebox' === ( $show['path'] ?? '' ));
158167

168+
\DataMachineCode\Workspace\RemoteWorkspaceBackend::$worktree_returns_remote_success = true;
169+
\DataMachineCode\Workspace\RemoteWorkspaceBackend::$worktree_input = array();
159170
$worktree = \DataMachineCode\Abilities\WorkspaceAbilities::worktreeAdd(
160171
array(
161172
'repo' => 'wpcom-codebox',
@@ -171,12 +182,13 @@ function is_wp_error( $value ): bool
171182
)
172183
);
173184

174-
$assert('remote worktree add attempted first', 'wpcom-codebox' === ( \DataMachineCode\Workspace\RemoteWorkspaceBackend::$worktree_input['repo_name'] ?? '' ));
185+
$assert('remote github worktree row is ignored when local primary exists', array() === \DataMachineCode\Workspace\RemoteWorkspaceBackend::$worktree_input);
175186
$assert('local worktree add fallback attempted', 'wpcom-codebox' === ( \DataMachineCode\Workspace\Workspace::$worktree_input['repo'] ?? '' ));
176187
$assert('worktree add preserves base ref', 'origin/main' === ( \DataMachineCode\Workspace\Workspace::$worktree_input['from'] ?? '' ));
177188
$assert('worktree add preserves local options', false === ( \DataMachineCode\Workspace\Workspace::$worktree_input['inject_context'] ?? null ) && true === ( \DataMachineCode\Workspace\Workspace::$worktree_input['allow_stale'] ?? null ));
178189
$assert('worktree add preserves task metadata', 'Extra-Chill/data-machine-code#635' === ( \DataMachineCode\Workspace\Workspace::$worktree_input['task']['task_ref'] ?? '' ));
179190
$assert('worktree add returns local worktree result', is_array($worktree) && 'wpcom-codebox@fix-listed-primary-resolution' === ( $worktree['handle'] ?? '' ));
191+
$assert('worktree add returns editable local path', is_array($worktree) && '/Users/chubes/Developer/wpcom-codebox@fix-listed-primary-resolution' === ( $worktree['path'] ?? '' ));
180192

181193
if ( $failures ) {
182194
echo "\nFailures:\n";

0 commit comments

Comments
 (0)