git_revise/git/add.rs
1// use git2::{IndexAddOption, Repository};
2
3// use crate::ReviseResult;
4
5// pub trait GitAdd {
6// fn git_add(repo: &Repository, paths: &[String]) -> ReviseResult<()> {
7// let mut index = repo.index()?;
8// index.add_all(paths.iter(), IndexAddOption::DEFAULT, None)?;
9
10// index.write()?;
11// Ok(())
12// }
13// }
14
15// #[cfg(test)]
16// mod tests {
17// use super::*;
18
19// pub struct TestGitAdd {}
20
21// impl GitAdd for TestGitAdd {}
22
23// #[ignore]
24// #[test]
25// fn test_git_add() {
26// let repo = Repository::open(".").unwrap();
27// let paths = vec![".".to_string()];
28// let result = TestGitAdd::git_add(&repo, &paths);
29// assert!(result.is_ok());
30// }
31// }
32
33use std::process::Command;
34
35use crate::error::ReviseResult;
36
37pub trait GitAdd {
38 fn git_add(paths: &[String]) -> ReviseResult<()> {
39 let mut args = vec!["add"];
40 args.extend(paths.iter().map(std::string::String::as_str));
41
42 let output = Command::new("git").args(&args).output()?;
43
44 if !output.status.success() {
45 return Err(anyhow::anyhow!(
46 "Git add failed: {}",
47 String::from_utf8_lossy(&output.stderr)
48 ));
49 }
50
51 Ok(())
52 }
53}