Skip to main content

git_proc/
clone.rs

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