Skip to content

Commit ad3080e

Browse files
authored
Merge pull request #688 from Extra-Chill/fix/issue-687-local-workspace-precedence
Fix local workspace path precedence
2 parents 841d3ac + 0cca06a commit ad3080e

2 files changed

Lines changed: 72 additions & 12 deletions

File tree

inc/Abilities/WorkspaceAbilities.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2493,14 +2493,21 @@ public static function getCapabilities( array $input ): array { // phpcs:ignor
24932493
* @return array Result.
24942494
*/
24952495
public static function showRepo( array $input ): array|\WP_Error {
2496+
$workspace = new Workspace();
2497+
if ( RemoteWorkspaceBackend::should_handle() ) {
2498+
$local_result = self::showLocalWorkspaceHandleIfPresent($workspace, (string) ( $input['name'] ?? '' ));
2499+
if ( null !== $local_result ) {
2500+
return $local_result;
2501+
}
2502+
}
2503+
24962504
if ( RemoteWorkspaceBackend::should_handle() ) {
24972505
$result = ( new RemoteWorkspaceBackend() )->show($input['name'] ?? '');
24982506
if ( ! self::shouldFallbackToLocalWorkspace($result) ) {
24992507
return $result;
25002508
}
25012509
}
25022510

2503-
$workspace = new Workspace();
25042511
return $workspace->show_repo($input['name'] ?? '');
25052512
}
25062513

@@ -3461,17 +3468,34 @@ public static function worktreeAdd( array $input ): array|\WP_Error {
34613468
* Whether a repo argument resolves to an editable local primary checkout.
34623469
*/
34633470
private static function hasLocalPrimaryCheckout( Workspace $workspace, string $repo ): bool {
3464-
if ( '' === trim($repo) ) {
3471+
$result = self::showLocalWorkspaceHandleIfPresent($workspace, $repo);
3472+
if ( null === $result ) {
34653473
return false;
34663474
}
34673475

3468-
$result = $workspace->show_repo($repo);
3476+
$path = (string) ( $result['path'] ?? '' );
3477+
return ! str_contains(basename($path), '@');
3478+
}
3479+
3480+
/**
3481+
* Return local workspace details for an existing local handle, if present.
3482+
*/
3483+
private static function showLocalWorkspaceHandleIfPresent( Workspace $workspace, string $handle ): ?array {
3484+
if ( '' === trim($handle) ) {
3485+
return null;
3486+
}
3487+
3488+
$result = $workspace->show_repo($handle);
34693489
if ( is_wp_error($result) ) {
3470-
return false;
3490+
return null;
34713491
}
34723492

34733493
$path = (string) ( $result['path'] ?? '' );
3474-
return '' !== $path && ! str_starts_with($path, 'github://') && ! str_contains(basename($path), '@');
3494+
if ( '' === $path || str_starts_with($path, 'github://') ) {
3495+
return null;
3496+
}
3497+
3498+
return $result;
34753499
}
34763500

34773501
/**

tests/smoke-workspace-local-fallback.php

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

2728
public static function should_handle(): bool
2829
{
2930
return true;
3031
}
3132

32-
public function show( string $handle ): \WP_Error
33+
public function show( string $handle ): array|\WP_Error
3334
{
3435
self::$show_input = compact('handle');
36+
if ( self::$show_returns_remote_success ) {
37+
return array(
38+
'success' => true,
39+
'name' => $handle,
40+
'repo' => $handle,
41+
'is_worktree' => str_contains($handle, '@'),
42+
'path' => 'github://Automattic/' . str_replace('@', '#', $handle),
43+
);
44+
}
45+
3546
return new \WP_Error(
3647
'remote_workspace_repo_not_found',
3748
'Remote workspace repository "wpcom-codebox" is not registered. Call workspace_clone first.'
@@ -64,13 +75,17 @@ class Workspace
6475
public function show_repo( string $handle ): array
6576
{
6677
self::$show_input = compact('handle');
78+
$parsed = explode('@', $handle, 2);
79+
$repo = $parsed[0];
80+
$slug = $parsed[1] ?? '';
81+
$path = '/Users/chubes/Developer/' . $handle;
6782
return array(
6883
'success' => true,
6984
'name' => $handle,
70-
'repo' => $handle,
71-
'is_worktree' => false,
72-
'path' => '/Users/chubes/Developer/' . $handle,
73-
'branch' => 'main',
85+
'repo' => $repo,
86+
'is_worktree' => '' !== $slug,
87+
'path' => $path,
88+
'branch' => '' !== $slug ? str_replace('-', '/', $slug) : 'main',
7489
'remote' => 'git@github.a8c.com:Automattic/wpcom-codebox.git',
7590
'commit' => 'abc123 listed primary',
7691
'dirty' => 0,
@@ -161,10 +176,31 @@ function is_wp_error( $value ): bool
161176
)
162177
);
163178

164-
$assert('remote show attempted first', 'wpcom-codebox' === ( \DataMachineCode\Workspace\RemoteWorkspaceBackend::$show_input['handle'] ?? '' ));
165-
$assert('local show fallback attempted', 'wpcom-codebox' === ( \DataMachineCode\Workspace\Workspace::$show_input['handle'] ?? '' ));
179+
$assert('remote show is not consulted when local primary exists', array() === \DataMachineCode\Workspace\RemoteWorkspaceBackend::$show_input);
180+
$assert('local show attempted first', 'wpcom-codebox' === ( \DataMachineCode\Workspace\Workspace::$show_input['handle'] ?? '' ));
166181
$assert('show returns local listed primary', is_array($show) && '/Users/chubes/Developer/wpcom-codebox' === ( $show['path'] ?? '' ));
167182

183+
\DataMachineCode\Workspace\RemoteWorkspaceBackend::$show_returns_remote_success = true;
184+
\DataMachineCode\Workspace\RemoteWorkspaceBackend::$show_input = array();
185+
\DataMachineCode\Workspace\Workspace::$show_input = array();
186+
$duplicate_show = \DataMachineCode\Abilities\WorkspaceAbilities::showRepo(
187+
array(
188+
'name' => 'wpcom-codebox',
189+
)
190+
);
191+
192+
$assert('duplicate remote show is not consulted when local primary exists', array() === \DataMachineCode\Workspace\RemoteWorkspaceBackend::$show_input);
193+
$assert('duplicate local primary wins show path', is_array($duplicate_show) && '/Users/chubes/Developer/wpcom-codebox' === ( $duplicate_show['path'] ?? '' ));
194+
195+
$duplicate_path = \DataMachineCode\Abilities\WorkspaceAbilities::getPath(
196+
array(
197+
'name' => 'wpcom-codebox@fix-listed-primary-resolution',
198+
)
199+
);
200+
201+
$assert('workspace path returns local worktree when duplicate remote exists', is_array($duplicate_path) && '/Users/chubes/Developer/wpcom-codebox@fix-listed-primary-resolution' === ( $duplicate_path['path'] ?? '' ));
202+
$assert('workspace path marks local worktree handle as existing', is_array($duplicate_path) && true === ( $duplicate_path['exists'] ?? null ));
203+
168204
\DataMachineCode\Workspace\RemoteWorkspaceBackend::$worktree_returns_remote_success = true;
169205
\DataMachineCode\Workspace\RemoteWorkspaceBackend::$worktree_input = array();
170206
$worktree = \DataMachineCode\Abilities\WorkspaceAbilities::worktreeAdd(

0 commit comments

Comments
 (0)