Skip to main content

lean_ctx/core/patterns/git/
mod.rs

1mod commit;
2mod diff;
3mod log;
4mod parser;
5mod status;
6
7use parser::extract_git_subcommand;
8
9macro_rules! static_regex {
10    ($pattern:expr) => {{
11        static RE: std::sync::OnceLock<regex::Regex> = std::sync::OnceLock::new();
12        RE.get_or_init(|| {
13            regex::Regex::new($pattern).expect(concat!("BUG: invalid static regex: ", $pattern))
14        })
15    }};
16}
17
18fn status_branch_re() -> &'static regex::Regex {
19    static_regex!(r"On branch (\S+)")
20}
21fn ahead_re() -> &'static regex::Regex {
22    static_regex!(r"ahead of .+ by (\d+) commit")
23}
24fn commit_hash_re() -> &'static regex::Regex {
25    static_regex!(r"\[([\w/.:-]+)\s+([a-f0-9]+)\]")
26}
27fn insertions_re() -> &'static regex::Regex {
28    static_regex!(r"(\d+) insertions?\(\+\)")
29}
30fn deletions_re() -> &'static regex::Regex {
31    static_regex!(r"(\d+) deletions?\(-\)")
32}
33fn files_changed_re() -> &'static regex::Regex {
34    static_regex!(r"(\d+) files? changed")
35}
36fn clone_objects_re() -> &'static regex::Regex {
37    static_regex!(r"Receiving objects:.*?(\d+)")
38}
39fn stash_re() -> &'static regex::Regex {
40    static_regex!(r"stash@\{(\d+)\}:\s*(.+)")
41}
42
43fn is_diff_or_stat_line(line: &str) -> bool {
44    let t = line.trim();
45    t.starts_with("diff --git")
46        || t.starts_with("index ")
47        || t.starts_with("--- a/")
48        || t.starts_with("+++ b/")
49        || t.starts_with("@@ ")
50        || t.starts_with("Binary files")
51        || t.starts_with("new file mode")
52        || t.starts_with("deleted file mode")
53        || t.starts_with("old mode")
54        || t.starts_with("new mode")
55        || t.starts_with("similarity index")
56        || t.starts_with("rename from")
57        || t.starts_with("rename to")
58        || t.starts_with("copy from")
59        || t.starts_with("copy to")
60        || (t.starts_with('+') && !t.starts_with("+++"))
61        || (t.starts_with('-') && !t.starts_with("---"))
62        || (t.contains(" | ") && t.chars().any(|c| c == '+' || c == '-'))
63}
64
65fn extract_change_stats(output: &str) -> String {
66    let files = files_changed_re()
67        .captures(output)
68        .and_then(|c| c[1].parse::<u32>().ok())
69        .unwrap_or(0);
70    let ins = insertions_re()
71        .captures(output)
72        .and_then(|c| c[1].parse::<u32>().ok())
73        .unwrap_or(0);
74    let del = deletions_re()
75        .captures(output)
76        .and_then(|c| c[1].parse::<u32>().ok())
77        .unwrap_or(0);
78
79    if files > 0 || ins > 0 || del > 0 {
80        format!("{files} files, +{ins}/-{del}")
81    } else {
82        String::new()
83    }
84}
85
86fn compact_lines(text: &str, max: usize) -> String {
87    let lines: Vec<&str> = text.lines().filter(|l| !l.trim().is_empty()).collect();
88    if lines.len() <= max {
89        return lines.join("\n");
90    }
91    format!(
92        "{}\n... ({} more lines)",
93        lines[..max].join("\n"),
94        lines.len() - max
95    )
96}
97
98pub fn compress(command: &str, output: &str) -> Option<String> {
99    let sub = extract_git_subcommand(command)?;
100    match sub {
101        "status" => Some(status::compress_status(output)),
102        "log" => Some(log::compress_log(command, output)),
103        "diff" => Some(diff::compress_diff(output)),
104        "add" => Some(commit::compress_add(output)),
105        "commit" => Some(commit::compress_commit(output)),
106        "push" => Some(commit::compress_push(output)),
107        "pull" => Some(commit::compress_pull(output)),
108        "fetch" => Some(commit::compress_fetch(output)),
109        "clone" => Some(commit::compress_clone(output)),
110        "branch" => Some(commit::compress_branch(output)),
111        "checkout" | "switch" => Some(commit::compress_checkout(output)),
112        "merge" => Some(commit::compress_merge(output)),
113        "stash" => {
114            if command.contains("stash show") || command.contains("show stash") {
115                return Some(commit::compress_show(output));
116            }
117            Some(commit::compress_stash(output))
118        }
119        "tag" => Some(commit::compress_tag(output)),
120        "reset" => Some(commit::compress_reset(output)),
121        "remote" => {
122            if command.contains("remote add") {
123                return Some(commit::compress_add(output));
124            }
125            Some(commit::compress_remote(output))
126        }
127        "blame" => Some(commit::compress_blame(output)),
128        "cherry-pick" => Some(commit::compress_cherry_pick(output)),
129        "show" => Some(commit::compress_show(output)),
130        "rebase" => Some(commit::compress_rebase(output)),
131        "submodule" => Some(commit::compress_submodule(output)),
132        "worktree" => Some(commit::compress_worktree(output)),
133        "bisect" => Some(commit::compress_bisect(output)),
134        _ => None,
135    }
136}
137
138#[cfg(test)]
139mod tests {
140    use super::*;
141
142    #[test]
143    fn git_status_compresses() {
144        let output = "On branch main\nYour branch is up to date with 'origin/main'.\n\nChanges not staged for commit:\n  (use \"git add <file>...\" to update what will be committed)\n\n\tmodified:   src/main.rs\n\tmodified:   src/lib.rs\n\nno changes added to commit (use \"git add\" and/or \"git commit -a\")\n";
145        let result = compress("git status", output).unwrap();
146        assert!(result.contains("main"), "should contain branch name");
147        assert!(result.contains("main.rs"), "should list modified files");
148        assert!(result.len() < output.len(), "should be shorter than input");
149    }
150
151    #[test]
152    fn git_add_compresses_to_ok() {
153        let result = compress("git add .", "").unwrap();
154        assert!(result.contains("ok"), "git add should compress to 'ok'");
155    }
156
157    #[test]
158    fn git_commit_extracts_hash() {
159        let output =
160            "[main abc1234] fix: resolve bug\n 2 files changed, 10 insertions(+), 3 deletions(-)\n";
161        let result = compress("git commit -m 'fix'", output).unwrap();
162        assert!(result.contains("abc1234"), "should extract commit hash");
163    }
164
165    #[test]
166    fn git_push_compresses() {
167        let output = "Enumerating objects: 5, done.\nCounting objects: 100% (5/5), done.\nDelta compression using up to 8 threads\nCompressing objects: 100% (3/3), done.\nWriting objects: 100% (3/3), 1.2 KiB | 1.2 MiB/s, done.\nTotal 3 (delta 2), reused 0 (delta 0)\nTo github.com:user/repo.git\n   abc1234..def5678  main -> main\n";
168        let result = compress("git push", output).unwrap();
169        assert!(result.len() < output.len(), "should compress push output");
170    }
171
172    #[test]
173    fn git_log_compresses() {
174        let output = "commit abc1234567890\nAuthor: User <user@email.com>\nDate:   Mon Mar 25 10:00:00 2026 +0100\n\n    feat: add feature\n\ncommit def4567890abc\nAuthor: User <user@email.com>\nDate:   Sun Mar 24 09:00:00 2026 +0100\n\n    fix: resolve issue\n";
175        let result = compress("git log", output).unwrap();
176        assert!(result.len() < output.len(), "should compress log output");
177    }
178
179    #[test]
180    fn git_log_oneline_truncates_long() {
181        let lines: Vec<String> = (0..150)
182            .map(|i| format!("abc{i:04} feat: commit number {i}"))
183            .collect();
184        let output = lines.join("\n");
185        let result = compress("git log --oneline", &output).unwrap();
186        assert!(
187            result.contains("... (50 more commits"),
188            "should truncate to 100 entries"
189        );
190        assert!(
191            result.lines().count() <= 102,
192            "should have at most 101 lines (100 + summary)"
193        );
194    }
195
196    #[test]
197    fn git_log_oneline_short_unchanged() {
198        let output = "abc1234 feat: one\ndef5678 fix: two\nghi9012 docs: three";
199        let result = compress("git log --oneline", output).unwrap();
200        assert_eq!(result, output, "short oneline should pass through");
201    }
202
203    #[test]
204    fn git_log_standard_truncates_long() {
205        let mut output = String::new();
206        for i in 0..130 {
207            output.push_str(&format!(
208                "commit {i:07}abc1234\nAuthor: U <u@e.com>\nDate:   Mon\n\n    msg {i}\n\n"
209            ));
210        }
211        let result = compress("git log", &output).unwrap();
212        assert!(
213            result.contains("... (30 more commits"),
214            "should truncate standard log at 100"
215        );
216    }
217
218    #[test]
219    fn git_diff_compresses() {
220        let output = "diff --git a/src/main.rs b/src/main.rs\nindex abc1234..def5678 100644\n--- a/src/main.rs\n+++ b/src/main.rs\n@@ -1,3 +1,4 @@\n fn main() {\n+    println!(\"hello\");\n     let x = 1;\n }";
221        let result = compress("git diff", output).unwrap();
222        assert!(result.contains("main.rs"), "should reference changed file");
223    }
224
225    #[test]
226    fn git_push_preserves_pipeline_url() {
227        let output = "Enumerating objects: 5, done.\nCounting objects: 100% (5/5), done.\nDelta compression using up to 8 threads\nCompressing objects: 100% (3/3), done.\nWriting objects: 100% (3/3), 1.2 KiB | 1.2 MiB/s, done.\nTotal 3 (delta 2), reused 0 (delta 0)\nremote:\nremote: To create a merge request for main, visit:\nremote:   https://gitlab.com/user/repo/-/merge_requests/new?source=main\nremote:\nremote: View pipeline for this push:\nremote:   https://gitlab.com/user/repo/-/pipelines/12345\nremote:\nTo gitlab.com:user/repo.git\n   abc1234..def5678  main -> main\n";
228        let result = compress("git push", output).unwrap();
229        assert!(
230            result.contains("pipeline"),
231            "should preserve pipeline URL, got: {result}"
232        );
233        assert!(
234            result.contains("merge_request"),
235            "should preserve merge request URL"
236        );
237        assert!(result.contains("->"), "should contain ref update line");
238    }
239
240    #[test]
241    fn git_push_preserves_github_pr_url() {
242        let output = "Enumerating objects: 5, done.\nremote:\nremote: Create a pull request for 'feature' on GitHub by visiting:\nremote:   https://github.com/user/repo/pull/new/feature\nremote:\nTo github.com:user/repo.git\n   abc1234..def5678  feature -> feature\n";
243        let result = compress("git push", output).unwrap();
244        assert!(
245            result.contains("pull/"),
246            "should preserve GitHub PR URL, got: {result}"
247        );
248    }
249
250    #[test]
251    fn git_commit_preserves_hook_output() {
252        let output = "Running pre-commit hooks...\ncheck-yaml..........passed\ncheck-json..........passed\nruff.................failed\nfixing src/app.py\n[main abc1234] fix: resolve bug\n 2 files changed, 10 insertions(+), 3 deletions(-)\n";
253        let result = compress("git commit -m 'fix'", output).unwrap();
254        assert!(
255            result.contains("ruff"),
256            "should preserve hook output, got: {result}"
257        );
258        assert!(
259            result.contains("abc1234"),
260            "should still extract commit hash"
261        );
262    }
263
264    #[test]
265    fn git_commit_no_hooks() {
266        let output =
267            "[main abc1234] fix: resolve bug\n 2 files changed, 10 insertions(+), 3 deletions(-)\n";
268        let result = compress("git commit -m 'fix'", output).unwrap();
269        assert!(result.contains("abc1234"), "should extract commit hash");
270        assert!(
271            !result.contains("hook"),
272            "should not mention hooks when none present"
273        );
274    }
275
276    #[test]
277    fn git_log_with_patch_keeps_hunks_for_few_commits() {
278        let output = "commit abc1234567890\nAuthor: User <user@email.com>\nDate:   Mon Mar 25 10:00:00 2026 +0100\n\n    feat: add feature\n\ndiff --git a/src/main.rs b/src/main.rs\nindex abc1234..def5678 100644\n--- a/src/main.rs\n+++ b/src/main.rs\n@@ -1,3 +1,4 @@\n fn main() {\n+    println!(\"hello\");\n     let x = 1;\n }\n\ncommit def4567890abc\nAuthor: User <user@email.com>\nDate:   Sun Mar 24 09:00:00 2026 +0100\n\n    fix: resolve issue\n\ndiff --git a/src/lib.rs b/src/lib.rs\nindex 111..222 100644\n--- a/src/lib.rs\n+++ b/src/lib.rs\n@@ -1 +1,2 @@\n+pub fn helper() {}\n";
279        let result = compress("git log -p", output).unwrap();
280        assert!(
281            result.contains("println"),
282            "1-3 commits with -p should KEEP diff hunks, got: {result}"
283        );
284        assert!(result.contains("abc1234"), "should contain commit hash");
285        assert!(
286            result.contains("feat: add feature"),
287            "should contain commit message"
288        );
289    }
290
291    #[test]
292    fn git_log_with_patch_summarizes_many_commits() {
293        let mut output = String::new();
294        for i in 0..10 {
295            output.push_str(&format!(
296                "commit {i:07}abc1234\nAuthor: U <u@e.com>\nDate:   Mon\n\n    msg {i}\n\ndiff --git a/src/f{i}.rs b/src/f{i}.rs\nindex 111..222 100644\n--- a/src/f{i}.rs\n+++ b/src/f{i}.rs\n@@ -1 +1,2 @@\n+line {i}\n\n"
297            ));
298        }
299        let result = compress("git log -p", &output).unwrap();
300        assert!(
301            result.contains("msg 0"),
302            "newest commit message should be present"
303        );
304        assert!(
305            result.contains("+line 0"),
306            "newest commit should have hunks preserved"
307        );
308        assert!(
309            result.len() < output.len(),
310            "should be compressed ({} vs {})",
311            result.len(),
312            output.len()
313        );
314    }
315
316    #[test]
317    fn git_log_with_stat_filters_stat_content() {
318        let mut output = String::new();
319        for i in 0..5 {
320            output.push_str(&format!(
321                "commit {i:07}abc1234\nAuthor: U <u@e.com>\nDate:   Mon\n\n    msg {i}\n\n src/file{i}.rs | 10 ++++------\n 1 file changed, 4 insertions(+), 6 deletions(-)\n\n"
322            ));
323        }
324        let result = compress("git log --stat", &output).unwrap();
325        assert!(
326            result.len() < output.len() / 2,
327            "stat output should be compressed ({} vs {})",
328            result.len(),
329            output.len()
330        );
331    }
332
333    #[test]
334    fn git_commit_with_feature_branch() {
335        let output = "[feature/my-branch abc1234] feat: add new thing\n 3 files changed, 20 insertions(+), 5 deletions(-)\n";
336        let result = compress("git commit -m 'feat'", output).unwrap();
337        assert!(
338            result.contains("abc1234"),
339            "should extract hash from feature branch, got: {result}"
340        );
341        assert!(
342            result.contains("feature/my-branch"),
343            "should preserve branch name, got: {result}"
344        );
345    }
346
347    #[test]
348    fn git_commit_many_hooks_compressed() {
349        let mut output = String::new();
350        for i in 0..30 {
351            output.push_str(&format!("check-{i}..........passed\n"));
352        }
353        output.push_str("[main abc1234] fix: resolve bug\n 1 file changed, 1 insertion(+)\n");
354        let result = compress("git commit -m 'fix'", &output).unwrap();
355        assert!(result.contains("abc1234"), "should contain commit hash");
356        assert!(
357            result.contains("hooks passed"),
358            "should summarize passed hooks, got: {result}"
359        );
360        assert!(
361            result.len() < output.len() / 2,
362            "should compress verbose hook output ({} vs {})",
363            result.len(),
364            output.len()
365        );
366    }
367
368    #[test]
369    fn stash_push_preserves_short_message() {
370        let output = "Saved working directory and index state WIP on main: abc1234 fix stuff\n";
371        let result = compress("git stash", output).unwrap();
372        assert!(
373            result.contains("Saved working directory"),
374            "short stash messages must be preserved, got: {result}"
375        );
376    }
377
378    #[test]
379    fn stash_drop_preserves_short_message() {
380        let output = "Dropped refs/stash@{0} (abc123def456)\n";
381        let result = compress("git stash drop", output).unwrap();
382        assert!(
383            result.contains("Dropped"),
384            "short drop messages must be preserved, got: {result}"
385        );
386    }
387
388    #[test]
389    fn stash_list_short_preserved() {
390        let output = "stash@{0}: WIP on main: abc1234 fix\nstash@{1}: On feature: def5678 add\n";
391        let result = compress("git stash list", output).unwrap();
392        assert!(
393            result.contains("stash@{0}"),
394            "short stash list must be preserved, got: {result}"
395        );
396    }
397
398    #[test]
399    fn stash_list_long_reformats() {
400        let lines: Vec<String> = (0..10)
401            .map(|i| format!("stash@{{{i}}}: WIP on main: abc{i:04} commit {i}"))
402            .collect();
403        let output = lines.join("\n");
404        let result = compress("git stash list", &output).unwrap();
405        assert!(result.contains("@0:"), "should reformat @0, got: {result}");
406        assert!(result.contains("@9:"), "should reformat @9, got: {result}");
407    }
408
409    #[test]
410    fn stash_show_routes_to_show_compressor() {
411        let output = " src/main.rs | 10 +++++-----\n src/lib.rs  |  3 ++-\n 2 files changed, 7 insertions(+), 6 deletions(-)\n";
412        let result = compress("git stash show", output).unwrap();
413        assert!(
414            result.contains("main.rs"),
415            "stash show should preserve file names, got: {result}"
416        );
417    }
418
419    #[test]
420    fn stash_show_patch_not_over_compressed() {
421        let lines: Vec<String> = (0..40)
422            .map(|i| format!("+line {i}: some content here"))
423            .collect();
424        let output = format!(
425            "diff --git a/file.rs b/file.rs\n--- a/file.rs\n+++ b/file.rs\n{}",
426            lines.join("\n")
427        );
428        let result = compress("git stash show -p", &output).unwrap();
429        let result_lines = result.lines().count();
430        assert!(
431            result_lines >= 10,
432            "stash show -p must not over-compress to 3 lines, got {result_lines} lines"
433        );
434    }
435
436    #[test]
437    fn show_stash_ref_routes_correctly() {
438        let output = "commit abc1234\nAuthor: User <u@e.com>\nDate: Mon Jan 1\n\n    WIP on main\n\ndiff --git a/f.rs b/f.rs\n";
439        let result = compress("git show stash@{0}", output).unwrap();
440        assert!(
441            result.len() > 10,
442            "git show stash@{{0}} must not be over-compressed, got: {result}"
443        );
444    }
445
446    #[test]
447    fn extract_subcommand_basic() {
448        assert_eq!(extract_git_subcommand("git status"), Some("status"));
449        assert_eq!(extract_git_subcommand("git log --oneline"), Some("log"));
450        assert_eq!(extract_git_subcommand("git diff HEAD~1"), Some("diff"));
451        assert_eq!(
452            extract_git_subcommand("git -C /tmp commit -m 'x'"),
453            Some("commit")
454        );
455    }
456
457    #[test]
458    fn extract_subcommand_avoids_filename_ambiguity() {
459        assert_eq!(
460            extract_git_subcommand("git log status.txt"),
461            Some("log"),
462            "should NOT match 'status' in filename"
463        );
464        assert_eq!(
465            extract_git_subcommand("git add commit.rs"),
466            Some("add"),
467            "should NOT match 'commit' in filename"
468        );
469    }
470
471    #[test]
472    fn extract_subcommand_full_path() {
473        assert_eq!(
474            extract_git_subcommand("/usr/bin/git status"),
475            Some("status")
476        );
477    }
478
479    #[test]
480    fn extract_subcommand_no_git() {
481        assert_eq!(extract_git_subcommand("cargo build"), None);
482    }
483
484    #[test]
485    fn filename_not_treated_as_subcommand() {
486        let output = "On branch main\nnothing to commit\n";
487        assert!(
488            compress("git log status.txt", output).is_some(),
489            "should route to log, not status"
490        );
491    }
492}