lowfat 0.4.2

CLI binary for lowfat
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
//! Native git filter — compact status, diff, log, show output.

use anyhow::Result;
use lowfat_core::level::Level;
use lowfat_plugin::plugin::{FilterInput, FilterOutput, FilterPlugin, PluginInfo};

pub struct GitFilter;

impl FilterPlugin for GitFilter {
    fn info(&self) -> PluginInfo {
        PluginInfo {
            name: "git-compact".into(),
            version: env!("CARGO_PKG_VERSION").into(),
            commands: vec!["git".into()],
            // The first four have dedicated filter arms below; the rest are
            // listed so `lowfat history` breaks them out by subcommand instead
            // of collapsing them under bare `git`. They fall through to the
            // generic head_nonblank handler, which is fine for typically-short
            // output like `git add` or `git commit`.
            subcommands: vec![
                "status".into(),
                "log".into(),
                "diff".into(),
                "show".into(),
                "add".into(),
                "commit".into(),
                "checkout".into(),
                "switch".into(),
                "restore".into(),
                "branch".into(),
                "merge".into(),
                "rebase".into(),
                "reset".into(),
                "revert".into(),
                "cherry-pick".into(),
                "stash".into(),
                "tag".into(),
                "fetch".into(),
                "pull".into(),
                "push".into(),
                "clone".into(),
                "remote".into(),
                "init".into(),
                "config".into(),
                "blame".into(),
                "reflog".into(),
                "describe".into(),
                "rm".into(),
                "mv".into(),
                "clean".into(),
                "bisect".into(),
                "grep".into(),
            ],
        }
    }

    fn filter(&self, input: &FilterInput) -> Result<FilterOutput> {
        let text = match input.subcommand.as_str() {
            "status" => filter_status(&input.raw, input.level),
            "log" => filter_log(&input.raw, input.level),
            "diff" => filter_diff(&input.raw, input.level),
            "show" => filter_show(&input.raw, input.level),
            _ => head_nonblank(&input.raw, input.level.head_limit(30)),
        };
        Ok(FilterOutput {
            passthrough: text.is_empty(),
            text,
        })
    }
}

fn filter_status(raw: &str, level: Level) -> String {
    let limit = match level {
        Level::Lite => 60,
        Level::Full => 30,
        Level::Ultra => 15,
    };

    let lines: Vec<&str> = raw
        .lines()
        .filter(|line| match level {
            // Ultra: only short-status file lines (e.g. " M src/main.rs")
            Level::Ultra => {
                let trimmed = line.trim_start();
                trimmed.len() >= 2
                    && trimmed.as_bytes().get(1).copied() == Some(b' ')
                    && is_status_char(trimmed.as_bytes()[0])
            }
            // Lite: status lines + context headers
            Level::Lite => {
                let trimmed = line.trim_start();
                is_status_line(trimmed)
                    || trimmed.starts_with("## ")
                    || trimmed.starts_with("On branch")
                    || trimmed.starts_with("Changes")
                    || trimmed.starts_with("Untracked")
            }
            // Full: status lines + branch header
            Level::Full => {
                let trimmed = line.trim_start();
                is_status_line(trimmed) || trimmed.starts_with("## ")
            }
        })
        .take(limit)
        .collect();

    if lines.is_empty() {
        "git status: clean".into()
    } else {
        lines.join("\n")
    }
}

fn filter_log(raw: &str, level: Level) -> String {
    // All levels apply the same two redundancy drops as `git show`: strip
    // trailers from message bodies and abbreviate the long commit hash.
    match level {
        Level::Ultra => raw
            .lines()
            .filter(|l| (l.starts_with("commit ") || l.starts_with("    ")) && !is_trailer(l))
            .take(10)
            .map(|l| abbreviate_commit_line(l).unwrap_or_else(|| l.to_string()))
            .collect::<Vec<_>>()
            .join("\n"),
        // Lite/Full keep the full log shape; only the line cap differs.
        _ => {
            let limit = if level == Level::Lite { 50 } else { 25 };
            raw.lines()
                .filter(|l| !is_trailer(l))
                .take(limit)
                .map(|l| abbreviate_commit_line(l).unwrap_or_else(|| l.to_string()))
                .collect::<Vec<_>>()
                .join("\n")
        }
    }
}

