cargo_mobile2/util/git/
mod.rs

1pub mod lfs;
2pub mod repo;
3pub mod submodule;
4
5use std::{fs, io, path::Path};
6
7#[derive(Clone, Copy, Debug)]
8pub struct Git<'a> {
9    root: &'a Path,
10}
11
12impl<'a> Git<'a> {
13    pub fn new(root: &'a Path) -> Self {
14        Self { root }
15    }
16
17    pub fn root(&'a self) -> &'a Path {
18        self.root
19    }
20
21    pub fn command(&self) -> duct::Expression {
22        duct::cmd(
23            "git",
24            ["-C", self.root.as_os_str().to_str().unwrap_or_default()],
25        )
26    }
27
28    pub fn command_parse(&self, arg_str: impl AsRef<str>) -> duct::Expression {
29        let mut args = vec!["-C", self.root.as_os_str().to_str().unwrap_or_default()];
30        for arg in arg_str.as_ref().split(' ') {
31            args.push(arg)
32        }
33        duct::cmd("git", args)
34    }
35
36    pub fn init(&self) -> std::io::Result<()> {
37        if !self.root.join(".git").exists() {
38            self.command()
39                .before_spawn(|cmd| {
40                    cmd.arg("init");
41                    Ok(())
42                })
43                .run()?;
44        }
45        Ok(())
46    }
47
48    pub fn config(&self) -> io::Result<Option<String>> {
49        let path = self.root.join(".git/config");
50        if path.exists() {
51            fs::read_to_string(&path).map(Some)
52        } else {
53            Ok(None)
54        }
55    }
56
57    pub fn modules(&self) -> io::Result<Option<String>> {
58        let path = self.root.join(".gitmodules");
59        if path.exists() {
60            fs::read_to_string(&path).map(Some)
61        } else {
62            Ok(None)
63        }
64    }
65
66    pub fn user_name(&self) -> std::io::Result<String> {
67        self.command()
68            .before_spawn(|cmd| {
69                cmd.args(["config", "user.name"]);
70                Ok(())
71            })
72            .read()
73    }
74
75    pub fn user_email(&self) -> std::io::Result<String> {
76        self.command()
77            .before_spawn(|cmd| {
78                cmd.args(["config", "user.email"]);
79                Ok(())
80            })
81            .read()
82    }
83}