fix(vcs): History for Selection no longer lists Local Changes when the newest commit is a merge (#6034)

History for Selection listed a Local Changes row on a clean file whenever the newest commit touching the selected lines was a merge: the list compared the current lines with the newest row's `after` block, and `git log -L` prints no patch for a merge, so that block was empty. The backend now returns HEAD's block (`BlockHistory.head`) and the list compares against it, falling back to the newest row for servers that predate the field.

Co-authored-by: audichuang <[email protected]>
Цей коміт міститься в:
audichuang authored and GitHub committed 2026-11-26 16:35:31 +08:00
1 джерело 527add030a
коміт b0d7bfef24
6 files changed
+51 -5

No files matched your search

+9 -3
Переглянути файл
@@ -24,7 +24,7 @@ pub(crate) fn block_history(
crate::repo::blob_at(&repo, &rel, "HEAD")?
};
let Some(head) = head else {
return Ok(BlockHistory { head_lines: None, revisions: Vec::new() });
return Ok(BlockHistory { head_lines: None, head: None, revisions: Vec::new() });
};
let head = decode(&head, encoding);
let current = match current_text {
@@ -32,12 +32,18 @@ pub(crate) fn block_history(
None => decode(&std::fs::read(r.abs_path(&rel))?, encoding).into_owned(),
};
let Some((start, end)) = head_range(&crate::diff::line_hunks(&head, &current), lines) else {
return Ok(BlockHistory { head_lines: None, revisions: Vec::new() });
return Ok(BlockHistory { head_lines: None, head: None, revisions: Vec::new() });
};
let range = format!("-L{},{}:{rel}", start + 1, end + 1);
let args = ["log", "--no-color", "--no-ext-diff", "--no-textconv", FORMAT, &range, "HEAD"];
let out = cli::run_cancellable(r.root(), &args, cancel)?;
Ok(BlockHistory { head_lines: Some((start, end)), revisions: parse_log(&out.stdout, encoding) })
// The newest row's block can't stand in for HEAD's: a merge row has none.
let mut block = String::new();
for line in head.lines().skip(start as usize).take((end - start) as usize + 1) {
push_line(&mut block, line);
}
let revisions = parse_log(&out.stdout, encoding);
Ok(BlockHistory { head_lines: Some((start, end)), head: Some(block), revisions })
}
/// The HEAD lines (0-based, inclusive) of `lines` in the current text, given the
+3
Переглянути файл
@@ -139,6 +139,9 @@ pub struct BlameLine {
pub struct BlockHistory {
/// The selected lines in HEAD (0-based, inclusive); None when none of them is committed.
pub head_lines: Option<(u32, u32)>,
/// Those lines as HEAD has them, each ending with `\n`; None from servers that predate it.
#[serde(default)]
pub head: Option<String>,
pub revisions: Vec<BlockRevision>,
}
+23 -1
Переглянути файл
@@ -76,6 +76,8 @@ fn follows_the_block_through_edits_and_a_rename() {
let fx = renamed_history();
let h = history(&fx.repo(), &fx.path("b.txt"), None, (1, 2));
assert_eq!(h.head_lines, Some((1, 2)));
// Without a merge on top, HEAD's block is the newest row's.
assert_eq!(h.head.as_deref(), Some(h.revisions[0].after.as_str()));
let shown: Vec<_> = h.revisions.iter().map(|r| (r.summary.as_str(), r.path.as_str())).collect();
// Edits outside the block and the pure rename are left out.
assert_eq!(shown, [("Shout three", "b.txt"), ("Shout two", "a.txt"), ("Add a", "a.txt")]);
@@ -86,6 +88,26 @@ fn follows_the_block_through_edits_and_a_rename() {
assert!(newest.id.starts_with(&newest.short_id) && newest.time_ms > 0.0);
}
#[test]
fn newest_row_a_merge_without_a_diff() {
let fx = Fixture::new();
fx.write("a.txt", "a\nb\nc\nd\n");
fx.commit("Add a");
fx.git(&["switch", "-q", "-c", "side"]);
fx.write("a.txt", "A\nb\nc\nd\n");
fx.commit("Side edit");
fx.git(&["switch", "-q", "main"]);
fx.write("a.txt", "a\nb\nc\nD\n");
fx.commit("Main edit");
fx.git(&["merge", "-q", "--no-edit", "side"]);
let h = history(&fx.repo(), &fx.path("a.txt"), None, (0, 3));
let newest = &h.revisions[0];
assert!(newest.summary.starts_with("Merge"), "{newest:?}");
// Git prints no patch for the merge, so its block says nothing about HEAD.
assert_eq!((newest.before.as_str(), newest.after.as_str()), ("", ""));
assert_eq!(h.head.as_deref(), Some("A\nb\nc\nD\n"));
}
#[test]
fn maps_an_edited_selection_to_head_lines() {
let fx = renamed_history();
@@ -93,7 +115,7 @@ fn maps_an_edited_selection_to_head_lines() {
// Two unsaved lines inserted at the top: lines 3..=4 are HEAD's 1..=2.
let edited = "new\nnew\none\nTWO\nTHREE\nfour\nFIVE\n";
let h = history(&repo, &fx.path("b.txt"), Some(edited), (3, 4));
assert_eq!(h.head_lines, Some((1, 2)));
assert_eq!((h.head_lines, h.head.as_deref()), (Some((1, 2)), Some("TWO\nTHREE\n")));
assert_eq!(h.revisions.len(), 3);
// Only new lines selected, or a file Git does not know: nothing committed to show.
let h = history(&repo, &fx.path("b.txt"), Some(edited), (0, 1));
+2
Переглянути файл
@@ -3780,6 +3780,8 @@ export type BlameLine_Serialize = {
export type BlockHistory = {
/** The selected lines in HEAD (0-based, inclusive); None when none of them is committed. */
headLines: [number, number] | null,
/** Those lines as HEAD has them, each ending with `\n`; None from servers that predate it. */
head?: string | null,
revisions: BlockRevision[],
};
+2 -1
Переглянути файл
@@ -37,7 +37,8 @@ export function blockOf(text: string, [from, to]: readonly [number, number]): st
/** The rows of the list: Local Changes when the block differs from HEAD, then the commits. */
export function historyRows(history: BlockHistory, current: string): HistoryRow[] {
const rows: HistoryRow[] = history.revisions.map((revision) => ({ kind: "revision", revision }));
const head = history.revisions[0]?.after;
// Servers that predate `head` only give the newest row's block, which a merge leaves empty.
const head = history.head ?? history.revisions[0]?.after;
if (history.headLines && head !== undefined && head !== current) rows.unshift({ kind: "local", head, current });
return rows;
}
+12
Переглянути файл
@@ -92,6 +92,18 @@ describe("rows", () => {
expect(edited).toHaveLength(3);
expect(historyRows({ headLines: null, revisions: [] }, "new\n")).toEqual([]);
});
it("compares with HEAD's block, not the newest row, when that row is a merge without a diff", () => {
// `git log -L` prints no patch for a merge, so its row has empty sides.
const merge = revision("m", "Merge branch 'side'", "", "");
const merged: BlockHistory = { headLines: [1, 2], head: "TWO\nTHREE\n", revisions: [merge, ...history.revisions] };
expect(historyRows(merged, "TWO\nTHREE\n").map((r) => r.kind)).toEqual(["revision", "revision", "revision"]);
expect(historyRows(merged, "TWO\nTHREE!\n")[0]).toEqual({
kind: "local",
head: "TWO\nTHREE\n",
current: "TWO\nTHREE!\n",
});
});
});
describe("History for Selection tab", () => {