fn filter_diff(raw: &str, level: Level) -> String {
    let limit = match level {
        Level::Lite => 400,
        Level::Ultra => 30,
        Level::Full => 200,
    };

    let lines = compact_diff_body(raw, level, limit);
    if lines.is_empty() {
        // Nothing matched — output is likely `git diff --stat` / `--name-only` /
        // `--shortstat`, which has no `diff `/`@@ ` markers. Return a compact
        // pass instead of empty (which would trigger raw passthrough and
        // record zero savings).
        return head_nonblank(raw, level.head_limit(50));
    }
    lines.join("\n")
}

fn filter_show(raw: &str, level: Level) -> String {
    match level {
        Level::Lite => {
            // Permissive: drop only index/mode meta and message trailers,
            // abbreviate the commit hash. Keep everything else, including
            // unchanged context lines, so callers asking for high-fidelity
            // output still get it.
            let cleaned: Vec<String> = raw
                .lines()
                .filter(|l| !is_index_meta(l) && !is_trailer(l))
                .take(200)
                .map(|l| abbreviate_commit_line(l).unwrap_or_else(|| l.to_string()))
                .collect();
            cleaned.join("\n")
        }
        Level::Ultra => {
            let cleaned: Vec<String> = raw
                .lines()
                .filter(|l| {
                    !is_trailer(l)
                        && (l.starts_with("commit ")
                            || l.starts_with("Author:")
                            || l.starts_with("Date:")
                            || l.starts_with("    ")
                            || l.starts_with("diff --git")
                            || (l.contains(" | ") && l.chars().any(|c| c == '+' || c == '-')))
                })
                .take(20)
                .map(|l| abbreviate_commit_line(l).unwrap_or_else(|| l.to_string()))
                .collect();
            cleaned.join("\n")
        }
        // Full: commit header + diff-content lines (same treatment as `git diff`).
        // The state machine drops three categories of redundancy:
        //   - pre-hunk metadata (`--- a/X`, `+++ b/X`, `index …`, mode lines) —
        //     `--- ` / `+++ ` always duplicate the path already on `diff --git`;
        //   - commit-message trailers (`Signed-off-by:`, `Co-authored-by:`, …)
        //     which add nothing for code understanding;
        //   - the long form of the commit hash (`commit <40-hex>` → 12-hex).
        // The 4-space rule for commit-message bodies is positional: once we cross
        // the first `diff ` line, leading "    " is deeply indented diff context
        // (Rust source, JSON…) and must drop, not bleed in as message body.
        Level::Full => {
            let mut in_diff = false;
            let mut in_hunk = false;
            let mut output: Vec<String> = Vec::with_capacity(64);
            for line in raw.lines() {
                if output.len() >= 100 {
                    break;
                }
                if line.starts_with("diff ") {
                    in_diff = true;
                    in_hunk = false;
                    output.push(line.to_string());
                    continue;
                }
                if line.starts_with("@@ ") {
                    in_hunk = true;
                    output.push(line.to_string());
                    continue;
                }
                if in_diff && !in_hunk {
                    // Pre-hunk metadata — `--- `, `+++ `, `index `, mode, …
                    continue;
                }
                if in_hunk {
                    if line.starts_with('+') || line.starts_with('-') {
                        output.push(line.to_string());
                    }
                    continue;
                }
                // Pre-diff: commit header / message body. Trailers and the long
                // hash form are the two redundancy sources.
                if is_trailer(line) || !is_commit_header(line) {
                    continue;
                }
                match abbreviate_commit_line(line) {
                    Some(abbrev) => output.push(abbrev),
                    None => output.push(line.to_string()),
                }
            }
            if output.is_empty() {
                // No commit/diff structure recognized — possibly `git show <tag>`
                // or unusual format. Fall back to compact pass.
                return head_nonblank(raw, level.head_limit(60));
            }
            output.join("\n")
        }
    }
}

