git_revise/git/cmit.rs
1// pub trait GitCommit {
2// fn git_cmit(
3// repo: &git2::Repository,
4// message: &str,
5// ) -> Result<(), git2::Error> {
6// // 写入index
7// let mut index = repo.index()?;
8// let oid = index.write_tree()?;
9
10// let tree = repo.find_tree(oid)?;
11
12// // 获取当前HEAD的commit
13// let head = repo.head().ok();
14// let parent_commit = head.as_ref().and_then(|h|
15// h.peel_to_commit().ok());
16
17// let parents = parent_commit.iter().collect::<Vec<_>>();
18
19// let conf = git2::Config::open_default()?;
20// let name = conf.get_string("user.name")?;
21// let email = conf.get_string("user.email")?;
22// let signature = git2::Signature::now(&name, &email)?;
23
24// repo.commit(
25// Some("HEAD"),
26// &signature,
27// &signature,
28// message,
29// &tree,
30// &parents,
31// )?;
32
33// Ok(())
34// }
35// }
36use std::process::Command;
37
38use crate::error::ReviseResult;
39
40pub trait GitCommit {
41 fn git_cmit(message: &str) -> ReviseResult<()> {
42 let output = Command::new("git")
43 .args(["commit", "-m", message])
44 .output()?;
45
46 if !output.status.success() {
47 return Err(anyhow::anyhow!(
48 "Git commit failed: {}",
49 String::from_utf8_lossy(&output.stderr)
50 ));
51 }
52
53 Ok(())
54 }
55}