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:
parent
d2c853c42f
commit
b814c49733
2 changed files with 57 additions and 4 deletions
|
|
@ -174,6 +174,19 @@ struct Job {
|
||||||
repo: Option<Arc<Repo>>,
|
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.
|
/// What one command does on its thread.
|
||||||
enum Op {
|
enum Op {
|
||||||
/// `git.status`.
|
/// `git.status`.
|
||||||
|
|
@ -197,6 +210,18 @@ enum Op {
|
||||||
Branches,
|
Branches,
|
||||||
/// `git.checkout`.
|
/// `git.checkout`.
|
||||||
Checkout(String),
|
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`.
|
/// `git.createBranch`.
|
||||||
CreateBranch {
|
CreateBranch {
|
||||||
/// The new branch.
|
/// The new branch.
|
||||||
|
|
@ -211,6 +236,9 @@ enum Op {
|
||||||
impl Op {
|
impl Op {
|
||||||
/// Whether the call changes the repository (and sends `gitChanged`).
|
/// Whether the call changes the repository (and sends `gitChanged`).
|
||||||
fn mutates(&self) -> bool {
|
fn mutates(&self) -> bool {
|
||||||
|
if let Self::Custom { mutates, .. } = self {
|
||||||
|
return *mutates;
|
||||||
|
}
|
||||||
!matches!(
|
!matches!(
|
||||||
self,
|
self,
|
||||||
Self::Status | Self::LastMessage | Self::Show(..) | Self::Branches
|
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.
|
/// Runs `op` on a new thread and returns the pending outcome.
|
||||||
///
|
///
|
||||||
/// # Errors
|
/// # Errors
|
||||||
|
|
@ -436,6 +488,7 @@ fn run_op(repo: &Repo, op: &Op) -> Result<Value, VcsError> {
|
||||||
Ok(json!({"current": branches.current, "local": local, "remote": remote}))
|
Ok(json!({"current": branches.current, "local": local, "remote": remote}))
|
||||||
}
|
}
|
||||||
Op::Checkout(branch) => repo.checkout(branch).map(|()| json!({})),
|
Op::Checkout(branch) => repo.checkout(branch).map(|()| json!({})),
|
||||||
|
Op::Custom { run, .. } => run(repo),
|
||||||
Op::CreateBranch {
|
Op::CreateBranch {
|
||||||
name,
|
name,
|
||||||
from,
|
from,
|
||||||
|
|
@ -576,7 +629,7 @@ pub fn pump(state: &mut State) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Resolves a path param to a project-relative path with `/`.
|
/// 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)?))
|
Ok(path_string(&project_path(state, command, path)?))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ pub const STATUS_CAP: usize = 64 * 1024 * 1024;
|
||||||
pub const FILE_CAP: usize = 32 * 1024 * 1024;
|
pub const FILE_CAP: usize = 32 * 1024 * 1024;
|
||||||
|
|
||||||
/// Cap on other read output (refs, rev-parse).
|
/// 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.
|
/// How many leading bytes [`Repo::show`] checks for NUL, as git does.
|
||||||
const BINARY_PROBE: usize = 8000;
|
const BINARY_PROBE: usize = 8000;
|
||||||
|
|
@ -240,7 +240,7 @@ impl Repo {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Runs git in the work tree root.
|
/// 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)
|
runner::run(&self.git.executable, call)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -689,7 +689,7 @@ impl Repo {
|
||||||
|
|
||||||
/// Rejects branch names and revisions git could read as an option or
|
/// Rejects branch names and revisions git could read as an option or
|
||||||
/// that hold control characters.
|
/// 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) {
|
if name.is_empty() || name.starts_with('-') || name.chars().any(char::is_control) {
|
||||||
return Err(VcsError::InvalidName(name.to_string()));
|
return Err(VcsError::InvalidName(name.to_string()));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue