Each command module keeps its own COMMAND_IDS next to its commands; the engine listing test checks their union against the registry, so a missing or extra command still fails without one shared list. The menu and palette tests derive their expectation from the listing and check grouping and ordering rules; the catalog's runtime keys are scanned from the sources. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
121 lines
4.1 KiB
Rust
121 lines
4.1 KiB
Rust
//! `git.blame` (M4.7): which commit last changed each line of a file, for
|
|
//! the editor's annotation gutter. Read-only and run like every `git.*`
|
|
//! command (a pending outcome off the lock, git and the repository cached
|
|
//! by [`super::git`]).
|
|
//!
|
|
//! Without `rev` an open buffer for the path is blamed as it stands
|
|
//! (`--contents -`, the text as it would be saved, BOM included), so
|
|
//! unsaved edits show as not committed and the lines match the editor.
|
|
//! Under the lock only the buffer's rope is cloned (shared chunks); the
|
|
//! text is built on the git thread.
|
|
|
|
use lutra_text::Buffer;
|
|
use lutra_vcs::BlameLine;
|
|
use serde::Deserialize;
|
|
use serde_json::{Value, json};
|
|
|
|
use super::git::start_with;
|
|
use super::params::{parse, project_path};
|
|
use crate::workspace::path_string;
|
|
use crate::{Command, CommandSpec, EngineError, Outcome, Registry, State};
|
|
|
|
/// The UTF-8 byte order mark.
|
|
const BOM: &str = "\u{feff}";
|
|
|
|
/// Registers `git.blame`.
|
|
///
|
|
/// # Errors
|
|
/// [`EngineError::Internal`] if the id is taken.
|
|
pub fn register(registry: &mut Registry) -> Result<(), EngineError> {
|
|
registry.register(Box::new(Blame))
|
|
}
|
|
|
|
/// One line of `git.blame`.
|
|
fn line_json(line: &BlameLine) -> Value {
|
|
json!({
|
|
"hash": line.hash,
|
|
"author": line.author,
|
|
"date": line.date,
|
|
"summary": line.summary,
|
|
"origLine": line.orig_line,
|
|
})
|
|
}
|
|
|
|
/// Params of `git.blame`.
|
|
#[derive(Debug, Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
struct BlameParams {
|
|
/// The file.
|
|
path: String,
|
|
/// A revision to blame the file at instead of the working copy.
|
|
#[serde(default)]
|
|
rev: Option<String>,
|
|
}
|
|
|
|
/// `git.blame`.
|
|
struct Blame;
|
|
|
|
impl Command for Blame {
|
|
fn spec(&self) -> &CommandSpec {
|
|
&CommandSpec {
|
|
id: "git.blame",
|
|
label: "Git: Blame",
|
|
menu_path: None,
|
|
default_shortcut: None,
|
|
params: "{ path: project-relative path, rev?: commit hash or revision (default: the working copy, an open buffer's unsaved text included) }; returns { lines: [{ hash, author, date (ISO 8601), summary, origLine }], version } with one entry per line in file order (hash all zeros for lines not committed yet; origLine 1-based in that commit); no lines when the file is untracked or the repository has no commits; version is the blamed buffer's version, null when no buffer was used",
|
|
}
|
|
}
|
|
|
|
fn enabled(&self, state: &State) -> bool {
|
|
state.has_project()
|
|
}
|
|
|
|
fn run(&self, state: &mut State, params: Value) -> Result<Outcome, EngineError> {
|
|
let command = self.spec().id;
|
|
let p: BlameParams = parse(command, params)?;
|
|
let rel = project_path(state, command, &p.path)?;
|
|
let path = path_string(&rel);
|
|
let rev = p
|
|
.rev
|
|
.map(|r| r.trim().to_string())
|
|
.filter(|r| !r.is_empty());
|
|
let buffer: Option<(Buffer, bool, u64)> = if rev.is_none() {
|
|
state
|
|
.buffers
|
|
.find(&rel)
|
|
.and_then(|id| state.buffers.get(id))
|
|
.map(|open| (open.buffer.clone(), open.bom, open.version))
|
|
} else {
|
|
None
|
|
};
|
|
start_with(
|
|
state,
|
|
command,
|
|
false,
|
|
Box::new(move |repo| {
|
|
let contents = buffer.as_ref().map(|(buffer, bom, _)| {
|
|
let text = buffer.save_text();
|
|
if *bom { format!("{BOM}{text}") } else { text }
|
|
});
|
|
let lines: Vec<Value> = repo
|
|
.blame(
|
|
&path,
|
|
rev.as_deref(),
|
|
contents.as_deref().map(str::as_bytes),
|
|
)?
|
|
.iter()
|
|
.map(line_json)
|
|
.collect();
|
|
Ok(json!({
|
|
"lines": lines,
|
|
"version": buffer.as_ref().map(|(_, _, version)| *version),
|
|
}))
|
|
}),
|
|
)
|
|
}
|
|
}
|
|
|
|
/// The ids of the commands this module registers, checked against the
|
|
/// registry by the engine's listing test. Add a new command's id here.
|
|
#[cfg(test)]
|
|
pub(crate) const COMMAND_IDS: &[&str] = &["git.blame"];
|