1use miette::{Context as _, IntoDiagnostic, Result};
2use std::{
3 path::{Path, PathBuf},
4 process::Stdio,
5};
6
7pub struct Git {
8 root_path: PathBuf,
9 git_dir_path: PathBuf,
10}
11impl Git {
12 pub fn new(root_path: impl Into<PathBuf>, git_dir_path: impl Into<PathBuf>) -> Self {
13 Self {
14 root_path: root_path.into(),
15 git_dir_path: git_dir_path.into(),
16 }
17 }
18
19 pub fn start_git_command_builder(&self) -> std::process::Command {
20 let mut cmd = std::process::Command::new("git");
21 cmd.current_dir(&self.root_path).args([
22 "--git-dir",
23 &self.git_dir_path.to_string_lossy(),
24 "--work-tree",
25 &self.root_path.to_string_lossy(),
26 ]);
27 cmd
28 }
29
30 pub fn add(&self, path: impl AsRef<Path>) -> Result<()> {
31 let output = self
32 .start_git_command_builder()
33 .args(["add", &path.as_ref().display().to_string()])
34 .stdin(Stdio::null())
35 .stdout(Stdio::inherit())
36 .stderr(Stdio::inherit())
37 .output()
38 .into_diagnostic()
39 .wrap_err("git add failed to run")?;
40 miette::ensure!(output.status.success(), "git add failed");
41 Ok(())
42 }
43}