git-prism 0.9.1

Agent-optimized git data MCP server — structured change manifests and full file snapshots for LLM agents
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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
//! Adapters from `Classification` variants to `tools::*` functions.
//!
//! Each handler resolves the repository via `gix::discover`, calls the
//! corresponding tool function, serialises the result to stdout, and returns
//! `ExitCode::SUCCESS`.

use std::io::Write;
use std::path::Path;
use std::process::ExitCode;

use crate::git::reader::RepoReader;
use crate::git::refs::RefRange;
use crate::git::refs::parse_range;
use crate::shim::classify::Classification;
use crate::tools::size::estimate_response_tokens;
use crate::tools::types::{
    CommitSignature, ShowCommitDetail, ShowDiffstat, ShowFileEntry, ShowManifestResponse,
};
use crate::tools::{
    ContextOptions, ManifestOptions, SnapshotOptions, build_function_context_with_options,
    build_snapshots, collect_all_history_pages, collect_all_manifest_pages,
    collect_all_worktree_manifest_pages,
};

/// Dispatch a classified git command to the appropriate tool function and
/// write the JSON result to `out`.  Returns `ExitCode::SUCCESS` on success
/// or `ExitCode::FAILURE` on error.
pub(crate) fn handle<W: Write>(
    classification: &Classification<'_>,
    repo_path: &Path,
    out: &mut W,
) -> ExitCode {
    match dispatch(classification, repo_path, out) {
        Ok(()) => ExitCode::SUCCESS,
        Err(e) => {
            eprintln!("git-prism shim: handler error: {e}");
            ExitCode::FAILURE
        }
    }
}

fn dispatch<W: Write>(
    classification: &Classification<'_>,
    repo_path: &Path,
    out: &mut W,
) -> anyhow::Result<()> {
    match classification {
        Classification::Manifest { range } => handle_manifest(range, repo_path, out),
        Classification::History { range } => handle_history(range, repo_path, out),
        Classification::FunctionContext {
            range,
            pickaxe_term,
        } => handle_function_context(*range, pickaxe_term, repo_path, out),
        Classification::ShowSnapshot { sha } => handle_show_snapshot(sha, repo_path, out),
        Classification::BlameSnapshot { path } => handle_blame_snapshot(path, repo_path, out),
        Classification::GhPrDiff { pr_number } => handle_gh_pr_diff(pr_number, repo_path, out),
        Classification::Passthrough => {
            anyhow::bail!("dispatch called with Passthrough — caller bug")
        }
    }
}

fn handle_manifest<W: Write>(range: &str, repo_path: &Path, out: &mut W) -> anyhow::Result<()> {
    let options = ManifestOptions {
        include_patterns: vec![],
        exclude_patterns: vec![],
        include_function_analysis: false,
        max_response_tokens: Some(8192),
    };
    let result = match parse_range(range) {
        RefRange::CommitRange { base, head } => {
            collect_all_manifest_pages(repo_path, base, head, &options, 500)?
        }
        RefRange::WorktreeCompare { base } => {
            collect_all_worktree_manifest_pages(repo_path, base, &options, 500)?
        }
    };
    serde_json::to_writer_pretty(out, &result)?;
    Ok(())
}

fn handle_history<W: Write>(range: &str, repo_path: &Path, out: &mut W) -> anyhow::Result<()> {
    let (base, head) = match parse_range(range) {
        RefRange::CommitRange { base, head } => (base, head),
        RefRange::WorktreeCompare { .. } => {
            anyhow::bail!("history requires a commit range")
        }
    };
    let options = ManifestOptions {
        include_patterns: vec![],
        exclude_patterns: vec![],
        include_function_analysis: true,
        max_response_tokens: None,
    };
    let result = collect_all_history_pages(repo_path, base, head, &options, 500)?;
    serde_json::to_writer_pretty(out, &result)?;
    Ok(())
}

fn handle_function_context<W: Write>(
    range: Option<&str>,
    _pickaxe_term: &str,
    repo_path: &Path,
    out: &mut W,
) -> anyhow::Result<()> {
    let effective_range = range.unwrap_or("HEAD~1..HEAD");
    let (base, head) = match parse_range(effective_range) {
        RefRange::CommitRange { base, head } => (base, head),
        RefRange::WorktreeCompare { .. } => {
            anyhow::bail!("function context requires a commit range")
        }
    };
    let options = ContextOptions {
        cursor: None,
        page_size: 25,
        function_names: None,
        max_response_tokens: Some(8192),
    };
    let result = build_function_context_with_options(repo_path, base, head, &options)?;
    serde_json::to_writer_pretty(out, &result)?;
    Ok(())
}

