Skip to main content

git_proc/
ls_remote.rs

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