M4.7: blame annotations in the editor gutter

- crates/vcs: blame.rs (git blame --porcelain, --contents - from a
  buffer, revisions, untracked files and empty repositories give no
  lines; property-tested parser, ISO dates without a date crate)
- engine: git.blame {path, rev?} -> {lines, version} through start_with;
  an open buffer is blamed as it would be saved (line ending, BOM)
- ui: blame.ts gutter toggled per file (vcs.toggleBlame, Git menu, no
  shortcut), date + author per run, bar coloured by age from tokens,
  hover card with the full commit message, click opens the commit in
  the Log (vcs.showCommit); refreshed on edits and gitChanged
- docs: architecture "Blame (M4.7)"

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-10-30 17:52:44 +01:00
parent dc4a00ea0b
commit 2f758d76b9
18 changed files with 2003 additions and 3 deletions

190
crates/vcs/tests/blame.rs Normal file
View file

@ -0,0 +1,190 @@
//! Blame (M4.7) against real repositories in temp folders: the working
//! copy, buffer contents under `core.autocrlf`, a revision, a rename, a
//! project inside a larger repository, untracked files and an empty
//! repository. Every test skips with a message when git is not installed.
#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::panic,
clippy::indexing_slicing
)]
use std::path::Path;
use std::process::Command;
use lutra_vcs::{Git, Repo, VcsError, is_uncommitted};
/// The installed git, or `None` (with a message) when there is none.
fn installed() -> Option<Git> {
match Git::detect("") {
Ok(git) => Some(git),
Err(e) => {
eprintln!("skipped: no usable git installed ({e})");
None
}
}
}
/// Runs git in `dir` for test setup and asserts it succeeded.
fn git(dir: &Path, args: &[&str]) -> String {
let out = Command::new("git")
.args(args)
.current_dir(dir)
.env_remove("GIT_DIR")
.env_remove("GIT_WORK_TREE")
.env_remove("GIT_INDEX_FILE")
.output()
.unwrap();
assert!(
out.status.success(),
"git {args:?}: {}",
String::from_utf8_lossy(&out.stderr)
);
String::from_utf8_lossy(&out.stdout).trim().to_string()
}
/// A new repository on `main` with a local identity.
fn init(dir: &Path) {
git(dir, &["init", "-q", "-b", "main"]);
for (key, value) in [
("user.name", "Lutra Test"),
("user.email", "test@lutra.invalid"),
("commit.gpgsign", "false"),
("tag.gpgsign", "false"),
("core.autocrlf", "false"),
("core.hooksPath", ".git/hooks"),
] {
git(dir, &["config", key, value]);
}
}
/// Writes a file, creating its folders.
fn write(dir: &Path, path: &str, text: &str) {
let full = dir.join(path);
std::fs::create_dir_all(full.parent().unwrap()).unwrap();
std::fs::write(full, text).unwrap();
}
/// Stages everything and commits as `author` (`Name <mail>`), returning
/// the hash.
fn commit(dir: &Path, message: &str, author: &str) -> String {
git(dir, &["add", "-A"]);
git(dir, &["commit", "-q", "--author", author, "-m", message]);
git(dir, &["rev-parse", "HEAD"])
}
/// Opens the repository at `dir`.
fn open(git: &Git, dir: &Path) -> Repo {
git.open(dir).unwrap().expect("a repository")
}
/// The hashes of a blame.
fn hashes(repo: &Repo, path: &str, rev: Option<&str>, contents: Option<&str>) -> Vec<String> {
repo.blame(path, rev, contents.map(str::as_bytes))
.unwrap()
.into_iter()
.map(|l| l.hash)
.collect()
}
#[test]
fn working_copy_contents_and_revisions() {
let Some(g) = installed() else { return };
let dir = tempfile::tempdir().unwrap();
let d = dir.path();
init(d);
write(d, "a.php", "<?php\nfunction a() {}\n");
let first = commit(d, "First", "Ann <ann@x.invalid>");
write(d, "a.php", "<?php\nfunction a() {}\nfunction b() {}\n");
let second = commit(d, "Second", "Bo <bo@x.invalid>");
let repo = open(&g, d);
let lines = repo.blame("a.php", None, None).unwrap();
assert_eq!(lines.len(), 3);
assert_eq!(lines[0].hash, first);
assert_eq!(lines[0].author, "Ann");
assert_eq!(lines[2].hash, second);
assert_eq!(lines[2].summary, "Second");
assert_eq!(lines[2].orig_line, 3);
// Unsaved text with CRLF under autocrlf: only the new line is new.
git(d, &["config", "core.autocrlf", "true"]);
let blamed = hashes(
&repo,
"a.php",
None,
Some("<?php\r\nfunction a() {}\r\n// new\r\nfunction b() {}\r\n"),
);
assert_eq!(blamed.len(), 4);
assert_eq!(blamed[0], first);
assert!(is_uncommitted(&blamed[2]));
assert_eq!(blamed[3], second);
// A revision ignores the contents.
assert_eq!(
hashes(&repo, "a.php", Some(&first), Some("x\n")),
[first.clone(), first.clone()]
);
assert_eq!(
hashes(&repo, "a.php", Some("HEAD^"), None),
[first.clone(), first.clone()]
);
// Renamed: blame follows the file.
git(d, &["mv", "a.php", "b.php"]);
let renamed = commit(d, "Rename", "Cy <cy@x.invalid>");
assert_ne!(renamed, second);
assert_eq!(
hashes(&repo, "b.php", None, None),
[first.clone(), first, second]
);
// Untracked and bad input.
write(d, "new.php", "<?php\n");
assert!(repo.blame("new.php", None, None).unwrap().is_empty());
assert!(
repo.blame("new.php", None, Some(b"<?php\n"))
.unwrap()
.is_empty()
);
assert!(matches!(
repo.blame("b.php", Some("-L1"), None),
Err(VcsError::InvalidName(_))
));
assert!(matches!(
repo.blame("b.php", Some("HEAD:b.php"), None),
Err(VcsError::InvalidName(_))
));
assert!(matches!(
repo.blame("../b.php", None, None),
Err(VcsError::InvalidPath(_))
));
assert!(matches!(
repo.blame("b.php", Some("nope"), None),
Err(VcsError::Git(_))
));
}
#[test]
fn a_project_inside_a_repository_and_an_empty_one() {
let Some(g) = installed() else { return };
let dir = tempfile::tempdir().unwrap();
let d = dir.path();
init(d);
write(d, "app/src/a.php", "<?php\n");
let hash = commit(d, "First", "Ann <ann@x.invalid>");
let repo = open(&g, &d.join("app"));
assert_eq!(hashes(&repo, "src/a.php", None, None), [hash]);
let empty = tempfile::tempdir().unwrap();
init(empty.path());
write(empty.path(), "a.php", "<?php\n");
let repo = open(&g, empty.path());
assert!(repo.blame("a.php", None, None).unwrap().is_empty());
assert!(
repo.blame("a.php", None, Some(b"<?php\n"))
.unwrap()
.is_empty()
);
}