Skip to main content

git_proc/
init.rs

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