Skip to content

Commit a99b7a3

Browse files
Refactor worktree metadata inventory lookup (#774)
* Refactor worktree metadata inventory lookup * fix: prepare inventory metadata lookup --------- Co-authored-by: homeboy-ci[bot] <266378653+homeboy-ci[bot]@users.noreply.github.com>
1 parent 42b92bc commit a99b7a3

3 files changed

Lines changed: 55 additions & 10 deletions

File tree

inc/Storage/WorktreeInventoryRepository.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,37 @@ public function delete( string $handle ): bool {
129129
return false !== $result;
130130
}
131131

132+
/**
133+
* Fetch one inventory row by handle.
134+
*
135+
* @return array<string,mixed>|null
136+
*/
137+
public function get( string $handle ): ?array {
138+
global $wpdb;
139+
140+
$handle = trim($handle);
141+
if ( '' === $handle || ! isset($wpdb) ) {
142+
return null;
143+
}
144+
145+
if ( method_exists($wpdb, 'get_row') && method_exists($wpdb, 'prepare') ) {
146+
$table = self::table_name();
147+
$row = $wpdb->get_row(
148+
$wpdb->prepare('SELECT * FROM %i WHERE handle = %s LIMIT 1', $table, $handle),
149+
ARRAY_A
150+
);
151+
return is_array($row) ? $this->decode_row($row) : null;
152+
}
153+
154+
foreach ( $this->list() as $row ) {
155+
if ( (string) ( $row['handle'] ?? '' ) === $handle ) {
156+
return $row;
157+
}
158+
}
159+
160+
return null;
161+
}
162+
132163
/**
133164
* Mark a known row as missing on disk instead of dropping it silently.
134165
*/

inc/Workspace/WorktreeContextInjector.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@
7373

7474
defined('ABSPATH') || exit;
7575

76+
require_once __DIR__ . '/WorkspaceHandle.php';
77+
7678
class WorktreeContextInjector {
7779

7880

@@ -1862,18 +1864,18 @@ private static function upsert_inventory_metadata( string $handle, array $metada
18621864
return;
18631865
}
18641866

1865-
$handle_parts = explode('@', $handle, 2);
1866-
$task = is_array($metadata['origin_task'] ?? null) ? (array) $metadata['origin_task'] : array();
1867-
$session = is_array($metadata['origin_session'] ?? null) ? self::summarize_session($metadata) : array();
1867+
$workspace_handle = WorkspaceHandle::parse($handle);
1868+
$task = is_array($metadata['origin_task'] ?? null) ? (array) $metadata['origin_task'] : array();
1869+
$session = is_array($metadata['origin_session'] ?? null) ? self::summarize_session($metadata) : array();
18681870

18691871
$repository->upsert(
18701872
array(
18711873
'handle' => $handle,
1872-
'repo' => (string) ( $metadata['repo'] ?? $handle_parts[0] ),
1873-
'branch' => $metadata['branch'] ?? ( $handle_parts[1] ?? null ),
1874+
'repo' => (string) ( $metadata['repo'] ?? $workspace_handle->repo() ),
1875+
'branch' => $metadata['branch'] ?? $workspace_handle->branch_slug(),
18741876
'path' => (string) ( $metadata['path'] ?? '' ),
18751877
'primary_path' => $metadata['primary_path'] ?? null,
1876-
'is_primary' => ! str_contains($handle, '@'),
1878+
'is_primary' => ! $workspace_handle->is_worktree(),
18771879
'lifecycle_state' => $metadata['lifecycle_state'] ?? null,
18781880
'origin_site' => $metadata['origin_site'] ?? null,
18791881
'origin_agent' => $metadata['origin_agent'] ?? null,
@@ -1901,10 +1903,9 @@ private static function get_inventory_metadata( string $handle ): ?array {
19011903
return null;
19021904
}
19031905

1904-
foreach ( $repository->list() as $row ) {
1905-
if ( (string) ( $row['handle'] ?? '' ) === $handle && is_array($row['metadata'] ?? null) ) {
1906-
return (array) $row['metadata'];
1907-
}
1906+
$row = $repository->get($handle);
1907+
if ( is_array($row) && is_array($row['metadata'] ?? null) ) {
1908+
return (array) $row['metadata'];
19081909
}
19091910

19101911
return null;

tests/worktree-add-lifecycle.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ final class Datamachine_Code_Test_Wpdb {
8282
public string $last_error = '';
8383
public int $insert_id = 0;
8484
public int $rows_affected = 0;
85+
public int $get_row_calls = 0;
8586

8687
/** @var array<string,array<string,mixed>> */
8788
public array $rows = array();
@@ -132,6 +133,17 @@ public function get_results( string $sql, string $output = ARRAY_A ): array {
132133
return array_values($this->rows);
133134
}
134135

136+
public function get_row( string $sql, string $output = ARRAY_A ): ?array {
137+
++$this->get_row_calls;
138+
foreach ( $this->rows as $handle => $row ) {
139+
if ( str_contains($sql, (string) $handle) ) {
140+
return $row;
141+
}
142+
}
143+
144+
return null;
145+
}
146+
135147
public function prepare( string $query, mixed ...$args ): string {
136148
foreach ( $args as $arg ) {
137149
$query = preg_replace('/%s/', addslashes((string) $arg), $query, 1) ?? $query;
@@ -225,6 +237,7 @@ function create_primary_checkout( string $workspace_root ): void {
225237

226238
$show = $workspace->show_repo('homeboy@audit-primitives-20260616');
227239
assert_true(! is_wp_error($show), 'persisted worktree is not visible to show_repo');
240+
assert_true(0 < $wpdb->get_row_calls, 'persisted worktree metadata did not use direct inventory lookup');
228241

229242
$list = $workspace->worktree_list('homeboy', null, array( 'include_status' => false, 'include_disk' => false ));
230243
$handles = array_map(static fn( array $row ): string => (string) $row['handle'], $list['worktrees'] ?? array());

0 commit comments

Comments
 (0)