/// State-aware diff compactor used by `filter_diff` for Lite/Full/Ultra.
///
/// Tracks `in_hunk` to drop pre-hunk metadata (`--- `, `+++ `, `index `, mode)
/// without false-positives against removed source lines that happen to start
/// with `--- ` (e.g. removed comment delimiters). Returns an empty Vec for
/// inputs with no `diff `/`@@ ` markers — caller decides the fallback.
fn compact_diff_body(raw: &str, level: Level, limit: usize) -> Vec<&str> {
    let mut output: Vec<&str> = Vec::with_capacity(64);
    let mut in_hunk = false;

    for line in raw.lines() {
        if output.len() >= limit {
            break;
        }
        if line.starts_with("diff ") {
            in_hunk = false;
            output.push(line);
            continue;
        }
        if line.starts_with("@@ ") {
            in_hunk = true;
            // Ultra: strip the trailing function-context tail to save a few
            // tokens per hunk. Full/Lite keep it — useful for the LLM to know
            // which function the change is in.
            output.push(if level == Level::Ultra {
                trim_hunk_header(line)
            } else {
                line
            });
            continue;
        }
        if level == Level::Ultra {
            // Ultra keeps only `diff `/`@@ ` markers; drop everything else.
            continue;
        }
        if !in_hunk {
            // Pre-hunk: drop `--- `, `+++ `, `index `, mode lines.
            continue;
        }
        if line.starts_with('+') || line.starts_with('-') {
            output.push(line);
        }
    }
    output
}

// --- helpers ---

fn is_status_char(b: u8) -> bool {
    matches!(b, b'M' | b'A' | b'D' | b'R' | b'C' | b'U' | b'?' | b'!')
}

fn is_status_line(s: &str) -> bool {
    s.len() >= 3 && is_status_char(s.as_bytes()[0]) && s.as_bytes()[1] == b' '
        || s.len() >= 4
            && s.as_bytes()[0] == b' '
            && is_status_char(s.as_bytes()[1])
            && s.as_bytes()[2] == b' '
}

fn is_commit_header(l: &str) -> bool {
    l.starts_with("commit ")
        || l.starts_with("Merge:")
        || l.starts_with("Author:")
        || l.starts_with("Date:")
        || l.starts_with("    ")
}

fn is_index_meta(l: &str) -> bool {
    l.starts_with("index ") || l.starts_with("mode ") || l.starts_with("similarity ")
}

/// Commit-message trailers. They sit in the message body (4-space indent) but
/// add no signal for code understanding — bot accounts (`Co-authored-by` from
/// pair programming, `Signed-off-by` from DCO repos) can pile up noticeably.
fn is_trailer(line: &str) -> bool {
    let trimmed = line.trim_start();
    trimmed.starts_with("Signed-off-by:")
        || trimmed.starts_with("Co-authored-by:")
        || trimmed.starts_with("Change-Id:")
        || trimmed.starts_with("Reviewed-by:")
        || trimmed.starts_with("Acked-by:")
        || trimmed.starts_with("Tested-by:")
        || trimmed.starts_with("Reported-by:")
        || trimmed.starts_with("Cc:")
}

/// `commit <40-hex>[ decoration]` → `commit <12-hex>[ decoration]`.
/// Returns None if the line isn't a hash header (caller falls back to original).
/// Decoration like `(HEAD -> main)` from `--decorate` is preserved.
fn abbreviate_commit_line(line: &str) -> Option<String> {
    let rest = line.strip_prefix("commit ")?;
    let hash_end = rest
        .find(|c: char| !c.is_ascii_hexdigit())
        .unwrap_or(rest.len());
    if hash_end < 40 {
        return None;
    }
    Some(format!("commit {}{}", &rest[..12], &rest[hash_end..]))
}

/// `@@ -A,B +C,D @@ context` → `@@ -A,B +C,D @@`. Used by Ultra to drop the
/// trailing function-context tail. Returns the input unchanged if the second
/// `@@` marker isn't found.
fn trim_hunk_header(line: &str) -> &str {
    if !line.starts_with("@@ ") {
        return line;
    }
    // Search past the first `@@ ` for the closing ` @@`.
    if let Some(idx) = line[3..].find(" @@") {
        return &line[..3 + idx + 3];
    }
    line
}

