git_proc/
remote.rs

1use std::path::Path;
2
3use crate::CommandError;
4use crate::url::RemoteName;
5
6/// Create a `git remote get-url` command builder.
7#[must_use]
8pub fn get_url(name: &RemoteName) -> Remote<'_> {
9    Remote::get_url(name)
10}
11
12/// Builder for `git remote` command.
13///
14/// See `git remote --help` for full documentation.
15#[derive(Debug)]
16pub struct Remote<'a> {
17    repo_path: Option<&'a Path>,
18    subcommand: RemoteSubcommand<'a>,
19}
20
21#[derive(Debug)]
22enum RemoteSubcommand<'a> {
23    GetUrl { name: &'a RemoteName },
24}
25
26impl<'a> Remote<'a> {
27    #[must_use]
28    fn get_url(name: &'a RemoteName) -> Self {
29        Self {
30            repo_path: None,
31            subcommand: RemoteSubcommand::GetUrl { name },
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    /// Capture stdout from this command.
43    #[must_use]
44    pub fn stdout(self) -> cmd_proc::Capture {
45        self.build().stdout()
46    }
47
48    /// Execute and return full output regardless of exit status.
49    ///
50    /// Use this when you need to inspect stderr on failure.
51    pub fn output(self) -> Result<cmd_proc::Output, CommandError> {
52        self.build().output()
53    }
54
55    fn build(self) -> cmd_proc::Command {
56        let RemoteSubcommand::GetUrl { name } = self.subcommand;
57
58        crate::base_command(self.repo_path)
59            .argument("remote")
60            .argument("get-url")
61            .argument(name)
62    }
63}
64
65#[cfg(feature = "test-utils")]
66impl Remote<'_> {
67    /// Compare the built command with another command using debug representation.
68    pub fn test_eq(&self, other: &cmd_proc::Command) {
69        let command = Self {
70            repo_path: self.repo_path,
71            subcommand: match &self.subcommand {
72                RemoteSubcommand::GetUrl { name } => RemoteSubcommand::GetUrl { name },
73            },
74        }
75        .build();
76        command.test_eq(other);
77    }
78}