git_proc/
ls_remote.rs

1use std::path::Path;
2
3use crate::url::Remote;
4
5/// Create a new `git ls-remote` command builder.
6#[must_use]
7pub fn new() -> LsRemote<'static> {
8    LsRemote::new()
9}
10
11/// Builder for `git ls-remote` command.
12///
13/// See `git ls-remote --help` for full documentation.
14#[derive(Debug)]
15pub struct LsRemote<'a> {
16    repo_path: Option<&'a Path>,
17    heads: bool,
18    symref: bool,
19    remote: Option<&'a Remote>,
20    pattern: Option<&'a str>,
21}
22
23impl<'a> LsRemote<'a> {
24    #[must_use]
25    fn new() -> Self {
26        Self {
27            repo_path: None,
28            heads: false,
29            symref: false,
30            remote: None,
31            pattern: None,
32        }
33    }
34
35    /// Set the repository path (`-C <path>`).
36    #[must_use]
37    pub fn repo_path(mut self, path: &'a Path) -> Self {
38        self.repo_path = Some(path);
39        self
40    }
41
42    crate::flag_methods! {
43        /// Limit to refs/heads (branches only).
44        ///
45        /// Corresponds to `--heads`.
46        pub fn heads / heads_if, heads, "Conditionally limit to refs/heads."
47    }
48
49    crate::flag_methods! {
50        /// Show underlying ref in addition to the object.
51        ///
52        /// Corresponds to `--symref`. Useful for finding the default branch.
53        pub fn symref / symref_if, symref, "Conditionally show underlying ref."
54    }
55
56    /// Set the remote repository to query.
57    #[must_use]
58    pub fn remote(mut self, remote: &'a Remote) -> Self {
59        self.remote = Some(remote);
60        self
61    }
62
63    /// Set the pattern to filter refs.
64    #[must_use]
65    pub fn pattern(mut self, pattern: &'a str) -> Self {
66        self.pattern = Some(pattern);
67        self
68    }
69
70    /// Capture stdout from this command.
71    #[must_use]
72    pub fn stdout(self) -> cmd_proc::Capture {
73        self.build().stdout()
74    }
75
76    fn build(self) -> cmd_proc::Command {
77        crate::base_command(self.repo_path)
78            .argument("ls-remote")
79            .optional_argument(self.heads.then_some("--heads"))
80            .optional_argument(self.symref.then_some("--symref"))
81            .optional_argument(self.remote)
82            .optional_argument(self.pattern)
83    }
84}
85
86impl Default for LsRemote<'_> {
87    fn default() -> Self {
88        Self::new()
89    }
90}
91
92#[cfg(feature = "test-utils")]
93impl LsRemote<'_> {
94    /// Compare the built command with another command using debug representation.
95    pub fn test_eq(&self, other: &cmd_proc::Command) {
96        let command = Self {
97            repo_path: self.repo_path,
98            heads: self.heads,
99            symref: self.symref,
100            remote: self.remote,
101            pattern: self.pattern,
102        }
103        .build();
104        command.test_eq(other);
105    }
106}