PM: merge M4.6 step 1 vcs extension hook
Some checks are pending
ci / ci (macos-15) (push) Waiting to run
ci / ci (ubuntu-24.04) (push) Waiting to run
ci / ci (windows-2025) (push) Waiting to run

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-10-28 17:45:36 +01:00
commit 0bf0b1427b
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)?))
}

View file

@ -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<runner::Output, VcsError> {
pub(crate) fn run(&self, call: Call<'_>) -> Result<runner::Output, VcsError> {
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()));
}