@firstpick/pi-package-webui 0.11.0 → 0.11.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (318) hide show
  1. package/DEVELOPMENT.md +181 -12
  2. package/README.md +41 -9
  3. package/TECHNICAL.md +91 -8
  4. package/bin/pi-webui.mjs +5432 -11428
  5. package/index.ts +10 -3
  6. package/lib/codex-cli-usage.mjs +232 -0
  7. package/lib/codex-usage-auth.mjs +39 -3
  8. package/lib/codex-usage-source.mjs +36 -0
  9. package/lib/component-update-state.mjs +2 -2
  10. package/lib/git-workflow-preference-values.mjs +122 -0
  11. package/lib/git-workflow-preferences.mjs +35 -563
  12. package/lib/package-updates.mjs +260 -0
  13. package/lib/rpc-supervisor-client.mjs +8 -4
  14. package/lib/server/app-runners.mjs +1667 -0
  15. package/lib/server/codex-fast-mode.mjs +161 -0
  16. package/lib/server/conversation-mode.mjs +218 -0
  17. package/lib/server/conversation-voice.mjs +318 -0
  18. package/lib/server/extension-ui-requests.mjs +114 -0
  19. package/lib/server/git-generation.mjs +459 -0
  20. package/lib/server/git-mutations.mjs +1827 -0
  21. package/lib/server/git-reads.mjs +608 -0
  22. package/lib/server/git-worktree-sessions.mjs +408 -0
  23. package/lib/server/helper-requests.mjs +82 -0
  24. package/lib/server/no-project-workspaces.mjs +103 -0
  25. package/lib/server/rpc-transports.mjs +394 -0
  26. package/lib/server/settings-store.mjs +544 -0
  27. package/lib/server/sse-delivery.mjs +138 -0
  28. package/lib/server/workspace-files.mjs +563 -0
  29. package/lib/update/supervisor.mjs +4 -0
  30. package/lib/webui-workspaces.mjs +2 -0
  31. package/package.json +5 -5
  32. package/public/activity-agents-controller.mjs +269 -0
  33. package/public/activity-events-controller.mjs +236 -0
  34. package/public/activity-intercom-controller.mjs +627 -0
  35. package/public/activity-queue-controller.mjs +534 -0
  36. package/public/activity-todo-view.mjs +89 -0
  37. package/public/activity-usage-controller.mjs +589 -0
  38. package/public/app.js +3761 -3878
  39. package/public/conversation-overview.css +84 -0
  40. package/public/conversation-overview.mjs +193 -0
  41. package/public/draft-conversations.mjs +135 -0
  42. package/public/focused-composer-controls.css +111 -0
  43. package/public/focused-composer-controls.mjs +143 -0
  44. package/public/focused-composer-disclosure.mjs +223 -0
  45. package/public/focused-shell-adapter.css +264 -0
  46. package/public/focused-shell-adapter.mjs +257 -0
  47. package/public/index.html +137 -80
  48. package/public/model-catalog-order.mjs +63 -0
  49. package/public/platform-event-stream.mjs +64 -0
  50. package/public/project-artifact-controller.mjs +218 -0
  51. package/public/project-file-viewer-controller.mjs +383 -0
  52. package/public/project-git-tools-controller.mjs +181 -0
  53. package/public/project-runners-controller.mjs +220 -0
  54. package/public/service-worker.js +52 -1
  55. package/public/session-attention-scheduler.mjs +182 -0
  56. package/public/session-composer-store.mjs +56 -0
  57. package/public/session-context.mjs +51 -0
  58. package/public/session-extension-requests.mjs +63 -0
  59. package/public/session-shelf-state.mjs +151 -0
  60. package/public/session-shelves.css +171 -0
  61. package/public/session-shelves.mjs +95 -0
  62. package/public/settings-navigation-controller.mjs +249 -0
  63. package/public/settings-navigation.css +61 -0
  64. package/public/settings-resource-controller.mjs +232 -0
  65. package/public/settings-workspace.css +19 -0
  66. package/public/settings-workspace.mjs +175 -0
  67. package/public/shell-actions.mjs +87 -0
  68. package/public/shell-attention-inbox.css +72 -0
  69. package/public/shell-attention-inbox.mjs +198 -0
  70. package/public/shell-context-actions.css +61 -0
  71. package/public/shell-context-actions.mjs +168 -0
  72. package/public/shell-inspector-resize.mjs +124 -0
  73. package/public/shell-navigation-resize.mjs +107 -0
  74. package/public/shell-navigation.mjs +127 -0
  75. package/public/shell-preferences.mjs +108 -0
  76. package/public/shell-surfaces.mjs +99 -0
  77. package/public/shell-tools-launcher.css +97 -0
  78. package/public/shell-tools-launcher.mjs +222 -0
  79. package/public/shell-view-host.mjs +115 -0
  80. package/public/shell-workspace.css +266 -0
  81. package/public/shell-workspace.mjs +402 -0
  82. package/public/sidebar-update-menu.css +190 -0
  83. package/public/sidebar-update-menu.mjs +512 -0
  84. package/public/styles.css +320 -169
  85. package/public/subagent-launch-slot-state.mjs +2 -2
  86. package/public/theme-contract.mjs +1 -1
  87. package/public/themed-selects.css +156 -0
  88. package/public/workspace-choices.mjs +104 -0
  89. package/tests/activity-agents-controller.test.mjs +652 -0
  90. package/tests/activity-events-controller.test.mjs +511 -0
  91. package/tests/app-runner-terminal-static.test.mjs +3 -3
  92. package/tests/append-system-http.test.mjs +7 -6
  93. package/tests/append-system-selector-static.test.mjs +6 -6
  94. package/tests/aur-review-widget-static.test.mjs +11 -3
  95. package/tests/boot-failure-diagnostics.test.mjs +8 -3
  96. package/tests/browser/activity-intercom-controller.spec.mjs +457 -0
  97. package/tests/browser/activity-navigation-integration.spec.mjs +45 -0
  98. package/tests/browser/activity-owner-integration.spec.mjs +201 -0
  99. package/tests/browser/activity-queue-controller.spec.mjs +405 -0
  100. package/tests/browser/activity-section-picker.spec.mjs +294 -0
  101. package/tests/browser/activity-todo-view.spec.mjs +60 -0
  102. package/tests/browser/activity-usage-controller.spec.mjs +416 -0
  103. package/tests/browser/compact-layout-integration.spec.mjs +58 -0
  104. package/tests/browser/composer-action-grid-reorder.spec.mjs +16 -0
  105. package/tests/browser/composer-disclosure.spec.mjs +389 -0
  106. package/tests/browser/composer-toolbar-polish.spec.mjs +343 -0
  107. package/tests/browser/control-deck-component-updates.spec.mjs +5 -14
  108. package/tests/browser/control-deck-side-panels.spec.mjs +27 -12
  109. package/tests/browser/control-visibility.spec.mjs +5 -0
  110. package/tests/browser/controls-layout.spec.mjs +21 -2
  111. package/tests/browser/conversation-overview.spec.mjs +251 -0
  112. package/tests/browser/draft-conversations.spec.mjs +276 -0
  113. package/tests/browser/empty-startup-fixture.mjs +135 -0
  114. package/tests/browser/empty-startup.spec.mjs +282 -0
  115. package/tests/browser/events-tree-context-menu.spec.mjs +1 -0
  116. package/tests/browser/focused-composer-more.spec.mjs +127 -0
  117. package/tests/browser/focused-model-shortcuts.spec.mjs +120 -0
  118. package/tests/browser/focused-navigation-fixture.mjs +32 -0
  119. package/tests/browser/focused-project-activity.spec.mjs +111 -0
  120. package/tests/browser/focused-shell-integration.spec.mjs +208 -0
  121. package/tests/browser/folder-picker-layout-fixture.mjs +13 -0
  122. package/tests/browser/folder-picker-layout.spec.mjs +302 -0
  123. package/tests/browser/footer-model-advanced-layout.spec.mjs +27 -14
  124. package/tests/browser/git-setup-cancel.spec.mjs +251 -0
  125. package/tests/browser/guided-git-fallback.spec.mjs +40 -21
  126. package/tests/browser/inspector-header.spec.mjs +221 -0
  127. package/tests/browser/inspector-overview-integration.spec.mjs +104 -0
  128. package/tests/browser/inspector-resize.spec.mjs +227 -0
  129. package/tests/browser/inspector-topic-overflow.spec.mjs +56 -0
  130. package/tests/browser/interaction-action-discovery.spec.mjs +332 -0
  131. package/tests/browser/interaction-attention-inbox.spec.mjs +364 -0
  132. package/tests/browser/interaction-attention-integration.spec.mjs +53 -0
  133. package/tests/browser/interaction-composer-controls.spec.mjs +267 -0
  134. package/tests/browser/interaction-composer-integration.spec.mjs +175 -0
  135. package/tests/browser/interaction-continuity.spec.mjs +50 -26
  136. package/tests/browser/interaction-model-picker.spec.mjs +86 -0
  137. package/tests/browser/last-terminal-close.spec.mjs +280 -0
  138. package/tests/browser/mobile-foundation.spec.mjs +172 -20
  139. package/tests/browser/model-catalog-groups.spec.mjs +104 -0
  140. package/tests/browser/more-row-menus.spec.mjs +258 -0
  141. package/tests/browser/new-tab-hit-target.spec.mjs +228 -0
  142. package/tests/browser/no-project-conversation.spec.mjs +113 -0
  143. package/tests/browser/optimistic-prompt.spec.mjs +3 -0
  144. package/tests/browser/overhaul-w7-accessibility.spec.mjs +227 -0
  145. package/tests/browser/overhaul-w7-capabilities.spec.mjs +298 -0
  146. package/tests/browser/overhaul-w7-companions.spec.mjs +290 -0
  147. package/tests/browser/overhaul-w7-focus-repairs.spec.mjs +210 -0
  148. package/tests/browser/overhaul-w7-layout-repairs.spec.mjs +183 -0
  149. package/tests/browser/overhaul-w7-parent-repairs.spec.mjs +97 -0
  150. package/tests/browser/overhaul-w7-pwa.spec.mjs +247 -0
  151. package/tests/browser/overhaul-w7-responsive.spec.mjs +450 -0
  152. package/tests/browser/overhaul-w7-update.spec.mjs +215 -0
  153. package/tests/browser/overhaul-w7-zoom.spec.mjs +243 -0
  154. package/tests/browser/overview-agent-ownership.spec.mjs +45 -0
  155. package/tests/browser/persistent-ui-layout.spec.mjs +3 -0
  156. package/tests/browser/project-artifact-controller.spec.mjs +324 -0
  157. package/tests/browser/project-artifact-integration.spec.mjs +103 -0
  158. package/tests/browser/project-file-buffer-integration.spec.mjs +230 -0
  159. package/tests/browser/project-git-tools-integration.spec.mjs +369 -0
  160. package/tests/browser/session-fixture.mjs +118 -0
  161. package/tests/browser/session-ownership.spec.mjs +175 -0
  162. package/tests/browser/session-shelves-integration.spec.mjs +219 -0
  163. package/tests/browser/session-shelves.spec.mjs +340 -0
  164. package/tests/browser/session-sidebar-resize.spec.mjs +138 -0
  165. package/tests/browser/session-summary.spec.mjs +17 -12
  166. package/tests/browser/settings-complete-home.spec.mjs +463 -0
  167. package/tests/browser/settings-entrypoint.spec.mjs +104 -0
  168. package/tests/browser/settings-navigation-controller.spec.mjs +383 -0
  169. package/tests/browser/settings-navigation-integration.spec.mjs +348 -0
  170. package/tests/browser/settings-resource-lifetime.spec.mjs +349 -0
  171. package/tests/browser/settings-workspace-integration.spec.mjs +132 -0
  172. package/tests/browser/shell-ownership.spec.mjs +398 -0
  173. package/tests/browser/shell-rollout-fixture.mjs +259 -0
  174. package/tests/browser/shell-rollout.spec.mjs +357 -0
  175. package/tests/browser/shell-tools-launcher.spec.mjs +260 -0
  176. package/tests/browser/shell-workspace.spec.mjs +433 -0
  177. package/tests/browser/side-panel-section-reorder.spec.mjs +3 -1
  178. package/tests/browser/sidebar-floating-tooltips.spec.mjs +37 -0
  179. package/tests/browser/sidebar-update-integration.spec.mjs +277 -0
  180. package/tests/browser/sidebar-update-menu.spec.mjs +402 -0
  181. package/tests/browser/sidebar-update-progress.spec.mjs +472 -0
  182. package/tests/browser/stream-output-isolation.spec.mjs +6 -3
  183. package/tests/browser/themed-selects.spec.mjs +297 -0
  184. package/tests/browser/thread-start-flow.spec.mjs +481 -0
  185. package/tests/browser/ui-polish-activity-controls.spec.mjs +177 -0
  186. package/tests/browser/ui-polish-features.spec.mjs +85 -0
  187. package/tests/browser/ui-polish-file-header.spec.mjs +43 -0
  188. package/tests/browser/ui-polish-local-git.spec.mjs +183 -0
  189. package/tests/browser/ui-polish-mobile-recovery.spec.mjs +36 -0
  190. package/tests/browser/ui-polish-navigation.spec.mjs +127 -0
  191. package/tests/browser/ui-polish-session-actions.spec.mjs +282 -0
  192. package/tests/browser/user-noticeable-fixes.spec.mjs +10 -3
  193. package/tests/browser/windows-drive-parent.spec.mjs +4 -2
  194. package/tests/browser/workspace-draft-selection.spec.mjs +266 -0
  195. package/tests/codex-cli-usage.test.mjs +189 -0
  196. package/tests/codex-fast-mode-static.test.mjs +16 -11
  197. package/tests/codex-speed-mode-browser-state.test.mjs +6 -3
  198. package/tests/codex-usage-auth.test.mjs +168 -9
  199. package/tests/codex-usage-http.test.mjs +124 -0
  200. package/tests/codex-usage-source.test.mjs +146 -0
  201. package/tests/component-update-api-static.test.mjs +2 -2
  202. package/tests/composer-action-grid-reorder-static.test.mjs +3 -3
  203. package/tests/control-deck-component-updates-static.test.mjs +9 -4
  204. package/tests/control-visibility-static.test.mjs +3 -3
  205. package/tests/controls-layout-static.test.mjs +5 -5
  206. package/tests/conversation-overview-adapter.test.mjs +100 -0
  207. package/tests/document-artifact-static.test.mjs +2 -1
  208. package/tests/draft-conversations.test.mjs +205 -0
  209. package/tests/empty-startup-launcher.test.mjs +197 -0
  210. package/tests/events-tree-context-menu-static.test.mjs +22 -18
  211. package/tests/fake-pi-session-ownership.test.mjs +302 -0
  212. package/tests/fast-mode-client-static.test.mjs +1 -1
  213. package/tests/feature-decision-output-popup-static.test.mjs +2 -2
  214. package/tests/feature-system-prompt-optional-feature-static.test.mjs +1 -1
  215. package/tests/file-viewer-image-static.test.mjs +15 -11
  216. package/tests/file-viewer-search-static.test.mjs +5 -5
  217. package/tests/fixtures/fake-codex-app-server.mjs +61 -0
  218. package/tests/fixtures/fake-pi.mjs +41 -9
  219. package/tests/footer-provider-usage-visual-static.test.mjs +7 -6
  220. package/tests/foreground-transcript-catch-up-static.test.mjs +7 -4
  221. package/tests/git-footer-context-menu-static.test.mjs +1 -1
  222. package/tests/git-panel-file-preview-static.test.mjs +41 -48
  223. package/tests/git-panel-live-updates-static.test.mjs +2 -1
  224. package/tests/git-workflow-dispatch.test.mjs +66 -0
  225. package/tests/git-workflow-preferences.test.mjs +9 -1
  226. package/tests/guided-git-activation-static.test.mjs +14 -11
  227. package/tests/guided-git-fallback-ui-static.test.mjs +8 -5
  228. package/tests/guided-git-message-reliability-static.test.mjs +10 -6
  229. package/tests/guided-git-publication-backend-static.test.mjs +16 -11
  230. package/tests/http-endpoints-harness.test.mjs +29 -9
  231. package/tests/interaction-state-stability-static.test.mjs +4 -3
  232. package/tests/intercom-conversation-viewer-static.test.mjs +29 -17
  233. package/tests/intercom-tag-layout-static.test.mjs +13 -12
  234. package/tests/intercom-tag-overflow-layout.test.mjs +11 -10
  235. package/tests/main-output-loading-static.test.mjs +4 -4
  236. package/tests/middle-button-drag-scroll-static.test.mjs +3 -3
  237. package/tests/mobile-compact-layout.test.mjs +2 -2
  238. package/tests/mobile-continuity-tablet-static.test.mjs +3 -3
  239. package/tests/mobile-foundation-static.test.mjs +10 -7
  240. package/tests/mobile-phone-experience-static.test.mjs +3 -2
  241. package/tests/mobile-static.test.mjs +173 -135
  242. package/tests/model-catalog-order.test.mjs +103 -0
  243. package/tests/native-parity.test.mjs +6 -4
  244. package/tests/native-session-choice-owner.test.mjs +102 -0
  245. package/tests/native-update-lifecycle.test.mjs +10 -5
  246. package/tests/no-project-http-harness.test.mjs +328 -0
  247. package/tests/no-project-navigation.test.mjs +54 -0
  248. package/tests/no-project-workspaces.test.mjs +77 -0
  249. package/tests/open-issue-wizard-static.test.mjs +1 -1
  250. package/tests/optional-feature-migration-frontend.test.mjs +4 -4
  251. package/tests/optional-feature-prompt-impact-static.test.mjs +3 -3
  252. package/tests/package-updates.test.mjs +630 -0
  253. package/tests/persistent-ui-layout-static.test.mjs +3 -3
  254. package/tests/platform-event-stream.test.mjs +253 -0
  255. package/tests/project-file-viewer-controller.test.mjs +652 -0
  256. package/tests/project-git-tools-adapter.test.mjs +103 -0
  257. package/tests/project-git-tools-controller.test.mjs +577 -0
  258. package/tests/project-runners-controller.test.mjs +453 -0
  259. package/tests/prompt-input-vertical-scroll-static.test.mjs +3 -3
  260. package/tests/prompt-routing-input-clear-static.test.mjs +2 -2
  261. package/tests/questionnaire-dialog.test.mjs +6 -6
  262. package/tests/queue-edit-reorder-static.test.mjs +48 -51
  263. package/tests/rpc-supervisor-host.test.mjs +4 -1
  264. package/tests/safety-guard-auto-review-static.test.mjs +1 -1
  265. package/tests/sampling-native-controls-user-flow.test.mjs +7 -1
  266. package/tests/server-app-runners.test.mjs +536 -0
  267. package/tests/server-codex-fast-mode.test.mjs +346 -0
  268. package/tests/server-conversation-mode.test.mjs +386 -0
  269. package/tests/server-conversation-voice.test.mjs +426 -0
  270. package/tests/server-extension-ui-requests.test.mjs +357 -0
  271. package/tests/server-git-generation.test.mjs +558 -0
  272. package/tests/server-git-mutations.test.mjs +729 -0
  273. package/tests/server-git-reads.test.mjs +381 -0
  274. package/tests/server-git-worktree-sessions.test.mjs +502 -0
  275. package/tests/server-helper-requests.test.mjs +364 -0
  276. package/tests/server-rpc-transports.test.mjs +553 -0
  277. package/tests/server-settings-store.test.mjs +132 -0
  278. package/tests/server-shell-rollout.test.mjs +478 -0
  279. package/tests/server-sse-delivery.test.mjs +410 -0
  280. package/tests/server-workspace-files.test.mjs +546 -0
  281. package/tests/session-attention-scheduler.test.mjs +514 -0
  282. package/tests/session-composer-store.test.mjs +153 -0
  283. package/tests/session-context.test.mjs +134 -0
  284. package/tests/session-extension-requests.test.mjs +193 -0
  285. package/tests/session-ownership-adapter.test.mjs +130 -0
  286. package/tests/session-sampling-ui-static.test.mjs +10 -4
  287. package/tests/session-send-lifetime.test.mjs +140 -0
  288. package/tests/session-shelf-state.test.mjs +348 -0
  289. package/tests/session-shelves-adapter.test.mjs +79 -0
  290. package/tests/session-summary-lifetime.test.mjs +54 -0
  291. package/tests/settings-resource-controller.test.mjs +548 -0
  292. package/tests/shell-action-adapter.test.mjs +169 -0
  293. package/tests/shell-actions.test.mjs +182 -0
  294. package/tests/shell-navigation.test.mjs +348 -0
  295. package/tests/shell-preferences.test.mjs +367 -0
  296. package/tests/side-panel-section-height-removal-static.test.mjs +3 -3
  297. package/tests/stream-output-isolation-static.test.mjs +1 -1
  298. package/tests/subagent-gate-visibility.test.mjs +1 -1
  299. package/tests/subagent-launch-slot-state.test.mjs +2 -2
  300. package/tests/subagent-launch-slots-static.test.mjs +1 -1
  301. package/tests/subagent-observability-static.test.mjs +2 -2
  302. package/tests/subagent-reliability-integration.test.mjs +13 -30
  303. package/tests/syntax-highlighting-static.test.mjs +3 -3
  304. package/tests/tab-close-continuity-static.test.mjs +1 -1
  305. package/tests/tab-metadata-ordering.test.mjs +99 -0
  306. package/tests/terminal-cwd-grouping-static.test.mjs +2 -2
  307. package/tests/terminal-tab-prompt-tooltip-static.test.mjs +2 -0
  308. package/tests/terminal-tab-workspace-static.test.mjs +5 -4
  309. package/tests/terminal-tabs-resize-static.test.mjs +1 -1
  310. package/tests/theme-contract.test.mjs +10 -3
  311. package/tests/theme-customizer-static.test.mjs +1 -1
  312. package/tests/thread-display-title.test.mjs +44 -0
  313. package/tests/update-api-harness.test.mjs +15 -14
  314. package/tests/update-tab-restore-harness.test.mjs +3 -0
  315. package/tests/webui-settings-locking.test.mjs +2 -2
  316. package/tests/windows-drive-cwd-picker-static.test.mjs +7 -9
  317. package/tests/workspace-choices.test.mjs +114 -0
  318. package/tests/workspace-save-load-static.test.mjs +7 -4