fn head_nonblank(raw: &str, limit: usize) -> String {
    raw.lines()
        .filter(|l| !l.is_empty())
        .take(limit)
        .collect::<Vec<_>>()
        .join("\n")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn status_clean() {
        let out = filter_status("", Level::Full);
        assert_eq!(out, "git status: clean");
    }

    #[test]
    fn status_modified() {
        let raw = " M src/main.rs\n M Cargo.toml\n";
        let out = filter_status(raw, Level::Full);
        assert!(out.contains("src/main.rs"));
        assert!(out.contains("Cargo.toml"));
    }

    #[test]
    fn diff_ultra_headers_only() {
        let raw = "diff --git a/f b/f\nindex abc..def\n--- a/f\n+++ b/f\n@@ -1 +1 @@\n-old\n+new\n";
        let out = filter_diff(raw, Level::Ultra);
        assert!(out.contains("diff --git"));
        assert!(out.contains("@@ "));
        assert!(!out.contains("-old"));
    }

    #[test]
    fn show_full_drops_context_and_meta() {
        let raw = "\
commit abc123
Author: zdk
Date:   Mon

    fix bug

diff --git a/f b/f
index abc..def 100644
--- a/f
+++ b/f
@@ -1,3 +1,3 @@
 unchanged context
-old line
+new line
 more context
";
        let out = filter_show(raw, Level::Full);
        assert!(out.contains("commit abc123"));
        assert!(out.contains("    fix bug"));
        assert!(out.contains("diff --git"));
        assert!(out.contains("-old line"));
        assert!(out.contains("+new line"));
        assert!(!out.contains("unchanged context"), "should drop context: {out}");
        assert!(!out.contains("index abc"), "should drop index meta: {out}");
    }

    #[test]
    fn show_full_drops_indented_context_after_diff() {
        // Regression: `    ` rule for commit-message body must not match
        // deeply indented diff context (Rust source is 4-space indented).
        let raw = "\
commit abc123
Author: zdk
Date:   Mon

    refactor

diff --git a/f b/f
@@ -1,3 +1,3 @@
     let x = 1;
-    let y = 2;
+    let y = 3;
     println!(\"{x} {y}\");
";
        let out = filter_show(raw, Level::Full);
        assert!(out.contains("    refactor"), "keep message body: {out}");
        assert!(out.contains("-    let y = 2;"));
        assert!(out.contains("+    let y = 3;"));
        assert!(
            !out.contains("    let x = 1;"),
            "must drop indented context: {out}"
        );
        assert!(
            !out.contains("println!"),
            "must drop indented context: {out}"
        );
    }

    #[test]
    fn diff_full_drops_redundant_minus_plus_and_index() {
        // `--- a/X` and `+++ b/X` always duplicate the path on `diff --git`
        // and must be dropped to save tokens.
        let raw = "\
diff --git a/f b/f
index abc..def 100644
--- a/f
+++ b/f
@@ -1 +1 @@
-old
+new
";
        let out = filter_diff(raw, Level::Full);
        assert!(out.contains("diff --git"));
        assert!(out.contains("@@ "));
        assert!(out.contains("-old"));
        assert!(out.contains("+new"));
        assert!(!out.contains("--- a/f"), "drop redundant ---: {out}");
        assert!(!out.contains("+++ b/f"), "drop redundant +++: {out}");
        assert!(!out.contains("index abc"), "drop index meta: {out}");
    }

    #[test]
    fn diff_full_keeps_removed_dashes_in_hunk_body() {
        // Removed source lines starting with `--- ` (e.g. comment delimiters)
        // must NOT be confused with the pre-hunk metadata header.
        let raw = "\
diff --git a/f b/f
@@ -1,2 +1,2 @@
---- old comment ----
+++ new comment +++
";
        let out = filter_diff(raw, Level::Full);
        assert!(out.contains("---- old comment ----"), "keep removed content: {out}");
        assert!(out.contains("+++ new comment +++"), "keep added content: {out}");
    }

    #[test]
    fn diff_stat_falls_back_instead_of_passthrough() {
        // `git diff --stat` has no `diff `/`@@ ` markers — the old filter
        // returned empty and triggered raw passthrough (zero savings). Now we
        // run a compact pass so blank lines are at least dropped.
        let raw = "\
 crates/foo.rs | 5 +-
 bar/baz.rs    | 12 +++++++++++

 2 files changed, 15 insertions(+), 2 deletions(-)
";
        let out = filter_diff(raw, Level::Full);
        assert!(!out.is_empty(), "fall back, don't return empty: {out}");
        assert!(out.contains("crates/foo.rs"));
        assert!(out.contains("2 files changed"));
        // Blank line between stat and summary should be collapsed.
        assert!(!out.contains("\n\n"), "collapse blank lines: {out:?}");
    }

    #[test]
    fn diff_ultra_strips_hunk_function_context() {
        let raw = "\
diff --git a/f b/f
@@ -23,13 +23,18 @@ pub struct Foo {
-old
+new
";
        let out = filter_diff(raw, Level::Ultra);
        assert!(out.contains("@@ -23,13 +23,18 @@"));
        assert!(!out.contains("pub struct Foo"), "drop tail context: {out}");
    }

    #[test]
    fn show_full_drops_message_trailers() {
        let raw = "\
commit abc123
Author: zdk <z@d.k>
Date:   Fri May 8 13:01:39 2026 +0700

    fix bug

    Signed-off-by: zdk <z@d.k>
    Co-authored-by: someone <s@o.m>
    Change-Id: I0123456789abcdef

diff --git a/f b/f
@@ -1 +1 @@
-old
+new
";
        let out = filter_show(raw, Level::Full);
        assert!(out.contains("    fix bug"));
        assert!(!out.contains("Signed-off-by"), "drop trailer: {out}");
        assert!(!out.contains("Co-authored-by"), "drop trailer: {out}");
        assert!(!out.contains("Change-Id"), "drop trailer: {out}");
    }

    #[test]
    fn show_full_abbreviates_full_commit_hash() {
        let raw = "\
commit fd9858806e241a70eec9d23017ccf00d90b64c4c
Author: zdk
Date:   Mon

    fix
";
        let out = filter_show(raw, Level::Full);
        assert!(out.contains("commit fd9858806e24"), "abbreviate: {out}");
        assert!(!out.contains("fd9858806e241a70eec9d23017ccf00d90b64c4c"), "drop full hash: {out}");
    }

    #[test]
    fn show_full_preserves_decoration_after_abbreviated_hash() {
        let raw = "\
commit fd9858806e241a70eec9d23017ccf00d90b64c4c (HEAD -> main, origin/main)
Author: zdk
Date:   Mon

    fix
";
        let out = filter_show(raw, Level::Full);
        assert!(
            out.contains("commit fd9858806e24 (HEAD -> main, origin/main)"),
            "preserve --decorate suffix: {out}"
        );
    }

    #[test]
    fn show_full_drops_redundant_minus_plus_in_diff() {
        // Same redundancy fix as `git diff` — but verified through `git show`.
        let raw = "\
commit abc123
Author: zdk
Date:   Mon

    fix

diff --git a/f b/f
index 1..2 100644
--- a/f
+++ b/f
@@ -1 +1 @@
-old
+new
";
        let out = filter_show(raw, Level::Full);
        assert!(!out.contains("--- a/f"), "drop redundant ---: {out}");
        assert!(!out.contains("+++ b/f"), "drop redundant +++: {out}");
        assert!(!out.contains("index 1..2"), "drop index meta: {out}");
    }

    #[test]
    fn log_full_drops_trailers_and_abbreviates_hash() {
        let raw = "\
commit fd9858806e241a70eec9d23017ccf00d90b64c4c
Author: zdk
Date:   Mon

    fix bug

    Signed-off-by: zdk <z@d.k>
    Co-authored-by: someone <s@o.m>

commit abc123
Author: zdk
Date:   Sun

    other
";
        let out = filter_log(raw, Level::Full);
        assert!(out.contains("commit fd9858806e24"), "abbreviate hash: {out}");
        assert!(
            !out.contains("fd9858806e241a70eec9d23017ccf00d90b64c4c"),
            "drop full hash: {out}"
        );
        assert!(!out.contains("Signed-off-by"), "drop trailer: {out}");
        assert!(!out.contains("Co-authored-by"), "drop trailer: {out}");
        assert!(out.contains("    fix bug"), "keep message body: {out}");
        // Short hash stays as-is — the abbreviator only kicks in at ≥40 hex.
        assert!(out.contains("commit abc123"));
    }

    #[test]
    fn log_ultra_compact() {
        let raw = "commit abc123\nAuthor: zdk\nDate: Mon\n\n    fix bug\n\ncommit def456\n";
        let out = filter_log(raw, Level::Ultra);
        assert!(out.contains("commit abc123"));
        assert!(out.contains("    fix bug"));
        // Author/Date stripped in ultra
        assert!(!out.contains("Author:"));
    }
}