1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
pub mod repo;
pub mod submodule;

use std::path::Path;

#[derive(Clone, Copy, Debug)]
pub struct Git<'a> {
    root: &'a Path,
}

impl<'a> Git<'a> {
    pub fn new(root: &'a Path) -> Self {
        Self { root }
    }

    pub fn root(&'a self) -> &'a Path {
        self.root
    }

    pub fn command(&self) -> bossy::Command {
        bossy::Command::impure("git")
            .with_arg("-C")
            .with_arg(self.root)
    }

    pub fn command_parse(&self, arg_str: impl AsRef<str>) -> bossy::Command {
        self.command().with_parsed_args(arg_str)
    }

    pub fn init(&self) -> bossy::Result<()> {
        if !self.root.join(".git").exists() {
            self.command().with_arg("init").run_and_wait()?;
        }
        Ok(())
    }

    pub fn config(&self) -> std::io::Result<Option<String>> {
        let path = self.root.join(".git/config");
        if path.exists() {
            std::fs::read_to_string(&path).map(Some)
        } else {
            Ok(None)
        }
    }

    pub fn modules(&self) -> std::io::Result<Option<String>> {
        let path = self.root.join(".gitmodules");
        if path.exists() {
            std::fs::read_to_string(&path).map(Some)
        } else {
            Ok(None)
        }
    }

    pub fn user_name(&self) -> bossy::Result<bossy::Output> {
        self.command()
            .with_args(&["config", "user.name"])
            .run_and_wait_for_output()
    }

    pub fn user_email(&self) -> bossy::Result<bossy::Output> {
        self.command()
            .with_args(&["config", "user.email"])
            .run_and_wait_for_output()
    }
}