C3: chunk staging, unstaging and revert from the gutter and the diff viewer

- crates/vcs chunk.rs: chunks of a file (buffer text vs index, HEAD vs
  index), a minimal patch applied with git apply --cached, revert as a
  byte edit; token refusal when buffer, index or HEAD changed; CRLF
  (autocrlf and stored), no final newline, BOM, staged renames, first
  commit; binary and untracked refused. Real-git tests + property test.
- engine: git.chunks, git.stageChunk, git.unstageChunk, git.revertChunk
  (revert edits the buffer under the lock, undoable in the editor);
  cmd::git::start_then; git.lineChanges shares the vcs line diff.
- UI: chunk popup on a gutter marker click (Stage / Unstage / Revert /
  Previous / Next), hollow staged markers, vcs.showChunk, diff viewer
  hint, "partly staged" badge in the Changes window.
- Docs: G8 row, Git parity row evidence, ui-parity, architecture.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-11-01 12:50:11 +01:00
parent d8e66a4652
commit 301443db7d
28 changed files with 3159 additions and 101 deletions

View file

@ -628,6 +628,19 @@ Watched directories: the VFS watcher is non-recursive, so `project.children` wat
- **Look:** both sides use the Lutra editor theme and the file's language; changed lines are tinted with `color-mix` of `--lt-vcs-deleted` (left) and `--lt-vcs-added` (right) into `--lt-editor-bg`, changed words more strongly, a change bar in each gutter, unchanged stretches over 8 lines collapse to a row (3 lines of context). No colour outside the tokens.
- **Tests:** `ui/tests/vcs/DiffView.test.ts` (21 Vitest tests against a fake engine: params and `git.show` parsing, EOL normalisation, index and HEAD sides, one tab per side, the palette without a path, bad params and no file as toasts, disabled without a project, edits on the right reaching `buffer.edit`, engine changes reaching the right side, the left side refresh on `gitChanged`, closing with the buffer, binary / too large / git error / deleted / new file, switching between diff and file tabs, next/previous change with wrap-around, the tree context menu by right click and by Shift+F10 with Escape).
### Chunk staging (C8 C3)
- **Backend** (`crates/vcs/src/chunk.rs`, through the installed git, D20): `Repo::chunks(path, worktree)` reads the index blob (`:0:<path>`) and the HEAD blob (under the old path of a staged rename, from `git diff --cached --name-status -z -M HEAD`) as stored and diffs them with the caller's working text: *unstaged* chunks index → working text, *staged* chunks HEAD → index, the latter placed on the working-text lines their index lines map to through the unstaged changes. The diff is the one `git.lineChanges` uses (`chunk::line_hunks`: lines split at `\n`, `\r\n` and a lone `\r`, compared without their break, a blob's BOM ignored, Myers with the `Replace` hook and a 1 s deadline), so chunks and markers line up. At most 10,000 chunks per side and 500 shown lines per side of a chunk.
- **Token:** FNV-1a over the path, the working text, the index blob, the HEAD path and the HEAD blob. `stage_chunk`, `unstage_chunk` and `revert_chunk (path, worktree, n, token)` recompute everything and refuse with `VcsError::Stale` ("the file or the index changed since the chunk was shown") on another token or a missing chunk. Stage and unstage hold the repository's mutation lock.
- **Patches:** stage replaces the chunk's index lines with the working lines; unstage puts HEAD's lines back (the forward patch against the index, equivalent to `git apply --cached --reverse` of the staged chunk). `build_patch` writes a `diff --git` header (paths C-quoted like git when they hold control characters, quotes or backslashes), one hunk with up to 3 context lines from the target blob as stored, and git's "\ No newline at end of file" marker; `git apply --cached --whitespace=nowarn -` applies it, and git's own context check is a second guard. New lines take the target's first line break (else the source's, else `\n`), so a CRLF working copy over an LF (autocrlf) index stays LF and a CRLF blob stays CRLF; the final break follows the source when the chunk reaches the end of both texts. Two widenings keep patches valid: an insertion after a last line without a break takes that line along, and deleting the first lines of a blob with a BOM takes the next line along so the BOM survives. Refused (`VcsError::ChunkRefused`): binary or too large blobs, a path without a stage-0 entry (untracked, conflicted), lines broken by a lone `\r`.
- **Revert** returns a byte edit of the working text (its own line break and final break kept); `lutra-vcs` writes nothing.
- **Commands** (`crates/engine/src/cmd/git_chunks.rs`, `COMMAND_IDS` in the module, no default shortcuts): `git.chunks {id}` → `{version, token, headPath, unstaged, staged}` with `{kind, startLine, endLine, oldStart, oldEnd, newStart, newEnd, removed, added, truncated}` per chunk; `git.stageChunk`, `git.unstageChunk`, `git.revertChunk {id, index, token}`. All are pending outcomes started through `cmd::git::start_with` / `start_then` (new: a step under the lock after git answered); the working text is the open buffer's text taken under the lock, unsaved changes included. Stage and unstage send `gitChanged`. Revert's step checks that the buffer is still at the version the chunk was computed for and applies the edit through `buffer.edit` (journal, LSP and syntax see it like typing), leaving the buffer dirty.
- **Gutter** (`gitGutter.ts`): besides `git.lineChanges`, the plugin reads `git.chunks` when the view shows the state and on every refresh (`gitChanged`), not while typing; lines of staged chunks that no unstaged chunk covers get a hollow marker (`--lt-vcs-modified` border), mapped through edits. A click on any marker opens the chunk popup for that line.
- **Popup** (`chunkPopup.ts`, a CodeMirror tooltip at the line): the unstaged chunk at the line (removed and added lines tinted with `--lt-vcs-deleted` / `--lt-vcs-added`, Stage, Revert) and the staged one (Unstage), Previous / Next (caret to the previous or next chunk start, wrapping, scrolled into view) and a close button; Escape, an edit or a click elsewhere in the editor closes it. Actions pass the chunk's position and the token; a refusal shows inside the popup. After Stage or Unstage it re-reads and stays on the line, so a stage can be undone at once. Revert runs inside the buffer sync's `beginUndoableRemote` / `endUndoableRemote(version)`, so the editor's undo takes it back, then closes. `vcs.showChunk` (UI command, Git › Show Changed Chunk, no shortcut) opens it at the caret line of the editor shown last.
- **Diff viewer:** its right side is an ordinary editor of the buffer, so the same gutter and popup work there; a hint line under the caption says so. **Changes window:** a partly staged file was already listed in Staged and in Changes; both rows now carry a "partly staged" badge (`ChangeRow.partial`).
- **Shortcuts:** none. Lutra's `Alt+Shift+]` / `Alt+Shift+[` stay the diff's next/previous change; the popup has its own Previous / Next buttons.
- **Tests:** `chunk.rs` unit tests (splitting, hunks without line breaks, line mapping, patches with context and missing final breaks, quoting, replacements, widening, rename parsing, tokens, staged placement); `crates/vcs/tests/chunk.rs` against real temp repositories (stage and unstage one of two chunks, a stale token and stale texts, CRLF with autocrlf and stored CRLF, no final newline both ways, a BOM, revert edits, a staged rename unstaged against the old path, the first commit, binary and untracked refusals, a path with spaces and quotes, and a property test: staging every chunk reaches the working text, unstaging every chunk returns to HEAD); `crates/engine/tests/git_chunks.rs` (an unsaved buffer staged, a stale token refused, revert as a dirty buffer edit, unstage, `gitChanged`, bad params); Vitest `ui/tests/editor/chunkPopup.test.ts` (11: parsing, coverage, previous/next, staged markers on show and refresh only, opening from a marker click, stage and unstage with the token, revert in an undo window, refusals, unreadable chunks, Next, Escape and closing on an edit), plus `gitGutter.test.ts` and the partial flag in `ChangesToolWindow.test.ts`.
### History (M4.6)
- **Backend (`crates/vcs/src/log.rs`, appended to `Repo`):**