diff --git a/crates/ide-vcs/src/block_history.rs b/crates/ide-vcs/src/block_history.rs index c47d1cb868..d54c598f44 100644 --- a/crates/ide-vcs/src/block_history.rs +++ b/crates/ide-vcs/src/block_history.rs @@ -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, ¤t), 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 diff --git a/crates/ide-vcs/src/lib.rs b/crates/ide-vcs/src/lib.rs index 3ba9c4bbbf..1e8e0d91b1 100644 --- a/crates/ide-vcs/src/lib.rs +++ b/crates/ide-vcs/src/lib.rs @@ -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, pub revisions: Vec, } diff --git a/crates/ide-vcs/tests/block_history.rs b/crates/ide-vcs/tests/block_history.rs index cad40209fb..ed0cd846df 100644 --- a/crates/ide-vcs/tests/block_history.rs +++ b/crates/ide-vcs/tests/block_history.rs @@ -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)); diff --git a/src/bindings.ts b/src/bindings.ts index a48e3a649f..43cd0232aa 100644 --- a/src/bindings.ts +++ b/src/bindings.ts @@ -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[], }; diff --git a/src/features/vcs/SelectionHistory.tsx b/src/features/vcs/SelectionHistory.tsx index 1d02d9fb30..9b9949119a 100644 --- a/src/features/vcs/SelectionHistory.tsx +++ b/src/features/vcs/SelectionHistory.tsx @@ -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; } diff --git a/src/features/vcs/__tests__/selectionHistory.test.tsx b/src/features/vcs/__tests__/selectionHistory.test.tsx index 3de2e7465c..35d4c0cf2f 100644 --- a/src/features/vcs/__tests__/selectionHistory.test.tsx +++ b/src/features/vcs/__tests__/selectionHistory.test.tsx @@ -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", () => {