fn handle_show_snapshot<W: Write>(sha: &str, repo_path: &Path, out: &mut W) -> anyhow::Result<()> {
    let commit = build_show_commit_detail(repo_path, sha)?;

    let files: Vec<ShowFileEntry> = if commit.parents.is_empty() {
        // Root commit: no parent, so `<sha>^` doesn't resolve.  Walk the
        // commit tree directly and report every blob as Added.
        let reader =
            RepoReader::open(repo_path).map_err(|e| anyhow::anyhow!("failed to open repo: {e}"))?;
        let diff = reader
            .diff_root_commit(sha)
            .map_err(|e| anyhow::anyhow!("failed to diff root commit: {e}"))?;
        diff.files
            .into_iter()
            .map(file_change_to_show_entry)
            .collect()
    } else {
        // Normal commit: diff against first parent via the manifest pipeline.
        // Use the resolved commit SHA (not the raw `sha` input) so that
        // annotated tags — where `sha` is e.g. "v1.0" and "v1.0^" is not a
        // resolvable ref — still produce a valid parent ref.
        let manifest_options = ManifestOptions {
            include_patterns: vec![],
            exclude_patterns: vec![],
            include_function_analysis: false,
            max_response_tokens: None,
        };
        let base_ref = format!("{}^", commit.sha);
        let manifest =
            collect_all_manifest_pages(repo_path, &base_ref, &commit.sha, &manifest_options, 500)?;
        manifest
            .files
            .into_iter()
            .map(|f| ShowFileEntry {
                path: f.path,
                old_path: f.old_path,
                change_type: f.change_type,
                additions: f.lines_added,
                deletions: f.lines_removed,
                is_binary: f.is_binary,
            })
            .collect()
    };

    // Derive diffstat directly from per-file counts — no net-delta arithmetic.
    let insertions: usize = files.iter().map(|f| f.additions).sum();
    let deletions: usize = files.iter().map(|f| f.deletions).sum();
    let diffstat = ShowDiffstat {
        files_changed: files.len(),
        insertions,
        deletions,
    };

    let mut result = ShowManifestResponse {
        commit,
        diffstat,
        files,
        token_estimate: 0,
    };
    result.token_estimate = estimate_response_tokens(&result);
    serde_json::to_writer_pretty(out, &result)?;
    Ok(())
}

/// Map a raw `FileChange` (from `diff_root_commit`) to the show-specific
/// `ShowFileEntry` shape.
fn file_change_to_show_entry(f: crate::git::diff::FileChange) -> ShowFileEntry {
    ShowFileEntry {
        path: f.path,
        old_path: f.old_path,
        change_type: f.change_type,
        additions: f.lines_added,
        deletions: f.lines_removed,
        is_binary: f.is_binary,
    }
}

/// Extract structured commit metadata for `sha` using gix (no shell out).
fn build_show_commit_detail(repo_path: &Path, sha: &str) -> anyhow::Result<ShowCommitDetail> {
    let reader =
        RepoReader::open(repo_path).map_err(|e| anyhow::anyhow!("failed to open repo: {e}"))?;
    let commit = reader
        .peel_to_commit(sha)
        .map_err(|e| anyhow::anyhow!("failed to resolve commit {sha}: {e}"))?;

    let full_sha = commit.id().to_string();
    let short_sha = full_sha.chars().take(8).collect::<String>();

    let parents = commit
        .parent_ids()
        .map(|id| id.to_string())
        .collect::<Vec<_>>();

    let author_sig = commit
        .author()
        .map_err(|e| anyhow::anyhow!("failed to read author: {e}"))?;
    let committer_sig = commit
        .committer()
        .map_err(|e| anyhow::anyhow!("failed to read committer: {e}"))?;

    let author = signature_to_commit_signature(&author_sig)?;
    let committer = signature_to_commit_signature(&committer_sig)?;

    let raw_message = commit
        .message_raw()
        .map_err(|e| anyhow::anyhow!("failed to read message: {e}"))?;
    let message = std::str::from_utf8(raw_message.as_ref())
        .map_err(|e| anyhow::anyhow!("commit message not UTF-8: {e}"))?;
    let (subject, body) = split_commit_message(message);

    Ok(ShowCommitDetail {
        sha: full_sha,
        short_sha,
        parents,
        author,
        committer,
        subject,
        body,
    })
}