@@ -0,0 +1,608 @@
1
+ import { readFile, stat } from "node:fs/promises";
2
+ import path from "node:path";
3
+ import { pathInside } from "../git-worktrees.mjs";
4
+
5
+ const GIT_CHANGES_COMMAND_TIMEOUT_MS = 5000;
6
+ const GIT_CHANGES_DIFF_MAX_OUTPUT = 500_000;
7
+ const GIT_FILE_DIFF_CATEGORIES = new Set(["staged", "unstaged", "conflicted", "untracked"]);
8
+ const GIT_FILE_DIFF_LABELS = Object.freeze({
9
+ staged: "Staged",
10
+ unstaged: "Unstaged",
11
+ conflicted: "Conflicted",
12
+ untracked: "Untracked",
13
+ });
14
+ const GIT_STATUS_KIND_RANK = Object.freeze({ changed: 0, untracked: 1, modified: 2, staged: 3, conflicted: 4 });
15
+
16
+ export function createGitReadService({
17
+ runCommand,
18
+ makeHttpError,
19
+ makeUserFacingError,
20
+ formatBytes,
21
+ sanitizeError,
22
+ formatGitCommand,
23
+ }) {
24
+ async function getGitRoot(cwd) {
25
+ const result = await runCommand("git", ["rev-parse", "--show-toplevel"], { cwd, timeoutMs: 2000 });
26
+ if (result.exitCode !== 0) {
27
+ throw makeUserFacingError((result.stderr || result.stdout || "Not inside a git repository").trim());
28
+ }
29
+ return path.resolve(result.stdout.trim());
30
+ }
31
+
32
+ async function runGitReadCommandDetailed(root, args, { timeoutMs = GIT_CHANGES_COMMAND_TIMEOUT_MS, maxOutputLength = GIT_CHANGES_DIFF_MAX_OUTPUT } = {}) {
33
+ const result = await runCommand("git", args, {
34
+ cwd: root,
35
+ timeoutMs,
36
+ maxOutputLength,
37
+ // Prevent read-only status/diff commands from refreshing the index and
38
+ // feeding their own metadata writes back into the live Git watcher.
39
+ env: { GIT_OPTIONAL_LOCKS: "0" },
40
+ });
41
+ if (result.exitCode === 0 && !result.timedOut && !result.error) {
42
+ return { output: result.stdout, truncated: result.stdoutTruncated === true, capBytes: maxOutputLength };
43
+ }
44
+ const command = formatGitCommand(args);
45
+ const message = result.timedOut
46
+ ? `${command} timed out`
47
+ : (result.stderr || result.stdout || result.error || `${command} failed with exit code ${result.exitCode ?? "unknown"}`);
48
+ throw makeUserFacingError(String(message).trim());
49
+ }
50
+
51
+ async function runGitReadCommand(root, args, options = {}) {
52
+ return (await runGitReadCommandDetailed(root, args, options)).output;
53
+ }
54
+
55
+ function gitBranchFromPorcelainStatus(statusText) {
56
+ for (const line of String(statusText || "").split(/\r?\n/)) {
57
+ if (!line.startsWith("# branch.head ")) continue;
58
+ const branch = line.slice("# branch.head ".length).trim();
59
+ return branch && branch !== "(detached)" ? branch : "detached";
60
+ }
61
+ return "detached";
62
+ }
63
+
64
+ function addGitPorcelainTrackedSummary(summary, xy) {
65
+ const x = xy?.[0] || ".";
66
+ const y = xy?.[1] || ".";
67
+ if (x !== ".") summary.staged += 1;
68
+ if (y !== ".") summary.unstaged += 1;
69
+ }
70
+
71
+ function summarizeGitPorcelainStatus(statusText) {
72
+ const summary = { staged: 0, unstaged: 0, untracked: 0, conflicted: 0, ahead: 0, behind: 0 };
73
+ for (const line of String(statusText || "").split(/\r?\n/)) {
74
+ if (!line) continue;
75
+ if (line.startsWith("# branch.ab ")) {
76
+ const match = line.match(/\+(\d+)\s+-(\d+)/);
77
+ if (match) {
78
+ summary.ahead = Number.parseInt(match[1] || "0", 10) || 0;
79
+ summary.behind = Number.parseInt(match[2] || "0", 10) || 0;
80
+ }
81
+ continue;
82
+ }
83
+ if (line.startsWith("1 ") || line.startsWith("2 ")) {
84
+ addGitPorcelainTrackedSummary(summary, line.split(" ")[1] || "..");
85
+ continue;
86
+ }
87
+ if (line.startsWith("u ")) {
88
+ summary.conflicted += 1;
89
+ continue;
90
+ }
91
+ if (line.startsWith("? ")) summary.untracked += 1;
92
+ }
93
+ return summary;
94
+ }
95
+
96
+ function normalizeGitStatusPath(value = "") {
97
+ return String(value || "").replace(/\\/g, "/").replace(/^\.\/+/, "").replace(/\/+$/g, "");
98
+ }
99
+
100
+ function gitStatusKindFromPorcelainEntry(entry = {}) {
101
+ const x = entry.x || " ";
102
+ const y = entry.y || " ";
103
+ if (x === "?" && y === "?") return "untracked";
104
+ if (x === "U" || y === "U" || (x === "A" && y === "A") || (x === "D" && y === "D")) return "conflicted";
105
+ if (x !== " " && x !== "?" && x !== ".") return "staged";
106
+ if (y !== " " && y !== "?" && y !== ".") return "modified";
107
+ return "changed";
108
+ }
109
+
110
+ function gitStatusLabelFromPorcelainEntry(entry = {}) {
111
+ const x = entry.x || " ";
112
+ const y = entry.y || " ";
113
+ if (x === "?" && y === "?") return "??";
114
+ return `${x}${y}`.replace(/\s/g, "").trim() || "•";
115
+ }
116
+
117
+ function mergeGitStatusIndexEntry(index, repoPath, patch = {}) {
118
+ const key = normalizeGitStatusPath(repoPath);
119
+ if (!key) return;
120
+ const existing = index.get(key) || { changed: true, kind: "changed", status: "", direct: false, changedDescendants: 0 };
121
+ const existingRank = GIT_STATUS_KIND_RANK[existing.kind] ?? 0;
122
+ const nextRank = GIT_STATUS_KIND_RANK[patch.kind] ?? 0;
123
+ index.set(key, {
124
+ changed: true,
125
+ kind: nextRank > existingRank ? patch.kind : existing.kind,
126
+ status: patch.direct === true ? (patch.status || existing.status || "") : (existing.status || ""),
127
+ direct: existing.direct === true || patch.direct === true,
128
+ changedDescendants: (Number(existing.changedDescendants || 0) || 0) + (Number(patch.changedDescendants || 0) || 0),
129
+ });
130
+ }
131
+
132
+ function addGitStatusAncestorEntries(index, repoPath, kind) {
133
+ const parts = normalizeGitStatusPath(repoPath).split("/").filter(Boolean);
134
+ parts.pop();
135
+ let current = "";
136
+ for (const part of parts) {
137
+ current = current ? `${current}/${part}` : part;
138
+ mergeGitStatusIndexEntry(index, current, { kind, changedDescendants: 1 });
139
+ }
140
+ }
141
+
142
+ function buildGitStatusIndexFromPorcelain(statusText = "") {
143
+ const index = new Map();
144
+ for (const entry of parseGitPorcelainZEntries(statusText)) {
145
+ const filePath = normalizeGitStatusPath(entry.path);
146
+ if (!filePath) continue;
147
+ const kind = gitStatusKindFromPorcelainEntry(entry);
148
+ mergeGitStatusIndexEntry(index, filePath, { kind, status: gitStatusLabelFromPorcelainEntry(entry), direct: true });
149
+ addGitStatusAncestorEntries(index, filePath, kind);
150
+ if (entry.oldPath) addGitStatusAncestorEntries(index, entry.oldPath, kind);
151
+ }
152
+ return index;
153
+ }
154
+
155
+ async function readWorkspaceGitStatusIndex(workspaceRoot) {
156
+ try {
157
+ const root = await getGitRoot(workspaceRoot);
158
+ const statusText = await runGitReadCommand(root, ["status", "--porcelain=v1", "-z", "--untracked-files=all"], { maxOutputLength: 120_000 });
159
+ return { root, entries: buildGitStatusIndexFromPorcelain(statusText) };
160
+ } catch {
161
+ return null;
162
+ }
163
+ }
164
+
165
+ function gitStatusForWorkspaceEntry(workspaceRoot, entry, gitStatus) {
166
+ if (!gitStatus?.root || !gitStatus.entries?.size || !entry?.path) return null;
167
+ const absolute = path.resolve(workspaceRoot, entry.path);
168
+ if (absolute !== gitStatus.root && !pathInside(gitStatus.root, absolute)) return null;
169
+ const repoPath = normalizeGitStatusPath(path.relative(gitStatus.root, absolute).split(path.sep).join("/"));
170
+ const status = gitStatus.entries.get(repoPath);
171
+ if (!status?.changed) return null;
172
+ return {
173
+ changed: true,
174
+ kind: status.kind || "changed",
175
+ status: status.status || "",
176
+ direct: status.direct === true,
177
+ changedDescendants: Number(status.changedDescendants || 0) || 0,
178
+ };
179
+ }
180
+
181
+ function withFileTreeGitStatus(entries = [], workspaceRoot, gitStatus) {
182
+ if (!gitStatus?.entries?.size) return entries;
183
+ return entries.map((entry) => {
184
+ const status = gitStatusForWorkspaceEntry(workspaceRoot, entry, gitStatus);
185
+ return status ? { ...entry, gitStatus: status } : entry;
186
+ });
187
+ }
188
+
189
+ async function readWorkspaceGitIgnoredPaths(workspaceRoot, entries = [], gitRoot = "") {
190
+ try {
191
+ const root = path.resolve(gitRoot || await getGitRoot(workspaceRoot));
192
+ const candidates = new Set();
193
+ for (const entry of entries) {
194
+ if (!entry?.path) continue;
195
+ const absolute = path.resolve(workspaceRoot, entry.path);
196
+ if (absolute !== root && !pathInside(root, absolute)) continue;
197
+ const relative = path.relative(root, absolute);
198
+ if (!relative || path.isAbsolute(relative) || relative.startsWith(`..${path.sep}`)) continue;
199
+ candidates.add(relative.split(path.sep).join("/"));
200
+ }
201
+ if (!candidates.size) return null;
202
+ const input = `${[...candidates].join("\0")}\0`;
203
+ const result = await runCommand("git", ["check-ignore", "-z", "--stdin"], {
204
+ cwd: root,
205
+ timeoutMs: GIT_CHANGES_COMMAND_TIMEOUT_MS,
206
+ maxOutputLength: Math.max(20_000, input.length + 1),
207
+ env: { GIT_OPTIONAL_LOCKS: "0" },
208
+ input,
209
+ utf8Output: true,
210
+ });
211
+ if (![0, 1].includes(result.exitCode) || result.timedOut || result.error || result.stdoutTruncated) return null;
212
+ return { root, paths: new Set(result.stdout.split("\0").filter(Boolean)) };
213
+ } catch {
214
+ return null;
215
+ }
216
+ }
217
+
218
+ function withFileTreeGitIgnored(entries = [], workspaceRoot, gitIgnored) {
219
+ if (!gitIgnored?.root || !gitIgnored.paths?.size) return entries;
220
+ return entries.map((entry) => {
221
+ if (!entry?.path) return entry;
222
+ const absolute = path.resolve(workspaceRoot, entry.path);
223
+ if (absolute !== gitIgnored.root && !pathInside(gitIgnored.root, absolute)) return entry;
224
+ const relative = path.relative(gitIgnored.root, absolute).split(path.sep).join("/");
225
+ return gitIgnored.paths.has(relative) ? { ...entry, gitIgnored: true } : entry;
226
+ });
227
+ }
228
+
229
+ function fileTreeGitStatusPayload(workspaceRoot, gitStatus) {
230
+ if (!gitStatus?.root || !gitStatus.entries?.size) return null;
231
+ const root = path.resolve(workspaceRoot);
232
+ const entries = [];
233
+ for (const [repoPath, status] of gitStatus.entries.entries()) {
234
+ const absolute = path.resolve(gitStatus.root, repoPath);
235
+ if (absolute !== root && !pathInside(root, absolute)) continue;
236
+ entries.push({
237
+ path: path.relative(root, absolute).split(path.sep).join("/"),
238
+ changed: true,
239
+ kind: status.kind || "changed",
240
+ status: status.status || "",
241
+ direct: status.direct === true,
242
+ changedDescendants: Number(status.changedDescendants || 0) || 0,
243
+ });
244
+ }
245
+ return { root: gitStatus.root, entries };
246
+ }
247
+
248
+ function resolveGitRelativePath(root, relativePath) {
249
+ const normalized = String(relativePath || "").trim();
250
+ if (!normalized || normalized.includes("\0")) throw new Error("Invalid git path");
251
+ const resolved = path.resolve(root, normalized);
252
+ if (resolved !== root && !resolved.startsWith(`${root}${path.sep}`)) throw new Error(`Git path escapes repository: ${normalized}`);
253
+ return resolved;
254
+ }
255
+
256
+ function isLikelyBinaryBuffer(buffer) {
257
+ return Buffer.isBuffer(buffer) && buffer.includes(0);
258
+ }
259
+
260
+ function normalizeGitRelativePath(root, relativePath) {
261
+ const resolved = resolveGitRelativePath(root, relativePath);
262
+ return path.relative(root, resolved).split(path.sep).join("/");
263
+ }
264
+
265
+ async function readGitUntrackedEntry(root, file, { maxBytes = Number.POSITIVE_INFINITY } = {}) {
266
+ const normalized = normalizeGitRelativePath(root, file);
267
+ const filePath = resolveGitRelativePath(root, normalized);
268
+ const info = await stat(filePath);
269
+ if (!info.isFile()) return { path: normalized, size: info.size, binary: false, content: "", error: "Not a regular file" };
270
+ if (Number.isFinite(maxBytes) && info.size > maxBytes) {
271
+ return { path: normalized, size: info.size, binary: false, content: "", error: `File is too large to preview (limit ${formatBytes(maxBytes)})` };
272
+ }
273
+ const buffer = await readFile(filePath);
274
+ const binary = isLikelyBinaryBuffer(buffer);
275
+ return {
276
+ path: normalized,
277
+ size: info.size,
278
+ binary,
279
+ content: binary ? "" : buffer.toString("utf8"),
280
+ };
281
+ }
282
+
283
+ async function readGitUntrackedEntries(root, files) {
284
+ const entries = [];
285
+ for (const file of files) {
286
+ try {
287
+ entries.push(await readGitUntrackedEntry(root, file));
288
+ } catch (error) {
289
+ entries.push({ path: file, size: 0, binary: false, content: "", error: sanitizeError(error) });
290
+ }
291
+ }
292
+ return entries;
293
+ }
294
+
295
+ async function readGitUntrackedFile(cwd, requestedPath, options = {}) {
296
+ const root = await getGitRoot(cwd);
297
+ const normalized = normalizeGitRelativePath(root, requestedPath);
298
+ const listed = await runGitReadCommand(root, ["ls-files", "--others", "--exclude-standard", "--", normalized], { maxOutputLength: 120_000 });
299
+ const files = listed.split(/\r?\n/).map((line) => line.trim()).filter(Boolean);
300
+ if (!files.includes(normalized)) throw new Error(`Not an untracked file: ${normalized}`);
301
+ return readGitUntrackedEntry(root, normalized, options);
302
+ }
303
+
304
+ function normalizeGitFileDiffCategory(requestedCategory) {
305
+ const category = String(requestedCategory || "").trim();
306
+ if (!GIT_FILE_DIFF_CATEGORIES.has(category)) {
307
+ throw new Error("Git file diff category must be staged, unstaged, conflicted, or untracked");
308
+ }
309
+ return category;
310
+ }
311
+
312
+ function gitFileDiffArgs(category, normalizedPath) {
313
+ const args = ["diff"];
314
+ if (category === "staged") args.push("--cached");
315
+ if (category === "conflicted") args.push("--cc");
316
+ args.push("--no-ext-diff", "--no-textconv", "--no-color", "--unified=3", "--src-prefix=a/", "--dst-prefix=b/", "--", normalizedPath);
317
+ return args;
318
+ }
319
+
320
+ async function readGitFileDiff(cwd, requestedPath, requestedCategory) {
321
+ const root = await getGitRoot(cwd);
322
+ const category = normalizeGitFileDiffCategory(requestedCategory);
323
+ const normalizedPath = normalizeGitRelativePath(root, requestedPath);
324
+ const label = GIT_FILE_DIFF_LABELS[category];
325
+
326
+ if (category === "untracked") {
327
+ const untracked = await readGitUntrackedFile(root, normalizedPath, { maxBytes: GIT_CHANGES_DIFF_MAX_OUTPUT });
328
+ return {
329
+ root,
330
+ path: normalizedPath,
331
+ category,
332
+ label,
333
+ command: formatGitCommand(["ls-files", "--others", "--exclude-standard", "--", normalizedPath]),
334
+ diff: "",
335
+ truncated: false,
336
+ capBytes: GIT_CHANGES_DIFF_MAX_OUTPUT,
337
+ ...untracked,
338
+ };
339
+ }
340
+
341
+ const args = gitFileDiffArgs(category, normalizedPath);
342
+ const result = await runGitReadCommandDetailed(root, args);
343
+ return {
344
+ root,
345
+ path: normalizedPath,
346
+ category,
347
+ label,
348
+ command: formatGitCommand(args),
349
+ diff: result.output.trimEnd(),
350
+ truncated: result.truncated,
351
+ capBytes: result.capBytes,
352
+ };
353
+ }
354
+
355
+ async function gitUpstreamRef(root) {
356
+ try {
357
+ const upstream = await runGitReadCommand(root, ["rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{upstream}"], { timeoutMs: 5000, maxOutputLength: 10_000 });
358
+ return upstream.trim();
359
+ } catch {
360
+ return "";
361
+ }
362
+ }
363
+
364
+ async function readGitIncomingChanges(root, summary) {
365
+ const upstream = await gitUpstreamRef(root);
366
+ const ahead = Number(summary?.ahead || 0) || 0;
367
+ const behind = Number(summary?.behind || 0) || 0;
368
+ const diverged = ahead > 0 && behind > 0;
369
+ const remote = {
370
+ upstream,
371
+ ahead,
372
+ behind,
373
+ diverged,
374
+ // Fast-forward pull is only possible when we are strictly behind.
375
+ canPull: !!upstream && behind > 0 && !diverged,
376
+ };
377
+ if (!upstream || behind <= 0) return { remote, section: null };
378
+
379
+ const diffArgs = ["diff", "--no-ext-diff", "--no-color", "--find-renames", "--unified=0", "--src-prefix=a/", "--dst-prefix=b/", "HEAD..@{upstream}"];
380
+ try {
381
+ const diff = await runGitReadCommandDetailed(root, diffArgs);
382
+ return {
383
+ remote,
384
+ section: {
385
+ key: "incoming",
386
+ label: `Incoming from ${upstream}`,
387
+ command: `git diff --unified=0 HEAD..${upstream}`,
388
+ diff: diff.output.trimEnd(),
389
+ truncated: diff.truncated,
390
+ capBytes: diff.capBytes,
391
+ },
392
+ };
393
+ } catch (error) {
394
+ return { remote: { ...remote, error: sanitizeError(error) }, section: null };
395
+ }
396
+ }
397
+
398
+ async function readGitChanges(cwd) {
399
+ const root = await getGitRoot(cwd);
400
+ const diffArgs = ["diff", "--no-ext-diff", "--no-color", "--find-renames", "--unified=0", "--src-prefix=a/", "--dst-prefix=b/"];
401
+ const [statusText, porcelainStatusText, unstagedDiff, stagedDiff, untrackedText] = await Promise.all([
402
+ runGitReadCommand(root, ["status", "--short", "--branch", "--untracked-files=all"], { maxOutputLength: 120_000 }),
403
+ runGitReadCommand(root, ["status", "--porcelain=2", "--branch", "--untracked-files=all"], { maxOutputLength: 120_000 }),
404
+ runGitReadCommandDetailed(root, diffArgs),
405
+ runGitReadCommandDetailed(root, ["diff", "--cached", "--no-ext-diff", "--no-color", "--find-renames", "--unified=0", "--src-prefix=a/", "--dst-prefix=b/"]),
406
+ runGitReadCommand(root, ["ls-files", "--others", "--exclude-standard"], { maxOutputLength: 120_000 }),
407
+ ]);
408
+ const summary = summarizeGitPorcelainStatus(porcelainStatusText);
409
+ const incoming = await readGitIncomingChanges(root, summary);
410
+ const untrackedFiles = untrackedText.split(/\r?\n/).map((line) => line.trim()).filter(Boolean);
411
+ const untracked = await readGitUntrackedEntries(root, untrackedFiles);
412
+ return {
413
+ cwd,
414
+ root,
415
+ branch: gitBranchFromPorcelainStatus(porcelainStatusText),
416
+ generatedAt: new Date().toISOString(),
417
+ summary,
418
+ remote: incoming.remote,
419
+ status: statusText.trimEnd(),
420
+ sections: [
421
+ incoming.section,
422
+ { key: "staged", label: "Staged", command: "git diff --cached --unified=0", diff: stagedDiff.output.trimEnd(), truncated: stagedDiff.truncated, capBytes: stagedDiff.capBytes },
423
+ { key: "unstaged", label: "Unstaged", command: "git diff --unified=0", diff: unstagedDiff.output.trimEnd(), truncated: unstagedDiff.truncated, capBytes: unstagedDiff.capBytes },
424
+ ].filter(Boolean),
425
+ untracked,
426
+ };
427
+ }
428
+
429
+ const GIT_PANEL_HISTORY_LIMIT = 30;
430
+ const GIT_PANEL_MAX_OUTPUT = 240_000;
431
+
432
+ function parseGitNumstatZ(text = "") {
433
+ const stats = new Map();
434
+ for (const record of String(text || "").split("\0")) {
435
+ if (!record) continue;
436
+ const firstTab = record.indexOf("\t");
437
+ const secondTab = firstTab < 0 ? -1 : record.indexOf("\t", firstTab + 1);
438
+ if (firstTab < 0 || secondTab < 0) continue;
439
+ const additionsText = record.slice(0, firstTab);
440
+ const deletionsText = record.slice(firstTab + 1, secondTab);
441
+ const filePath = normalizeGitStatusPath(record.slice(secondTab + 1));
442
+ if (!filePath) continue;
443
+ const binary = additionsText === "-" || deletionsText === "-";
444
+ stats.set(filePath, {
445
+ additions: binary ? 0 : Number.parseInt(additionsText || "0", 10) || 0,
446
+ deletions: binary ? 0 : Number.parseInt(deletionsText || "0", 10) || 0,
447
+ binary,
448
+ });
449
+ }
450
+ return stats;
451
+ }
452
+
453
+ function gitPanelPathStats(entry, stagedStats, unstagedStats) {
454
+ const paths = [entry.path, entry.oldPath].map(normalizeGitStatusPath).filter(Boolean);
455
+ const aggregate = (stats) => {
456
+ const value = { additions: 0, deletions: 0, binary: false };
457
+ for (const filePath of paths) {
458
+ const current = stats.get(filePath);
459
+ if (!current) continue;
460
+ value.additions += Number(current.additions || 0) || 0;
461
+ value.deletions += Number(current.deletions || 0) || 0;
462
+ value.binary ||= current.binary === true;
463
+ }
464
+ return value;
465
+ };
466
+ const staged = aggregate(stagedStats);
467
+ const unstaged = aggregate(unstagedStats);
468
+ return {
469
+ additions: staged.additions + unstaged.additions,
470
+ deletions: staged.deletions + unstaged.deletions,
471
+ binary: staged.binary || unstaged.binary,
472
+ stagedAdditions: staged.additions,
473
+ stagedDeletions: staged.deletions,
474
+ stagedBinary: staged.binary,
475
+ unstagedAdditions: unstaged.additions,
476
+ unstagedDeletions: unstaged.deletions,
477
+ unstagedBinary: unstaged.binary,
478
+ };
479
+ }
480
+
481
+ function gitPanelChangeEntry(entry, stagedStats, unstagedStats) {
482
+ const x = entry.x || " ";
483
+ const y = entry.y || " ";
484
+ const untracked = x === "?" && y === "?";
485
+ const conflicted = x === "U" || y === "U" || (x === "A" && y === "A") || (x === "D" && y === "D");
486
+ return {
487
+ path: normalizeGitStatusPath(entry.path),
488
+ oldPath: normalizeGitStatusPath(entry.oldPath || ""),
489
+ status: `${x}${y}`,
490
+ staged: !untracked && x !== " " && x !== "?",
491
+ unstaged: !untracked && y !== " " && y !== "?",
492
+ untracked,
493
+ conflicted,
494
+ ...gitPanelPathStats(entry, stagedStats, unstagedStats),
495
+ };
496
+ }
497
+
498
+ function parseGitPanelHistory(text = "") {
499
+ const fields = String(text || "").split("\0").filter(Boolean);
500
+ const history = [];
501
+ for (let index = 0; index + 4 < fields.length && history.length < GIT_PANEL_HISTORY_LIMIT; index += 5) {
502
+ const [hash, shortHash, author, authoredAt, subject] = fields.slice(index, index + 5);
503
+ if (!hash) continue;
504
+ history.push({ hash, shortHash, author, authoredAt, subject });
505
+ }
506
+ return history;
507
+ }
508
+
509
+ async function readGitPanel(cwd) {
510
+ const root = await getGitRoot(cwd);
511
+ const historyPromise = runGitReadCommand(root, [
512
+ "log", `-n${GIT_PANEL_HISTORY_LIMIT}`, "-z", "--date=iso-strict", "--format=%H%x00%h%x00%an%x00%aI%x00%s",
513
+ ], { maxOutputLength: GIT_PANEL_MAX_OUTPUT }).catch(() => "");
514
+ const [statusText, porcelainStatusText, stagedNumstatText, unstagedNumstatText, historyText] = await Promise.all([
515
+ runGitReadCommand(root, ["status", "--porcelain=v1", "-z", "--untracked-files=all"], { maxOutputLength: GIT_PANEL_MAX_OUTPUT }),
516
+ runGitReadCommand(root, ["status", "--porcelain=2", "--branch", "--untracked-files=all"], { maxOutputLength: GIT_PANEL_MAX_OUTPUT }),
517
+ runGitReadCommand(root, ["diff", "--cached", "--numstat", "-z", "--no-renames", "--"], { maxOutputLength: GIT_PANEL_MAX_OUTPUT }),
518
+ runGitReadCommand(root, ["diff", "--numstat", "-z", "--no-renames", "--"], { maxOutputLength: GIT_PANEL_MAX_OUTPUT }),
519
+ historyPromise,
520
+ ]);
521
+ const stagedStats = parseGitNumstatZ(stagedNumstatText);
522
+ const unstagedStats = parseGitNumstatZ(unstagedNumstatText);
523
+ const changes = parseGitPorcelainZEntries(statusText)
524
+ .map((entry) => gitPanelChangeEntry(entry, stagedStats, unstagedStats))
525
+ .filter((entry) => entry.path);
526
+ return {
527
+ cwd,
528
+ root,
529
+ branch: gitBranchFromPorcelainStatus(porcelainStatusText),
530
+ generatedAt: new Date().toISOString(),
531
+ summary: summarizeGitPorcelainStatus(porcelainStatusText),
532
+ changes,
533
+ history: parseGitPanelHistory(historyText),
534
+ };
535
+ }
536
+
537
+ async function readGitCommit(cwd, requestedHash) {
538
+ const hash = String(requestedHash || "").trim().toLowerCase();
539
+ if (!/^[a-f0-9]{40}([a-f0-9]{24})?$/.test(hash)) throw makeHttpError(400, "A full Git commit hash is required");
540
+ const root = await getGitRoot(cwd);
541
+ const verified = (await runGitReadCommand(root, ["rev-parse", "--verify", `${hash}^{commit}`], { maxOutputLength: 1000 })).trim();
542
+ if (verified.toLowerCase() !== hash) throw makeHttpError(404, "Git commit not found in this repository");
543
+ const [metaText, diff] = await Promise.all([
544
+ runGitReadCommand(root, ["show", "-s", "-z", "--date=iso-strict", "--format=%H%x00%h%x00%an%x00%aI%x00%s", hash], { maxOutputLength: 20_000 }),
545
+ runGitReadCommandDetailed(root, ["show", "--format=", "--no-ext-diff", "--no-color", "--find-renames", "--unified=0", "--src-prefix=a/", "--dst-prefix=b/", hash]),
546
+ ]);
547
+ const [commit] = parseGitPanelHistory(metaText);
548
+ return {
549
+ root,
550
+ branch: "",
551
+ generatedAt: new Date().toISOString(),
552
+ commit: commit || { hash, shortHash: hash.slice(0, 12), author: "", authoredAt: "", subject: "" },
553
+ summary: { staged: 0, unstaged: 0, untracked: 0, conflicted: 0, ahead: 0, behind: 0 },
554
+ remote: { upstream: "", ahead: 0, behind: 0, diverged: false, canPull: false },
555
+ sections: [{
556
+ key: "commit",
557
+ label: `Commit ${commit?.shortHash || hash.slice(0, 12)}`,
558
+ command: `git show --unified=0 ${hash}`,
559
+ diff: diff.output.trimEnd(),
560
+ truncated: diff.truncated,
561
+ capBytes: diff.capBytes,
562
+ }],
563
+ untracked: [],
564
+ };
565
+ }
566
+
567
+ function parseGitPorcelainZEntries(text) {
568
+ const fields = String(text || "").split("\0").filter(Boolean);
569
+ const entries = [];
570
+ for (let index = 0; index < fields.length; index++) {
571
+ const field = fields[index];
572
+ if (field.length < 4) {
573
+ entries.push({ x: "", y: "", path: field, unsupported: true });
574
+ continue;
575
+ }
576
+ const x = field[0] || " ";
577
+ const y = field[1] || " ";
578
+ const filePath = field.slice(3);
579
+ const entry = { x, y, path: filePath };
580
+ if ((x === "R" || x === "C") && index + 1 < fields.length) entry.oldPath = fields[++index];
581
+ entries.push(entry);
582
+ }
583
+ return entries;
584
+ }
585
+
586
+ return {
587
+ getGitRoot,
588
+ runGitReadCommandDetailed,
589
+ runGitReadCommand,
590
+ readGitChanges,
591
+ readGitFileDiff,
592
+ readGitPanel,
593
+ readGitCommit,
594
+ readGitUntrackedFile,
595
+ readWorkspaceGitStatusIndex,
596
+ readWorkspaceGitIgnoredPaths,
597
+ withFileTreeGitStatus,
598
+ withFileTreeGitIgnored,
599
+ fileTreeGitStatusPayload,
600
+ resolveGitRelativePath,
601
+ normalizeGitRelativePath,
602
+ isLikelyBinaryBuffer,
603
+ parseGitPorcelainZEntries,
604
+ gitBranchFromPorcelainStatus,
605
+ summarizeGitPorcelainStatus,
606
+ gitUpstreamRef,
607
+ };
608
+ }