Skip to main content

dial/git/
mod.rs

1use crate::errors::{DialError, Result};
2use std::process::Command;
3
4pub fn git_is_repo() -> bool {
5    Command::new("git")
6        .args(["rev-parse", "--git-dir"])
7        .output()
8        .map(|o| o.status.success())
9        .unwrap_or(false)
10}
11
12pub fn git_has_changes() -> bool {
13    Command::new("git")
14        .args(["status", "--porcelain"])
15        .output()
16        .map(|o| !o.stdout.is_empty())
17        .unwrap_or(false)
18}
19
20pub fn git_commit(message: &str) -> Result<Option<String>> {
21    // Stage all changes
22    let add_result = Command::new("git")
23        .args(["add", "-A"])
24        .output()?;
25
26    if !add_result.status.success() {
27        return Err(DialError::GitError("Failed to stage changes".to_string()));
28    }
29
30    // Commit
31    let commit_result = Command::new("git")
32        .args(["commit", "-m", message])
33        .output()?;
34
35    if !commit_result.status.success() {
36        return Ok(None);
37    }
38
39    // Get commit hash
40    let hash_result = Command::new("git")
41        .args(["rev-parse", "HEAD"])
42        .output()?;
43
44    if hash_result.status.success() {
45        let hash = String::from_utf8_lossy(&hash_result.stdout).trim().to_string();
46        Ok(Some(hash))
47    } else {
48        Ok(None)
49    }
50}
51
52pub fn git_revert_to(commit_hash: &str) -> Result<bool> {
53    let result = Command::new("git")
54        .args(["reset", "--hard", commit_hash])
55        .output()?;
56
57    Ok(result.status.success())
58}
59
60pub fn git_get_last_commit() -> Option<String> {
61    Command::new("git")
62        .args(["rev-parse", "HEAD"])
63        .output()
64        .ok()
65        .filter(|o| o.status.success())
66        .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
67}