/// Convert a gix `SignatureRef` into our serialisable `CommitSignature`.
///
/// `SignatureRef.time` is a raw `&str` like `"1780329644 +0000"`.
/// We call `sig.time()` (the decode method) to get `gix_date::Time` with
/// typed `seconds: i64` and `offset: i32` fields.
///
/// Returns an error instead of silently substituting epoch 0 so callers
/// see a real failure instead of corrupted `%ct`-equivalent data.
fn signature_to_commit_signature(
    sig: &gix::actor::SignatureRef<'_>,
) -> anyhow::Result<CommitSignature> {
    let gix_time = sig
        .time()
        .map_err(|e| anyhow::anyhow!("failed to parse commit time for {}: {e}", sig.email))?;
    let epoch = gix_time.seconds;
    let offset_seconds = gix_time.offset;
    let naive = chrono::DateTime::from_timestamp(epoch, 0)
        .ok_or_else(|| anyhow::anyhow!("commit timestamp {epoch} out of representable range"))?
        .naive_utc();
    let offset = chrono::FixedOffset::east_opt(offset_seconds)
        .ok_or_else(|| anyhow::anyhow!("commit tz offset {offset_seconds}s out of range"))?;
    let dt = chrono::DateTime::<chrono::FixedOffset>::from_naive_utc_and_offset(naive, offset);
    Ok(CommitSignature {
        name: sig.name.to_string(),
        email: sig.email.to_string(),
        date_iso: dt.to_rfc3339(),
        date_epoch: epoch,
    })
}

/// Split a raw commit message into (subject, body).
///
/// The subject is the first non-empty line. The body is everything after the
/// first blank line following the subject, trimmed. Returns `None` for body
/// when the message has no body paragraph.
fn split_commit_message(message: &str) -> (String, Option<String>) {
    let mut lines = message.lines();
    let subject = lines.next().unwrap_or("").trim().to_string();
    // Skip blank lines separating subject from body
    let body_lines: Vec<&str> = lines.skip_while(|l| l.trim().is_empty()).collect();
    let body = if body_lines.is_empty() {
        None
    } else {
        Some(body_lines.join("\n").trim().to_string())
    };
    (subject, body)
}

/// Handle `gh pr diff <N>` by resolving the PR's base..head ref range via the
/// `gh` CLI, then feeding it through the existing manifest pipeline.
///
/// Execs `gh pr view <N> --json baseRefOid,headRefOid` to obtain the commit
/// SHAs, then calls `handle_manifest` with `"base_sha..head_sha"`.
///
/// `repo_path` may be a subdirectory of the git repo (e.g. `bdd/`); this
/// function discovers the actual git root before opening it, mirroring what
/// `git rev-parse --show-toplevel` does.
fn handle_gh_pr_diff<W: Write>(
    pr_number: &str,
    repo_path: &Path,
    out: &mut W,
) -> anyhow::Result<()> {
    let range = resolve_pr_range(pr_number)?;
    // Discover the real git root so gix::open() doesn't fail on subdirectories.
    let git_root = discover_git_root(repo_path)?;
    handle_manifest(&range, &git_root, out)
}

/// Walk up the directory tree from `start` until we find a directory that
/// contains a `.git` entry (file or directory), and return that directory.
///
/// This mirrors what `git rev-parse --show-toplevel` does and is necessary
/// because `gix::open` requires the exact repo root, not a subdirectory.
fn discover_git_root(start: &Path) -> anyhow::Result<std::path::PathBuf> {
    let mut current = start.to_path_buf();
    loop {
        if current.join(".git").exists() {
            return Ok(current);
        }
        match current.parent() {
            Some(parent) => current = parent.to_path_buf(),
            None => anyhow::bail!(
                "could not find git repository root from {}",
                start.display()
            ),
        }
    }
}

