Skip to main content

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 async fn status(self) -> Result<(), CommandError> {
48        crate::Build::build(self).status().await
49    }
50}
51
52impl crate::Build for Clone<'_> {
53    fn build(self) -> cmd_proc::Command {
54        cmd_proc::Command::new("git")
55            .argument("clone")
56            .optional_flag(self.bare, "--bare")
57            .argument(self.url)
58            .optional_argument(self.directory)
59    }
60}
61
62#[cfg(feature = "test-utils")]
63impl Clone<'_> {
64    /// Compare the built command with another command using debug representation.
65    pub fn test_eq(&self, other: &cmd_proc::Command) {
66        let command = crate::Build::build(Self {
67            url: self.url,
68            directory: self.directory,
69            bare: self.bare,
70        });
71        command.test_eq(other);
72    }
73}