cargo-mobile2 0.22.4

Rust on mobile made easy!
Documentation
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(&self) -> &Path {
        self.root
    }

    pub fn command(&self) -> duct::Expression {
        duct::cmd("git", ["-C".as_ref(), self.root.as_os_str()])
    }

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