/// Resolve a PR number to a `"base_sha..head_sha"` ref range string by
/// calling `gh pr view <N> --json baseRefOid,headRefOid`.
///
/// Uses commit SHAs (not branch names) so the range works even after the PR
/// branch has been deleted (merged PRs).  The SHAs are permanent; branch names
/// are ephemeral.
///
/// Returns an error when `gh` is not on PATH, exits non-zero, or returns
/// JSON that cannot be parsed.
fn resolve_pr_range(pr_number: &str) -> anyhow::Result<String> {
    let output = std::process::Command::new("gh")
        .args(["pr", "view", pr_number, "--json", "baseRefOid,headRefOid"])
        .output()
        .map_err(|e| anyhow::anyhow!("failed to run gh pr view: {e}"))?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        anyhow::bail!(
            "gh pr view {pr_number} failed with status {}: {stderr}",
            output.status
        );
    }

    let json: serde_json::Value = serde_json::from_slice(&output.stdout)
        .map_err(|e| anyhow::anyhow!("gh pr view returned invalid JSON: {e}"))?;

    let base_sha = json["baseRefOid"]
        .as_str()
        .ok_or_else(|| anyhow::anyhow!("gh pr view JSON missing baseRefOid"))?;
    let head_sha = json["headRefOid"]
        .as_str()
        .ok_or_else(|| anyhow::anyhow!("gh pr view JSON missing headRefOid"))?;

    Ok(format!("{base_sha}..{head_sha}"))
}

