1use std::path::Path;
2
3use crate::CommandError;
4
5#[must_use]
7pub fn new() -> Init<'static> {
8 Init::new()
9}
10
11#[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 #[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 pub fn bare / bare_if, bare, "Conditionally create a bare repository."
41 }
42
43 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 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}