diff --git a/src/features/vcs/__tests__/stagingArea.test.tsx b/src/features/vcs/__tests__/stagingArea.test.tsx index 5aa9e45b4..df87bf93a 100644 --- a/src/features/vcs/__tests__/stagingArea.test.tsx +++ b/src/features/vcs/__tests__/stagingArea.test.tsx @@ -59,6 +59,26 @@ describe("staging area inclusion", () => { expect(plan.stage).toEqual(["/p/src/Unstaged.kt", "/p/src/Gone.kt"]); }); + it("lists and commits a staged addition deleted from disk; the change lists leave it out", () => { + const gone = file("src/New.txt", "added", "deleted"); + const status = [gone, unstagedOnly]; + expect(isCommitAll(status)).toBe(false); + const plan = planCommit(status, stagingChecks(status)); + expect(plan.included.map((f) => f.relPath)).toEqual(["src/New.txt"]); + expect(plan.exclude).toEqual([]); + // Staged shows the addition, Unstaged the deletion. + const tree = buildChangeTree(status, "/p", false, undefined, [], true); + const kinds = (group: string) => + tree.find((n) => n.group === group)?.children?.flatMap((n) => (n.label === "New.txt" ? [n.kind] : [])); + expect(kinds("staged")).toEqual(["added"]); + expect(kinds("unstaged")).toEqual(["deleted"]); + // Without the staging area it is no change against HEAD: listed nowhere, never committed. + expect(buildChangeTree(status, "/p", false).flatMap((n) => n.files.map((f) => f.relPath))).toEqual([ + "src/Unstaged.kt", + ]); + expect(planCommit(status, new Map([[unstagedOnly.path, "all" as const]])).exclude).toEqual(["/p/src/New.txt"]); + }); + it("is not Commit All when only unversioned files changed", () => { expect(isCommitAll([untracked])).toBe(false); }); diff --git a/src/features/vcs/changeOps.tsx b/src/features/vcs/changeOps.tsx index 7df28b90f..fd9ebb35c 100644 --- a/src/features/vcs/changeOps.tsx +++ b/src/features/vcs/changeOps.tsx @@ -4,7 +4,7 @@ import type { FileStatus, Stash } from "@/bindings"; import { call, commands } from "@/platform/ipc"; import { Checkbox, Dialog, Icon, IconButton, Input, T, fileIconName } from "@/ui"; import { notifications } from "@/workbench/contributions"; -import { groupOf, remainingOverrides, renamedFromPath } from "./changes"; +import { addedThenDeleted, groupOf, remainingOverrides, renamedFromPath } from "./changes"; import { runCommitChecks } from "./commitChecks"; import { contributedCheckProblems, currentPlan, selectionOf } from "./commitSelection"; import { forgetAmendDraft, keepMessageAfterCommit, rememberMessage, useCommit } from "./commitStore"; @@ -54,7 +54,10 @@ export async function commitChanges(): Promise { }); return undefined; } - const candidates = plan.included.map((f) => ({ path: stripSlash(f.path), exists: effectiveKind(f) !== "deleted" })); + const candidates = plan.included.map((f) => ({ + path: stripSlash(f.path), + exists: effectiveKind(f) !== "deleted" && !addedThenDeleted(f), + })); // IDEA: failed checks show in the commit panel; the next Commit ("Commit Anyway") skips them. const checked = !useCommit.getState().checkProblems.length; if (checked) { diff --git a/src/features/vcs/changes.ts b/src/features/vcs/changes.ts index 00c71d67e..8bdf2e24c 100644 --- a/src/features/vcs/changes.ts +++ b/src/features/vcs/changes.ts @@ -57,6 +57,14 @@ export function groupOf(fs: FileStatus): GroupId | null { return "changes"; } +/** + * Added to the index, then deleted from disk: no change against HEAD, so the change list leaves + * it out, but the staging area shows the staged addition (and the deletion, unstaged). + */ +export function addedThenDeleted(fs: Pick): boolean { + return fs.staged === "added" && fs.unstaged === "deleted"; +} + export function isPartiallyStaged(fs: FileStatus): boolean { return ( !!fs.staged && @@ -99,7 +107,9 @@ const KIND_ORDER: GroupId[] = ["conflicts", "changes", "unversioned"]; function fileNode(fs: FileStatus, root: string, group: GroupId, flat: boolean, scope: string = group): ChangeNode { const path = stripSlash(fs.path); const rel = relTo(root, path); - const kind = effectiveKind(fs) ?? "modified"; + // Under Staged / Unstaged, a file without an overall kind (added, then deleted) shows its side's. + const side = group === "staged" ? fs.staged : group === "unstaged" ? fs.unstaged : null; + const kind = effectiveKind(fs) ?? (side === "added" || side === "deleted" ? side : "modified"); const dir = dirName(rel); return { key: `${scope}:${path}`, @@ -259,7 +269,7 @@ export function buildChangeTree( const repoRoots = byRepo ? repos : []; const byGroup = new Map(); for (const fs of status) { - const g = groupOf(fs); + const g = groupOf(fs) ?? (byStaging && addedThenDeleted(fs) ? "changes" : null); if (!g) continue; let list = byGroup.get(g); if (!list) byGroup.set(g, (list = [])); @@ -400,8 +410,12 @@ export function planCommit(status: readonly FileStatus[], checks: ReadonlyMap @@ -31,6 +31,8 @@ export function stagingChecks(status: readonly FileStatus[]): Map(); for (const f of status) { + // A staged addition deleted from disk is committed as staged (nothing against HEAD for Commit All). + if (addedThenDeleted(f) && !all) out.set(f.path, "staged"); if (groupOf(f) !== "changes") continue; out.set(f.path, all ? "all" : staged(f) ? "staged" : "none"); }