fix(changes): Discard takes only the ticked files and blocks (#87)
The Changes panel's "Discard all" threw away every changed file, ticked or not. It is now "Discard (n)": the same selection Commit sends — whole ticked files as `paths`, a partly ticked file's ticked blocks as `hunks` — and is disabled with nothing ticked. The server's `discard` accepts `hunks` (`selectionPatch` in reverse, `git apply -R`), building every patch before applying any so a stale selection undoes nothing. Closes #87
This commit is contained in:
@@ -851,6 +851,51 @@ describe("runGitAction — one change block inside a hunk (issue #50)", () => {
|
||||
assert.equal(fs.readFileSync(path.join(LANE, "tail.txt"), "utf8"), "l1\nl2\nl3\nl4\nl5\nl6");
|
||||
});
|
||||
|
||||
it("discards only the ticked blocks and whole files, nothing unticked (issue #87)", async () => {
|
||||
const edited = threeBlocks();
|
||||
write(LANE, "keep.txt", "untouched by discard\n");
|
||||
write(LANE, "drop.txt", "gone\n");
|
||||
const [hunk] = await hunkOf();
|
||||
await assert.rejects(
|
||||
() =>
|
||||
runGitAction(LANE, "discard", {
|
||||
paths: ["drop.txt"],
|
||||
hunks: { "near.txt": [{ hunk: hunk.header, blocks: [0, 2] }] },
|
||||
}),
|
||||
{ code: "ECONFIRM" }
|
||||
);
|
||||
await runGitAction(LANE, "discard", {
|
||||
paths: ["drop.txt"],
|
||||
hunks: { "near.txt": [{ hunk: hunk.header, blocks: [0, 2] }] },
|
||||
confirm: true,
|
||||
});
|
||||
const want = [...edited];
|
||||
want.splice(11, 0, "line 11");
|
||||
want.splice(3, 2, "line 4", "line 5");
|
||||
assert.deepEqual(read(), [...want, ""]);
|
||||
assert.equal(fs.existsSync(path.join(LANE, "drop.txt")), false);
|
||||
assert.equal(fs.readFileSync(path.join(LANE, "keep.txt"), "utf8"), "untouched by discard\n");
|
||||
});
|
||||
|
||||
it("discards ticked blocks alone, and refuses a stale one with nothing undone", async () => {
|
||||
const edited = threeBlocks();
|
||||
const [hunk] = await hunkOf();
|
||||
await assert.rejects(
|
||||
() =>
|
||||
runGitAction(LANE, "discard", {
|
||||
hunks: { "near.txt": [{ hunk: "@@ -1,2 +1,2 @@", blocks: [0] }] },
|
||||
confirm: true,
|
||||
}),
|
||||
{ code: "EHUNKGONE" }
|
||||
);
|
||||
assert.deepEqual(read(), [...edited, ""]);
|
||||
await runGitAction(LANE, "discard", {
|
||||
hunks: { "near.txt": [{ hunk: hunk.header, blocks: [1] }] },
|
||||
confirm: true,
|
||||
});
|
||||
assert.deepEqual(read(), [...edited.filter((l) => l !== "INSERTED"), ""]);
|
||||
});
|
||||
|
||||
it("refuses a block the hunk does not have", async () => {
|
||||
threeBlocks();
|
||||
const [hunk] = await hunkOf();
|
||||
|
||||
Reference in New Issue
Block a user