mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-18 16:08:55 +00:00
fix(vcs): the staging area commits a staged addition deleted from disk (#3740)
A file added to the index and then deleted from disk is no change against HEAD, so the change lists leave it out; the staging area applied the same rule and listed it nowhere, keeping Commit disabled or silently leaving the staged addition out of the commit. With the staging area on, it is now listed under Staged (added) and Unstaged (deleted) and committed as staged.
This commit is contained in:
@@ -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);
|
||||
});
|
||||
|
||||
@@ -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<string | undefined> {
|
||||
});
|
||||
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) {
|
||||
|
||||
@@ -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<FileStatus, "staged" | "unstaged">): 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<GroupId, FileStatus[]>();
|
||||
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<st
|
||||
const plan: CommitPlan = { stage: [], exclude: [], included: [], partial: [] };
|
||||
for (const fs of status) {
|
||||
if (!groupOf(fs)) {
|
||||
// Listed nowhere, yet staged: added to the index, then deleted from disk. It is no
|
||||
// change against HEAD, so the commit must not pick up the staged addition.
|
||||
// Added to the index, then deleted from disk: no change against HEAD, so the commit
|
||||
// leaves the staged addition out, unless the staging area commits it (checked "staged").
|
||||
if (addedThenDeleted(fs) && (checks.get(fs.path) ?? checks.get(stripSlash(fs.path))) === "staged") {
|
||||
plan.included.push(fs);
|
||||
continue;
|
||||
}
|
||||
if (fs.staged && fs.staged !== "conflicted" && fs.unstaged !== "ignored") plan.exclude.push(stripSlash(fs.path));
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
*/
|
||||
import type { ChangelistState, FileStatus } from "@/bindings";
|
||||
import { useWorkflowSettings, workflowSettings } from "@/platform/workflowSettings";
|
||||
import { groupOf, type CheckState } from "./changes";
|
||||
import { addedThenDeleted, groupOf, type CheckState } from "./changes";
|
||||
import { withListDefaults } from "./changelists";
|
||||
|
||||
const enabled = (vcs: { git: { stagingArea: boolean }; commit: { nonModal: boolean } }) =>
|
||||
@@ -31,6 +31,8 @@ export function stagingChecks(status: readonly FileStatus[]): Map<string, CheckS
|
||||
const all = isCommitAll(status);
|
||||
const out = new Map<string, CheckState>();
|
||||
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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user