diff --git a/crates/engine/src/cmd/git.rs b/crates/engine/src/cmd/git.rs index 4f4a827..3ad6f81 100644 --- a/crates/engine/src/cmd/git.rs +++ b/crates/engine/src/cmd/git.rs @@ -174,6 +174,19 @@ struct Job { repo: Option>, } +/// 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/.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_.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 Result + 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 { + 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 { 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 { +pub(super) fn rel_path(state: &State, command: &str, path: &str) -> Result { Ok(path_string(&project_path(state, command, path)?)) } diff --git a/crates/vcs/src/repo.rs b/crates/vcs/src/repo.rs index 82fea6d..1e055cf 100644 --- a/crates/vcs/src/repo.rs +++ b/crates/vcs/src/repo.rs @@ -24,7 +24,7 @@ pub const STATUS_CAP: usize = 64 * 1024 * 1024; pub const FILE_CAP: usize = 32 * 1024 * 1024; /// Cap on other read output (refs, rev-parse). -const SMALL_CAP: usize = 16 * 1024 * 1024; +pub(crate) const SMALL_CAP: usize = 16 * 1024 * 1024; /// How many leading bytes [`Repo::show`] checks for NUL, as git does. const BINARY_PROBE: usize = 8000; @@ -240,7 +240,7 @@ impl Repo { } /// Runs git in the work tree root. - fn run(&self, call: Call<'_>) -> Result { + pub(crate) fn run(&self, call: Call<'_>) -> Result { runner::run(&self.git.executable, call) } @@ -689,7 +689,7 @@ impl Repo { /// Rejects branch names and revisions git could read as an option or /// that hold control characters. -fn check_name(name: &str) -> Result<(), VcsError> { +pub(crate) fn check_name(name: &str) -> Result<(), VcsError> { if name.is_empty() || name.starts_with('-') || name.chars().any(char::is_control) { return Err(VcsError::InvalidName(name.to_string())); }