Skip to main content

gcop_rs/git/
commit.rs

1use std::process::Command;
2
3use crate::error::{GcopError, Result};
4
5/// Execute git commit
6///
7/// Use git CLI instead of git2 to support:
8/// - GPG signature (commit.gpgsign, user.signingkey)
9/// - Git hooks (pre-commit, commit-msg, etc.)
10/// - All git config configurations
11///
12/// # Arguments
13/// * `message` - Commit message
14pub fn commit_changes(message: &str) -> Result<()> {
15    let output = Command::new("git")
16        .args(["commit", "-m", message])
17        .output()?;
18
19    if !output.status.success() {
20        let stderr = String::from_utf8_lossy(&output.stderr);
21        let error_msg = if stderr.trim().is_empty() {
22            // Some git errors are output to stdout instead of stderr
23            String::from_utf8_lossy(&output.stdout).trim().to_string()
24        } else {
25            stderr.trim().to_string()
26        };
27        return Err(GcopError::GitCommand(error_msg));
28    }
29
30    Ok(())
31}
32
33/// Execute git commit --amend
34///
35/// Use git CLI instead of git2 to support:
36/// - GPG signature (commit.gpgsign, user.signingkey)
37/// - Git hooks (pre-commit, commit-msg, etc.)
38/// - All git config configurations
39///
40/// # Arguments
41/// * `message` - New commit message
42pub fn commit_amend_changes(message: &str) -> Result<()> {
43    let output = Command::new("git")
44        .args(["commit", "--amend", "-m", message])
45        .output()?;
46
47    if !output.status.success() {
48        let stderr = String::from_utf8_lossy(&output.stderr);
49        let error_msg = if stderr.trim().is_empty() {
50            String::from_utf8_lossy(&output.stdout).trim().to_string()
51        } else {
52            stderr.trim().to_string()
53        };
54        return Err(GcopError::GitCommand(error_msg));
55    }
56
57    Ok(())
58}