diff --git a/COHERENCE.md b/COHERENCE.md index e9cb970..08f000d 100644 --- a/COHERENCE.md +++ b/COHERENCE.md @@ -1293,6 +1293,17 @@ Primitive-by-primitive against the list above: that found it (§25). All four remain in `lsp.lua`; per §25 the symbols are authoritative and the `ad41cf1` line numbers have drifted. + **Updated again: FIVE, and the fifth is the first outside + `lsp.lua`.** Git Stage 1's `*git-status*` + (`builtin/runtime/git.lua`) is the concrete evidence P5 asked for + that the primitive generalizes past its first consumer — the + remediation here was always adoption, not construction. It also + added the primitive's one extension: an optional **`keys`** table on + the open spec, installed once with the panel's buffer and compared + (not re-bound) on reopen, because `Keymap::bind` refuses duplicates + and an async consumer re-opens on every refresh. `*buffer-list*` and + project-search remain the un-migrated hand-rolled pair. + **`*lsp*` is the only one of the four with a working refresh** — it is the only one supplying `on_refresh`. `g` is bound on all four unconditionally by `bind_local_keymap`, so the other three carry a @@ -1422,10 +1433,25 @@ What does not: - **Code actions apply the first action blindly** — no picker (a roadmap "dark matter" item still true at audit). -- **There is no Git integration at all** — no status, stage, diff, - blame, or gutter markers anywhere in the tree (gutter git riders and - the `ResourceOffer` diff/blame family are named deferrals). The Git - affordance list above has nothing to attach to yet. +- **Git integration reaches status and diff, and no further.** Stage 1 + (`docs/git-integration-framing.md`) ships `*git-status*` — a + `listview` panel over `git status --porcelain=v2 --branch -z`, with + RET visiting the file and `d` showing its file-level diff. There is + still **no stage, revert, blame, or gutter marker** anywhere in the + tree; gutter git riders need new `DecorationKind` variants (Stage 2, + which must be scheduled alone), and the `ResourceOffer` diff/blame + family remains a named deferral. The Git affordance list above now has + something to attach to; the affordances themselves are unbuilt, and + the menu's context vocabulary (`src/menu.rs`) has no `git` context to + host them. + + The original audit said "there is no Git integration at all … anywhere + in the tree", and that was **literally false when it was written**: + `tests/fixtures/pmacs-magit/` is a tracked, installable package that + spawns git and parses porcelain v2, with a 32-test acceptance suite + (`tests/m8_6_acceptance.rs`). The **product** gap it described was + real; the sentence overstated it, and the framing that found the + overstatement is the one that closed the gap. - No test run/debug affordances (DAP is a future arc, `docs/dap-debugging-framing.md`). - No missing-tool guidance affordances (§1.2 — the diagnostic that diff --git a/builtin/runtime/git.lua b/builtin/runtime/git.lua new file mode 100644 index 0000000..c19b9a7 --- /dev/null +++ b/builtin/runtime/git.lua @@ -0,0 +1,925 @@ +-- git.lua --- Git integration Stage 1: read-only status and diff. +-- Framing: docs/git-integration-framing.md (revision 5). +-- +-- Two surfaces and nothing else: +-- +-- *git-status* a `pmacs.listview` panel over +-- `git --no-optional-locks -C status +-- --porcelain=v2 --branch -z`. RET visits the file, +-- `d` shows its diff, `g` refreshes. +-- *git-diff* the diff for the FILE under point, in a generated +-- buffer rendered as plain text. There is no bundled +-- `diff` grammar (checked: `BUILTIN_LANGUAGES` in +-- src/syntax.rs has no entry), and there is no hunk +-- model anywhere in the tree --- hunks are what gutter +-- markers need, and that is Stage 2's protocol work. +-- +-- NO WIRE CHANGE. Stage 2 (gutter markers) needs new `DecorationKind` +-- variants and a `PROTOCOL_VERSION` bump, and must be scheduled alone. +-- +-- Three facts this module is built on, each measured rather than +-- reasoned about: +-- +-- * `ProjectKind::Git` means a BARE repository ("no language marker +-- found inside", src/project.rs), and a language marker beside +-- `.git` WINS --- so pmacs reports `kind = "rust"` for its own +-- repository. This module therefore never asks pmacs whether +-- something is a git repo: it runs `git -C rev-parse +-- --show-toplevel` and lets a non-zero exit be the answer. Git's own +-- resolution handles submodules, worktrees, `GIT_DIR` and `.git` +-- files; a marker walk reimplements a subset and gets it wrong. +-- * `git diff --no-index` implies `--exit-code`: it exits 1 when it +-- SUCCESSFULLY finds differences. For the untracked path the success +-- predicate is therefore exit in {0, 1}; only >= 2 is a failure. +-- That asymmetry is confined to `--no-index`. +-- * An unborn `HEAD` makes `git diff HEAD` exit 128 with +-- `fatal: bad revision 'HEAD'`. It is detected from +-- `# branch.oid (initial)` in the `--branch` output this module +-- already parses --- NOT from a second `rev-parse` process for a +-- fact the first one hands over. +-- +-- Background-work attribution is a NEGATIVE (COHERENCE.md §9): a +-- spawned process does not appear in `*workers*` at all --- that buffer +-- is `async.lua`'s job list, while processes live under +-- `pmacs.process.list`. Every spawn here is labelled, which is strictly +-- better than an anonymous `git`, but a label is not attribution and +-- this module does not pretend otherwise. Accepted because these are +-- short-lived reads. + +pmacs.git = pmacs.git or {} + +local STATUS_PANEL = "*git-status*" +local DIFF_BUFFER = "*git-diff*" + +--- The git program name. +--- +--- A module-local rather than a setting: Q#G-4 defines exactly one +--- setting and says to resist more until there is use evidence. It is +--- reachable from Lua because the missing-binary path (Q#G-2's named +--- risk) has to be WITNESSED, and there is no other way to reach it in +--- process: Rust's `Command` resolves the program against the PARENT +--- process's `PATH`, so a child `env` cannot hide git, and +--- `std::env::set_var` is `unsafe` in edition 2024, which this project +--- forbids. Pointing this at a name that is not on `PATH` produces +--- exactly the ENOENT a missing git produces. +pmacs.git._program = "git" + +--- The most recent spawn's `{ command, args, cwd, label }`. +--- +--- Public because the `--no-optional-locks` contract is witnessed +--- STRUCTURALLY: a lock that was not taken cannot be observed directly, +--- so the assembled invocation is what gets pinned (Q#G-6). +pmacs.git._last_spawn = nil + +--- The last few spawns' argv, oldest first. +--- +--- A bounded diagnostic ring rather than an unbounded log. It exists +--- because one of this module's contracts is about a process that must +--- NOT be spawned: unborn `HEAD` is detected from `# branch.oid +--- (initial)` in output already in hand, and nobody should later +--- reintroduce a `rev-parse --verify HEAD` for it. "Which processes did +--- that open run?" is not answerable from `_last_spawn` alone. +pmacs.git._spawn_log = {} + +local SPAWN_LOG_LIMIT = 16 + +-- --------------------------------------------------------------------- +-- Configuration (Q#G-4) +-- --------------------------------------------------------------------- + +pmacs.config.define { + name = "git.enabled", + description = "Whether the Git commands (*git-status*, *git-diff*) run git at all. Turning this off makes them report that they are disabled rather than spawning anything.", + type = "boolean", + default = true, + mutability = "live", +} + +local function git_enabled() + local ok, value = pcall(pmacs.config.get, "git.enabled") + if not ok then return true end + return value ~= false +end + +-- --------------------------------------------------------------------- +-- Text safety at the binding boundary (Q#G-8) +-- --------------------------------------------------------------------- +-- +-- Git hands back PATH BYTES. `pmacs.process.spawn` takes +-- `args: Vec` and `pmacs.buffer.find_or_open` takes +-- `path: String` --- both Rust `String`, i.e. UTF-8 by construction --- +-- and the rope is UTF-8 by project invariant, so a path that is valid +-- bytes but not valid UTF-8 can be READ and DISPLAYED and cannot be +-- passed back for a diff, nor opened. The honest boundary is: parse it, +-- show it (escaped, since the raw bytes cannot enter a rope), and +-- REFUSE the gesture with a message. + +-- Length of the valid UTF-8 sequence starting at byte `i`, or nil. +-- Rejects overlongs, surrogates and anything past U+10FFFF, so +-- "displayable" means the same thing here as it does to Rust. +local function utf8_seq_len(s, i) + local b1 = s:byte(i) + if not b1 then return nil end + if b1 < 0x80 then return 1 end + local n, cp + if b1 >= 0xC2 and b1 <= 0xDF then + n, cp = 2, b1 - 0xC0 + elseif b1 >= 0xE0 and b1 <= 0xEF then + n, cp = 3, b1 - 0xE0 + elseif b1 >= 0xF0 and b1 <= 0xF4 then + n, cp = 4, b1 - 0xF0 + else + return nil + end + if i + n - 1 > #s then return nil end + for k = 1, n - 1 do + local b = s:byte(i + k) + if b < 0x80 or b > 0xBF then return nil end + cp = cp * 64 + (b - 0x80) + end + if n == 3 and cp < 0x800 then return nil end + if n == 4 and cp < 0x10000 then return nil end + if cp >= 0xD800 and cp <= 0xDFFF then return nil end + if cp > 0x10FFFF then return nil end + return n +end + +--- True when `s` is valid UTF-8, i.e. when it can cross the binding +--- boundary at all. +function pmacs.git.is_text(s) + if type(s) ~= "string" then return false end + local i, n = 1, #s + while i <= n do + local len = utf8_seq_len(s, i) + if not len then return false end + i = i + len + end + return true +end + +--- `s` rendered for a ONE-LINE panel row: invalid UTF-8 bytes and every +--- control byte become `\xNN`. +--- +--- Escaping controls is not cosmetic. A path may contain a newline --- +--- that is exactly what `-z` buys and what a quoted parser gets wrong +--- --- and a raw newline in a row would split it across two lines and +--- desynchronize every line-to-row mapping in the panel. +function pmacs.git.display_path(s) + if type(s) ~= "string" then return "" end + local out = {} + local i, n = 1, #s + while i <= n do + local len = utf8_seq_len(s, i) + local b = s:byte(i) + if len and not (len == 1 and (b < 0x20 or b == 0x7F)) then + out[#out + 1] = s:sub(i, i + len - 1) + i = i + len + else + out[#out + 1] = string.format("\\x%02X", b) + i = i + 1 + end + end + return table.concat(out) +end + +--- `s` with invalid UTF-8 bytes replaced by U+FFFD, controls left +--- alone. For MULTI-LINE bodies (a patch), where newlines and tabs are +--- content rather than a hazard. +local function utf8_clean(s) + if pmacs.git.is_text(s) then return s end + local out = {} + local i, n = 1, #s + while i <= n do + local len = utf8_seq_len(s, i) + if len then + out[#out + 1] = s:sub(i, i + len - 1) + i = i + len + else + out[#out + 1] = "\239\191\189" -- U+FFFD + i = i + 1 + end + end + return table.concat(out) +end + +-- --------------------------------------------------------------------- +-- Running git (Q#G-2) +-- --------------------------------------------------------------------- + +--- The argv for a git invocation rooted at `root`. +--- +--- Every call site goes through here, so `--no-optional-locks` cannot +--- be dropped by one of them. The flag is part of the contract, not a +--- nicety (Q#G-6): `git status` is not strictly read-only --- it may +--- refresh and write the index --- and this module runs it +--- asynchronously from an editor while the user may be running git in a +--- terminal, which is the exact scenario the flag exists for. +function pmacs.git.argv(root, rest) + local args = { "--no-optional-locks" } + if root then + args[#args + 1] = "-C" + args[#args + 1] = root + end + for _, a in ipairs(rest) do args[#args + 1] = a end + return args +end + +-- proc raw id -> { procid, out, err, on_done } +local pump = {} + +-- Spawn git and call `on_done { ok, code, kind, stdout, stderr }` once +-- it terminates. A spawn failure calls `on_done` too, with +-- `spawn_error` set --- §1.2's silence asymmetry: the failure must be +-- surfaced with guidance, never swallowed. +local function run_git(label, root, rest, on_done) + local spec = { + label = label, + command = pmacs.git._program, + args = pmacs.git.argv(root, rest), + stdin = "null", + } + if root then spec.cwd = root end + pmacs.git._last_spawn = { + command = spec.command, args = spec.args, cwd = spec.cwd, label = spec.label, + } + local log = pmacs.git._spawn_log + log[#log + 1] = spec.args + while #log > SPAWN_LOG_LIMIT do table.remove(log, 1) end + local ok, proc = pcall(pmacs.process.spawn, spec) + if not ok then + on_done { + ok = false, kind = "spawn_failed", spawn_error = tostring(proc), + stdout = "", stderr = "", + } + return nil + end + pump[proc:raw()] = { procid = proc, out = {}, err = {}, on_done = on_done } + return proc +end + +pmacs.hook.add("process.after-tick", function() + for raw, entry in pairs(pump) do + for _, ev in ipairs(pmacs.process.events_take(entry.procid)) do + local kind = ev.kind + if kind == "stdout" then + entry.out[#entry.out + 1] = ev.bytes + elseif kind == "stderr" then + entry.err[#entry.err + 1] = ev.bytes + elseif kind == "exited" or kind == "signaled" or kind == "crashed" then + -- The supervisor drains all remaining output BEFORE pushing the + -- terminal event (`final_drain_runtime`, src/process.rs), so one + -- pass in event order captures everything. + pump[raw] = nil + pcall(pmacs.process.forget, entry.procid) + local result = { + ok = (kind == "exited"), + code = (kind == "exited") and (ev.code or 0) or nil, + kind = kind, + signal = ev.signal, + error = ev.error, + stdout = table.concat(entry.out), + stderr = table.concat(entry.err), + } + local called, err = pcall(entry.on_done, result) + if not called then + pmacs.editor.set_status("git: " .. tostring(err)) + end + end + end + end +end) + +--- The first line of `text`, trimmed, or `""`. +local function first_line(text) + local line = (text or ""):match("^[^\r\n]*") or "" + return (line:gsub("%s+$", "")) +end + +--- A one-line description of why a git invocation failed. +local function failure_reason(res) + if res.spawn_error then + return string.format( + "cannot run %q (%s) --- is git installed and on PATH?", + pmacs.git._program, first_line(res.spawn_error)) + end + local detail = first_line(utf8_clean(res.stderr)) + if res.kind == "signaled" then + return string.format("git was killed by %s%s", + res.signal or "a signal", detail ~= "" and (": " .. detail) or "") + end + if res.kind == "crashed" then + return string.format("git crashed: %s", res.error or detail) + end + return string.format("git exited with code %d%s", + res.code or -1, detail ~= "" and (": " .. detail) or "") +end + +-- --------------------------------------------------------------------- +-- Porcelain v2 parsing (Q#G-6) +-- --------------------------------------------------------------------- +-- +-- The SEPARATION here --- pure `parse_*` functions that take a string +-- and return structure, testable with no repository --- is ported from +-- `tests/fixtures/pmacs-magit/status.lua`, whose 32-test suite proves +-- the shape works. The record TOKENIZER is deliberately NOT ported: the +-- fixture reads newline-delimited v2 and this reads `-z`, and those are +-- different grammars. Under `-z` a record's fields are NUL-terminated, +-- so a rename carries its two paths as SEPARATE fields rather than +-- tab-joined inside one, and C quoting is removed from the problem +-- entirely rather than obliging a hand-written unquoter. +-- +-- The fixture is left untouched (Q#G-0): its purpose is to prove the +-- PACKAGE SYSTEM can host this, and bundled code becoming its +-- dependency would make `tests/m8_6_acceptance.rs` test less than it +-- claims. The duplication is deliberate and stated rather than quiet. + +-- Split NUL-terminated fields. A trailing empty fragment after the last +-- NUL is dropped; an empty payload yields no fields. +local function nul_fields(text) + local out = {} + if type(text) ~= "string" or text == "" then return out end + local i = 1 + while true do + local nul = text:find("\0", i, true) + if not nul then + if i <= #text then out[#out + 1] = text:sub(i) end + break + end + out[#out + 1] = text:sub(i, nul - 1) + i = nul + 1 + end + return out +end + +--- Parse `git status --porcelain=v2 --branch -z` output. +--- +--- Returns `{ branch = {...}, rows = {...} }` where each row is +--- +--- { kind = "ordinary"|"rename"|"unmerged"|"untracked"|"ignored", +--- xy, x, y, path, orig, score } +--- +--- `path` is the CURRENT path --- what the panel shows and what RET +--- visits --- and `orig` remembers where a rename or copy came from. +--- Both are raw bytes; nothing here assumes they are text. +function pmacs.git.parse_status(text) + local branch = { unborn = false } + local rows = {} + local fields = nul_fields(text) + local i = 1 + while i <= #fields do + local field = fields[i] + local tag = field:sub(1, 1) + if tag == "#" then + local key, value = field:match("^# (%S+) (.*)$") + if key == "branch.oid" then + branch.oid = value + -- Unborn HEAD, from the output already being parsed. A second + -- `rev-parse --verify HEAD` would be a whole extra process for + -- a fact this line hands over. + branch.unborn = (value == "(initial)") + elseif key == "branch.head" then + branch.head = value + elseif key == "branch.upstream" then + branch.upstream = value + elseif key == "branch.ab" then + branch.ahead = tonumber(value:match("^%+(%d+)") or "") + branch.behind = tonumber(value:match("%-(%d+)$") or "") + end + elseif tag == "1" then + -- 1 + local xy, path = field:match("^1 (%S%S) %S+ %S+ %S+ %S+ %S+ %S+ (.+)$") + if xy then + rows[#rows + 1] = { + kind = "ordinary", xy = xy, x = xy:sub(1, 1), y = xy:sub(2, 2), path = path, + } + end + elseif tag == "2" then + -- 2 \0 + -- + -- The origin is the NEXT field, not a tab-joined suffix. That is + -- the whole reason this tokenizer is not the fixture's. + local xy, score, path = + field:match("^2 (%S%S) %S+ %S+ %S+ %S+ %S+ %S+ (%S+) (.+)$") + if xy then + i = i + 1 + rows[#rows + 1] = { + kind = "rename", xy = xy, x = xy:sub(1, 1), y = xy:sub(2, 2), + path = path, orig = fields[i], score = score, + } + end + elseif tag == "u" then + -- u

+ local xy, path = + field:match("^u (%S%S) %S+ %S+ %S+ %S+ %S+ %S+ %S+ %S+ (.+)$") + if xy then + rows[#rows + 1] = { + kind = "unmerged", xy = xy, x = xy:sub(1, 1), y = xy:sub(2, 2), path = path, + } + end + elseif tag == "?" then + local path = field:match("^%? (.+)$") + if path then + rows[#rows + 1] = { kind = "untracked", xy = "??", x = "?", y = "?", path = path } + end + elseif tag == "!" then + local path = field:match("^! (.+)$") + if path then + rows[#rows + 1] = { kind = "ignored", xy = "!!", x = "!", y = "!", path = path } + end + end + i = i + 1 + end + return { branch = branch, rows = rows } +end + +-- --------------------------------------------------------------------- +-- Panel state +-- --------------------------------------------------------------------- + +-- `display` maps a panel DATA LINE (1-based; the header is line 0) to +-- the git row rendered there. It is this module's own copy because +-- listview's line map is private, and `d` needs the row under the +-- cursor. `rows` is the same information as an array, for re-seating. +local state = { + root = nil, + branch = nil, + rows = {}, + display = {}, + buffer = nil, + diff_buffer = nil, + failure = nil, + generation = 0, +} + +local function status_line_text(row) + local shown = pmacs.git.display_path(row.path) + if row.orig then + shown = string.format("%s <- %s", shown, pmacs.git.display_path(row.orig)) + end + return string.format("%s %s", row.xy, shown) +end + +local function status_header() + -- A failed run has no branch and no rows, and rendering "0 changes" + -- above a failure row would be a small lie in the one place the panel + -- most needs to be honest. + if state.failure then + return "git: status failed g retry q quit" + end + local branch = state.branch or {} + local where + if branch.unborn then + where = string.format("%s (no commits yet)", branch.head or "HEAD") + elseif branch.head == "(detached)" or branch.head == nil then + where = string.format("detached at %s", (branch.oid or "?"):sub(1, 8)) + else + where = branch.head + end + local n = #state.rows + return string.format( + "git: %s --- %d change%s RET visit d diff n/p move g refresh q quit", + where, n, n == 1 and "" or "s") +end + +-- Build the listview rows and (re)build `state.display` alongside them, +-- so the two can never drift. +local function listview_rows(extra_text) + state.failure = nil + state.display = {} + local out = {} + for _, row in ipairs(state.rows) do + out[#out + 1] = { text = status_line_text(row), item = row } + state.display[#out] = row + end + if extra_text then + out[#out + 1] = { text = extra_text } + end + return out +end + +-- Row-level failure (Q#G-1 item 4): a failure is a ROW, not a silence. +local function failure_rows(reason) + state.failure = reason + state.rows = {} + state.display = {} + return { { text = "! " .. reason } } +end + +-- --------------------------------------------------------------------- +-- Visiting (RET) +-- --------------------------------------------------------------------- + +local function join_root(path) + if path:sub(1, 1) == "/" then return path end + return (state.root or ".") .. "/" .. path +end + +local function refuse_unrepresentable(row) + pmacs.editor.set_status(string.format( + "git: %s is not valid UTF-8, so pmacs cannot open or diff it", + pmacs.git.display_path(row.path))) +end + +local function visit_row(row) + if type(row) ~= "table" or not row.path then return end + if not pmacs.git.is_text(row.path) then + refuse_unrepresentable(row) + return + end + local target = join_root(row.path) + pmacs.editor.push_jump() + -- A visit FROM a panel lands in the DOCUMENT target and leaves the + -- panel where it is (Q#BP11b) --- `display_file`, never the raw + -- switch, which would clobber the panel with the source. + local ok, err = pcall(pmacs.window.display_file, target, { select = true }) + if not ok then + pmacs.editor.jump_back() + pmacs.editor.set_status(string.format("git: cannot open %s: %s", + pmacs.git.display_path(row.path), first_line(tostring(err)))) + end +end + +-- --------------------------------------------------------------------- +-- The status refresh (Q#G-1) +-- --------------------------------------------------------------------- + +local function open_status_panel(rows) + pmacs.listview.open { + name = STATUS_PANEL, + header = status_header(), + rows = rows, + -- `d` is not on listview's key surface (RET SPC n p TAB + -- g q), and it cannot be bound from outside the primitive safely: + -- a name collision disambiguates to `<2>`, so the name passed here + -- is not necessarily the buffer that came back. The `keys` table + -- binds it through the primitive's own buffer-local path, so no key + -- is intercepted and COHERENCE.md §6 stays at six shadows. + keys = { d = "git.diff-file" }, + on_visit = visit_row, + on_refresh = function() return pmacs.git._on_refresh() end, + } + -- `listview.open` takes `select = true`, so the panel is the active + -- buffer here. Captured rather than looked up by name, because the + -- name may have been disambiguated. + state.buffer = pmacs.window.buffer() +end + +--- The refresh generation currently in force. +--- +--- Exposed alongside `_deliver_status` below, and for the same reason: +--- the discard rule is about a completion arriving LATE, and a caller +--- cannot construct a stale request without knowing what "current" +--- means. +function pmacs.git._generation() + return state.generation +end + +--- Deliver a completed `git status`. +--- +--- Exposed because concurrent refresh is asserted by DRIVING two +--- refreshes and completing them out of order, which no arrangement of +--- real subprocess timing can guarantee. +function pmacs.git._deliver_status(request, res) + -- Generation (Q#G-1 item 3): a second `g` while one is in flight + -- bumps the generation, and the older completion DISCARDS its rows + -- rather than racing. It does not terminate the first process --- + -- reaping is `process.forget`'s job and killing git mid-read buys + -- nothing. + if request.generation ~= state.generation then return end + -- Panel lifetime (Q#G-1 item 5): if the buffer this refresh belongs + -- to is gone, drop the result. A FIRST open carries no expectation. + if request.expect_buffer ~= nil then + local live, valid = pcall(request.expect_buffer.is_valid, request.expect_buffer) + if not (live and valid) then return end + end + + local rows + if res.ok and res.code == 0 then + local parsed = pmacs.git.parse_status(res.stdout) + state.branch = parsed.branch + state.rows = parsed.rows + rows = listview_rows(nil) + else + local reason = failure_reason(res) + state.branch = state.branch or {} + rows = failure_rows(reason) + pmacs.editor.set_status("git status: " .. reason) + end + + open_status_panel(rows) + -- `listview.open` resets collapse and does NOT preserve selection --- + -- only `listview.refresh` does, and that is the synchronous path this + -- model cannot use. So re-seating is owned here. + -- + -- And `open`'s own `seat_cursor(p, 1)` cannot be relied on either: it + -- walks DOWN from wherever the cursor is, on the premise that a fresh + -- `switch_active_buffer` zeroed it. Re-opening a panel that is + -- already displayed does not zero anything, so that walk would land + -- one row below the previous cursor instead of on row 1. The handler + -- therefore seats unconditionally, from line 0. + local target = 1 + if request.selected_path then + for i, row in ipairs(state.rows) do + if row.path == request.selected_path then + target = i + break + end + end + end + -- If the captured path is gone --- the commonest case, since a file + -- that stopped being modified drops out of status --- `target` stays + -- 1 and nothing is said about it. That is the correct answer, not a + -- failure. + pmacs.editor.clear_selection() + pmacs.editor.set_view_top(0) + pmacs.editor.move_to_line(0) + -- Walked with `move_down` rather than a single `move_to_line(target)` + -- because motion is what drags the viewport along; a bare cursor set + -- would leave a long status list scrolled to the top with the cursor + -- off screen. This is `listview.refresh`'s own idiom. + for _ = 1, target do pmacs.editor.move_down() end +end + +-- The path of the row under the cursor right now, or nil. +local function selected_path() + local row = state.display[pmacs.editor.cursor_line()] + return row and row.path or nil +end + +local function start_status(root, expect_buffer, want_selection) + state.generation = state.generation + 1 + local request = { + generation = state.generation, + expect_buffer = expect_buffer, + selected_path = want_selection and selected_path() or nil, + } + run_git("git status", root, + { "status", "--porcelain=v2", "--branch", "-z" }, + function(res) pmacs.git._deliver_status(request, res) end) +end + +--- `g` inside the panel. +--- +--- `on_refresh` stays SYNCHRONOUS and honest: it returns the current +--- rows immediately with a marker appended and KICKS OFF the spawn, so +--- `g` always re-renders and always shows that work started. A `g` that +--- silently does nothing is a defect this primitive already names. +function pmacs.git._on_refresh() + if not git_enabled() then + return listview_rows("(git.enabled is false --- nothing was run)") + end + if not state.root then + return listview_rows("(no repository --- run M-x git.status)") + end + start_status(state.root, state.buffer, true) + return listview_rows("(refreshing...)") +end + +-- --------------------------------------------------------------------- +-- Entry point +-- --------------------------------------------------------------------- + +local function directory_of(path) + local dir = path:match("^(.*)/[^/]*$") + if dir == nil or dir == "" then return "/" end + return dir +end + +--- The directory a status run should resolve its repository from. +--- +--- The ACTIVE FILE's directory, falling back to the daemon's working +--- directory when the active buffer is pathless (a dired listing, the +--- scratch buffer). Deliberately not a project-marker walk: git resolves +--- its own worktree, and `ProjectKind` cannot answer the question at all. +local function active_directory() + local buf = pmacs.window.buffer() + if buf then + local ok, path = pcall(function() return buf:path() end) + if ok and type(path) == "string" and path ~= "" then + return directory_of(path) + end + end + local ok, id = pcall(pmacs.instance.identity) + if ok and type(id) == "table" and type(id.working_directory) == "string" then + return id.working_directory + end + return nil +end + +--- Open (or re-open) `*git-status*` for the repository containing the +--- active file. +function pmacs.git.status() + if not git_enabled() then + pmacs.editor.set_status("git: disabled by the `git.enabled` setting") + return + end + local dir = active_directory() + if not dir then + pmacs.editor.set_status("git: no directory to resolve a repository from") + return + end + -- The root rule (Q#G-2): ask git, and let a non-zero exit BE the + -- "not a repository" answer. `-C ` with no root of our own. + run_git("git rev-parse", nil, { "-C", dir, "rev-parse", "--show-toplevel" }, + function(res) + if not (res.ok and res.code == 0) then + if res.spawn_error then + pmacs.editor.set_status("git: " .. failure_reason(res)) + else + pmacs.editor.set_status(string.format("git: %s is not inside a repository", dir)) + end + return + end + local root = first_line(res.stdout) + if root == "" then + pmacs.editor.set_status("git: rev-parse returned no worktree root") + return + end + state.root = root + -- A fresh open carries no buffer expectation, so a panel the user + -- killed earlier does not make this run drop its own first result. + state.buffer = nil + start_status(root, nil, false) + end) +end + +pmacs.command.define { + name = "git.status", + description = "Show the working tree's Git status in a *git-status* panel.", + fn = pmacs.git.status, +} + +-- --------------------------------------------------------------------- +-- The diff gesture (Q#G-7) +-- --------------------------------------------------------------------- +-- +-- RET visits the file --- the behaviour a list of files should have --- +-- so the diff needs its own key, and `d` is it. +-- +-- What `d` shows answers the lane's own question, "what have I +-- changed?", against HEAD. A porcelain-v2 row carries an XY pair (X +-- staged, Y unstaged) and the three plausible diffs answer three +-- different questions: `git diff` shows only Y, `--cached` only X, and +-- neither shows an untracked file at all. One view of the TOTAL change +-- is right for reading; splitting X from Y is a staging UI, which is +-- Stage 3. +-- +-- `--no-color` because this renders as plain text and a user with +-- `color.ui = always` would otherwise get escape sequences in a buffer +-- with no ANSI parser behind it. + +local SPLIT_HEADER = + "no commits yet --- split view: staged (index) above, unstaged (worktree) below" + +-- The invocations `d` runs for `row`, as +-- `{ { label = string|nil, args = {...}, no_index = bool }, ... }`, +-- plus the header describing what the result shows. +local function diff_plan(row, unborn) + local path = row.path + if row.kind == "untracked" or row.kind == "ignored" then + -- A normal diff shows NOTHING for an untracked file. Without this + -- case `d` is silently dead on the rows a user is most likely to + -- press it on. + return { + header = "untracked --- shown against /dev/null", + steps = { { args = { "diff", "--no-color", "--no-index", "--", "/dev/null", path }, + no_index = true } }, + } + end + if not unborn then + if row.kind == "rename" and row.orig then + -- Both paths, which is what lets rename detection render this as + -- a rename rather than an unrelated add plus delete. + return { + header = string.format("against HEAD (renamed from %s)", + pmacs.git.display_path(row.orig)), + steps = { { args = { "diff", "--no-color", "HEAD", "--", row.orig, path } } }, + } + end + return { + header = "against HEAD", + steps = { { args = { "diff", "--no-color", "HEAD", "--", path } } }, + } + end + -- Unborn HEAD: there is nothing to total AGAINST, so the split + -- appears here and only here. `AM` and `AD` carry BOTH states at + -- once, which is exactly the gap: `--cached` alone loses the worktree + -- delta and plain `git diff` alone loses the staged base. + local staged = row.x ~= "." and row.x ~= " " + local unstaged = row.y ~= "." and row.y ~= " " + local cached = { label = "staged (index)", + args = { "diff", "--no-color", "--cached", "--", path } } + local worktree = { label = "unstaged (worktree)", + args = { "diff", "--no-color", "--", path } } + if staged and unstaged then + return { header = SPLIT_HEADER, steps = { cached, worktree } } + end + if staged then + return { header = "no commits yet --- staged (index) only", steps = { cached } } + end + return { header = "no commits yet --- unstaged (worktree) only", steps = { worktree } } +end + +local function diff_step_ok(step, res) + if not res.ok then return false end + -- `--no-index` implies `--exit-code`: exit 1 means it SUCCESSFULLY + -- found differences, which is the whole point of running it. Under a + -- plain "non-zero is failure" predicate every untracked diff would + -- render a failure row instead of the diff it just produced. The + -- asymmetry is confined to this invocation. + if step.no_index then return (res.code or 0) <= 1 end + return (res.code or 0) == 0 +end + +-- Ownership is the HANDLE this module holds, never a name match --- +-- listview's Q#GB13 rule and dired's F7 rule, for the same reason. +-- `pmacs.buffer.create` takes any caller-chosen name, so a user may +-- already have a buffer called `*git-diff*`; adopting it would clobber +-- their bytes and then lock the rope. A fresh create leaves theirs +-- untouched, and this module writes only to the handle it made. +local function show_diff_buffer(title, body) + local buf = state.diff_buffer + local live = buf ~= nil and select(2, pcall(buf.is_valid, buf)) == true + if not live then + buf = pmacs.buffer.create(DIFF_BUFFER) + pmacs.buffer.add_intercept(buf, function() + error(DIFF_BUFFER .. " is read-only") + end) + pmacs.buffer.set_round_trip_input(buf, true) + state.diff_buffer = buf + end + pmacs.buffer.set_generated_contents(buf, title .. "\n\n" .. body) + -- The DOCUMENT target, so the status panel it was invoked from stays + -- visible beside it. + pcall(pmacs.window.display, buf, { select = true }) +end + +-- Run the plan's steps in order, then render. +local function run_diff_plan(row, plan) + local pieces = {} + local index = 0 + local step_done + local function next_step() + index = index + 1 + local step = plan.steps[index] + if not step then + local body = table.concat(pieces, "\n") + if body:gsub("%s", "") == "" then + body = "(no differences)" + end + show_diff_buffer(string.format("git diff --- %s\n%s", + pmacs.git.display_path(row.path), plan.header), body) + return + end + run_git("git diff", state.root, step.args, function(res) step_done(step, res) end) + end + step_done = function(step, res) + if not diff_step_ok(step, res) then + local reason = failure_reason(res) + show_diff_buffer(string.format("git diff --- %s", + pmacs.git.display_path(row.path)), reason) + pmacs.editor.set_status("git diff: " .. reason) + return + end + local text = utf8_clean(res.stdout) + if step.label then + pieces[#pieces + 1] = string.format("=== %s ===\n%s", step.label, + text ~= "" and text or "(no changes)\n") + else + pieces[#pieces + 1] = text + end + next_step() + end + next_step() +end + +pmacs.command.define { + name = "git.diff-file", + description = "Show the diff for the file under the cursor in *git-status*.", + fn = function() + -- Bound buffer-locally on the panel, so `d` can only reach this + -- there; the identity check is for the `M-x` path. Compared against + -- the CAPTURED handle, never a name lookup: listview disambiguates + -- a collision to `<2>`, so the name is not the identity. + local active = pmacs.window.buffer() + if not (state.buffer and active and active == state.buffer) then + pmacs.editor.set_status("git: no *git-status* row here") + return + end + local row = state.display[pmacs.editor.cursor_line()] + if not (type(row) == "table" and row.path) then + pmacs.editor.set_status("git: no file on this line") + return + end + if not pmacs.git.is_text(row.path) + or (row.orig and not pmacs.git.is_text(row.orig)) then + refuse_unrepresentable(row) + return + end + if not git_enabled() then + pmacs.editor.set_status("git: disabled by the `git.enabled` setting") + return + end + run_diff_plan(row, diff_plan(row, (state.branch or {}).unborn == true)) + end, +} diff --git a/builtin/runtime/listview.lua b/builtin/runtime/listview.lua index a10cf57..bae8d14 100644 --- a/builtin/runtime/listview.lua +++ b/builtin/runtime/listview.lua @@ -25,6 +25,7 @@ -- rows = { { text = "src/foo.rs:12:4", item = }, ... }, -- on_visit = function(item) ... end, -- RET/SPC (optional) -- on_refresh = function() return rows end, -- g (optional) +-- keys = { d = "git.diff-file" }, -- extra buffer-local keys -- } pmacs.listview = pmacs.listview or {} @@ -263,19 +264,173 @@ local function seat_cursor(p, line) end end +-- The primitive's own key surface, named ONCE so the binder below and +-- the `keys` validator consult the same list. Previously this was a +-- sequence of `bind(...)` calls and the set existed nowhere as data, +-- which is why the git framing had to quote it from the source +-- (docs/git-integration-framing.md Q#G-7). +local FIXED_KEYS = { + { "RET", "listview.visit" }, + { "SPC", "listview.visit" }, + { "n", "cursor.down" }, + { "", "cursor.down" }, + { "p", "cursor.up" }, + { "", "cursor.up" }, + { "TAB", "listview.toggle" }, + { "g", "listview.refresh" }, + { "q", "listview.quit" }, +} + local function bind_local_keymap(buf) - local function bind(seq, command) - pmacs.keymap.bind { scope = "buffer", buffer = buf, sequence = seq, command = command } + for _, entry in ipairs(FIXED_KEYS) do + pmacs.keymap.bind { + scope = "buffer", buffer = buf, sequence = entry[1], command = entry[2], + } end - bind("RET", "listview.visit") - bind("SPC", "listview.visit") - bind("n", "cursor.down") - bind("", "cursor.down") - bind("p", "cursor.up") - bind("", "cursor.up") - bind("TAB", "listview.toggle") - bind("g", "listview.refresh") - bind("q", "listview.quit") +end + +-- --------------------------------------------------------------------- +-- Consumer-supplied keys (Q#G-7) +-- --------------------------------------------------------------------- +-- +-- An optional `keys = { = }` on the open +-- spec, bound through the SAME `pmacs.keymap.bind { scope = "buffer" }` +-- path as the fixed set above. It exists because a consumer cannot +-- safely bind its own key from outside: `open` disambiguates a name +-- collision to `<2>`, so the name a consumer passed is not necessarily +-- the buffer it got, and this module is the only place the handle is +-- known. No key is intercepted anywhere — COHERENCE.md §6's shadow +-- count is unchanged by this. +-- +-- INSTALL-ONCE, MATCH-ON-REOPEN. `Keymap::bind` refuses duplicates +-- (`KeymapError::DuplicateBinding`, "Refuse rather than silently +-- overwrite", src/keymap_tree.rs), and a consumer built on the async +-- completion model calls `open` again on EVERY refresh. So keys are +-- installed when the buffer is created and a later `open` for a live +-- panel does not re-bind — it COMPARES, and errors on divergence. +-- Silently keeping the old binding would hand the consumer a key that +-- does something other than what it just asked for, which is the dead- +-- or-lying-key defect this module already condemns for `g`. + +-- A key sequence's whitespace-separated chord tokens. That is exactly +-- how `parse_sequence` (src/key.rs) splits one, so a prefix relation +-- computed here is the same relation the trie would find. +local function chords_of(sequence) + local out = {} + for token in sequence:gmatch("%S+") do out[#out + 1] = token end + return out +end + +-- True when one chord list is a STRICT prefix of the other. Either +-- direction is a conflict: `Keymap` refuses both turning a leaf into a +-- submap (`WouldExtendLeaf`) and shadowing a submap with a leaf +-- (`WouldShadowSubmap`), and a `keys` table must not be able to reach +-- either. +local function prefix_conflict(a, b) + local short, long = a, b + if #a > #b then short, long = b, a end + if #short == 0 or #short == #long then return false end + for i = 1, #short do + if short[i] ~= long[i] then return false end + end + return true +end + +-- Normalize `keys` into a sorted array of `{ sequence, command }`. +-- Sorted so the comparison on reopen and every error message are +-- deterministic (`pairs` order is not). +local function normalized_keys(keys) + if keys == nil then return {} end + if type(keys) ~= "table" then + error(string.format( + "listview: `keys` must be a table of sequence -> command name; got %s", + type(keys))) + end + local out = {} + for sequence, command in pairs(keys) do + if type(sequence) ~= "string" or sequence == "" then + error("listview: every `keys` entry must be keyed by a non-empty key sequence") + end + if type(command) ~= "string" or command == "" then + error(string.format( + "listview: `keys[%q]` must be a command NAME (a non-empty string); got %s", + sequence, type(command))) + end + out[#out + 1] = { sequence = sequence, command = command } + end + table.sort(out, function(a, b) return a.sequence < b.sequence end) + return out +end + +-- Reject collisions BEFORE anything is created or bound, so a bad +-- `keys` table leaves no half-built panel behind. +local function check_key_collisions(entries) + for i, entry in ipairs(entries) do + local mine = chords_of(entry.sequence) + for _, fixed in ipairs(FIXED_KEYS) do + if entry.sequence == fixed[1] then + error(string.format( + "listview: `keys` may not rebind %q --- it is part of the panel's " + .. "own key surface (RET SPC n p TAB g q), bound to %q", + entry.sequence, fixed[2])) + end + if prefix_conflict(mine, chords_of(fixed[1])) then + error(string.format( + "listview: `keys` entry %q conflicts with the panel's own %q --- " + .. "one is a prefix of the other, which the keymap refuses rather " + .. "than turning a binding into a submap", + entry.sequence, fixed[1])) + end + end + for j = i + 1, #entries do + if prefix_conflict(mine, chords_of(entries[j].sequence)) then + error(string.format( + "listview: `keys` entries %q and %q conflict --- one is a prefix " + .. "of the other", entry.sequence, entries[j].sequence)) + end + end + end +end + +-- Bind the validated entries, rolling back on failure so a refusal the +-- structural check above could not predict (an alias spelling of a +-- chord, say) still leaves nothing half-installed. +local function install_keys(buf, entries) + local bound = {} + for _, entry in ipairs(entries) do + local ok, err = pcall(pmacs.keymap.bind, { + scope = "buffer", buffer = buf, + sequence = entry.sequence, command = entry.command, + }) + if not ok then + for _, sequence in ipairs(bound) do + pcall(pmacs.keymap.unbind, { scope = "buffer", buffer = buf, sequence = sequence }) + end + error(string.format( + "listview: cannot bind %q to %q: %s", + entry.sequence, entry.command, tostring(err))) + end + bound[#bound + 1] = entry.sequence + end +end + +local function keys_match(a, b) + if #a ~= #b then return false end + for i = 1, #a do + if a[i].sequence ~= b[i].sequence or a[i].command ~= b[i].command then + return false + end + end + return true +end + +local function render_keys(entries) + if #entries == 0 then return "none" end + local parts = {} + for i, entry in ipairs(entries) do + parts[i] = string.format("%s=%s", entry.sequence, entry.command) + end + return table.concat(parts, " ") end -- Build the persistent panel record for `name`. A user-killed panel @@ -293,9 +448,23 @@ end -- collision disambiguates `<2>`..`<99>`, and exhausting the limit raises -- rather than adopting --- the rule terminal.lua:300-305 states and -- dired.lua:476-504 already implements. -local function ensure_panel(name) +local function ensure_panel(name, key_entries) local p = panel_for_requested_name(name) - if p then return p end + if p then + -- Match-on-reopen (Q#G-7). A live panel keeps the keys it was + -- created with; a DIFFERENT table is a consumer asking for + -- something it will not get, so it is an error rather than a + -- silently ignored request. + if not keys_match(p.keys, key_entries) then + error(string.format( + "listview: %s is already open with keys [%s]; this open asks for " + .. "[%s]. Keys are installed once with the panel's buffer, so the " + .. "second table would be silently ignored --- close the panel " + .. "first, or pass the same keys", + name, render_keys(p.keys), render_keys(key_entries))) + end + return p + end local actual = name if find_buffer_by_name(actual) then @@ -315,8 +484,8 @@ local function ensure_panel(name) local buf = pmacs.buffer.create(actual) p = { requested_name = name, buffer = buf, line_to_item = {}, - line_to_row = {}, collapsed = {}, rows = {}, visible = 0 } - panels[#panels + 1] = p + line_to_row = {}, collapsed = {}, rows = {}, visible = 0, + keys = key_entries } -- Read-only (Q#P3): every non-bypass edit is rejected, with a NAMED -- error. Kept beside the rope lock, not replaced by it: the layering -- at terminal.lua:351-366 --- the rope lock protects the daemon copy, @@ -331,13 +500,23 @@ local function ensure_panel(name) -- is focused (RET = visit, not an optimistic newline). pmacs.buffer.set_round_trip_input(buf, true) bind_local_keymap(buf) + install_keys(buf, key_entries) + -- Registered LAST, deliberately: an `install_keys` failure must leave + -- no record claiming keys it did not bind. Nothing above needs the + -- panel to be in `panels` --- the intercept, the round-trip mark and + -- the keymap all address the buffer directly. + panels[#panels + 1] = p return p end function pmacs.listview.open(spec) assert(type(spec) == "table" and type(spec.name) == "string", "listview.open: spec.name (string) required") - local p = ensure_panel(spec.name) + -- Validated BEFORE `ensure_panel`, so a colliding `keys` table never + -- reaches buffer creation (Q#G-7: rejected at install time). + local key_entries = normalized_keys(spec.keys) + check_key_collisions(key_entries) + local p = ensure_panel(spec.name, key_entries) p.header = spec.header or spec.name p.on_visit = spec.on_visit p.on_refresh = spec.on_refresh diff --git a/docs/active-work.md b/docs/active-work.md index 08f1198..3f8cf13 100644 --- a/docs/active-work.md +++ b/docs/active-work.md @@ -265,7 +265,7 @@ also removed: this branch's "R8 NEEDS A LANE" investigation block, and durable facts are in the retired registry row and the handoff §6 census. -## Git integration Stage 1 — BRANCHED, pre-implementation +## Git integration Stage 1 — IMPLEMENTED, gates green, PR not opened **Written with the lane's first commit, before the PR exists** — the standing correction from #171 and #215. This session it was missed on @@ -300,7 +300,52 @@ authoritative tip** — the ref, not a SHA. Recover with refreshes not raising `DuplicateBinding`, and a non-UTF-8 path that parses and displays but **refuses** its gestures at the `String`-typed binding boundary. -- **Gates:** `scripts/gate --acceptance `. +- **Gates:** `scripts/gate --acceptance git_status_stage1_acceptance`. + +**Implemented.** `builtin/runtime/git.lua` (new, loaded after +`linewrap.lua`), the `keys` extension in `builtin/runtime/listview.lua`, +one chunk-load line in `src/editor.rs`, and +`tests/git_status_stage1_acceptance.rs` (25 tests, one per §6 bullet). +No `pmacs-protocol` change, no `PROTOCOL_VERSION` change, no +`DecorationKind` change — the no-wire property held. + +Five things worth carrying, all found by biting the suite rather than by +reading: + +- **`listview.open`'s `seat_cursor` walks DOWN from wherever the cursor + is**, on the premise that a fresh `switch_active_buffer` zeroed it. + Re-opening an already-displayed panel — which is exactly what the + async completion model does — zeroes nothing, so the walk lands one + row *below* the previous cursor. The completion handler seats + unconditionally from line 0 instead of trusting `open`. +- **A selection test that inserts ONE row above the selection is + vacuous**, because that accidental off-by-one lands on the right row. + The fixture inserts two. +- **`{:?}` on a Rust string containing NUL cannot build a `-z` fixture.** + Debug renders NUL as `\0`, and Lua's decimal escape swallows the + digits after it — so `\0` before a `1` record becomes + `string.char(1)` and the record merges into its predecessor. One test + passed while parsing nothing: the merged text landed in + `# branch.head`, the panel header rendered it, and a `contains` + assertion on the panel text was satisfied by the header. Payloads are + joined in Lua with `string.char(0)`. +- **A path may contain a newline, so a panel row must escape it.** + Parsing the bytes correctly and then writing them raw into a + one-row-per-line buffer desynchronizes every line-to-row map — and the + rope is UTF-8 by project invariant, so non-UTF-8 path bytes cannot go + in at all. Rows render `\xNN` escapes; the raw bytes stay on the + record, where the refusal check reads them. +- **Untracked rows sort AFTER every tracked row** in porcelain v2, so an + untracked file cannot be used to reorder a list above a selection. + +Two deliberate deviations from the framing's letter, both narrow: +`--no-color` on every diff invocation (a user with `color.ui = always` +would otherwise get escape sequences in a buffer with no ANSI parser +behind it), and `pmacs.git._program`, a module-local that the +missing-binary witness points at a name not on `PATH`. There is no other +in-process route to that branch: Rust's `Command` resolves the program +against the **parent** process's `PATH`, so a child `env` cannot hide +git, and `std::env::set_var` is `unsafe` in edition 2024. ## QoL arc retirement — PR #224 OPEN (docs only) diff --git a/docs/keybindings.md b/docs/keybindings.md index b08e4f4..181c637 100644 --- a/docs/keybindings.md +++ b/docs/keybindings.md @@ -231,12 +231,33 @@ all share one keymap: | `RET` / `SPC` | `listview.visit` — act on the item under the cursor | | `n` / `` | `cursor.down` | | `p` / `` | `cursor.up` | +| `TAB` | `listview.toggle` — collapse/expand the tree node under the cursor; a panel with no tree rows delegates to `buffer.tab` | | `g` | `listview.refresh` — re-run the data source and re-render | | `q` | `listview.quit` — restore the buffer that was active before the panel opened | +(`TAB` arrived with the tree primitive and this table had not recorded +it. Noted rather than quietly added: the omission predates the git lane +that found it.) + Panels currently built on this: `*references*`, `*outline*`, -`*lsp-help*` (hover docs). Header text always spells out the same -`RET`/`n`/`p`/`g`/`q` legend inline. +`*lsp-help*` (hover docs), `*lsp*` (`lsp.status`), and `*git-status*` +(`git.status`). Header text always spells out the panel's own legend +inline. + +A panel may add keys of its own through an optional `keys` table on the +open spec, bound through the same buffer-local path — so they are +inspectable by `describe-key` and rebindable from `init.lua`, exactly +like the fixed set. They are installed once with the panel's buffer and +may not collide with the fixed set, nor prefix it. One panel uses this +today: + +| Buffer | Key | Command | +|---|---|---| +| `*git-status*` (`git.status`) | `d` | `git.diff-file` — the diff for the file under the cursor, into `*git-diff*` | + +`git.status` gets **no global chord**: an opening key is a +command-surface decision the Stage 1 framing did not make, so the entry +point is `M-x git.status`. `*buffer-list*` (`editor.list-buffers`, `C-x C-b`) uses its own keymap, layered on the same idiom, in `builtin/commands/default.lua`: diff --git a/src/editor.rs b/src/editor.rs index 8d80a6c..a829714 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -769,6 +769,20 @@ impl EditorState { include_str!("../builtin/runtime/linewrap.lua"), ) .expect("load linewrap builtin chunk"); + // Git integration Stage 1 (docs/git-integration-framing.md): + // `*git-status*` and `*git-diff*`. Loaded after `listview.lua`, + // whose `open` (and whose new optional `keys` table) it drives, + // and after `window.lua`, which owns `window.panel-height` — the + // setting a `display = "panel"` listview resolves. It binds no + // global key: an opening chord is a command-surface decision and + // the framing did not make one, so the entry point is + // `M-x git.status`. + lua_host + .eval( + Some("@pmacs/builtin/runtime/git.lua"), + include_str!("../builtin/runtime/git.lua"), + ) + .expect("load git builtin chunk"); // T M7.11 bundled-package bootstrap. Through M7.10 the REPL // was loaded directly via `eval(include_str!(...))`; the // M7.11 deliverable migrates it to the package system so it diff --git a/tests/git_status_stage1_acceptance.rs b/tests/git_status_stage1_acceptance.rs new file mode 100644 index 0000000..26d861b --- /dev/null +++ b/tests/git_status_stage1_acceptance.rs @@ -0,0 +1,1485 @@ +//! Git integration Stage 1 acceptance — +//! `docs/git-integration-framing.md` §6, one test per verification +//! bullet. +//! +//! Dispatch-driven wherever a key is claimed: `RET`, `d`, `g`, `n`/`p` +//! and `q` are exercised through `dispatch_key`, never through +//! `pmacs.command.invoke`, so a dead binding fails these tests instead +//! of passing review. +//! +//! Repository fixtures are built with the REAL `git` in a tempdir, and +//! every one of them **bounds project detection** with +//! `pmacs.project.set_search_boundary`. That is not tidiness: R8 was a +//! fixture whose marker walk escaped into the developer's own +//! environment, retired two commits before this branch's base, and a +//! tempdir under `/tmp` is exactly the shape that reaches it. +//! +//! The pure `parse_*` half runs with **no repository at all** — the +//! separation `tests/fixtures/pmacs-magit/status.lua` already proves +//! works, and the reason this lane ported that separation rather than +//! inventing one (Q#G-0). + +use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers}; +use pmacs::editor::EditorState; +use pmacs::protocol::{CellSize, FrontendId}; +use std::path::{Path, PathBuf}; +use std::time::{Duration, Instant}; + +// --------------------------------------------------------------------------- +// Harness +// --------------------------------------------------------------------------- + +fn key(code: KeyCode, mods: KeyModifiers) -> KeyEvent { + KeyEvent { + code, + modifiers: mods, + kind: KeyEventKind::Press, + state: KeyEventState::empty(), + } +} + +fn press(s: &mut EditorState, code: KeyCode) { + s.dispatch_key(FrontendId::LOCAL, key(code, KeyModifiers::NONE)); +} + +fn exec(s: &EditorState, src: &str) { + s.lua_host.lua().load(src.to_string()).exec().unwrap(); +} + +fn eval(s: &EditorState, src: &str) -> T { + s.lua_host.lua().load(src.to_string()).eval().unwrap() +} + +fn status(s: &EditorState) -> String { + s.core.borrow().status.clone() +} + +fn errors_text(s: &EditorState) -> String { + s.lua_host.errors_buffer_text() +} + +fn active_name(s: &EditorState) -> String { + eval( + s, + "return pmacs.describe.buffer(pmacs.window.buffer()).name", + ) +} + +/// Text of the buffer named `name`, or `""` when absent. +fn named_text(s: &EditorState, name: &str) -> String { + let b: mlua::String = eval( + s, + &format!( + "for _, id in ipairs(pmacs.buffer.list()) do\n\ + if pmacs.describe.buffer(id).name == {name:?} then\n\ + return id:slice(0, id:len())\n\ + end\n\ + end\n\ + return \"\"" + ), + ); + String::from_utf8_lossy(&b.as_bytes()).into_owned() +} + +fn panel_text(s: &EditorState) -> String { + named_text(s, "*git-status*") +} + +fn diff_text(s: &EditorState) -> String { + named_text(s, "*git-diff*") +} + +/// Fresh editor with LSP spawning disabled and frame geometry declared. +/// +/// Geometry is not optional here: a listview opens into the PANEL by +/// default since bottom-panel Stage 3, and a panel is derived-hidden +/// while the frontend's frame size is unknown — so focus would fall +/// back to the document window and every assertion below would read the +/// wrong buffer. +fn editor() -> EditorState { + let s = EditorState::new_with_roots(&crate::iso::roots()); + exec(&s, "pmacs.lsp.config = {}"); + s.sync_frame_geometry(FrontendId::LOCAL, CellSize::new(24, 80)); + s +} + +/// Drive frames until `pred` holds, pumping the process supervisor — +/// the production `process.after-tick` path this module's pump rides. +fn pump_until( + s: &mut EditorState, + timeout_ms: u64, + mut pred: impl FnMut(&EditorState) -> bool, +) -> bool { + let stop = Instant::now() + Duration::from_millis(timeout_ms); + loop { + if pred(s) { + return true; + } + if Instant::now() >= stop { + return false; + } + s.tick_processes(); + std::thread::sleep(Duration::from_millis(5)); + } +} + +// --------------------------------------------------------------------------- +// Repository fixtures (real `git`, in a tempdir) +// --------------------------------------------------------------------------- + +fn git(root: &Path, args: &[&str]) -> String { + let out = std::process::Command::new("git") + .current_dir(root) + .args(args) + .output() + .unwrap_or_else(|e| panic!("running git {args:?}: {e}")); + assert!( + out.status.success(), + "git {args:?} failed: {}{}", + String::from_utf8_lossy(&out.stdout), + String::from_utf8_lossy(&out.stderr) + ); + String::from_utf8_lossy(&out.stdout).into_owned() +} + +/// `git init` plus the identity every commit needs, with the ambient +/// user's own config kept out of it. +fn init_repo(root: &Path) { + git(root, &["init", "-q", "-b", "main", "."]); + git(root, &["config", "user.email", "gate@example.invalid"]); + git(root, &["config", "user.name", "Gate"]); + // A developer's `commit.gpgsign = true` would make every fixture + // commit prompt or fail; a repo-local `false` outranks it. + git(root, &["config", "commit.gpgsign", "false"]); +} + +fn write(root: &Path, rel: &str, body: &str) { + let p = root.join(rel); + if let Some(parent) = p.parent() { + std::fs::create_dir_all(parent).expect("mkdir -p"); + } + std::fs::write(&p, body).expect("write fixture file"); +} + +/// A repository with **one row of every class `d` must answer**: +/// staged, unstaged, both, deleted, renamed, and untracked. Plus a +/// `Cargo.toml`, so `pmacs.project.detect` reports `rust` and NOT +/// `git` — the case a `kind == "git"` gate would have failed, and the +/// reason this module never asks pmacs whether something is a repo. +fn mixed_repo(root: &Path) { + init_repo(root); + write(root, "Cargo.toml", "[package]\nname = \"fixture\"\n"); + // Two tracked files that stay CLEAN, so they are absent from status + // until a test dirties them. `g6_11` needs to insert **two** rows + // above its selection: inserting one is satisfied by the accidental + // off-by-one that a missing re-seat produces, which is how that test + // was vacuous in its first form. + write(root, "a1.txt", "a1 base\n"); + write(root, "a2.txt", "a2 base\n"); + write(root, "staged.txt", "staged base\n"); + write(root, "unstaged.txt", "unstaged base\n"); + write(root, "both.txt", "both base\n"); + write(root, "deleted.txt", "deleted base\n"); + // Long enough that rename detection scores it at 100%. + write( + root, + "renamed_from.txt", + "a line of content long enough for rename detection to score it\n", + ); + git(root, &["add", "-A"]); + git(root, &["commit", "-qm", "init"]); + + write(root, "staged.txt", "staged base\nstaged edit\n"); + git(root, &["add", "staged.txt"]); + write(root, "unstaged.txt", "unstaged base\nworktree edit\n"); + write(root, "both.txt", "both base\nstaged edit\n"); + git(root, &["add", "both.txt"]); + write(root, "both.txt", "both base\nstaged edit\nworktree edit\n"); + std::fs::remove_file(root.join("deleted.txt")).expect("rm deleted.txt"); + git(root, &["mv", "renamed_from.txt", "renamed_to.txt"]); + write(root, "untracked.txt", "untracked body\n"); +} + +/// The unborn fixture, enumerated from a real unborn repository rather +/// than reasoned about: `git init`, stage three files, then edit one +/// (`AM`), delete one (`AD`), and `git mv` one — which produces an +/// ordinary `1 A.` add of the new path, never a `2` record. +fn unborn_repo(root: &Path) { + init_repo(root); + write(root, "am.txt", "am base\n"); + write(root, "ad.txt", "ad base\n"); + write(root, "r_orig.txt", "r base\n"); + git(root, &["add", "-A"]); + write(root, "am.txt", "am base\nworktree edit\n"); + std::fs::remove_file(root.join("ad.txt")).expect("rm ad.txt"); + git(root, &["mv", "r_orig.txt", "r_new.txt"]); + write(root, "untracked.txt", "untracked body\n"); +} + +/// Bound project detection to the fixture, open a file inside it so the +/// active buffer has a path there, and run `M-x git.status` to +/// completion. +/// +/// `set_search_boundary` is the R8 discipline: without it, detection +/// walks out of the tempdir and picks up whatever markers the developer +/// happens to have above `/tmp`. +fn open_panel(s: &mut EditorState, root: &Path, seed_file: &str) { + let root_str = root.display().to_string(); + let seed = root.join(seed_file).display().to_string(); + exec( + s, + &format!( + "pmacs.project.set_search_boundary({root_str:?})\n\ + pmacs.buffer.find_or_open({seed:?})" + ), + ); + exec(s, "pmacs.git.status()"); + assert!( + pump_until(s, 15_000, |s| !panel_text(s).is_empty()), + "the status panel must render; status was {:?}", + status(s) + ); +} + +/// The 1-based data line of the first panel row whose text contains +/// `needle`, or `None`. +fn row_line(s: &EditorState, needle: &str) -> Option { + panel_text(s) + .lines() + .enumerate() + .find(|(i, line)| *i > 0 && line.contains(needle)) + .map(|(i, _)| i) +} + +/// Seat the cursor on the row containing `needle` by pressing `n`, +/// which is listview's own binding — so a broken panel keymap fails +/// here rather than being stepped around. +fn seat_on(s: &mut EditorState, needle: &str) { + let target = row_line(s, needle) + .unwrap_or_else(|| panic!("no row matching {needle:?} in:\n{}", panel_text(s))); + let current: i64 = eval(s, "return pmacs.editor.cursor_line()"); + let current = usize::try_from(current).expect("cursor line fits"); + assert!( + current <= target, + "fixture: expected to walk down to {needle:?} (line {target}, cursor {current})" + ); + for _ in current..target { + press(s, KeyCode::Char('n')); + } + let now: i64 = eval(s, "return pmacs.editor.cursor_line()"); + assert_eq!( + usize::try_from(now).expect("cursor line fits"), + target, + "cursor must land on the {needle:?} row" + ); +} + +/// Pump frames for `ms` without waiting on anything — for the +/// assertions whose subject is that NOTHING happens. +fn pump_for(s: &mut EditorState, ms: u64) { + pump_until(s, ms, |_| false); +} + +/// Press `d` and pump until `*git-diff*` names `path_fragment`. +fn press_d_and_wait(s: &mut EditorState, path_fragment: &str) -> String { + let before = diff_text(s); + press(s, KeyCode::Char('d')); + assert!( + pump_until(s, 15_000, |s| { + let now = diff_text(s); + now != before && now.contains(path_fragment) + }), + "d must render a diff naming {path_fragment:?}; buffer was:\n{}\nstatus: {:?}", + diff_text(s), + status(s) + ); + diff_text(s) +} + +/// The all-zero object id porcelain v2 prints for an absent side of a +/// change. Module scope because clippy refuses an item after statements. +const H: &str = "0000000000000000000000000000000000000000"; + +/// A Lua EXPRESSION producing a `-z` status payload from `fields`. +/// +/// Deliberately assembled in Lua with `string.char(0)` rather than as a +/// Rust string interpolated with `{:?}`. Rust's `Debug` renders a NUL +/// as `\0`, and Lua's decimal escape then swallows the digits that +/// follow — so a `\0` immediately before a `1` record becomes +/// `string.char(1)` and that record silently merges into its +/// predecessor. The first draft of this suite did exactly that, and one +/// of its tests PASSED while parsing nothing at all: the merged text +/// landed in the `# branch.head` value, which the panel header then +/// rendered, and a `contains` assertion on the panel was satisfied by +/// the header. +fn z_payload(fields: &[&str]) -> String { + let quoted: Vec = fields.iter().map(|f| format!("{f:?}")).collect(); + format!( + "(table.concat({{ {} }}, string.char(0)) .. string.char(0))", + quoted.join(", ") + ) +} + +fn tempdir() -> (tempfile::TempDir, PathBuf) { + let dir = tempfile::tempdir().expect("tempdir"); + // Canonicalize: `git rev-parse --show-toplevel` reports a physical + // path, and `/tmp` is a symlink on some machines, so an + // uncanonicalized fixture root would not compare equal to the root + // the module resolves. + let root = std::fs::canonicalize(dir.path()).expect("canonicalize tempdir"); + (dir, root) +} + +// --------------------------------------------------------------------------- +// §6 — parsing, against a corpus rather than a case (Q#G-6) +// --------------------------------------------------------------------------- + +/// The pure parser, with **no repository**, over every row class the +/// framing's witness corpus names: modified, added, deleted, untracked, +/// renamed with BOTH paths, copied, a path with a space, and a path +/// with a **newline**. +/// +/// The newline case is what `-z` buys. A parser that passes only the +/// space case is the one that ships broken: without `-z` git C-quotes +/// such a path, and a hand-written unquoter is what this design exists +/// to avoid. +#[test] +fn g6_1_the_parser_covers_the_whole_v2_row_corpus() { + let s = editor(); + // Field layouts taken from real `git status --porcelain=v2 -z` + // output, not from the documentation: + // 1 + // 2 \0 + let fields = [ + "# branch.oid 16fa4d708a09af0c96212f66395c3e204049534a".to_string(), + "# branch.head main".to_string(), + format!("1 .M N... 100644 100644 100644 {H} {H} modified.txt"), + format!("1 A. N... 000000 100644 100644 {H} {H} added.txt"), + format!("1 .D N... 100644 100644 000000 {H} {H} deleted.txt"), + format!("2 R. N... 100644 100644 100644 {H} {H} R100 renamed_to.txt"), + "renamed_from.txt".to_string(), + format!("2 C. N... 100644 100644 100644 {H} {H} C085 copy_dst.txt"), + "copy_src.txt".to_string(), + format!("1 .M N... 100644 100644 100644 {H} {H} has space.txt"), + format!("1 .M N... 100644 100644 100644 {H} {H} new\nline.txt"), + "? untracked.txt".to_string(), + ]; + let refs: Vec<&str> = fields.iter().map(String::as_str).collect(); + + exec( + &s, + &format!("_G.PARSED = pmacs.git.parse_status({})", z_payload(&refs)), + ); + + // Rows are joined with RS (0x1E), not a newline: one of the paths + // in this corpus CONTAINS a newline, and splitting on `\n` would + // tear that very row in half — turning the case `-z` exists for + // into a test artefact. + let encoded: String = eval( + &s, + "local out = {}\n\ + for _, r in ipairs(_G.PARSED.rows) do\n\ + out[#out + 1] = table.concat({ r.kind, r.xy, r.path, r.orig or '-' }, '|')\n\ + end\n\ + return table.concat(out, string.char(30))", + ); + let rows: Vec<&str> = encoded.split('\u{1e}').collect(); + + assert_eq!( + rows, + vec![ + "ordinary|.M|modified.txt|-", + "ordinary|A.|added.txt|-", + "ordinary|.D|deleted.txt|-", + "rename|R.|renamed_to.txt|renamed_from.txt", + "rename|C.|copy_dst.txt|copy_src.txt", + "ordinary|.M|has space.txt|-", + "ordinary|.M|new\nline.txt|-", + "untracked|??|untracked.txt|-", + ], + "every corpus row must parse, and a rename/copy must carry BOTH \ + paths — the origin is the NEXT NUL-terminated field under -z, \ + not a tab-joined suffix" + ); + + let head: String = eval(&s, "return _G.PARSED.branch.head"); + assert_eq!(head, "main", "the --branch header is parsed too"); +} + +/// A path with a newline must not be able to split a panel row. +/// +/// This rides alongside the parse assertion above rather than instead +/// of it: parsing the byte correctly and then writing it raw into a +/// one-row-per-line buffer desynchronizes every line-to-row mapping in +/// the panel, which no parser test can see. +#[test] +fn g6_1b_a_newline_in_a_path_is_escaped_for_display() { + let s = editor(); + let shown: String = eval(&s, "return pmacs.git.display_path('new\\nline.txt')"); + assert_eq!(shown, "new\\x0Aline.txt"); + assert!( + !shown.contains('\n'), + "a rendered row must occupy exactly one line" + ); +} + +/// Unborn detection reads `# branch.oid (initial)` from the status +/// output already being parsed. Pinned so nobody later reintroduces a +/// second `rev-parse` process for a fact the first one hands over. +#[test] +fn g6_7_unborn_is_read_from_the_branch_oid_header() { + let s = editor(); + let unborn_payload = z_payload(&["# branch.oid (initial)", "# branch.head main"]); + let born_payload = z_payload(&["# branch.oid deadbeef", "# branch.head main"]); + let (unborn, born): (bool, bool) = eval( + &s, + &format!( + "return pmacs.git.parse_status({unborn_payload}).branch.unborn,\n\ + pmacs.git.parse_status({born_payload}).branch.unborn" + ), + ); + assert!(unborn, "`(initial)` is the unborn marker"); + assert!(!born, "a real oid is not"); +} + +// --------------------------------------------------------------------------- +// §6 — the non-UTF-8 boundary (Q#G-8) +// --------------------------------------------------------------------------- + +/// A non-UTF-8 path is **parsed and displayed**, and its gestures +/// **refuse with a message**. +/// +/// Not an end-to-end visit, and the framing does not claim one: +/// `pmacs.process.spawn` takes `args: Vec` and +/// `pmacs.buffer.find_or_open` takes `path: String`, both UTF-8 by +/// construction, and the rope is UTF-8 by project invariant. So the +/// witness is parse-and-display **plus the refusal** — a witnessed +/// refusal, not a stack trace and not a silent no-op. +#[test] +fn g6_2_a_non_utf8_path_parses_displays_and_refuses_its_gestures() { + use std::os::unix::ffi::OsStrExt; + + let (_dir, root) = tempdir(); + init_repo(&root); + write(&root, "ok.txt", "ok base\n"); + let bad = root.join(std::ffi::OsStr::from_bytes(b"bad\xff.txt")); + std::fs::write(&bad, b"bad base\n").expect("write non-utf8 path"); + git(&root, &["add", "-A"]); + git(&root, &["commit", "-qm", "init"]); + write(&root, "ok.txt", "ok base\nedit\n"); + std::fs::write(&bad, b"bad base\nedit\n").expect("edit non-utf8 path"); + + let mut s = editor(); + open_panel(&mut s, &root, "ok.txt"); + + // Displayed: the row is there, escaped, so the user is not lied to + // about what is modified. + let text = panel_text(&s); + assert!( + text.contains("bad\\xFF.txt"), + "the unrepresentable path must still appear as a row: {text}" + ); + + // RET refuses. + seat_on(&mut s, "bad\\xFF.txt"); + let before = active_name(&s); + press(&mut s, KeyCode::Enter); + assert!( + status(&s).contains("not valid UTF-8"), + "RET must report the refusal; status was {:?}", + status(&s) + ); + assert_eq!( + active_name(&s), + before, + "and must not have navigated anywhere" + ); + + // `d` refuses too, and renders no diff. + exec(&s, "pmacs.editor.set_status('')"); + press(&mut s, KeyCode::Char('d')); + assert!( + status(&s).contains("not valid UTF-8"), + "d must report the refusal; status was {:?}", + status(&s) + ); + pump_for(&mut s, 300); + assert!( + diff_text(&s).is_empty(), + "no *git-diff* buffer may appear for an unrepresentable path: {:?}", + diff_text(&s) + ); + + // The positive control: an ordinary neighbour still works, so the + // refusal above is about the path and not about the panel. + seat_on(&mut s, "ok.txt"); + let diff = press_d_and_wait(&mut s, "ok.txt"); + assert!(diff.contains("diff --git"), "control diff: {diff}"); +} + +// --------------------------------------------------------------------------- +// §6 — the invocation (Q#G-6) +// --------------------------------------------------------------------------- + +/// `--no-optional-locks` is asserted **structurally**, on the argv the +/// module really spawned. A lock that was not taken cannot be observed +/// directly, so the invocation is what gets pinned. +/// +/// The flag is part of the contract, not a nicety: `git status` may +/// refresh and write the index, and this module runs it asynchronously +/// from an editor while the user may be running git in a terminal. +#[test] +fn g6_3_every_invocation_carries_no_optional_locks() { + let (_dir, root) = tempdir(); + mixed_repo(&root); + let mut s = editor(); + open_panel(&mut s, &root, "staged.txt"); + + let log: Vec = eval( + &s, + "local out = {}\n\ + for _, args in ipairs(pmacs.git._spawn_log) do\n\ + out[#out + 1] = table.concat(args, ' ')\n\ + end\n\ + return out", + ); + assert!(!log.is_empty(), "the open must have spawned git"); + for argv in &log { + assert!( + argv.starts_with("--no-optional-locks"), + "every git argv must lead with --no-optional-locks; got {argv:?}" + ); + } + assert!( + log.iter() + .any(|a| a.contains("status --porcelain=v2 --branch -z")), + "the status invocation is the pinned one: {log:?}" + ); + + // And the diff path too, which is a separate call site. + seat_on(&mut s, "staged.txt"); + press_d_and_wait(&mut s, "staged.txt"); + let last: Vec = eval(&s, "return pmacs.git._last_spawn.args"); + assert_eq!( + last.first().map(String::as_str), + Some("--no-optional-locks"), + "the diff argv carries it as well: {last:?}" + ); +} + +// --------------------------------------------------------------------------- +// §6 — `d` on every row class (Q#G-7) +// --------------------------------------------------------------------------- + +/// `d` is witnessed on staged, unstaged, both, deleted, renamed **and +/// untracked** rows. +/// +/// Untracked is the load-bearing one: a normal `git diff` shows nothing +/// at all for an untracked file, so a missing `--no-index` case makes +/// `d` silently dead exactly where a user is most likely to press it. +#[test] +fn g6_4_d_answers_every_row_class() { + let (_dir, root) = tempdir(); + mixed_repo(&root); + let mut s = editor(); + open_panel(&mut s, &root, "staged.txt"); + + // Each row class, with a fragment only the RIGHT diff can contain. + // `both.txt` is asserted on BOTH halves: `git diff HEAD` is one view + // of the total change, so a `--cached`-only or plain-only answer + // would lose one of them and still look plausible. + for (needle, expect) in [ + ("staged.txt", &["+staged edit"][..]), + ("unstaged.txt", &["+worktree edit"][..]), + ("both.txt", &["+staged edit", "+worktree edit"][..]), + ("deleted.txt", &["-deleted base"][..]), + ("renamed_to.txt", &["rename from renamed_from.txt"][..]), + ("untracked.txt", &["+untracked body"][..]), + ] { + // `d` displays the diff in the DOCUMENT window, so focus has to + // come back to the panel before the next gesture. + refocus_panel(&mut s); + seat_on(&mut s, needle); + let diff = press_d_and_wait(&mut s, needle); + for fragment in expect { + assert!( + diff.contains(fragment), + "d on the {needle:?} row must render {fragment:?}; got:\n{diff}" + ); + } + assert!( + !diff.contains("exited with code"), + "…and not a failure body: {diff}" + ); + } +} + +/// Focus the `*git-status*` panel and seat the cursor on its first data +/// row, whatever `d` last displayed. +fn refocus_panel(s: &mut EditorState) { + exec( + s, + "for _, id in ipairs(pmacs.buffer.list()) do\n\ + if pmacs.describe.buffer(id).name == '*git-status*' then\n\ + pmacs.window.display(id, { side = 'bottom', select = true })\n\ + end\n\ + end\n\ + pmacs.editor.move_to_line(1)", + ); + assert_eq!( + active_name(s), + "*git-status*", + "the panel must be focused again before the next gesture" + ); +} + +/// The untracked diff renders **on exit 1**, not a failure row. +/// +/// `git diff --no-index` implies `--exit-code`: it exits 1 when it +/// SUCCESSFULLY finds differences. Under a plain "non-zero is failure" +/// predicate every untracked diff — the case `--no-index` exists to +/// serve — would render a failure instead of the diff it just produced. +/// +/// Measured rather than assumed: the same invocation run by hand +/// against a scratch repository exits 1 and prints the patch. +#[test] +fn g6_5_the_untracked_diff_renders_on_exit_one() { + let (_dir, root) = tempdir(); + mixed_repo(&root); + let mut s = editor(); + open_panel(&mut s, &root, "staged.txt"); + seat_on(&mut s, "untracked.txt"); + let diff = press_d_and_wait(&mut s, "untracked.txt"); + + // The invocation really was the `--no-index` one… + let last: Vec = eval(&s, "return pmacs.git._last_spawn.args"); + assert!( + last.iter().any(|a| a == "--no-index") && last.iter().any(|a| a == "/dev/null"), + "the untracked row must diff against /dev/null: {last:?}" + ); + // …and the same argv really does exit 1 here, so this test is about + // the predicate rather than about a git that happens to exit 0. + let out = std::process::Command::new("git") + .current_dir(&root) + .args([ + "--no-optional-locks", + "diff", + "--no-index", + "--", + "/dev/null", + "untracked.txt", + ]) + .output() + .expect("run the same invocation by hand"); + assert_eq!( + out.status.code(), + Some(1), + "fixture premise: --no-index exits 1 when it finds differences" + ); + + assert!( + diff.contains("+untracked body"), + "the patch must be rendered: {diff}" + ); + assert!( + !diff.contains("exited with code 1"), + "exit 1 must NOT be read as a failure here: {diff}" + ); +} + +// --------------------------------------------------------------------------- +// §6 — the unborn repository (Q#G-7b) +// --------------------------------------------------------------------------- + +/// An unborn repository, end to end, with an **`AM`** fixture — staged +/// then edited again, the shape a first commit actually has partway +/// through. +/// +/// `git init`, stage, edit, open the panel, press `d`, and get **two +/// labelled patches** with the split-view header — not +/// `fatal: bad revision 'HEAD'`, and not a single `--cached` patch that +/// silently drops the worktree edit. +#[test] +fn g6_6_an_unborn_am_row_renders_two_labelled_patches() { + let (_dir, root) = tempdir(); + unborn_repo(&root); + let mut s = editor(); + open_panel(&mut s, &root, "am.txt"); + + let text = panel_text(&s); + assert!( + text.contains("no commits yet"), + "the header names the unborn state: {text}" + ); + assert!(text.contains("AM am.txt"), "the AM row is present: {text}"); + + seat_on(&mut s, "am.txt"); + let diff = press_d_and_wait(&mut s, "am.txt"); + + assert!( + !diff.contains("bad revision"), + "`git diff HEAD` must never be attempted here: {diff}" + ); + assert!( + diff.contains( + "no commits yet --- split view: staged (index) above, unstaged (worktree) below" + ), + "the header must describe a SPLIT view, not a total against HEAD: {diff}" + ); + assert!( + diff.contains("=== staged (index) ===") && diff.contains("=== unstaged (worktree) ==="), + "two labelled patches: {diff}" + ); + assert!( + diff.contains("+am base"), + "the staged half carries the index patch: {diff}" + ); + assert!( + diff.contains("+worktree edit"), + "the unstaged half carries the worktree delta — the half a \ + --cached-only answer silently drops: {diff}" + ); +} + +/// `AD` rides the same fixture, since one repository can hold both, and +/// its second patch renders the deletion. +#[test] +fn g6_6b_an_unborn_ad_row_renders_the_deletion_in_its_second_patch() { + let (_dir, root) = tempdir(); + unborn_repo(&root); + let mut s = editor(); + open_panel(&mut s, &root, "am.txt"); + seat_on(&mut s, "ad.txt"); + let diff = press_d_and_wait(&mut s, "ad.txt"); + + assert!( + diff.contains("=== staged (index) ===") && diff.contains("=== unstaged (worktree) ==="), + "two labelled patches for AD too: {diff}" + ); + assert!( + diff.contains("deleted file mode"), + "the unstaged half renders the deletion: {diff}" + ); +} + +/// A single-state unborn row takes ONE patch, and says which one. +/// +/// Without this the split above could be an unconditional two-command +/// answer that happens to look right, and the `A.` row would carry a +/// header describing a view it is not. +#[test] +fn g6_6c_a_single_state_unborn_row_takes_one_patch_and_says_so() { + let (_dir, root) = tempdir(); + unborn_repo(&root); + let mut s = editor(); + open_panel(&mut s, &root, "am.txt"); + seat_on(&mut s, "r_new.txt"); + let diff = press_d_and_wait(&mut s, "r_new.txt"); + + assert!( + diff.contains("no commits yet --- staged (index) only"), + "the header must name the single state it shows: {diff}" + ); + assert!( + !diff.contains("=== unstaged (worktree) ==="), + "and there is no second patch to label: {diff}" + ); +} + +/// Rename/copy under an unborn `HEAD` is **unreachable**. +/// +/// The fixture `git mv`s a staged-but-uncommitted file and the parser +/// sees a `1 A.` record, never a `2`. Pinned so a future reader does not +/// "fix" the missing unborn rename policy by inventing one. +#[test] +fn g6_8_rename_under_an_unborn_head_is_unreachable() { + let (_dir, root) = tempdir(); + unborn_repo(&root); + let mut s = editor(); + open_panel(&mut s, &root, "am.txt"); + + let text = panel_text(&s); + assert!( + text.contains("A. r_new.txt"), + "the `git mv` produces an ordinary ADD of the new path: {text}" + ); + assert!( + !text.contains("<-"), + "and no rename row exists at all — with no HEAD there is nothing \ + to rename FROM: {text}" + ); + // Asserted against the REAL status output too, so a display-only + // change cannot make this pass. + let raw = git( + &root, + &[ + "--no-optional-locks", + "status", + "--porcelain=v2", + "--branch", + "-z", + ], + ); + exec(&s, &format!("_G.RAW = {raw:?}")); + let has_rename: bool = eval( + &s, + "for _, r in ipairs(pmacs.git.parse_status(_G.RAW).rows) do\n\ + if r.kind == 'rename' then return true end\n\ + end\n\ + return false", + ); + assert!( + !has_rename, + "the real unborn status output contains no `2` record" + ); +} + +// --------------------------------------------------------------------------- +// §6 — the `keys` lifecycle (Q#G-7) +// --------------------------------------------------------------------------- + +/// Two successive refreshes on a live panel: `d` still works and no +/// `DuplicateBinding` surfaces. +/// +/// This is the one that would have broken on **every** refresh. +/// `Keymap::bind` refuses duplicates and the completion model calls +/// `listview.open` again each time, so a naive `keys` implementation +/// errors on the second open. +#[test] +fn g6_9_two_refreshes_keep_d_working_and_raise_nothing() { + let (_dir, root) = tempdir(); + mixed_repo(&root); + let mut s = editor(); + open_panel(&mut s, &root, "staged.txt"); + + for round in 1..=2 { + let before: i64 = eval(&s, "return pmacs.git._generation()"); + press(&mut s, KeyCode::Char('g')); + assert!( + pump_until(&mut s, 15_000, |s| !panel_text(s).contains("refreshing")), + "refresh {round} must complete; panel:\n{}", + panel_text(&s) + ); + let after: i64 = eval(&s, "return pmacs.git._generation()"); + assert_eq!(after, before + 1, "refresh {round} really ran"); + assert!( + !status(&s).contains("already bound"), + "refresh {round} status: {:?}", + status(&s) + ); + } + + let errs = errors_text(&s); + assert!( + !errs.contains("already bound"), + "no DuplicateBinding may reach the errors buffer:\n{errs}" + ); + + // And `d` is still a live binding, not merely un-erroring. + seat_on(&mut s, "staged.txt"); + let diff = press_d_and_wait(&mut s, "staged.txt"); + assert!( + diff.contains("+staged edit"), + "d after two refreshes: {diff}" + ); +} + +/// A `keys` table colliding with the fixed set is rejected **at install +/// time**, as is a prefix conflict — in either direction. +#[test] +fn g6_10_a_colliding_or_prefixing_keys_table_is_rejected() { + let s = editor(); + + let fixed: String = eval( + &s, + "local ok, e = pcall(pmacs.listview.open, {\n\ + name = '*keys-a*', rows = {}, keys = { g = 'git.status' } })\n\ + return tostring(e)", + ); + assert!( + fixed.contains("own key surface"), + "rebinding `g` must be refused by name: {fixed}" + ); + + let extends: String = eval( + &s, + "local ok, e = pcall(pmacs.listview.open, {\n\ + name = '*keys-b*', rows = {}, keys = { ['g x'] = 'git.status' } })\n\ + return tostring(e)", + ); + assert!( + extends.contains("prefix"), + "`g x` would turn the `g` leaf into a submap: {extends}" + ); + + let internal: String = eval( + &s, + "local ok, e = pcall(pmacs.listview.open, {\n\ + name = '*keys-c*', rows = {},\n\ + keys = { ['d'] = 'git.status', ['d x'] = 'git.diff-file' } })\n\ + return tostring(e)", + ); + assert!( + internal.contains("prefix"), + "two `keys` entries may not prefix each other: {internal}" + ); + + // Nothing was created: a rejected table must leave no half-built + // panel behind, which is why validation runs before `ensure_panel`. + let names: Vec = eval( + &s, + "local out = {}\n\ + for _, id in ipairs(pmacs.buffer.list()) do out[#out+1] = pmacs.describe.buffer(id).name end\n\ + return out", + ); + for name in ["*keys-a*", "*keys-b*", "*keys-c*"] { + assert!( + !names.iter().any(|n| n == name), + "{name} must not exist after a rejected open: {names:?}" + ); + } +} + +/// Reopening a live panel with a DIFFERENT `keys` table errors rather +/// than silently keeping the old binding. +/// +/// Silently keeping it would hand the consumer a key that does +/// something other than what it just asked for — a dead or lying key, +/// which is the defect this primitive already condemns for `g`. +#[test] +fn g6_10b_reopening_with_different_keys_errors_instead_of_lying() { + let s = editor(); + exec( + &s, + "pmacs.listview.open { name = '*keys-d*', rows = {}, keys = { d = 'git.status' } }", + ); + let err: String = eval( + &s, + "local ok, e = pcall(pmacs.listview.open, {\n\ + name = '*keys-d*', rows = {}, keys = { d = 'git.diff-file' } })\n\ + return tostring(e)", + ); + assert!( + err.contains("already open with keys"), + "divergence must be reported, not ignored: {err}" + ); + // The same table reopens fine — otherwise the refresh path itself + // would be broken and this rule would be unusable. + exec( + &s, + "pmacs.listview.open { name = '*keys-d*', rows = {}, keys = { d = 'git.status' } }", + ); +} + +/// No new interaction island (COHERENCE.md §6): `d` is bound +/// **buffer-locally** through listview's own binding path, so +/// `describe-key` reports the truth and the key is rebindable from +/// `init.lua` — the observable difference between the keymap idiom and +/// a hardcoded shadow. +#[test] +fn g6_20_d_is_an_ordinary_buffer_local_binding() { + let (_dir, root) = tempdir(); + mixed_repo(&root); + let mut s = editor(); + open_panel(&mut s, &root, "staged.txt"); + + let (command, scope): (String, String) = eval( + &s, + "local info = pmacs.describe.key('d')\n\ + return info.command, info.scope", + ); + assert_eq!(command, "git.diff-file"); + assert!( + scope.starts_with("buffer"), + "`d` must live at BUFFER scope, not global: {scope}" + ); + + // Away from the panel it is not bound at all, which is what "no + // island" means: nothing intercepts `d` anywhere else. + exec( + &s, + "pmacs.window.switch_buffer(pmacs.buffer.create('*plain*'))", + ); + let elsewhere: Option = eval( + &s, + "local info = pmacs.describe.key('d')\n\ + return info and info.command or nil", + ); + assert_ne!( + elsewhere.as_deref(), + Some("git.diff-file"), + "`d` must not be bound outside the panel" + ); +} + +// --------------------------------------------------------------------------- +// §6 — refresh semantics (Q#G-1) +// --------------------------------------------------------------------------- + +/// `g` is **never a no-op**: it re-renders and marks that work started, +/// even mid-flight. A dead `g` is a defect this primitive already names. +#[test] +fn g6_16_g_marks_that_work_started_immediately() { + let (_dir, root) = tempdir(); + mixed_repo(&root); + let mut s = editor(); + open_panel(&mut s, &root, "staged.txt"); + assert!( + !panel_text(&s).contains("refreshing"), + "precondition: settled" + ); + + // No pumping: the marker must be there the instant `g` returns. + press(&mut s, KeyCode::Char('g')); + assert!( + panel_text(&s).contains("(refreshing...)"), + "g must re-render with a marker before any output arrives:\n{}", + panel_text(&s) + ); + // And again mid-flight, rather than being swallowed as "already + // running". + press(&mut s, KeyCode::Char('g')); + assert!( + panel_text(&s).contains("(refreshing...)"), + "a second g mid-flight still re-renders:\n{}", + panel_text(&s) + ); + + assert!( + pump_until(&mut s, 15_000, |s| !panel_text(s).contains("refreshing")), + "both refreshes settle" + ); +} + +/// Concurrent refresh **discards the stale generation** rather than +/// racing. +/// +/// Driven by completing two refreshes out of order, which no +/// arrangement of real subprocess timing can guarantee — so the +/// completion handler is called directly, with a request carrying the +/// older generation. The positive control at the end is what makes the +/// discard attributable to the generation and not to the payload. +#[test] +fn g6_17_a_stale_completion_discards_its_rows() { + let (_dir, root) = tempdir(); + mixed_repo(&root); + let mut s = editor(); + open_panel(&mut s, &root, "staged.txt"); + + let row = format!("1 .M N... 100644 100644 100644 {H} {H} SENTINEL.txt"); + let payload = z_payload(&["# branch.oid deadbeef", "# branch.head main", &row]); + + // A completion from one generation ago. + exec( + &s, + &format!( + "local current = pmacs.git._generation()\n\ + pmacs.git._deliver_status(\n\ + {{ generation = current - 1 }},\n\ + {{ ok = true, code = 0, stdout = {payload}, stderr = '' }})" + ), + ); + assert!( + !panel_text(&s).contains("SENTINEL.txt"), + "a stale completion must not reach the panel:\n{}", + panel_text(&s) + ); + + // The positive control: the same payload at the CURRENT generation + // does land, so the discard above was about the generation. + // + // Asserted on a ROW, not on the panel as a whole. The panel text + // includes the header, and a malformed payload can put arbitrary + // text there through `# branch.head` — which is precisely how the + // first draft of this test passed while parsing nothing. + exec( + &s, + &format!( + "pmacs.git._deliver_status(\n\ + {{ generation = pmacs.git._generation() }},\n\ + {{ ok = true, code = 0, stdout = {payload}, stderr = '' }})" + ), + ); + assert_eq!( + row_line(&s, "SENTINEL.txt"), + Some(1), + "the current generation must land as a ROW:\n{}", + panel_text(&s) + ); +} + +/// Selection is re-seated **by the completion handler**, across a +/// refresh that reorders rows. +/// +/// `listview.open` resets collapse and always seats line 1; it does not +/// preserve selection. Only `listview.refresh` does, and that is the +/// synchronous path this model cannot use — so the re-seating is owned +/// here, and this test is what says so. +#[test] +fn g6_11_selection_follows_the_path_across_a_reordering_refresh() { + let (_dir, root) = tempdir(); + mixed_repo(&root); + let mut s = editor(); + open_panel(&mut s, &root, "staged.txt"); + + seat_on(&mut s, "unstaged.txt"); + let line_before: i64 = eval(&s, "return pmacs.editor.cursor_line()"); + + // Dirty TWO tracked files that sort before it, so its line moves by + // two. Both details are load-bearing, and both were found by biting + // this test: + // + // * TRACKED, not a new untracked file — porcelain v2 emits every + // `1`/`2` record before any `?` record, so an untracked addition + // lands at the end and moves nothing at all; + // * TWO, not one — dropping the handler's re-seating entirely + // leaves the cursor exactly one row lower than it was, which a + // one-row insertion happens to match. + write(&root, "a1.txt", "a1 base\nedit\n"); + write(&root, "a2.txt", "a2 base\nedit\n"); + press(&mut s, KeyCode::Char('g')); + assert!( + pump_until(&mut s, 15_000, |s| !panel_text(s).contains("refreshing")), + "refresh must settle" + ); + + let line_after: i64 = eval(&s, "return pmacs.editor.cursor_line()"); + let row = panel_text(&s) + .lines() + .nth(usize::try_from(line_after).expect("line fits")) + .unwrap_or_default() + .to_string(); + assert!( + row.contains("unstaged.txt"), + "selection follows the PATH, not the line; landed on {row:?} in:\n{}", + panel_text(&s) + ); + assert_ne!( + line_before, line_after, + "fixture: the inserted row must have moved the selected one" + ); +} + +/// …and falls back to line 1 **without complaint** when the selected +/// path drops out of status — the common case, not an error, since a +/// file that stopped being modified simply leaves the list. +#[test] +fn g6_11b_a_vanished_selection_seats_line_one_silently() { + let (_dir, root) = tempdir(); + mixed_repo(&root); + let mut s = editor(); + open_panel(&mut s, &root, "staged.txt"); + + seat_on(&mut s, "untracked.txt"); + std::fs::remove_file(root.join("untracked.txt")).expect("rm untracked.txt"); + exec(&s, "pmacs.editor.set_status('')"); + press(&mut s, KeyCode::Char('g')); + assert!( + pump_until(&mut s, 15_000, |s| { + !panel_text(s).contains("refreshing") && !panel_text(s).contains("untracked.txt") + }), + "the refresh must settle without the vanished row:\n{}", + panel_text(&s) + ); + + let line: i64 = eval(&s, "return pmacs.editor.cursor_line()"); + assert_eq!(line, 1, "the cursor seats on the first data row"); + assert_eq!( + status(&s), + "", + "and nothing is reported — this is the correct answer, not a failure" + ); +} + +// --------------------------------------------------------------------------- +// §6 — the root rule (Q#G-2) and failure surfaces (§1.2) +// --------------------------------------------------------------------------- + +/// The root rule is witnessed on a repository whose `ProjectKind` is +/// **not** `Git`. +/// +/// `ProjectKind::Git` means a BARE repository — "no language marker +/// found inside" — and a language marker beside `.git` wins, so an +/// ordinary Rust project reports `kind = "rust"`. A lane gating on +/// `kind == "git"` would have been invisible on it, and on this +/// repository too. +#[test] +fn g6_14_the_root_rule_works_where_project_kind_is_not_git() { + let (_dir, root) = tempdir(); + mixed_repo(&root); + let root_str = root.display().to_string(); + + let mut s = editor(); + open_panel(&mut s, &root, "staged.txt"); + + let kind: String = eval( + &s, + &format!("return pmacs.project.detect({root_str:?}).kind"), + ); + assert_eq!( + kind, "rust", + "premise: the fixture's ProjectKind is NOT `git`, because \ + Cargo.toml sits beside .git" + ); + assert!( + panel_text(&s).contains("staged.txt"), + "and the panel resolved its root through git anyway:\n{}", + panel_text(&s) + ); + // The root really is the repository's, so a row's REPOSITORY-relative + // path resolves to a real file. Porcelain paths are relative to the + // repository root, not to the directory git ran in, so a root that + // was merely "some ancestor" would open nothing. + seat_on(&mut s, "unstaged.txt"); + press(&mut s, KeyCode::Enter); + let visited = active_name(&s); + assert_eq!( + visited, + root.join("unstaged.txt").display().to_string(), + "RET opens the file the row names, resolved against the git root; \ + status was {:?}", + status(&s) + ); +} + +/// A directory outside any repository reports it, rather than opening +/// an empty panel or saying nothing. +#[test] +fn g6_14b_a_directory_outside_a_repository_is_reported() { + let (_dir, root) = tempdir(); + write(&root, "loose.txt", "not in a repo\n"); + let root_str = root.display().to_string(); + let seed = root.join("loose.txt").display().to_string(); + + let mut s = editor(); + exec( + &s, + &format!( + "pmacs.project.set_search_boundary({root_str:?})\n\ + pmacs.buffer.find_or_open({seed:?})" + ), + ); + exec(&s, "pmacs.git.status()"); + assert!( + pump_until(&mut s, 15_000, |s| status(s) + .contains("not inside a repository")), + "the non-zero rev-parse exit IS the answer; status was {:?}", + status(&s) + ); + assert!( + panel_text(&s).is_empty(), + "and no panel is opened: {:?}", + panel_text(&s) + ); +} + +/// A missing `git` on `PATH` is witnessed, and surfaces **guidance** +/// rather than silence (§1.2's asymmetry; the same lesson #204 landed +/// for a missing language server). +/// +/// Reached by pointing `pmacs.git._program` at a name that is not on +/// `PATH`, which produces exactly the ENOENT an absent git produces. +/// There is no other in-process route: Rust's `Command` resolves the +/// program against the **parent** process's `PATH`, so a child `env` +/// cannot hide git, and `std::env::set_var` is `unsafe` in edition 2024 +/// — which this project forbids. +#[test] +fn g6_15_a_missing_git_binary_surfaces_guidance() { + let (_dir, root) = tempdir(); + mixed_repo(&root); + let root_str = root.display().to_string(); + let seed = root.join("staged.txt").display().to_string(); + + let mut s = editor(); + exec( + &s, + &format!( + "pmacs.project.set_search_boundary({root_str:?})\n\ + pmacs.buffer.find_or_open({seed:?})\n\ + pmacs.git._program = 'pmacs-no-such-git-binary'" + ), + ); + exec(&s, "pmacs.git.status()"); + // The spawn fails synchronously, so the guidance is already there; + // pumping anyway proves nothing later overwrites it with silence. + pump_for(&mut s, 200); + + let reported = status(&s); + assert!( + reported.contains("pmacs-no-such-git-binary") && reported.contains("PATH"), + "the failure must name the program and point at PATH; got {reported:?}" + ); + assert!( + panel_text(&s).is_empty(), + "and no panel is opened on a failed root resolution: {:?}", + panel_text(&s) + ); +} + +/// A failing `git status` renders a **row**, carrying the exit code and +/// the first line of stderr, plus a status message. +/// +/// Driven by removing `.git` under a live panel and pressing `g`: the +/// panel already holds its root, so the refresh really does run and +/// really does fail. +#[test] +fn g6_18_a_failing_status_renders_a_row_with_code_and_stderr() { + let (_dir, root) = tempdir(); + mixed_repo(&root); + let mut s = editor(); + open_panel(&mut s, &root, "staged.txt"); + + std::fs::remove_dir_all(root.join(".git")).expect("rm -rf .git"); + press(&mut s, KeyCode::Char('g')); + assert!( + pump_until(&mut s, 15_000, |s| panel_text(s) + .contains("exited with code")), + "the failure must become a ROW, not a silence; panel:\n{}\nstatus: {:?}", + panel_text(&s), + status(&s) + ); + + let text = panel_text(&s); + assert!( + text.contains("exited with code 128"), + "the row carries the exit code: {text}" + ); + assert!( + text.contains("not a git repository"), + "…and the first stderr line: {text}" + ); + assert!( + status(&s).contains("git status:"), + "…and a status message rides with it: {:?}", + status(&s) + ); +} + +// --------------------------------------------------------------------------- +// §6 — structural claims +// --------------------------------------------------------------------------- + +/// The panel **is** a `listview` adopter, asserted structurally, so a +/// future re-implementation of list behaviour inside git code fails the +/// test rather than passing review. +/// +/// Structural alone would not be enough — a comparison of two +/// authorities does not catch a misrouted consumer — so the behavioural +/// half rides with it: `n`, `p` and `q` are listview's own bindings and +/// are driven through `dispatch_key`. +#[test] +fn g6_19_the_panel_is_a_listview_adopter() { + const GIT: &str = include_str!("../builtin/runtime/git.lua"); + + assert!( + GIT.contains("pmacs.listview.open"), + "the panel must go through the primitive" + ); + let creates: Vec<&str> = GIT + .lines() + .filter(|l| !l.trim_start().starts_with("--")) + .filter(|l| l.contains("pmacs.buffer.create")) + .collect(); + assert_eq!( + creates.len(), + 1, + "the ONLY buffer this module creates itself is *git-diff*; the \ + status panel is listview's. Found: {creates:?}" + ); + assert!( + creates[0].contains("DIFF_BUFFER"), + "and it is the diff buffer: {creates:?}" + ); + let binds: Vec<&str> = GIT + .lines() + .filter(|l| !l.trim_start().starts_with("--")) + .filter(|l| l.contains("pmacs.keymap.bind")) + .collect(); + assert!( + binds.is_empty(), + "`d` is bound through listview's `keys` table, never by this \ + module reaching for the keymap itself: {binds:?}" + ); + // No second `rev-parse` for unborn detection: that fact comes from + // the status output already being parsed. Checked on CODE lines + // only — the module's own comment explains the rule and names the + // process it forbids, and a substring sweep would trip on it. + let verifies: Vec<&str> = GIT + .lines() + .filter(|l| !l.trim_start().starts_with("--")) + .filter(|l| l.contains("--verify")) + .collect(); + assert!( + verifies.is_empty(), + "unborn detection must not reintroduce `rev-parse --verify HEAD`: {verifies:?}" + ); + assert!( + GIT.contains("(initial)"), + "…it reads `# branch.oid (initial)` instead" + ); + + let (_dir, root) = tempdir(); + mixed_repo(&root); + let mut s = editor(); + open_panel(&mut s, &root, "staged.txt"); + + let start: i64 = eval(&s, "return pmacs.editor.cursor_line()"); + press(&mut s, KeyCode::Char('n')); + let down: i64 = eval(&s, "return pmacs.editor.cursor_line()"); + assert_eq!(down, start + 1, "`n` is listview's own motion"); + press(&mut s, KeyCode::Char('p')); + let up: i64 = eval(&s, "return pmacs.editor.cursor_line()"); + assert_eq!(up, start, "`p` too"); + press(&mut s, KeyCode::Char('q')); + assert_ne!( + active_name(&s), + "*git-status*", + "`q` leaves the panel, through listview's quit" + ); +} + +/// `git.enabled` is a real config-registry setting (Q#G-4), and turning +/// it off means nothing is spawned — reported, not silently inert. +#[test] +fn g6_config_git_enabled_is_registry_defined_and_honoured() { + let (_dir, root) = tempdir(); + mixed_repo(&root); + let root_str = root.display().to_string(); + let seed = root.join("staged.txt").display().to_string(); + + let mut s = editor(); + let (default, kind, described): (bool, String, bool) = eval( + &s, + "local d = pmacs.config.describe('git.enabled')\n\ + return pmacs.config.get('git.enabled'), d.type, #d.description > 0", + ); + assert!(default, "default is true"); + assert_eq!(kind, "boolean"); + assert!(described, "the registry carries a real description"); + + exec( + &s, + &format!( + "pmacs.project.set_search_boundary({root_str:?})\n\ + pmacs.buffer.find_or_open({seed:?})\n\ + pmacs.config.set('git.enabled', false)\n\ + pmacs.git._spawn_log = {{}}" + ), + ); + exec(&s, "pmacs.git.status()"); + pump_for(&mut s, 300); + assert!( + status(&s).contains("git.enabled"), + "the refusal names the setting; got {:?}", + status(&s) + ); + let spawned: i64 = eval(&s, "return #pmacs.git._spawn_log"); + assert_eq!(spawned, 0, "and nothing was spawned"); + assert!(panel_text(&s).is_empty(), "and no panel opened"); +} + +// Isolated bootstrap storage roots: an integration test is compiled +// without `cfg(test)`, so a raw `EditorState::new()` would read the +// developer's real `init.lua` and write into their real data root. +#[path = "common/iso.rs"] +mod iso;