Skip to main content

vcs_runner/
lib.rs

1//! VCS-specific helpers built on [`procpilot`]. Adds jj/git shorthand
2//! wrappers, repo detection, and output parsers.
3//!
4//! For generic subprocess execution (stdin, retry, timeout, custom envs),
5//! use [`procpilot::Cmd`] directly — it's re-exported here for convenience.
6
7mod detect;
8mod error;
9#[cfg(feature = "git-parse")]
10mod parse_git;
11#[cfg(feature = "jj-parse")]
12mod parse_jj;
13mod runner;
14mod types;
15
16pub use detect::{VcsBackend, detect_vcs};
17pub use error::RunError;
18#[cfg(feature = "git-parse")]
19pub use parse_git::parse_git_diff_name_status;
20#[cfg(feature = "jj-parse")]
21pub use parse_jj::{
22    BOOKMARK_TEMPLATE, LOG_TEMPLATE, BookmarkParseResult, LogParseResult, parse_bookmark_output,
23    parse_diff_summary, parse_log_output, parse_remote_list,
24};
25pub use runner::{
26    git_merge_base, is_transient_error, jj_current_operation_id, jj_divergent_change_ids,
27    jj_is_divergent_at_operation, jj_merge_base, jj_op_restore, jj_operation_log,
28    jj_revset_at_operation, jj_revset_history, run_git,
29    run_git_cancellable, run_git_utf8, run_git_utf8_cancellable, run_git_utf8_with_retry,
30    run_git_utf8_with_retry_cancellable, run_git_utf8_with_timeout, run_git_with_retry,
31    run_git_with_retry_cancellable, run_git_with_timeout, run_jj, run_jj_cancellable, run_jj_utf8,
32    run_jj_utf8_cancellable, run_jj_utf8_ignore_wc, run_jj_utf8_with_retry,
33    run_jj_utf8_with_retry_cancellable, run_jj_utf8_with_timeout, run_jj_with_retry,
34    run_jj_with_retry_cancellable, run_jj_with_timeout,
35};
36
37// Re-export procpilot's generic subprocess API so vcs-runner consumers have
38// one dependency. Prefer these for anything non-VCS-specific.
39pub use procpilot::{
40    Cmd, CmdDisplay, DefaultRunner, Redirection, RetryPolicy, RunOutput, Runner,
41    STREAM_SUFFIX_SIZE, SpawnedProcess, StdinData, binary_available, binary_version,
42    default_transient,
43};
44
45pub use types::JjOperation;
46#[cfg(any(feature = "jj-parse", feature = "git-parse"))]
47pub use types::{FileChange, FileChangeKind};
48#[cfg(feature = "jj-parse")]
49pub use types::{Bookmark, ConflictState, ContentState, GitRemote, LogEntry, RemoteStatus, WorkingCopy};
50
51/// Common types and helpers for everyday VCS subprocess work.
52///
53/// `use vcs_runner::prelude::*;` brings in procpilot's standard set
54/// (`Cmd`, `RunError`, `RunOutput`, `Redirection`, `RetryPolicy`,
55/// `StdinData`, `SpawnedProcess`) plus the VCS-specific helpers
56/// (`run_jj`, `run_git`, retry/timeout variants, `jj_merge_base`,
57/// `git_merge_base`, `is_transient_error`, and the binary-availability
58/// checks).
59///
60/// Parser types and constants (`LogEntry`, `BOOKMARK_TEMPLATE`, …) stay
61/// out of the prelude — those callers know they need them and can
62/// import explicitly.
63pub mod prelude {
64    pub use procpilot::prelude::*;
65
66    pub use crate::{
67        VcsBackend, detect_vcs, git_available, git_merge_base, git_version, is_transient_error,
68        jj_available, jj_current_operation_id, jj_divergent_change_ids,
69        jj_is_divergent_at_operation, jj_merge_base, jj_op_restore, jj_operation_log,
70        jj_revset_at_operation, jj_revset_history, jj_version,
71        run_git, run_git_cancellable, run_git_utf8, run_git_utf8_cancellable,
72        run_git_utf8_with_retry, run_git_utf8_with_retry_cancellable, run_git_utf8_with_timeout,
73        run_git_with_retry, run_git_with_retry_cancellable, run_git_with_timeout, run_jj,
74        run_jj_cancellable, run_jj_utf8, run_jj_utf8_cancellable, run_jj_utf8_ignore_wc,
75        run_jj_utf8_with_retry, run_jj_utf8_with_retry_cancellable, run_jj_utf8_with_timeout,
76        run_jj_with_retry, run_jj_with_retry_cancellable, run_jj_with_timeout,
77    };
78}
79
80/// Check whether the `jj` binary is available on PATH.
81pub fn jj_available() -> bool {
82    binary_available("jj")
83}
84
85/// Get the jj version string, if available.
86pub fn jj_version() -> Option<String> {
87    binary_version("jj")
88}
89
90/// Check whether the `git` binary is available on PATH.
91pub fn git_available() -> bool {
92    binary_available("git")
93}
94
95/// Get the git version string, if available.
96pub fn git_version() -> Option<String> {
97    binary_version("git")
98}
99
100#[cfg(test)]
101mod tests {
102    use super::*;
103
104    #[test]
105    fn jj_available_returns_bool() {
106        let _ = jj_available();
107    }
108
109    #[test]
110    fn jj_version_matches_availability() {
111        if jj_available() {
112            let v = jj_version().expect("jj is installed");
113            assert!(v.contains("jj"));
114        } else {
115            assert!(jj_version().is_none());
116        }
117    }
118
119    #[test]
120    fn git_available_returns_bool() {
121        let _ = git_available();
122    }
123
124    #[test]
125    fn git_version_matches_availability() {
126        if git_available() {
127            let v = git_version().expect("git is installed");
128            assert!(v.contains("git"));
129        } else {
130            assert!(git_version().is_none());
131        }
132    }
133}