git_proc/
lib.rs

1#![doc = include_str!("../README.md")]
2
3/// Generate a pair of flag methods: unconditional and conditional.
4///
5/// The unconditional method calls the conditional one with `true`.
6///
7/// # Example
8///
9/// ```ignore
10/// flag_methods! {
11///     /// Enable foo mode.
12///     ///
13///     /// Corresponds to `--foo`.
14///     pub fn foo / foo_if, foo_field, "Conditionally enable foo mode."
15/// }
16/// ```
17///
18/// Generates:
19/// - `pub fn foo(self) -> Self` - calls `foo_if(true)`
20/// - `pub fn foo_if(mut self, value: bool) -> Self` - sets `self.foo_field = value`
21#[doc(hidden)]
22#[macro_export]
23macro_rules! flag_methods {
24    (
25        $(#[$attr:meta])*
26        $vis:vis fn $name:ident / $name_if:ident, $field:ident, $doc_if:literal
27    ) => {
28        $(#[$attr])*
29        #[must_use]
30        $vis fn $name(self) -> Self {
31            self.$name_if(true)
32        }
33
34        #[doc = $doc_if]
35        #[must_use]
36        $vis fn $name_if(mut self, value: bool) -> Self {
37            self.$field = value;
38            self
39        }
40    };
41}
42
43pub mod add;
44pub mod branch;
45pub mod clone;
46pub mod commit;
47pub mod config;
48pub mod fetch;
49pub mod init;
50pub mod ls_remote;
51pub mod push;
52pub mod remote;
53pub mod rev_list;
54pub mod rev_parse;
55pub mod show;
56pub mod show_ref;
57pub mod status;
58pub mod url;
59pub mod worktree;
60
61use std::path::Path;
62
63pub use cmd_proc::CommandError;
64
65/// Create a command builder with optional repository path.
66///
67/// If `repo_path` is `Some`, adds `-C <path>` to the command.
68/// If `repo_path` is `None`, uses current working directory.
69fn base_command(repo_path: Option<&Path>) -> cmd_proc::Command {
70    cmd_proc::Command::new("git").optional_option("-C", repo_path)
71}