Skip to main content

git_proc/
init.rs

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