fn handle_blame_snapshot<W: Write>(
    path: &str,
    repo_path: &Path,
    out: &mut W,
) -> anyhow::Result<()> {
    // `git blame <path>` maps to get_file_snapshots for the whole file at HEAD.
    let options = SnapshotOptions {
        include_before: false,
        include_after: true,
        max_file_size_bytes: 100_000,
        line_range: None,
        include_diff_hunks: false,
    };
    let result = build_snapshots(repo_path, "HEAD^", "HEAD", &[path.to_string()], &options)?;
    serde_json::to_writer_pretty(out, &result)?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;
    use std::process::Command;

    use tempfile::TempDir;

    use super::*;

    // ---- fixture helpers ----

    fn init_repo_with_two_commits() -> (TempDir, PathBuf) {
        let dir = TempDir::new().unwrap();
        let path = dir.path().to_path_buf();

        let run = |args: &[&str]| {
            Command::new("git")
                .args(args)
                .current_dir(&path)
                .output()
                .unwrap()
        };

        run(&["init", "-b", "main"]);
        run(&["config", "user.email", "test@test.com"]);
        run(&["config", "user.name", "Test"]);

        std::fs::write(path.join("hello.txt"), "hello\n").unwrap();
        run(&["add", "hello.txt"]);
        run(&["commit", "-m", "first commit"]);

        std::fs::write(path.join("world.txt"), "world\n").unwrap();
        run(&["add", "world.txt"]);
        run(&["commit", "-m", "second commit"]);

        (dir, path)
    }

    fn head_sha(repo: &Path) -> String {
        let out = Command::new("git")
            .args(["rev-parse", "HEAD"])
            .current_dir(repo)
            .output()
            .unwrap();
        String::from_utf8(out.stdout).unwrap().trim().to_string()
    }

    // ---- tests ----

    #[test]
    fn it_handles_manifest_and_returns_files_array() {
        let (_dir, path) = init_repo_with_two_commits();
        let classification = Classification::Manifest {
            range: "HEAD~1..HEAD",
        };
        let mut out = Vec::new();
        let code = handle(&classification, &path, &mut out);
        assert_eq!(code, ExitCode::SUCCESS);

        let json: serde_json::Value = serde_json::from_slice(&out).unwrap();
        assert!(
            json.get("files").and_then(|f| f.as_array()).is_some(),
            "expected 'files' array in manifest output"
        );
    }

    #[test]
    fn it_handles_history_and_returns_commits_array() {
        let (_dir, path) = init_repo_with_two_commits();
        let classification = Classification::History {
            range: "HEAD~1..HEAD",
        };
        let mut out = Vec::new();
        let code = handle(&classification, &path, &mut out);
        assert_eq!(code, ExitCode::SUCCESS);

        let json: serde_json::Value = serde_json::from_slice(&out).unwrap();
        assert!(
            json.get("commits").and_then(|c| c.as_array()).is_some(),
            "expected 'commits' array in history output"
        );
    }

    #[test]
    fn it_handles_show_snapshot_and_returns_snapshots_key() {
        let (_dir, path) = init_repo_with_two_commits();
        let sha = head_sha(&path);
        let classification = Classification::ShowSnapshot { sha: &sha };
        let mut out = Vec::new();
        let code = handle(&classification, &path, &mut out);
        assert_eq!(code, ExitCode::SUCCESS);

        let json: serde_json::Value = serde_json::from_slice(&out).unwrap();
        // build_snapshots returns a response with a "files" array.
        assert!(
            json.get("files").and_then(|f| f.as_array()).is_some(),
            "expected 'files' array in show output, got: {json}"
        );
    }

    #[test]
    fn it_handles_show_snapshot_and_returns_commit_metadata() {
        let (_dir, path) = init_repo_with_two_commits();
        let sha = head_sha(&path);
        let classification = Classification::ShowSnapshot { sha: &sha };
        let mut out = Vec::new();
        let code = handle(&classification, &path, &mut out);
        assert_eq!(code, ExitCode::SUCCESS);

        let json: serde_json::Value = serde_json::from_slice(&out).unwrap();
        let commit = json
            .get("commit")
            .expect("expected 'commit' key in show output");
        // Exact-value assertions (F7: strengthen weak shape checks).
        assert_eq!(
            commit["author"]["name"].as_str().unwrap(),
            "Test",
            "commit.author.name must be 'Test'"
        );
        assert_eq!(
            commit["author"]["email"].as_str().unwrap(),
            "test@test.com",
            "commit.author.email must be 'test@test.com'"
        );
        // The fixture adds world.txt in the second commit (1 file, 1 insertion).
        let diffstat = json
            .get("diffstat")
            .expect("expected 'diffstat' key in show output");
        assert_eq!(
            diffstat["files_changed"].as_u64().unwrap(),
            1,
            "diffstat.files_changed must be 1"
        );
        assert_eq!(
            diffstat["insertions"].as_u64().unwrap(),
            1,
            "diffstat.insertions must be 1"
        );
        assert_eq!(
            diffstat["deletions"].as_u64().unwrap(),
            0,
            "diffstat.deletions must be 0"
        );
    }

    #[test]
    fn it_handles_show_snapshot_commit_sha_is_full_40_chars() {
        let (_dir, path) = init_repo_with_two_commits();
        let sha = head_sha(&path);
        let classification = Classification::ShowSnapshot { sha: &sha };
        let mut out = Vec::new();
        handle(&classification, &path, &mut out);
        let json: serde_json::Value = serde_json::from_slice(&out).unwrap();
        let commit_sha = json["commit"]["sha"].as_str().unwrap();
        assert_eq!(commit_sha.len(), 40, "sha must be 40 hex chars");
        let short_sha = json["commit"]["short_sha"].as_str().unwrap();
        assert_eq!(short_sha.len(), 8, "short_sha must be 8 hex chars");
        assert!(
            commit_sha.starts_with(short_sha),
            "short_sha must be a prefix of sha"
        );
    }

    #[test]
    fn it_handles_show_snapshot_subject_matches_commit_message() {
        let (_dir, path) = init_repo_with_two_commits();
        let sha = head_sha(&path);
        let classification = Classification::ShowSnapshot { sha: &sha };
        let mut out = Vec::new();
        handle(&classification, &path, &mut out);
        let json: serde_json::Value = serde_json::from_slice(&out).unwrap();
        // The fixture commits "second commit" as HEAD.
        assert_eq!(json["commit"]["subject"].as_str().unwrap(), "second commit");
        // Single-line message — body must be absent (null).
        assert!(
            json["commit"]["body"].is_null(),
            "single-line commit must have null body"
        );
    }

    #[test]
    fn it_handles_show_snapshot_parents_array_has_one_entry_for_normal_commit() {
        let (_dir, path) = init_repo_with_two_commits();
        let sha = head_sha(&path);
        let classification = Classification::ShowSnapshot { sha: &sha };
        let mut out = Vec::new();
        handle(&classification, &path, &mut out);
        let json: serde_json::Value = serde_json::from_slice(&out).unwrap();
        let parents = json["commit"]["parents"].as_array().unwrap();
        assert_eq!(
            parents.len(),
            1,
            "non-root commit must have exactly one parent"
        );
        assert_eq!(
            parents[0].as_str().unwrap().len(),
            40,
            "parent sha must be 40 chars"
        );
    }

    #[test]
    fn it_handles_show_snapshot_author_epoch_is_positive_integer() {
        let (_dir, path) = init_repo_with_two_commits();
        let sha = head_sha(&path);
        let classification = Classification::ShowSnapshot { sha: &sha };
        let mut out = Vec::new();
        handle(&classification, &path, &mut out);
        let json: serde_json::Value = serde_json::from_slice(&out).unwrap();
        let epoch = json["commit"]["author"]["date_epoch"].as_i64().unwrap();
        assert!(epoch > 0, "date_epoch must be a positive unix timestamp");
    }

    #[test]
    fn it_handles_show_snapshot_committer_fields_present() {
        let (_dir, path) = init_repo_with_two_commits();
        let sha = head_sha(&path);
        let classification = Classification::ShowSnapshot { sha: &sha };
        let mut out = Vec::new();
        handle(&classification, &path, &mut out);
        let json: serde_json::Value = serde_json::from_slice(&out).unwrap();
        let committer = &json["commit"]["committer"];
        // Exact-value assertions — the fixture sets user.name=Test, user.email=test@test.com.
        assert_eq!(committer["name"].as_str().unwrap(), "Test");
        assert_eq!(committer["email"].as_str().unwrap(), "test@test.com");
        // date_iso must be parseable as RFC-3339.
        let date_iso = committer["date_iso"]
            .as_str()
            .expect("date_iso must be a string");
        chrono::DateTime::parse_from_rfc3339(date_iso)
            .unwrap_or_else(|e| panic!("date_iso '{date_iso}' must parse as RFC-3339: {e}"));
        // date_epoch must be a positive unix timestamp.
        assert!(
            committer["date_epoch"].as_i64().unwrap() > 0,
            "date_epoch must be a positive unix timestamp"
        );
    }

    #[test]
    fn it_handles_show_snapshot_diffstat_files_changed_matches_files_array() {
        let (_dir, path) = init_repo_with_two_commits();
        let sha = head_sha(&path);
        let classification = Classification::ShowSnapshot { sha: &sha };
        let mut out = Vec::new();
        handle(&classification, &path, &mut out);
        let json: serde_json::Value = serde_json::from_slice(&out).unwrap();
        let files_changed = json["diffstat"]["files_changed"].as_u64().unwrap();
        let files_count = json["files"].as_array().unwrap().len() as u64;
        assert_eq!(
            files_changed, files_count,
            "diffstat.files_changed must equal files array length"
        );
    }

    #[test]
    fn it_handles_blame_snapshot_and_returns_files_array() {
        let (_dir, path) = init_repo_with_two_commits();
        let classification = Classification::BlameSnapshot { path: "hello.txt" };
        let mut out = Vec::new();
        let code = handle(&classification, &path, &mut out);
        assert_eq!(code, ExitCode::SUCCESS);

        let json: serde_json::Value = serde_json::from_slice(&out).unwrap();
        // build_snapshots returns a response with a "files" array.
        assert!(
            json.get("files").and_then(|f| f.as_array()).is_some(),
            "expected 'files' array in blame output, got: {json}"
        );
    }

    // --- Item 5: split_commit_message unit tests ---

    #[test]
    fn split_commit_message_single_line_has_no_body() {
        let (subject, body) = split_commit_message("fix: correct the thing");
        assert_eq!(subject, "fix: correct the thing");
        assert!(body.is_none());
    }

    #[test]
    fn split_commit_message_subject_and_single_paragraph_body() {
        let (subject, body) = split_commit_message("feat: add widget\n\nThis adds the widget.");
        assert_eq!(subject, "feat: add widget");
        assert_eq!(body.as_deref(), Some("This adds the widget."));
    }

    #[test]
    fn split_commit_message_multi_paragraph_body_preserves_internal_blank_line() {
        let msg = "feat: multi\n\nParagraph one.\n\nParagraph two.";
        let (subject, body) = split_commit_message(msg);
        assert_eq!(subject, "feat: multi");
        let b = body.expect("body must be Some");
        assert!(b.contains("Paragraph one."), "body must contain first para");
        assert!(
            b.contains("Paragraph two."),
            "body must contain second para"
        );
    }

    #[test]
    fn split_commit_message_trailing_blank_lines_only_gives_no_body() {
        let (subject, body) = split_commit_message("fix: thing\n\n  \n  ");
        assert_eq!(subject, "fix: thing");
        assert!(body.is_none(), "trailing blanks only must yield None body");
    }

    #[test]
    fn split_commit_message_empty_string_gives_empty_subject_no_body() {
        let (subject, body) = split_commit_message("");
        assert_eq!(subject, "");
        assert!(body.is_none());
    }

    #[test]
    fn split_commit_message_trims_subject_whitespace() {
        let (subject, _) = split_commit_message("  padded subject  \n\nbody");
        assert_eq!(subject, "padded subject");
    }

    // --- Item 6: e2e show with multi-paragraph body ---

    #[test]
    fn it_handles_show_snapshot_multi_paragraph_body_is_present_and_not_in_subject() {
        let dir = TempDir::new().unwrap();
        let path = dir.path().to_path_buf();
        let run = |args: &[&str]| {
            Command::new("git")
                .args(args)
                .current_dir(&path)
                .output()
                .unwrap()
        };
        run(&["init", "-b", "main"]);
        run(&["config", "user.email", "test@test.com"]);
        run(&["config", "user.name", "Test"]);
        std::fs::write(path.join("a.txt"), "a\n").unwrap();
        run(&["add", "a.txt"]);
        // Commit with subject + two body paragraphs via multiple -m flags.
        run(&[
            "commit",
            "-m",
            "feat: the subject",
            "-m",
            "First paragraph of body.",
            "-m",
            "Second paragraph of body.",
        ]);
        let sha = head_sha(&path);
        let classification = Classification::ShowSnapshot { sha: &sha };
        let mut out = Vec::new();
        let code = handle(&classification, &path, &mut out);
        assert_eq!(code, ExitCode::SUCCESS);
        let json: serde_json::Value = serde_json::from_slice(&out).unwrap();
        assert_eq!(
            json["commit"]["subject"].as_str().unwrap(),
            "feat: the subject",
            "subject must not contain body paragraphs"
        );
        let body = json["commit"]["body"]
            .as_str()
            .expect("body must be non-null for multi-paragraph commit");
        assert!(
            body.contains("First paragraph"),
            "body must contain first paragraph"
        );
        assert!(
            body.contains("Second paragraph"),
            "body must contain second paragraph"
        );
        assert!(
            !body.contains("feat: the subject"),
            "subject must not leak into body"
        );
    }

    #[test]
    fn it_handles_function_context_and_returns_functions_key() {
        let (_dir, path) = init_repo_with_two_commits();
        let classification = Classification::FunctionContext {
            range: Some("HEAD~1..HEAD"),
            pickaxe_term: "hello",
        };
        let mut out = Vec::new();
        let code = handle(&classification, &path, &mut out);
        assert_eq!(code, ExitCode::SUCCESS);

        let json: serde_json::Value = serde_json::from_slice(&out).unwrap();
        // build_function_context_with_options returns a response with a "functions" array.
        assert!(
            json.get("functions").is_some(),
            "expected 'functions' key in function_context output, got: {json}"
        );
    }

    #[test]
    fn it_handles_show_snapshot_for_annotated_tag_without_error() {
        // Before the peel fix, `git show <annotated-tag>` would panic or exit
        // non-zero because peel_to_commit failed with "was kind tag". This test
        // confirms the handler completes successfully (exit SUCCESS, valid JSON
        // with a "files" array) after the fix.
        let (_dir, path) = init_repo_with_two_commits();

        Command::new("git")
            .args(["tag", "-a", "v1.0", "-m", "release v1.0"])
            .current_dir(&path)
            .output()
            .unwrap();

        let classification = Classification::ShowSnapshot { sha: "v1.0" };
        let mut out = Vec::new();
        let code = handle(&classification, &path, &mut out);
        assert_eq!(
            code,
            ExitCode::SUCCESS,
            "handle should exit SUCCESS for annotated tag, got non-zero"
        );

        let json: serde_json::Value = serde_json::from_slice(&out).unwrap();
        assert!(
            json.get("files").and_then(|f| f.as_array()).is_some(),
            "expected 'files' array in show output for annotated tag, got: {json}"
        );
    }

    // ===== ADVERSARIAL QA PROBES (issue #337 pen-test) =====

    // SUSPICIOUS (pre-existing, NOT a #337 regression): `git show <ref>` via the
    // shim always returns files: [] because handle_show_snapshot calls
    // build_snapshots with an empty `paths` slice (&[]). build_snapshots only
    // processes files explicitly listed in `paths`; it never enumerates the
    // changed files of the range. This is true for annotated tags, lightweight
    // tags, AND raw SHAs identically — the empty `&[]` predates this PR
    // (origin/main:src/shim/handlers.rs:134). The peel change is therefore
    // correct and unaffected here; the dev's
    // it_handles_show_snapshot_for_annotated_tag_without_error test passes only
    // because the snapshot is empty for everything, which masks whether the
    // peel actually surfaces the right commit's content. Reported as SUSPICIOUS,
    // not blocked, because it is out of scope for the #337 peel fix.

    /// Regression guard within #337 scope: `git show <annotated-tag>` and
    /// `git show <target-sha>` produce IDENTICAL output (both empty today, but
    /// must not diverge — peeling must map the tag to the same range the SHA
    /// produces). This stays green and protects the equivalence the peel change
    /// is supposed to guarantee.
    #[test]
    fn qa_show_annotated_tag_output_equals_target_sha_output() {
        let (_dir, path) = init_repo_with_two_commits();
        Command::new("git")
            .args(["tag", "-a", "v1.0", "-m", "release v1.0"])
            .current_dir(&path)
            .output()
            .unwrap();
        let target_sha = head_sha(&path);

        let mut out_tag = Vec::new();
        assert_eq!(
            handle(
                &Classification::ShowSnapshot { sha: "v1.0" },
                &path,
                &mut out_tag
            ),
            ExitCode::SUCCESS
        );
        let mut out_sha = Vec::new();
        assert_eq!(
            handle(
                &Classification::ShowSnapshot { sha: &target_sha },
                &path,
                &mut out_sha
            ),
            ExitCode::SUCCESS
        );

        let json_tag: serde_json::Value = serde_json::from_slice(&out_tag).unwrap();
        let json_sha: serde_json::Value = serde_json::from_slice(&out_sha).unwrap();
        // Compare the `files` arrays (metadata.base_ref/head_ref legitimately
        // differ: "v1.0^"/"v1.0" vs "<sha>^"/"<sha>"; generated_at also differs).
        assert_eq!(
            json_tag.get("files"),
            json_sha.get("files"),
            "annotated-tag show must produce the same files as target-sha show"
        );
    }

    /// The `Classification::Manifest` path peels BOTH ends of a range.  This
    /// test creates two annotated tags on consecutive commits and asserts that
    /// the manifest over `v0.1..v0.2` returns a non-empty `files` array that
    /// contains the file added in the second commit.  A "was kind tag" peel
    /// failure would surface here as either an error exit or an empty file list.
    #[test]
    fn qa_manifest_over_annotated_tag_range_returns_changed_files() {
        let (dir, path) = init_repo_with_two_commits();

        let git = |args: &[&str]| {
            Command::new("git")
                .args(args)
                .current_dir(&path)
                .output()
                .unwrap()
        };

        // v0.1 tags the first commit (HEAD~1 after init_repo_with_two_commits)
        git(&["tag", "-a", "v0.1", "-m", "v0.1 release", "HEAD~1"]);
        // v0.2 tags the current HEAD (second commit, which added world.txt)
        git(&["tag", "-a", "v0.2", "-m", "v0.2 release", "HEAD"]);

        let classification = Classification::Manifest {
            range: "v0.1..v0.2",
        };
        let mut out = Vec::new();
        let code = handle(&classification, &path, &mut out);
        assert_eq!(
            code,
            ExitCode::SUCCESS,
            "manifest over annotated tag range must exit SUCCESS"
        );

        let json: serde_json::Value = serde_json::from_slice(&out).unwrap();
        let files = json
            .get("files")
            .and_then(|f| f.as_array())
            .expect("expected 'files' array in manifest output");

        assert!(
            !files.is_empty(),
            "manifest over v0.1..v0.2 must be non-empty (expected world.txt)"
        );

        let paths: Vec<&str> = files
            .iter()
            .filter_map(|f| f.get("path").and_then(|p| p.as_str()))
            .collect();
        assert!(
            paths.contains(&"world.txt"),
            "expected world.txt in manifest files, got: {paths:?}"
        );

        drop(dir);
    }
}