Skip to main content

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
23crate::impl_repo_path!(LsRemote);
24
25impl<'a> LsRemote<'a> {
26    #[must_use]
27    fn new() -> Self {
28        Self {
29            repo_path: None,
30            heads: false,
31            symref: false,
32            remote: None,
33            pattern: None,
34        }
35    }
36
37    crate::flag_methods! {
38        /// Limit to refs/heads (branches only).
39        ///
40        /// Corresponds to `--heads`.
41        pub fn heads / heads_if, heads, "Conditionally limit to refs/heads."
42    }
43
44    crate::flag_methods! {
45        /// Show underlying ref in addition to the object.
46        ///
47        /// Corresponds to `--symref`. Useful for finding the default branch.
48        pub fn symref / symref_if, symref, "Conditionally show underlying ref."
49    }
50
51    /// Set the remote repository to query.
52    #[must_use]
53    pub fn remote(mut self, remote: &'a Remote) -> Self {
54        self.remote = Some(remote);
55        self
56    }
57
58    /// Set the pattern to filter refs.
59    #[must_use]
60    pub fn pattern(mut self, pattern: &'a str) -> Self {
61        self.pattern = Some(pattern);
62        self
63    }
64}
65
66impl Default for LsRemote<'_> {
67    fn default() -> Self {
68        Self::new()
69    }
70}
71
72impl crate::Build for LsRemote<'_> {
73    fn build(self) -> cmd_proc::Command {
74        crate::base_command(self.repo_path)
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            repo_path: self.repo_path,
89            heads: self.heads,
90            symref: self.symref,
91            remote: self.remote,
92            pattern: self.pattern,
93        });
94        command.test_eq(other);
95    }
96}