1use std::path::Path;
2
3use crate::url::Remote;
4
5#[must_use]
7pub fn new() -> LsRemote<'static> {
8 LsRemote::new()
9}
10
11#[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 #[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 pub fn heads / heads_if, heads, "Conditionally limit to refs/heads."
47 }
48
49 crate::flag_methods! {
50 pub fn symref / symref_if, symref, "Conditionally show underlying ref."
54 }
55
56 #[must_use]
58 pub fn remote(mut self, remote: &'a Remote) -> Self {
59 self.remote = Some(remote);
60 self
61 }
62
63 #[must_use]
65 pub fn pattern(mut self, pattern: &'a str) -> Self {
66 self.pattern = Some(pattern);
67 self
68 }
69
70 #[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 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}