1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use std::process::Command;
pub fn get_recent_changes() -> Option<String> {
// 1. Check if git is available and we are in a git repository
let is_git_repo = Command::new("git")
.arg("rev-parse")
.arg("--is-inside-work-tree")
.output()
.map(|o| o.status.success())
.unwrap_or(false);
if !is_git_repo {
return None;
}
// 2. Try to get uncommitted changes first (working directory diff)
let mut diff = Command::new("git")
.arg("diff")
.arg("HEAD")
.output()
.ok()
.and_then(|o| String::from_utf8(o.stdout).ok())
.unwrap_or_default();
// 3. If no uncommitted changes, get the last commit
if diff.trim().is_empty() {
diff = Command::new("git")
.arg("show")
.arg("HEAD")
.output()
.ok()
.and_then(|o| String::from_utf8(o.stdout).ok())
.unwrap_or_default();
}
// Limit diff size to prevent token blowup (e.g., limit to ~5,000 chars)
if diff.len() > 5000 {
diff.truncate(5000);
diff.push_str("\n... (diff truncated)");
}
if diff.trim().is_empty() {
None
} else {
Some(diff)
}
}