//! History commands (M4.6): `git.log`, `git.showCommit` and `git.showAt`, //! read-only and run like every `git.*` command (a pending outcome off the //! lock, git and the repository cached by [`super::git`]). use lutra_vcs::{CommitDetails, LOG_LIMIT, LogCommit, LogQuery}; use serde::Deserialize; use serde_json::{Value, json}; use super::git::{rel_path, start_with}; use super::params::parse; use crate::{Command, CommandSpec, EngineError, Outcome, Registry, State}; /// Registers the history commands. /// /// # Errors /// [`EngineError::Internal`] if an id is taken. pub fn register(registry: &mut Registry) -> Result<(), EngineError> { registry.register(Box::new(Log))?; registry.register(Box::new(ShowCommit))?; registry.register(Box::new(ShowAt))?; Ok(()) } /// One commit of `git.log`. fn commit_json(commit: &LogCommit) -> Value { json!({ "hash": commit.hash, "parents": commit.parents, "author": commit.author, "email": commit.email, "date": commit.date, "subject": commit.subject, "refs": commit.refs, }) } /// `git.showCommit`'s value. fn details_json(details: &CommitDetails) -> Value { let files: Vec = details .files .iter() .map(|f| { let mut value = json!({"path": f.path, "status": f.status}); if let (Some(orig), Some(map)) = (&f.orig_path, value.as_object_mut()) { map.insert("origPath".into(), json!(orig)); } value }) .collect(); json!({ "hash": details.hash, "parents": details.parents, "author": details.author, "email": details.email, "date": details.date, "message": details.message, "files": files, "truncated": details.truncated, }) } /// Params of `git.log`. #[derive(Debug, Deserialize)] #[serde(deny_unknown_fields)] struct LogParams { /// Commits to skip. #[serde(default)] skip: usize, /// Commits to return, 1 to 500. #[serde(default = "default_limit")] limit: usize, /// A file or folder whose history to list. #[serde(default)] path: Option, /// Text the message must contain. #[serde(default)] query: Option, /// Text the author name or email must contain. #[serde(default)] author: Option, } /// The default page size of `git.log`. fn default_limit() -> usize { 100 } /// `git.log`. struct Log; impl Command for Log { fn spec(&self) -> &CommandSpec { &CommandSpec { id: "git.log", label: "Git: Log", menu_path: None, default_shortcut: None, params: "{ skip?: number (0), limit?: 1..500 (100), path?: project-relative file or folder (HEAD's history, following renames for a file), query?: text in the message, author?: text in author name or email (both case-insensitive, literal) }; returns { commits: [{ hash, parents, author, email, date (ISO 8601), subject, refs }] } newest first in topological order over all branches, remote branches, tags and HEAD", } } fn enabled(&self, state: &State) -> bool { state.has_project() } fn run(&self, state: &mut State, params: Value) -> Result { let command = self.spec().id; let p: LogParams = parse(command, params)?; if p.limit == 0 || p.limit > LOG_LIMIT { return Err(EngineError::invalid_params( command, format!("limit must be 1 to {LOG_LIMIT}"), )); } let path = match p.path.as_deref().map(str::trim) { Some(path) if !path.is_empty() => Some(rel_path(state, command, path)?), _ => None, }; let query = LogQuery { skip: p.skip, limit: p.limit, path, query: p.query, author: p.author, }; start_with( state, command, false, Box::new(move |repo| { let commits: Vec = repo.log(&query)?.iter().map(commit_json).collect(); Ok(json!({"commits": commits})) }), ) } } /// Params of `git.showCommit`. #[derive(Debug, Deserialize)] #[serde(deny_unknown_fields)] struct ShowCommitParams { /// The commit. hash: String, } /// `git.showCommit`. struct ShowCommit; impl Command for ShowCommit { fn spec(&self) -> &CommandSpec { &CommandSpec { id: "git.showCommit", label: "Git: Show Commit", menu_path: None, default_shortcut: None, params: "{ hash: 4 to 64 hex digits }; returns { hash, parents, author, email, date, message, files: [{ path, status: added|modified|deleted|renamed|copied, origPath? }], truncated } with the files changed against the first parent (renames detected), inside the project, at most 10,000", } } fn enabled(&self, state: &State) -> bool { state.has_project() } fn run(&self, state: &mut State, params: Value) -> Result { let command = self.spec().id; let p: ShowCommitParams = parse(command, params)?; start_with( state, command, false, Box::new(move |repo| Ok(details_json(&repo.show_commit(&p.hash)?))), ) } } /// Params of `git.showAt`. #[derive(Debug, Deserialize)] #[serde(deny_unknown_fields)] struct ShowAtParams { /// The file. path: String, /// The commit or revision. rev: String, } /// `git.showAt`. struct ShowAt; impl Command for ShowAt { fn spec(&self) -> &CommandSpec { &CommandSpec { id: "git.showAt", label: "Git: Show File at Commit", menu_path: None, default_shortcut: None, params: "{ path: project-relative path, rev: commit hash or revision such as abc123^ }; returns { text: string | null, binary, tooLarge } like git.show (null when the path or revision does not exist there)", } } fn enabled(&self, state: &State) -> bool { state.has_project() } fn run(&self, state: &mut State, params: Value) -> Result { let command = self.spec().id; let p: ShowAtParams = parse(command, params)?; let path = rel_path(state, command, &p.path)?; let rev = p.rev; start_with( state, command, false, Box::new(move |repo| { let contents = repo.show_at(&path, &rev)?; Ok(json!({ "text": contents.text, "binary": contents.binary, "tooLarge": contents.too_large, })) }), ) } } /// 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.log", "git.showAt", "git.showCommit"];