Skip to main content

atomcode_core/agent/
git_checkpoint.rs

1//! Lightweight git checkpoints for edit rollback.
2//!
3//! Uses `git stash create` to snapshot the working tree WITHOUT modifying
4//! the stash list or working tree. Rollback via `git stash apply <ref>`.
5
6use std::path::Path;
7use std::process::Command;
8
9/// Create a checkpoint. Returns SHA if there are uncommitted changes, None if clean.
10pub fn create_checkpoint(working_dir: &Path) -> Option<String> {
11    // Check if git repo
12    let mut check_cmd = Command::new("git");
13    check_cmd.args(["rev-parse", "--git-dir"])
14        .current_dir(working_dir);
15    crate::process_utils::suppress_console_window_sync(&mut check_cmd);
16    let is_git = check_cmd
17        .output()
18        .ok()
19        .map(|o| o.status.success())
20        .unwrap_or(false);
21    if !is_git {
22        return None;
23    }
24
25    // git stash create: creates stash commit, returns SHA. Empty if clean.
26    let mut stash_cmd = Command::new("git");
27    stash_cmd.args(["stash", "create"])
28        .current_dir(working_dir);
29    crate::process_utils::suppress_console_window_sync(&mut stash_cmd);
30    let output = stash_cmd.output().ok()?;
31
32    let sha = String::from_utf8_lossy(&output.stdout).trim().to_string();
33    if sha.is_empty() {
34        None
35    } else {
36        Some(sha)
37    }
38}