Skip to content

Commit b54d971

Browse files
fix: fall back to local workspace git status
1 parent 352a978 commit b54d971

2 files changed

Lines changed: 77 additions & 3 deletions

File tree

inc/Abilities/WorkspaceAbilities.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3062,12 +3062,18 @@ private static function gitRebaseOutputSchema(): array {
30623062
* @return array
30633063
*/
30643064
public static function gitStatus( array $input ): array|\WP_Error {
3065+
$workspace = new Workspace();
3066+
if ( RemoteWorkspaceBackend::should_handle() && null !== self::showLocalWorkspaceHandleIfPresent($workspace, (string) ( $input['name'] ?? '' )) ) {
3067+
return $workspace->git_status($input['name'] ?? '');
3068+
}
3069+
30653070
if ( RemoteWorkspaceBackend::should_handle() ) {
30663071
$result = ( new RemoteWorkspaceBackend() )->git_status($input['name'] ?? '');
3067-
return self::decorate_remote_workspace_result('git_status', $result);
3072+
if ( ! self::shouldFallbackToLocalWorkspace($result) ) {
3073+
return self::decorate_remote_workspace_result('git_status', $result);
3074+
}
30683075
}
30693076

3070-
$workspace = new Workspace();
30713077
return $workspace->git_status($input['name'] ?? '');
30723078
}
30733079

tests/smoke-workspace-local-fallback.php

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ class RemoteWorkspaceBackend
2424
public static array $read_input = array();
2525
public static array $list_input = array();
2626
public static array $grep_input = array();
27+
public static array $git_status_input = array();
2728
public static array $worktree_input = array();
2829
public static bool $show_returns_remote_success = false;
2930
public static bool $read_returns_remote_success = false;
3031
public static bool $list_returns_remote_success = false;
3132
public static bool $grep_returns_remote_success = false;
33+
public static bool $git_status_returns_remote_success = false;
3234
public static bool $worktree_returns_remote_success = false;
3335

3436
public static function should_handle(): bool
@@ -130,16 +132,42 @@ public function grep( string $handle, string $pattern, ?string $path = null, ?st
130132
'Remote workspace repository "wpcom-codebox" is not registered. Call workspace_clone first.'
131133
);
132134
}
135+
136+
public function git_status( string $handle ): array|\WP_Error
137+
{
138+
self::$git_status_input = compact('handle');
139+
if ( self::$git_status_returns_remote_success ) {
140+
return array(
141+
'success' => true,
142+
'backend' => 'github_api',
143+
'name' => $handle,
144+
'path' => 'github://Automattic/' . str_replace('@', '#', $handle),
145+
'dirty' => 0,
146+
'files' => array(),
147+
);
148+
}
149+
150+
return new \WP_Error(
151+
'remote_workspace_repo_not_found',
152+
'Remote workspace repository "wpcom-codebox" is not registered. Call workspace_clone first.'
153+
);
154+
}
133155
}
134156

135157
class Workspace
136158
{
137159
public static array $show_input = array();
160+
public static array $git_status_input = array();
138161
public static array $worktree_input = array();
162+
public static bool $show_returns_local_success = true;
139163

140-
public function show_repo( string $handle ): array
164+
public function show_repo( string $handle ): array|\WP_Error
141165
{
142166
self::$show_input = compact('handle');
167+
if ( ! self::$show_returns_local_success ) {
168+
return new \WP_Error('workspace_repo_not_found', 'Workspace repository not found.');
169+
}
170+
143171
$parsed = explode('@', $handle, 2);
144172
$repo = $parsed[0];
145173
$slug = $parsed[1] ?? '';
@@ -175,6 +203,18 @@ public function worktree_add(
175203
'path' => '/Users/chubes/Developer/' . $repo . '@' . str_replace('/', '-', $branch),
176204
);
177205
}
206+
207+
public function git_status( string $handle ): array
208+
{
209+
self::$git_status_input = compact('handle');
210+
return array(
211+
'success' => true,
212+
'name' => $handle,
213+
'path' => '/Users/chubes/Developer/' . $handle,
214+
'dirty' => 1,
215+
'files' => array( ' M README.md' ),
216+
);
217+
}
178218
}
179219

180220
class WorkspaceReader
@@ -376,6 +416,34 @@ function is_wp_error( $value ): bool
376416
$assert('grep preserves options locally', '*.php' === ( \DataMachineCode\Workspace\WorkspaceReader::$grep_input['include_pattern'] ?? '' ) && 7 === ( \DataMachineCode\Workspace\WorkspaceReader::$grep_input['max_results'] ?? null ) && 2 === ( \DataMachineCode\Workspace\WorkspaceReader::$grep_input['context_lines'] ?? null ));
377417
$assert('grep returns local matches', is_array($grep) && 1 === ( $grep['count'] ?? null ));
378418

419+
\DataMachineCode\Workspace\RemoteWorkspaceBackend::$git_status_returns_remote_success = true;
420+
\DataMachineCode\Workspace\RemoteWorkspaceBackend::$git_status_input = array();
421+
\DataMachineCode\Workspace\Workspace::$git_status_input = array();
422+
$git_status = \DataMachineCode\Abilities\WorkspaceAbilities::gitStatus(
423+
array(
424+
'name' => 'wpcom-codebox',
425+
)
426+
);
427+
428+
$assert('remote git status is not consulted when local primary exists', array() === \DataMachineCode\Workspace\RemoteWorkspaceBackend::$git_status_input);
429+
$assert('local git status attempted for local primary', 'wpcom-codebox' === ( \DataMachineCode\Workspace\Workspace::$git_status_input['handle'] ?? '' ));
430+
$assert('git status returns local dirty state', is_array($git_status) && 1 === ( $git_status['dirty'] ?? null ));
431+
432+
\DataMachineCode\Workspace\Workspace::$show_returns_local_success = false;
433+
\DataMachineCode\Workspace\RemoteWorkspaceBackend::$git_status_returns_remote_success = false;
434+
\DataMachineCode\Workspace\RemoteWorkspaceBackend::$git_status_input = array();
435+
\DataMachineCode\Workspace\Workspace::$git_status_input = array();
436+
$miss_status = \DataMachineCode\Abilities\WorkspaceAbilities::gitStatus(
437+
array(
438+
'name' => 'wpcom-codebox',
439+
)
440+
);
441+
442+
$assert('remote git status miss is consulted before fallback', 'wpcom-codebox' === ( \DataMachineCode\Workspace\RemoteWorkspaceBackend::$git_status_input['handle'] ?? '' ));
443+
$assert('remote git status miss falls back locally', 'wpcom-codebox' === ( \DataMachineCode\Workspace\Workspace::$git_status_input['handle'] ?? '' ));
444+
$assert('git status fallback returns local result', is_array($miss_status) && '/Users/chubes/Developer/wpcom-codebox' === ( $miss_status['path'] ?? '' ));
445+
\DataMachineCode\Workspace\Workspace::$show_returns_local_success = true;
446+
379447
\DataMachineCode\Workspace\RemoteWorkspaceBackend::$worktree_returns_remote_success = true;
380448
\DataMachineCode\Workspace\RemoteWorkspaceBackend::$worktree_input = array();
381449
$worktree = \DataMachineCode\Abilities\WorkspaceAbilities::worktreeAdd(

0 commit comments

Comments
 (0)