M4.6 step 1: crate-internal extension hook for vcs/git modules

Repo::run, check_name and SMALL_CAP become pub(crate); cmd/git.rs gains
Op::Custom, start_with and a pub(super) rel_path, so a new module adds an
Op::Custom through start_with instead of editing git.rs or repo.rs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-10-27 17:41:04 +01:00
parent d2c853c42f
commit b814c49733
2 changed files with 57 additions and 4 deletions

View file

@ -174,6 +174,19 @@ struct Job {
repo: Option<Arc<Repo>>,
}
/// A git call defined outside this module, run on the git thread.
///
/// **Extension pattern** for new git commands (log, blame, merge, stash):
/// the backend goes into its own `crates/vcs/src/<area>.rs` as an
/// `impl Repo` block using the crate-internal `Repo::run`, `check_name` and
/// `SMALL_CAP`; the commands go into their own `cmd/git_<area>.rs`, whose
/// `run` resolves paths with [`rel_path`] and calls
/// `start_with(state, id, mutates, Box::new(move |repo| …))`. That becomes
/// an [`Op::Custom`] and runs like the commands here: a pending outcome,
/// git and the repository detected and cached, `gitChanged` after a call
/// with `mutates`. Neither this file nor `repo.rs` needs another change.
pub(super) type RepoFn = Box<dyn Fn(&Repo) -> Result<Value, VcsError> + Send>;
/// What one command does on its thread.
enum Op {
/// `git.status`.
@ -197,6 +210,18 @@ enum Op {
Branches,
/// `git.checkout`.
Checkout(String),
/// A command defined in another module (log, blame, merge, stash),
/// started through [`start_with`].
#[allow(
dead_code,
reason = "extension hook; constructed once the first module uses start_with"
)]
Custom {
/// Whether it changes the repository (and sends `gitChanged`).
mutates: bool,
/// The call against the repository.
run: RepoFn,
},
/// `git.createBranch`.
CreateBranch {
/// The new branch.
@ -211,6 +236,9 @@ enum Op {
impl Op {
/// Whether the call changes the repository (and sends `gitChanged`).
fn mutates(&self) -> bool {
if let Self::Custom { mutates, .. } = self {
return *mutates;
}
!matches!(
self,
Self::Status | Self::LastMessage | Self::Show(..) | Self::Branches
@ -228,6 +256,30 @@ fn engine_error(command: &str, error: VcsError) -> EngineError {
}
}
/// Runs a git call defined in another module (log, blame, merge, stash)
/// exactly like the commands here: a pending outcome, git and the
/// repository detected and cached, `gitChanged` after a change.
///
/// # Parameters
/// - `command`: the command id, for errors.
/// - `mutates`: the call changes the repository.
/// - `run`: the call; its errors map like the built-in ones.
///
/// # Errors
/// As [`start`].
#[allow(
dead_code,
reason = "extension hook; used by the history, blame, merge and stash modules"
)]
pub(super) fn start_with(
state: &mut State,
command: &'static str,
mutates: bool,
run: RepoFn,
) -> Result<Outcome, EngineError> {
start(state, command, Op::Custom { mutates, run })
}
/// Runs `op` on a new thread and returns the pending outcome.
///
/// # Errors
@ -436,6 +488,7 @@ fn run_op(repo: &Repo, op: &Op) -> Result<Value, VcsError> {
Ok(json!({"current": branches.current, "local": local, "remote": remote}))
}
Op::Checkout(branch) => repo.checkout(branch).map(|()| json!({})),
Op::Custom { run, .. } => run(repo),
Op::CreateBranch {
name,
from,
@ -576,7 +629,7 @@ pub fn pump(state: &mut State) {
}
/// Resolves a path param to a project-relative path with `/`.
fn rel_path(state: &State, command: &str, path: &str) -> Result<String, EngineError> {
pub(super) fn rel_path(state: &State, command: &str, path: &str) -> Result<String, EngineError> {
Ok(path_string(&project_path(state, command, path)?))
}