дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-03 16:51:16 +02:00
fix(vcs): open the annotate gutter's Show Diff at the clicked line's change
Show Diff from the blame gutter always opened at the commit's first change. git blame's porcelain header already names each line's number in the attributed commit; it is now kept as BlameLine.originLine, and Show Diff opens the diff at that line of the commit's version (IDEA scrolls to it the same way). Fixes #7169
Цей коміт міститься в:
1 джерело
c1e8864138
коміт
5cbcdd08f4
7 files changed
+48
-16
No files matched your search
@@ -16,17 +16,17 @@ struct CommitMeta {
|
||||
/// Parses `git blame --porcelain` output into one entry per final line.
|
||||
pub(crate) fn parse_porcelain(out: &str, root: &Path, relative_path: &str) -> Vec<BlameLine> {
|
||||
let mut metas: HashMap<String, CommitMeta> = HashMap::new();
|
||||
let mut lines: Vec<(u32, String, Option<IpcPath>)> = Vec::new();
|
||||
let mut current: Option<(u32, String)> = None;
|
||||
let mut lines: Vec<(u32, String, Option<IpcPath>, Option<u32>)> = Vec::new();
|
||||
let mut current: Option<(u32, String, Option<u32>)> = None;
|
||||
for line in out.split('\n') {
|
||||
if line.starts_with('\t') {
|
||||
if let Some((number, id)) = current.take() {
|
||||
if let Some((number, id, origin_line)) = current.take() {
|
||||
let origin = metas.get(&id).and_then(|m| m.origin_path.clone());
|
||||
lines.push((number, id, origin));
|
||||
lines.push((number, id, origin, origin_line));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if let Some((_, id)) = current.as_ref() {
|
||||
if let Some((_, id, _)) = current.as_ref() {
|
||||
let meta = metas.entry(id.clone()).or_default();
|
||||
if let Some(v) = line.strip_prefix("author ") {
|
||||
meta.author = v.to_string();
|
||||
@@ -48,7 +48,7 @@ pub(crate) fn parse_porcelain(out: &str, root: &Path, relative_path: &str) -> Ve
|
||||
}
|
||||
// Header: <sha> <orig-line> <final-line> [<group-count>]
|
||||
let mut parts = line.split(' ');
|
||||
let (Some(sha), Some(_orig), Some(final_line)) = (parts.next(), parts.next(), parts.next()) else {
|
||||
let (Some(sha), Some(orig_line), Some(final_line)) = (parts.next(), parts.next(), parts.next()) else {
|
||||
continue;
|
||||
};
|
||||
if sha.len() < 40 || !sha.bytes().all(|b| b.is_ascii_hexdigit()) {
|
||||
@@ -56,14 +56,23 @@ pub(crate) fn parse_porcelain(out: &str, root: &Path, relative_path: &str) -> Ve
|
||||
}
|
||||
let Ok(final_line) = final_line.parse::<u32>() else { continue };
|
||||
metas.entry(sha.to_string()).or_default();
|
||||
current = Some((final_line, sha.to_string()));
|
||||
// 1-based in the porcelain header.
|
||||
let origin_line = orig_line.parse::<u32>().ok().and_then(|n| n.checked_sub(1));
|
||||
current = Some((final_line, sha.to_string(), origin_line));
|
||||
}
|
||||
lines.sort_by_key(|(n, ..)| *n);
|
||||
lines
|
||||
.into_iter()
|
||||
.map(|(_, sha, origin_path)| {
|
||||
.map(|(_, sha, origin_path, origin_line)| {
|
||||
let meta = metas.get(&sha).cloned().unwrap_or_default();
|
||||
BlameLine { commit: sha, author: meta.author, time_ms: meta.time_ms, summary: meta.summary, origin_path }
|
||||
BlameLine {
|
||||
commit: sha,
|
||||
author: meta.author,
|
||||
time_ms: meta.time_ms,
|
||||
summary: meta.summary,
|
||||
origin_path,
|
||||
origin_line,
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
@@ -77,14 +86,16 @@ mod tests {
|
||||
let sha_a = "a".repeat(40);
|
||||
let sha_z = "0".repeat(40);
|
||||
let out = format!(
|
||||
"{sha_a} 1 1 2\nauthor Ann\nauthor-mail <a@x>\nauthor-time 100\nauthor-tz +0000\nsummary first\nfilename f\n\tl1\n\
|
||||
{sha_a} 2 2\n\tl2\n\
|
||||
{sha_z} 3 3 1\nauthor Not Committed Yet\nauthor-time 200\nsummary Version of f from f\nfilename f\n\tl3\n"
|
||||
"{sha_a} 11 17 2\nauthor Ann\nauthor-mail <a@x>\nauthor-time 100\nauthor-tz +0000\nsummary first\nfilename f\n\tl1\n\
|
||||
{sha_a} 12 18\n\tl2\n\
|
||||
{sha_z} 19 19 1\nauthor Not Committed Yet\nauthor-time 200\nsummary Version of f from f\nfilename f\n\tl3\n"
|
||||
);
|
||||
let lines = parse_porcelain(&out, Path::new("root"), "f");
|
||||
assert_eq!(lines.len(), 3);
|
||||
assert_eq!(lines[1].author, "Ann");
|
||||
assert_eq!(lines[1].time_ms, 100_000.0);
|
||||
assert_eq!(lines[2].author, "Not Committed Yet");
|
||||
// Where each line is in the attributed commit's file, 0-based: Show Diff opens there.
|
||||
assert_eq!(lines.iter().map(|l| l.origin_line).collect::<Vec<_>>(), [Some(10), Some(11), Some(18)]);
|
||||
}
|
||||
}
|
||||
@@ -131,6 +131,10 @@ pub struct BlameLine {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[specta(optional)]
|
||||
pub origin_path: Option<ide_core::IpcPath>,
|
||||
/// The line's 0-based number in the attributed commit's version of the file.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
#[specta(optional)]
|
||||
pub origin_line: Option<u32>,
|
||||
}
|
||||
|
||||
/// History for Selection: the commits that changed a block of lines, newest first.
|
||||
|
||||
@@ -287,6 +287,7 @@ fn uncommitted_lines(n: usize) -> Vec<BlameLine> {
|
||||
time_ms: now,
|
||||
summary: String::new(),
|
||||
origin_path: None,
|
||||
origin_line: None,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
@@ -4638,6 +4638,8 @@ export type BlameLine_Deserialize = {
|
||||
summary: string,
|
||||
/** The file blamed for this line when it had another path in the attributed commit. */
|
||||
originPath?: IpcPath | null,
|
||||
/** The line's 0-based number in the attributed commit's version of the file. */
|
||||
originLine?: number | null,
|
||||
};
|
||||
|
||||
export type BlameLine_Serialize = {
|
||||
@@ -4647,6 +4649,8 @@ export type BlameLine_Serialize = {
|
||||
summary: string,
|
||||
/** The file blamed for this line when it had another path in the attributed commit. */
|
||||
originPath?: IpcPath | null,
|
||||
/** The line's 0-based number in the attributed commit's version of the file. */
|
||||
originLine?: number | null,
|
||||
};
|
||||
|
||||
/** History for Selection: the commits that changed a block of lines, newest first. */
|
||||
|
||||
@@ -186,6 +186,13 @@ describe("show diff from the annotate gutter", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("opens at the clicked line's place in the commit's version of the file", async () => {
|
||||
await showRevisionDiff("/repo/a.py", sha, 100);
|
||||
expect(mocks.openDiff).toHaveBeenCalledWith(expect.objectContaining({ line: 100, lineSide: "right" }), {
|
||||
preview: false,
|
||||
});
|
||||
});
|
||||
|
||||
it("shows the file as added when the commit added it", async () => {
|
||||
mocks.commitFiles.mockResolvedValue({ status: "ok", data: [{ path: "a.py", kind: "added", oldPath: null }] });
|
||||
await showRevisionDiff("/repo/a.py", sha);
|
||||
|
||||
@@ -117,9 +117,10 @@ export async function annotatePreviousRevision(path: string, commit: string): Pr
|
||||
/**
|
||||
* The annotate gutter's Show Diff (IDEA's): what `commit` changed in the file, against its first
|
||||
* parent, so a file the commit added or renamed shows as such. The file's history gives its name
|
||||
* at that commit when it was renamed later.
|
||||
* at that commit when it was renamed later. `line` (0-based, in that commit's version of the file)
|
||||
* is where the diff opens: the change the clicked line came from, as IDEA scrolls to it.
|
||||
*/
|
||||
export async function showRevisionDiff(path: string, commit: string): Promise<void> {
|
||||
export async function showRevisionDiff(path: string, commit: string, line?: number | null): Promise<void> {
|
||||
const repo = repoOf(path) ?? currentRepoRoot();
|
||||
if (!repo) return;
|
||||
const relative = relativePath(ipcPath(repo).replace(/\/$/, ""), ipcPath(path));
|
||||
@@ -142,7 +143,8 @@ export async function showRevisionDiff(path: string, commit: string): Promise<vo
|
||||
if (!found) return;
|
||||
const name = (found === inHistory && found.historyPath) || relative;
|
||||
const changed = files.find((f) => f.path === name) ?? { path: name, kind: "modified", oldPath: null };
|
||||
await openDiff(commitFileDiffData({ ...found, repo }, changed), { preview: false });
|
||||
const data = commitFileDiffData({ ...found, repo }, changed);
|
||||
await openDiff(line == null ? data : { ...data, line, lineSide: "right" }, { preview: false });
|
||||
} catch (e) {
|
||||
notifyFailure(e, tx("Could not show diff"));
|
||||
}
|
||||
|
||||
@@ -665,7 +665,10 @@ function blameExtension(lines: BlameLine[], source: BlameSource): Extension {
|
||||
{
|
||||
label: "Show Diff",
|
||||
icon: "diff",
|
||||
run: () => void import("./annotateRevision").then((m) => m.showRevisionDiff(origin, l.commit)),
|
||||
run: () =>
|
||||
void import("./annotateRevision").then((m) =>
|
||||
m.showRevisionDiff(origin, l.commit, l.originLine),
|
||||
),
|
||||
},
|
||||
{
|
||||
label: "Copy Revision Number",
|
||||
|
||||
Reference in new issue
Заблокувати користувача