cargo_mobile2/util/git/
mod.rs

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
pub mod lfs;
pub mod repo;
pub mod submodule;

use std::{fs, io, 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) -> duct::Expression {
        duct::cmd(
            "git",
            ["-C", self.root.as_os_str().to_str().unwrap_or_default()],
        )
    }

    pub fn command_parse(&self, arg_str: impl AsRef<str>) -> duct::Expression {
        let mut args = vec!["-C", self.root.as_os_str().to_str().unwrap_or_default()];
        for arg in arg_str.as_ref().split(' ') {
            args.push(arg)
        }
        duct::cmd("git", args)
    }

    pub fn init(&self) -> std::io::Result<()> {
        if !self.root.join(".git").exists() {
            self.command()
                .before_spawn(|cmd| {
                    cmd.arg("init");
                    Ok(())
                })
                .run()?;
        }
        Ok(())
    }

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

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

    pub fn user_name(&self) -> std::io::Result<String> {
        self.command()
            .before_spawn(|cmd| {
                cmd.args(["config", "user.name"]);
                Ok(())
            })
            .read()
    }

    pub fn user_email(&self) -> std::io::Result<String> {
        self.command()
            .before_spawn(|cmd| {
                cmd.args(["config", "user.email"]);
                Ok(())
            })
            .read()
    }
}