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