computer icon indicating copy to clipboard operation
computer copied to clipboard

git status renders a fully staged deletion as `DD` (porcelain's unmerged both-deleted conflict code) instead of `D `

Open rajpreetcodes opened this issue 3 months ago β€’ 5 comments

Describe the bug

workspace.git.cli(["status", "--short"]) (and --porcelain / --porcelain=v2, and the worker-shell git command) reports a fully staged deletion with worktree code D, producing DD <path>. In porcelain v1, DD specifically means an unmerged "both deleted" merge conflict; in porcelain v2, DD on a 1 line is not a producible state at all (unmerged entries use u lines).

Cause: packages/computer/src/git/status.ts:111, first branch of worktreeCode:

if (workdir === 0 && stage === 0 && head === 1) return "D";

For the status-matrix row [path, 1, 0, 0] (present in HEAD, absent from workdir, absent from index) the workdir matches the index (the deletion is fully staged), so Y (workdir vs index) must be a space. The rendered forms come out of formatShort / formatPorcelainV1 (status.ts:152-173) and formatPorcelainV2 (status.ts:127-144).

The existing suite covers D (worktree delete, unstaged) but never the staged-deletion row [path,1,0,0].

Expected behavior

Real git, for git rm f && git status --porcelain: D f (X=D, Y=space).

Steps to reproduce

Same harness as packages/computer/src/git/status.test.ts: commit gone.txt, unlink it from the worktree, stage the deletion via the package's own git add -A path (addWith({ all: true }) in staging.ts), then run statusWith. Actual output:

statusMatrix: [["gone.txt",1,0,0]]        <- in HEAD, absent workdir, absent index
entries:  [{"path":"gone.txt","index":"D","worktree":"D"}]
short:    "DD gone.txt\n"
v1:       "DD gone.txt\n"
v2:       "1 DD gone.txt\n"

Expected: "D gone.txt\n" and {index:"D", worktree:" "}.

Impact

Any staged deletion (git rm, git add -A after a delete, commit -a flows) is misreported to porcelain parsers and to agents reading git status as a merge conflict. An agent driving the workspace via the shell backend will try to "resolve" conflicts that don't exist.

Proposed fix

In worktreeCode, the head === 1 && workdir === 0 && stage === 0 row should return " ", leaving the second branch (workdir === 0 && stage > 0, deletion not yet staged) as the only producer of Y=D. One-line change; add a matrix-row test for [p,1,0,0] asserting {index:"D", worktree:" "}.

Environment

  • cloudflare/computer at 76d9e75 (current main), local checkout
  • npx vitest run baseline in packages/computer: 897/897 pass before adding the repro
  • Node v22.18.0, Windows 11 (platform-independent)

rajpreetcodes avatar Aug 05 '26 20:08 rajpreetcodes

ε·₯具调用倱θ΄₯ζ€ŽδΉˆε›žζ»šηŠΆζ€ πŸ§ͺ

harrisonmichael809 avatar Aug 05 '26 21:08 harrisonmichael809

πŸ› οΈ GPU ζ˜Ύε­˜δΈε€Ÿζ—Άζœ‰ι‡εŒ–ζ–Ήζ‘ˆε—

wellsashley00 avatar Aug 05 '26 22:08 wellsashley00

Confirmed at 76d9e75, and I have a patch with a failing-first test if it's useful.

Root cause. worktreeCode (packages/computer/src/git/status.ts:110), first branch at :111. For a fully staged deletion isomorphic-git's row is [path, 1, 0, 0] β€” gone from the working tree and gone from the index β€” so the workdir matches the index and Y must be a space. That branch returns "D" instead. The second branch (workdir === 0 && stage > 0, :112) already covers the only legitimate Y=D case.

Two things beyond what the issue reports:

  1. It is not just git status. All three formatters concatenate index + worktree β€” formatShort (:166), formatPorcelainV1 (:152), formatPorcelainV2 (:127) β€” so --short, porcelain v1, and porcelain v2 were all emitting DD.
  2. DD was never a legitimate output of this module. There is no unmerged/conflict handling anywhere in packages/computer/src/git β€” StatusEntry.index/worktree (:59-63) have no U variant. So rather than needing to disambiguate a real both-deleted conflict at render time, the fix makes DD unproducible, which is correct.

Verified matrix (init β†’ commit β†’ unlink β†’ git.remove):

state row X Y rendered
deleted, not staged [1,0,1] " " D " D" βœ“ already correct
deletion fully staged [1,0,0] D D DD βœ—

Fix is one line at :111, return "D" β†’ return " ", plus the comment block above it. Test added to status.test.ts asserting the entry shape and all three formatter outputs; it fails at this sha with worktree: "D" / "DD gone.txt\n" and passes after. Whole src/git suite: 303 passing.

Branch: https://github.com/3x3xX3N0N/computer/tree/fix/git-status-staged-delete β€” not opening a PR, per CONTRIBUTING.md.

3x3xX3N0N avatar Aug 06 '26 01:08 3x3xX3N0N

Independently reproduced against main, with a fix and a passing test.

Root cause. worktreeCode in packages/computer/src/git/status.ts returns "D" for the status-matrix row [path, head=1, workdir=0, stage=0]. That row is a deletion that is already fully staged: the file is gone from both the workdir and the index, so they agree with each other, and the Y (workdir-vs-index) column should be unmodified. The second branch right below it (workdir === 0 && stage > 0) already covers the only case that should produce a Y of D: workdir gone, stage not yet updated to match.

Combined with indexCode returning "D" for the X column on the same row (head present, stage absent), the entry renders as DD β€” porcelain's code for an unmerged both-deleted conflict β€” for an ordinary git rm / staged delete.

Repro (added as a test in status.test.ts, using the existing memfs + isomorphic-git harness):

await init();
await commit("gone.txt", "x\n", "init");
await memfs.promises.unlink(`${DIR}/gone.txt`);
await git.remove({ fs: memfs, dir: DIR, filepath: "gone.txt" });
expect(await statusWith({ git: isogit, fs: memfs, dir: DIR })).toEqual([
  { path: "gone.txt", index: "D", worktree: " " },
]);

Fails at current main with worktree: "D" (i.e. DD) instead of worktree: " " (i.e. D ).

Fix, one line in worktreeCode:

 function worktreeCode(head: number, workdir: number, stage: number): StatusEntry["worktree"] {
-  if (workdir === 0 && stage === 0 && head === 1) return "D";
+  if (workdir === 0 && stage === 0 && head === 1) return " ";
   if (workdir === 0 && stage > 0) return "D";
   if (head === 0 && stage === 0 && workdir > 0) return "?";

Verification. With the fix: the new test passes, and the full @cloudflare/computer suite passes at 898/898 (897 baseline + the new test). npx tsc -p tsconfig.build.json --noEmit is clean. No other tests exercise this row, so nothing else was masking or depending on the old behavior.

Happy to share the full diff (touches status.ts and status.test.ts only, six lines changed plus the new test) if that's useful β€” not opening a pull request per CONTRIBUTING.md.

kundankumaryadav88 avatar Aug 06 '26 03:08 kundankumaryadav88

Both confirmations match what I see at 76d9e75. The all-three-formatters point checks out: formatShort, formatPorcelainV1, and formatPorcelainV2 each concatenate index + worktree, so every render path emitted DD. Same for the no-unmerged-variant observation, StatusEntry has no U anywhere, which makes the one-liner a correctness fix rather than a disambiguation question. Two independent patches landing on the same line should make this easy to triage.

rajpreetcodes avatar Aug 06 '26 11:08 rajpreetcodes