git_proc/
clone.rs

1use std::path::Path;
2
3use crate::CommandError;
4use crate::url::GitUrl;
5
6/// Create a new `git clone` command builder.
7#[must_use]
8pub fn new(url: &GitUrl) -> Clone<'_> {
9    Clone::new(url)
10}
11
12/// Builder for `git clone` command.
13///
14/// See `git clone --help` for full documentation.
15#[derive(Debug)]
16pub struct Clone<'a> {
17    url: &'a GitUrl,
18    directory: Option<&'a Path>,
19    bare: bool,
20}
21
22impl<'a> Clone<'a> {
23    #[must_use]
24    fn new(url: &'a GitUrl) -> Self {
25        Self {
26            url,
27            directory: None,
28            bare: false,
29        }
30    }
31
32    /// Set the destination directory.
33    #[must_use]
34    pub fn directory(mut self, path: &'a Path) -> Self {
35        self.directory = Some(path);
36        self
37    }
38
39    crate::flag_methods! {
40        /// Make a bare clone.
41        ///
42        /// Corresponds to `--bare`.
43        pub fn bare / bare_if, bare, "Conditionally make a bare clone."
44    }
45
46    /// Execute the command and return the exit status.
47    pub fn status(self) -> Result<(), CommandError> {
48        self.build().status()
49    }
50
51    fn build(self) -> cmd_proc::Command {
52        cmd_proc::Command::new("git")
53            .argument("clone")
54            .optional_argument(self.bare.then_some("--bare"))
55            .argument(self.url)
56            .optional_argument(self.directory)
57    }
58}
59
60#[cfg(feature = "test-utils")]
61impl Clone<'_> {
62    /// Compare the built command with another command using debug representation.
63    pub fn test_eq(&self, other: &cmd_proc::Command) {
64        let command = Self {
65            url: self.url,
66            directory: self.directory,
67            bare: self.bare,
68        }
69        .build();
70        command.test_eq(other);
71    }
72}