aboutsummaryrefslogtreecommitdiffstats
path: root/src/git.rs
diff options
context:
space:
mode:
authorsalmonik <salmonik@tuta.io>2026-11-18 12:45:05 +0100
committersalmonik <salmonik@tuta.io>2026-11-18 12:45:05 +0100
commit226c44dc770dd0ea42e322af4d0edd6c14ce14db (patch)
treee41318cd028770a1d16408712a5549ef59e36622 /src/git.rs
parent0.10.0: --json, /models /model /forget, git-repo gate for --yolo (diff)
downloadagent686-226c44dc770dd0ea42e322af4d0edd6c14ce14db.tar.gz
agent686-226c44dc770dd0ea42e322af4d0edd6c14ce14db.zip
0.11.0: /undo via git checkpoints, /compact, provider presets, leniency, system CA
- /undo resets to a git checkpoint taken before each turn - /compact summarises and rewrites the transcript (ADR 0003, the one sanctioned rewrite) - --provider presets for six OpenAI-compatible gateways, local ones lenient by default - --lenient and --system-ca - proxies already honoured via ureq; documented
Diffstat (limited to 'src/git.rs')
-rw-r--r--src/git.rs57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/git.rs b/src/git.rs
new file mode 100644
index 0000000..78348af
--- /dev/null
+++ b/src/git.rs
@@ -0,0 +1,57 @@
1//! Git helpers for the checkpoint/undo feature. In a separate module so they
2//! are unit-testable without driving the whole CLI.
3
4use std::path::Path;
5use std::process::Command;
6
7/// Runs `git -C <dir> <args>`, returning trimmed stdout on success.
8pub fn run(dir: &Path, args: &[&str]) -> Option<String> {
9 Command::new("git")
10 .arg("-C")
11 .arg(dir)
12 .args(args)
13 .output()
14 .ok()
15 .and_then(|output| {
16 output
17 .status
18 .success()
19 .then(|| String::from_utf8_lossy(&output.stdout).trim().to_string())
20 })
21}
22
23/// True when `dir` is inside a work tree (a `.git` dir or file up the tree).
24pub fn in_repo(dir: &Path) -> bool {
25 let mut current = dir.to_path_buf();
26 loop {
27 if current.join(".git").exists() {
28 return true;
29 }
30 if !current.pop() {
31 return false;
32 }
33 }
34}
35
36/// Snapshots the tree as a commit when it is dirty, and returns HEAD. Returns
37/// `None` outside a repository.
38pub fn checkpoint(dir: &Path) -> Option<String> {
39 if !in_repo(dir) {
40 return None;
41 }
42 run(dir, &["add", "-A"]);
43 let dirty = run(dir, &["status", "--porcelain"])
44 .map(|status| !status.is_empty())
45 .unwrap_or(false);
46 if dirty {
47 run(dir, &["commit", "-m", "agent686: checkpoint"]);
48 }
49 run(dir, &["rev-parse", "HEAD"])
50}
51
52/// Resets to the checkpoint commit and drops untracked files, undoing a turn's
53/// changes.
54pub fn undo(dir: &Path, sha: &str) {
55 run(dir, &["reset", "--hard", sha]);
56 run(dir, &["clean", "-fd"]);
57}