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