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_merge_base, run_git, run_git_utf8,
27    run_git_utf8_with_retry, run_git_utf8_with_timeout, run_git_with_retry, run_git_with_timeout,
28    run_jj, run_jj_utf8, run_jj_utf8_with_retry, run_jj_utf8_with_timeout, run_jj_with_retry,
29    run_jj_with_timeout,
30};
31
32// Re-export procpilot's generic subprocess API so vcs-runner consumers have
33// one dependency. Prefer these for anything non-VCS-specific.
34pub use procpilot::{
35    Cmd, CmdDisplay, DefaultRunner, Redirection, RetryPolicy, RunOutput, Runner,
36    STREAM_SUFFIX_SIZE, SpawnedProcess, StdinData, binary_available, binary_version,
37    default_transient,
38};
39
40#[cfg(any(feature = "jj-parse", feature = "git-parse"))]
41pub use types::{FileChange, FileChangeKind};
42#[cfg(feature = "jj-parse")]
43pub use types::{Bookmark, ConflictState, ContentState, GitRemote, LogEntry, RemoteStatus, WorkingCopy};
44
45/// Common types and helpers for everyday VCS subprocess work.
46///
47/// `use vcs_runner::prelude::*;` brings in procpilot's standard set
48/// (`Cmd`, `RunError`, `RunOutput`, `Redirection`, `RetryPolicy`,
49/// `StdinData`, `SpawnedProcess`) plus the VCS-specific helpers
50/// (`run_jj`, `run_git`, retry/timeout variants, `jj_merge_base`,
51/// `git_merge_base`, `is_transient_error`, and the binary-availability
52/// checks).
53///
54/// Parser types and constants (`LogEntry`, `BOOKMARK_TEMPLATE`, …) stay
55/// out of the prelude — those callers know they need them and can
56/// import explicitly.
57pub mod prelude {
58    pub use procpilot::prelude::*;
59
60    pub use crate::{
61        VcsBackend, detect_vcs, git_available, git_merge_base, git_version, is_transient_error,
62        jj_available, jj_merge_base, jj_version, run_git, run_git_utf8, run_git_utf8_with_retry,
63        run_git_utf8_with_timeout, run_git_with_retry, run_git_with_timeout, run_jj, run_jj_utf8,
64        run_jj_utf8_with_retry, run_jj_utf8_with_timeout, run_jj_with_retry, run_jj_with_timeout,
65    };
66}
67
68/// Check whether the `jj` binary is available on PATH.
69pub fn jj_available() -> bool {
70    binary_available("jj")
71}
72
73/// Get the jj version string, if available.
74pub fn jj_version() -> Option<String> {
75    binary_version("jj")
76}
77
78/// Check whether the `git` binary is available on PATH.
79pub fn git_available() -> bool {
80    binary_available("git")
81}
82
83/// Get the git version string, if available.
84pub fn git_version() -> Option<String> {
85    binary_version("git")
86}
87
88#[cfg(test)]
89mod tests {
90    use super::*;
91
92    #[test]
93    fn jj_available_returns_bool() {
94        let _ = jj_available();
95    }
96
97    #[test]
98    fn jj_version_matches_availability() {
99        if jj_available() {
100            let v = jj_version().expect("jj is installed");
101            assert!(v.contains("jj"));
102        } else {
103            assert!(jj_version().is_none());
104        }
105    }
106
107    #[test]
108    fn git_available_returns_bool() {
109        let _ = git_available();
110    }
111
112    #[test]
113    fn git_version_matches_availability() {
114        if git_available() {
115            let v = git_version().expect("git is installed");
116            assert!(v.contains("git"));
117        } else {
118            assert!(git_version().is_none());
119        }
120    }
121}