vcs-git 0.7.0

Automate Git from Rust: a typed, async wrapper that drives the real git CLI with its exact behavior, config, and credentials.
Documentation
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
//! End-to-end tests for the typed `vcs-git` client against a real temporary
//! repository. Ignored by default (require the `git` binary); run with
//! `cargo test -p vcs-git -- --ignored`.

use std::path::PathBuf;

// Scaffolding from vcs-testkit; most tests here drive `git.init()` themselves
// (initialisation IS the subject), so they use `TempDir` + `configure_identity`
// rather than `GitSandbox::init`. Note `configure_identity` also pins
// `core.autocrlf=false`, keeping byte-exact content assertions valid on Windows.
use vcs_git::{AnnotatedTag, Git, GitApi, MergeCommit, WorktreeAdd};
use vcs_testkit::{BareRemote, TempDir, configure_identity as configure};

#[tokio::test]
#[ignore = "requires the git binary"]
async fn init_status_add_commit_log_cycle() {
    let tmp = TempDir::new("cycle");
    let dir = tmp.path();
    let git = Git::new();

    git.init(dir).await.expect("init");
    configure(dir);

    // Untracked file shows up in status.
    std::fs::write(dir.join("file.txt"), "hello\n").expect("write file");
    let status = git.status(dir).await.expect("status");
    assert_eq!(status.len(), 1);
    assert_eq!(status[0].code, "??");
    assert_eq!(status[0].path, "file.txt");

    // Stage + commit, then status is clean.
    git.add(dir, &[PathBuf::from("file.txt")])
        .await
        .expect("add");
    git.commit(dir, "initial commit").await.expect("commit");
    assert!(git.status(dir).await.expect("status").is_empty());

    // Log reflects the commit, with the enriched fields.
    let log = git.log(dir, "HEAD", 10).await.expect("log");
    assert_eq!(log.len(), 1);
    assert_eq!(log[0].subject, "initial commit");
    assert_eq!(log[0].author, "Test");
    assert_eq!(log[0].hash.len(), 40, "full sha expected");
    assert!(!log[0].short_hash.is_empty() && log[0].hash.starts_with(&log[0].short_hash));
    assert!(
        log[0].date.starts_with("20"),
        "ISO date expected, got {:?}",
        log[0].date
    );

    // Branch introspection + create/checkout.
    let branch = git
        .current_branch(dir)
        .await
        .expect("current_branch")
        .expect("on a branch");
    assert!(!branch.is_empty());
    git.create_branch(dir, "feature")
        .await
        .expect("create_branch");
    git.checkout(dir, "feature").await.expect("checkout");
    assert_eq!(
        git.current_branch(dir).await.expect("branch").as_deref(),
        Some("feature")
    );
    let branches = git.branches(dir).await.expect("branches");
    assert!(branches.iter().any(|b| b.name == "feature"));

    // rev_parse resolves HEAD to the commit hash.
    assert_eq!(
        git.rev_parse(dir, "HEAD").await.expect("rev-parse"),
        log[0].hash
    );
}

// A freshly `init`'d repo (no commits) is on an **unborn** branch. `current_branch`
// must return that branch name, not error — the old `rev-parse --abbrev-ref HEAD`
// exited 128 here; `symbolic-ref --quiet --short HEAD` returns it cleanly.
#[tokio::test]
#[ignore = "requires the git binary"]
async fn current_branch_on_unborn_repo_returns_the_branch() {
    let tmp = TempDir::new("unborn");
    let dir = tmp.path();
    let git = Git::new();
    git.init(dir).await.expect("init");
    configure(dir);
    let branch = git
        .current_branch(dir)
        .await
        .expect("current_branch must not error on an unborn repo");
    // git's default initial branch is "main" or "master" depending on config;
    // either way it must be a non-empty Some, never None or an error.
    let branch = branch.expect("an unborn repo is still on a (named) branch");
    assert!(!branch.is_empty(), "unborn branch name should be non-empty");
}

#[tokio::test]
#[ignore = "requires the git binary"]
async fn diff_is_empty_tracks_worktree_changes() {
    let tmp = TempDir::new("diff");
    let dir = tmp.path();
    let git = Git::new();

    git.init(dir).await.expect("init");
    configure(dir);
    std::fs::write(dir.join("a.txt"), "one\n").expect("write");
    git.add(dir, &[PathBuf::from("a.txt")]).await.expect("add");
    git.commit(dir, "add a").await.expect("commit");

    assert!(
        git.diff_is_empty(dir).await.expect("clean"),
        "no changes yet"
    );

    std::fs::write(dir.join("a.txt"), "two\n").expect("modify");
    assert!(
        !git.diff_is_empty(dir).await.expect("dirty"),
        "unstaged change should be visible"
    );
}

// End-to-end check of the `-z` rename parsing: a real `git mv` must surface as a
// rename entry carrying both the new path and the original (`old_path`).
#[tokio::test]
#[ignore = "requires the git binary"]
async fn status_reports_rename_with_old_path() {
    let tmp = TempDir::new("rename");
    let dir = tmp.path();
    let git = Git::new();

    git.init(dir).await.expect("init");
    configure(dir);
    std::fs::write(dir.join("old.txt"), "hello\n").expect("write");
    git.add(dir, &[PathBuf::from("old.txt")])
        .await
        .expect("add");
    git.commit(dir, "add old").await.expect("commit");

    // Stage a rename, then read it back through the typed status.
    vcs_testkit::git(dir, &["mv", "old.txt", "new.txt"]);

    let status = git.status(dir).await.expect("status");
    let renamed = status
        .iter()
        .find(|e| e.code.starts_with('R'))
        .expect("a rename entry");
    assert_eq!(renamed.path, "new.txt", "new path");
    assert_eq!(
        renamed.old_path.as_deref(),
        Some("old.txt"),
        "original path"
    );
}

// Add a linked worktree on a new branch, see it in the porcelain listing, then
// remove it — the core flow agent-workspace drives.
#[tokio::test]
#[ignore = "requires the git binary"]
async fn worktree_add_list_remove_cycle() {
    let tmp = TempDir::new("wt-main");
    let dir = tmp.path();
    let git = Git::new();

    git.init(dir).await.expect("init");
    configure(dir);
    std::fs::write(dir.join("f.txt"), "x\n").expect("write");
    git.add(dir, &[PathBuf::from("f.txt")]).await.expect("add");
    git.commit(dir, "init").await.expect("commit");

    // common_dir points at the repo's `.git`.
    let common = git.common_dir(dir).await.expect("common_dir");
    assert!(common.to_string_lossy().contains(".git"), "{common:?}");

    // is_merged on real `branch --merged` output: a branch is merged into itself.
    let cur = git
        .current_branch(dir)
        .await
        .expect("current_branch")
        .expect("on a branch");
    assert!(git.is_merged(dir, &cur, &cur).await.expect("is_merged"));
    // No origin configured: `remote_head_branch` is `None`, not an error
    // (the `--quiet` path).
    assert!(
        git.remote_head_branch(dir)
            .await
            .expect("remote_head_branch")
            .is_none()
    );

    // A worktree path that doesn't exist yet, outside the repo.
    let wt_parent = TempDir::new("wt-linked");
    let wt = wt_parent.path().join("feature");

    git.worktree_add(
        dir,
        WorktreeAdd::create_branch(wt.clone(), "feature", "HEAD"),
    )
    .await
    .expect("worktree add");
    assert!(git.branch_exists(dir, "feature").await.expect("exists"));

    let list = git.worktree_list(dir).await.expect("list");
    assert!(
        list.iter().any(|w| w.branch.as_deref() == Some("feature")),
        "new worktree should be listed, got {list:?}"
    );

    git.worktree_remove(dir, &wt, true).await.expect("remove");
    assert!(
        !git.worktree_list(dir)
            .await
            .expect("list2")
            .iter()
            .any(|w| w.branch.as_deref() == Some("feature")),
        "worktree should be gone after remove"
    );
}

// New surface against a real git: the bound view (`git.at(dir)`) resolves the
// same as the dir-taking call, and `rev_parse_short` abbreviates HEAD.
#[tokio::test]
#[ignore = "requires the git binary"]
async fn bound_view_and_rev_parse_short() {
    let tmp = TempDir::new("bound");
    let dir = tmp.path();
    let git = Git::new();

    git.init(dir).await.expect("init");
    configure(dir);
    std::fs::write(dir.join("f.txt"), "x\n").expect("write");
    git.add(dir, &[PathBuf::from("f.txt")]).await.expect("add");
    git.commit(dir, "c1").await.expect("commit");

    // Bound view yields the same current branch as the dir-taking call.
    let bound = git.at(dir);
    assert_eq!(
        bound.current_branch().await.expect("branch"),
        git.current_branch(dir).await.expect("branch")
    );

    // `rev_parse_short` is a prefix of the full hash.
    let full = git.rev_parse(dir, "HEAD").await.expect("rev_parse");
    let short = bound.rev_parse_short("HEAD").await.expect("short");
    assert!(
        !short.is_empty() && full.starts_with(&short),
        "{short} vs {full}"
    );
}

// diff_text on an unborn repo (no commits) must not error on the unresolvable
// HEAD — it diffs against the empty tree and shows the staged additions.
#[tokio::test]
#[ignore = "requires the git binary"]
async fn diff_text_works_on_unborn_repo() {
    let tmp = TempDir::new("unborn");
    let dir = tmp.path();
    let git = Git::new();
    git.init(dir).await.expect("init");
    configure(dir);
    std::fs::write(dir.join("f.txt"), "hello\n").expect("write");
    git.add(dir, &[PathBuf::from("f.txt")]).await.expect("add");

    assert!(git.is_unborn(dir).await.expect("is_unborn"));
    let diff = git
        .diff_text(dir, vcs_git::DiffSpec::WorkingTree)
        .await
        .expect("diff_text must not error on unborn repo");
    assert!(diff.contains("f.txt"), "expected the new file in: {diff}");
}

// A real merge conflict must surface through `conflicted_files`, and a tree
// whose only change is an untracked file must read as tracked-clean.
#[tokio::test]
#[ignore = "requires the git binary"]
async fn conflicted_files_and_status_tracked() {
    let tmp = TempDir::new("conflict");
    let dir = tmp.path();
    let git = Git::new();

    git.init(dir).await.expect("init");
    configure(dir);
    std::fs::write(dir.join("a.txt"), "base\n").expect("write");
    git.add(dir, &[PathBuf::from("a.txt")]).await.expect("add");
    git.commit(dir, "base").await.expect("commit");
    let main = git
        .current_branch(dir)
        .await
        .expect("branch")
        .expect("on a branch");

    // Diverge: edit a.txt on both sides.
    git.create_branch(dir, "other").await.expect("branch");
    std::fs::write(dir.join("a.txt"), "main change\n").expect("write");
    git.add(dir, &[PathBuf::from("a.txt")]).await.expect("add");
    git.commit(dir, "main edit").await.expect("commit");
    git.checkout(dir, "other").await.expect("checkout");
    std::fs::write(dir.join("a.txt"), "other change\n").expect("write");
    git.add(dir, &[PathBuf::from("a.txt")]).await.expect("add");
    git.commit(dir, "other edit").await.expect("commit");

    // No conflicts before the merge.
    assert!(
        git.conflicted_files(dir)
            .await
            .expect("conflicted_files")
            .is_empty()
    );

    // The conflicting merge fails and leaves a.txt unmerged.
    assert!(
        git.merge_commit(dir, MergeCommit::branch(main.as_str()))
            .await
            .is_err()
    );
    assert_eq!(
        git.conflicted_files(dir).await.expect("conflicted_files"),
        ["a.txt"]
    );
    git.merge_abort(dir).await.expect("merge_abort");

    // An untracked file is uncommitted-dirty but tracked-clean.
    std::fs::write(dir.join("new.txt"), "untracked\n").expect("write");
    assert!(!git.status(dir).await.expect("status").is_empty());
    assert!(
        git.status_tracked(dir)
            .await
            .expect("status_tracked")
            .is_empty()
    );
}

// `merge_commit` with `no_ff` must create a real 2-parent merge commit even when
// a fast-forward was possible — the headline subtlety of the flag, and the one
// the conflict test (which only asserts the failing path) can't catch.
#[tokio::test]
#[ignore = "requires the git binary"]
async fn merge_commit_no_ff_creates_a_merge_commit() {
    let tmp = TempDir::new("mergenoff");
    let dir = tmp.path();
    let git = Git::new();

    git.init(dir).await.expect("init");
    configure(dir);
    std::fs::write(dir.join("a.txt"), "base\n").expect("write");
    git.add(dir, &[PathBuf::from("a.txt")]).await.expect("add");
    git.commit(dir, "base").await.expect("commit");
    let main = git
        .current_branch(dir)
        .await
        .expect("branch")
        .expect("on a branch");

    // A feature branch one commit ahead; main does not move, so a plain merge
    // would fast-forward.
    git.create_branch(dir, "feature").await.expect("branch");
    git.checkout(dir, "feature").await.expect("checkout");
    std::fs::write(dir.join("b.txt"), "feature\n").expect("write");
    git.add(dir, &[PathBuf::from("b.txt")]).await.expect("add");
    git.commit(dir, "feature work").await.expect("commit");
    git.checkout(dir, &main).await.expect("checkout");

    git.merge_commit(
        dir,
        MergeCommit::branch("feature")
            .no_ff()
            .message("merge feature"),
    )
    .await
    .expect("merge_commit");

    // A 2-parent merge commit resolves `HEAD^2`; a fast-forward would not.
    assert!(
        git.resolve_commit(dir, "HEAD^2").await.is_ok(),
        "no_ff merge must create a 2-parent merge commit (HEAD^2 should resolve)"
    );
    assert_eq!(
        git.last_commit_message(dir).await.expect("msg").trim(),
        "merge feature"
    );
}

// `is_merged` must distinguish a branch already merged into the target from one
// that isn't — the hermetic test only feeds canned output, so this pins the real
// `branch --merged` semantics.
#[tokio::test]
#[ignore = "requires the git binary"]
async fn is_merged_distinguishes_merged_and_unmerged() {
    let tmp = TempDir::new("ismerged");
    let dir = tmp.path();
    let git = Git::new();

    git.init(dir).await.expect("init");
    configure(dir);
    std::fs::write(dir.join("a.txt"), "base\n").expect("write");
    git.add(dir, &[PathBuf::from("a.txt")]).await.expect("add");
    git.commit(dir, "base").await.expect("commit");
    let main = git
        .current_branch(dir)
        .await
        .expect("branch")
        .expect("on a branch");

    // `done` branches off base and is merged back into main.
    git.create_branch(dir, "done").await.expect("branch");
    git.checkout(dir, "done").await.expect("checkout");
    std::fs::write(dir.join("b.txt"), "done\n").expect("write");
    git.add(dir, &[PathBuf::from("b.txt")]).await.expect("add");
    git.commit(dir, "done work").await.expect("commit");
    git.checkout(dir, &main).await.expect("checkout");
    git.merge_commit(
        dir,
        MergeCommit::branch("done").no_ff().message("merge done"),
    )
    .await
    .expect("merge_commit");

    // `pending` has a commit that was never merged into main.
    git.create_branch(dir, "pending").await.expect("branch");
    git.checkout(dir, "pending").await.expect("checkout");
    std::fs::write(dir.join("c.txt"), "pending\n").expect("write");
    git.add(dir, &[PathBuf::from("c.txt")]).await.expect("add");
    git.commit(dir, "pending work").await.expect("commit");
    git.checkout(dir, &main).await.expect("checkout");

    assert!(
        git.is_merged(dir, "done", &main)
            .await
            .expect("is_merged done"),
        "`done` was merged into main"
    );
    assert!(
        !git.is_merged(dir, "pending", &main)
            .await
            .expect("is_merged pending"),
        "`pending` was never merged into main"
    );
}

// `switch_with_stash` carries dirty state (tracked + untracked) across a branch
// switch, and restores it on the original branch when the checkout fails.
#[tokio::test]
#[ignore = "requires the git binary"]
async fn switch_with_stash_carries_changes_and_restores_on_failure() {
    let tmp = TempDir::new("switch");
    let dir = tmp.path();
    let git = Git::new();

    git.init(dir).await.expect("init");
    configure(dir); // pins core.autocrlf=false — the stash round-trip re-checks files out
    std::fs::write(dir.join("a.txt"), "base\n").expect("write");
    git.add(dir, &[PathBuf::from("a.txt")]).await.expect("add");
    git.commit(dir, "base").await.expect("commit");
    git.create_branch(dir, "feature").await.expect("branch");

    // Dirty tree: a tracked edit and an untracked file both travel.
    std::fs::write(dir.join("a.txt"), "edited\n").expect("write");
    std::fs::write(dir.join("new.txt"), "untracked\n").expect("write");
    git.switch_with_stash(dir, "feature").await.expect("switch");
    assert_eq!(
        git.current_branch(dir).await.expect("branch").as_deref(),
        Some("feature")
    );
    assert_eq!(
        std::fs::read_to_string(dir.join("a.txt")).expect("read"),
        "edited\n"
    );
    assert!(dir.join("new.txt").exists(), "untracked file must travel");

    // A failing checkout restores the dirty state where it was.
    assert!(
        git.switch_with_stash(dir, "no-such-branch").await.is_err(),
        "checkout of a missing branch must fail"
    );
    assert_eq!(
        git.current_branch(dir).await.expect("branch").as_deref(),
        Some("feature")
    );
    assert_eq!(
        std::fs::read_to_string(dir.join("a.txt")).expect("read"),
        "edited\n"
    );
    assert!(dir.join("new.txt").exists(), "untracked file must survive");
}

// Clone from a local bare fixture: the worktree materialises and history reads.
#[tokio::test]
#[ignore = "requires the git binary"]
async fn clone_repo_from_local_bare_remote() {
    let remote = BareRemote::seeded("clone");
    let tmp = TempDir::new("clone-dest");
    let dest = tmp.path().join("cloned");
    let git = Git::new();

    git.clone_repo(
        remote.url().as_str(),
        &dest,
        vcs_git::CloneSpec::new().branch("main"),
    )
    .await
    .expect("clone");
    assert!(dest.join("seed.txt").exists(), "worktree materialised");
    let log = git.log(&dest, "HEAD", 10).await.expect("log");
    assert_eq!(log.len(), 1);
    assert_eq!(log[0].subject, "seed");
    assert_eq!(
        git.current_branch(&dest).await.expect("branch").as_deref(),
        Some("main")
    );
}

// Tag cycle, file-at-revision, config and remote management round-trips.
#[tokio::test]
#[ignore = "requires the git binary"]
async fn tags_show_config_and_remotes_round_trip() {
    let tmp = TempDir::new("misc");
    let dir = tmp.path();
    let git = Git::new();
    git.init(dir).await.expect("init");
    configure(dir);
    std::fs::create_dir_all(dir.join("sub")).expect("mkdir");
    std::fs::write(dir.join("sub").join("f.txt"), "v1\n").expect("write");
    git.add(dir, &[PathBuf::from("sub/f.txt")])
        .await
        .expect("add");
    git.commit(dir, "base").await.expect("commit");

    // Tags: lightweight + annotated, list, delete.
    git.tag_create(dir, "v1", None).await.expect("tag");
    git.tag_create_annotated(dir, AnnotatedTag::new("v1.1", "first release"))
        .await
        .expect("tag -a");
    assert_eq!(git.tag_list(dir).await.expect("list"), ["v1", "v1.1"]);
    git.tag_delete(dir, "v1").await.expect("delete");
    assert_eq!(git.tag_list(dir).await.expect("list"), ["v1.1"]);

    // show_file resolves a subdir path. The backslash form is the Windows trap
    // (normalised internally there); on Unix a backslash is a legal filename
    // byte and passes through verbatim, so query with the native `/` instead.
    #[cfg(windows)]
    let sub_path = r"sub\f.txt";
    #[cfg(not(windows))]
    let sub_path = "sub/f.txt";
    assert_eq!(
        git.show_file(dir, "HEAD", sub_path).await.expect("show"),
        "v1"
    );

    // Config: set → get → unset key reads as None.
    git.config_set(dir, "vcs.test", "yes").await.expect("set");
    assert_eq!(
        git.config_get(dir, "vcs.test").await.expect("get"),
        Some("yes".to_string())
    );
    assert_eq!(
        git.config_get(dir, "vcs.unset-key").await.expect("get"),
        None
    );

    // Remotes: add, then re-point.
    git.remote_add(dir, "up", "https://example.com/a.git")
        .await
        .expect("remote add");
    git.remote_set_url(dir, "up", "https://example.com/b.git")
        .await
        .expect("set-url");
    assert_eq!(
        git.remote_url(dir, "up").await.expect("url"),
        "https://example.com/b.git"
    );
}

// blame maps lines to the commits that introduced them; cherry-pick and revert
// transplant/undo a commit.
#[tokio::test]
#[ignore = "requires the git binary"]
async fn blame_cherry_pick_and_revert_cycle() {
    let tmp = TempDir::new("blame");
    let dir = tmp.path();
    let git = Git::new();
    git.init(dir).await.expect("init");
    configure(dir); // pins core.autocrlf=false — cherry-pick/revert re-check files out
    std::fs::write(dir.join("f.txt"), "one\n").expect("write");
    git.add(dir, &[PathBuf::from("f.txt")]).await.expect("add");
    git.commit(dir, "first").await.expect("commit");
    let first = git.rev_parse(dir, "HEAD").await.expect("rev");
    std::fs::write(dir.join("f.txt"), "one\ntwo\n").expect("write");
    git.add(dir, &[PathBuf::from("f.txt")]).await.expect("add");
    git.commit(dir, "second").await.expect("commit");
    let second = git.rev_parse(dir, "HEAD").await.expect("rev");

    let blame = git.blame(dir, "f.txt", None).await.expect("blame");
    assert_eq!(blame.len(), 2);
    assert_eq!(blame[0].commit, first, "line 1 from the first commit");
    assert_eq!(blame[1].commit, second, "line 2 from the second commit");
    assert_eq!(blame[0].author, "Test");
    assert!(blame[0].author_time > 1_500_000_000, "sane epoch");
    assert_eq!(blame[1].content, "two");

    // Transplant "second" onto a branch cut at "first".
    git.create_branch(dir, "side").await.expect("branch");
    git.checkout(dir, "side").await.expect("checkout");
    git.reset_hard(dir, &first).await.expect("reset");
    git.cherry_pick(dir, &second).await.expect("cherry-pick");
    assert_eq!(
        std::fs::read_to_string(dir.join("f.txt")).expect("read"),
        "one\ntwo\n"
    );
    // And revert it again.
    git.revert(dir, "HEAD").await.expect("revert");
    assert_eq!(
        std::fs::read_to_string(dir.join("f.txt")).expect("read"),
        "one\n"
    );
}

// rebase_skip: only the `apply` backend refuses an emptied patch ("nothing to
// commit … skip this patch") — the default merge backend auto-drops it.
#[tokio::test]
#[ignore = "requires the git binary"]
async fn rebase_skip_finishes_an_emptied_patch() {
    let tmp = TempDir::new("skip");
    let dir = tmp.path();
    let git = Git::new();
    git.init(dir).await.expect("init");
    configure(dir);
    vcs_testkit::git(dir, &["config", "rebase.backend", "apply"]);

    std::fs::write(dir.join("f.txt"), "base\n").expect("write");
    git.add(dir, &[PathBuf::from("f.txt")]).await.expect("add");
    git.commit(dir, "base").await.expect("commit");
    let main = git
        .current_branch(dir)
        .await
        .expect("branch")
        .expect("on a branch");
    // A stack commit whose content the base branch then also adopts.
    git.create_branch(dir, "stack").await.expect("branch");
    git.checkout(dir, "stack").await.expect("checkout");
    std::fs::write(dir.join("f.txt"), "same change\n").expect("write");
    git.add(dir, &[PathBuf::from("f.txt")]).await.expect("add");
    git.commit(dir, "stack change").await.expect("commit");
    git.checkout(dir, &main).await.expect("checkout");
    std::fs::write(dir.join("f.txt"), "upstream version\n").expect("write");
    git.add(dir, &[PathBuf::from("f.txt")]).await.expect("add");
    git.commit(dir, "upstream change").await.expect("commit");
    git.checkout(dir, "stack").await.expect("checkout");

    // The rebase conflicts; resolving to EXACTLY the upstream content empties
    // the patch, so --continue refuses and --skip is the way out.
    assert!(git.rebase(dir, &main).await.is_err(), "conflict expected");
    std::fs::write(dir.join("f.txt"), "upstream version\n").expect("resolve");
    git.add(dir, &[PathBuf::from("f.txt")]).await.expect("add");
    assert!(
        git.rebase_continue(dir).await.is_err(),
        "apply backend refuses the emptied patch"
    );
    git.rebase_skip(dir).await.expect("rebase --skip");
    assert!(
        !git.is_rebase_in_progress(dir).await.expect("state"),
        "rebase finished after the skip"
    );
}

// capabilities round-trips against the real binary on PATH.
#[tokio::test]
#[ignore = "requires the git binary"]
async fn capabilities_probe_real_binary() {
    let caps = Git::new().capabilities().await.expect("capabilities");
    assert!(caps.is_supported(), "got {:?}", caps.version);
    caps.ensure_supported().expect("supported");
}

// The hardened profile must suppress repo-local hooks (the code-execution
// vector when driving an untrusted checkout) while a plain client runs them.
#[tokio::test]
#[ignore = "requires the git binary"]
async fn hardened_client_suppresses_repo_hooks() {
    let tmp = TempDir::new("harden");
    let dir = tmp.path();
    let plain = Git::new();
    plain.init(dir).await.expect("init");
    configure(dir);

    // A pre-commit hook that drops a marker file when it runs.
    let hooks = dir.join(".git").join("hooks");
    std::fs::create_dir_all(&hooks).expect("hooks dir");
    let hook = hooks.join("pre-commit");
    std::fs::write(&hook, "#!/bin/sh\necho ran >> hook-marker.txt\n").expect("write hook");
    // Unix git silently ignores a non-executable hook ("hook was ignored because
    // it's not set as executable"), and `fs::write` creates 0644 — without the
    // exec bit the plain-client half of this test never fires. Windows git runs
    // hooks through sh regardless, so no equivalent is needed there.
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(&hook, std::fs::Permissions::from_mode(0o755))
            .expect("make hook executable");
    }

    // Plain client: the hook fires.
    std::fs::write(dir.join("f.txt"), "one\n").expect("write");
    plain
        .add(dir, &[PathBuf::from("f.txt")])
        .await
        .expect("add");
    plain.commit(dir, "one").await.expect("commit");
    assert!(dir.join("hook-marker.txt").exists(), "hook ran unhardened");
    let runs_before = std::fs::read_to_string(dir.join("hook-marker.txt"))
        .expect("read")
        .lines()
        .count();

    // Hardened client: the hook must NOT fire.
    let hardened = Git::hardened();
    std::fs::write(dir.join("f.txt"), "two\n").expect("write");
    hardened
        .add(dir, &[PathBuf::from("f.txt")])
        .await
        .expect("add");
    hardened.commit(dir, "two").await.expect("commit");
    let runs_after = std::fs::read_to_string(dir.join("hook-marker.txt"))
        .expect("read")
        .lines()
        .count();
    assert_eq!(runs_after, runs_before, "hook suppressed under harden()");
}

// The vcs-cli-support classifiers branch on the REAL CLI's failure output; the
// hermetic tests in cli-support feed canned strings, so these three tests pin
// the classifiers against what live git actually prints. They run in the CI
// integration lane across git/jj versions, so any message drift that breaks a
// classifier gets caught here rather than at a consumer.
#[tokio::test]
#[ignore = "requires the git binary"]
async fn classifier_matches_real_merge_conflict() {
    let tmp = TempDir::new("cls-conflict");
    let dir = tmp.path();
    let git = Git::new();

    git.init(dir).await.expect("init");
    configure(dir);
    std::fs::write(dir.join("a.txt"), "base\n").expect("write");
    git.add(dir, &[PathBuf::from("a.txt")]).await.expect("add");
    git.commit(dir, "base").await.expect("commit");
    let main = git
        .current_branch(dir)
        .await
        .expect("branch")
        .expect("on a branch");

    // Branch A edits the line; main then edits the SAME line — a merge can't
    // auto-resolve, so it fails on a content conflict.
    git.create_branch(dir, "a").await.expect("branch");
    git.checkout(dir, "a").await.expect("checkout");
    std::fs::write(dir.join("a.txt"), "a change\n").expect("write");
    git.add(dir, &[PathBuf::from("a.txt")]).await.expect("add");
    git.commit(dir, "a edit").await.expect("commit");
    git.checkout(dir, &main).await.expect("checkout");
    std::fs::write(dir.join("a.txt"), "main change\n").expect("write");
    git.add(dir, &[PathBuf::from("a.txt")]).await.expect("add");
    git.commit(dir, "main edit").await.expect("commit");

    let err = git
        .merge_commit(dir, MergeCommit::branch("a"))
        .await
        .expect_err("conflicting merge must fail");
    assert!(
        vcs_git::is_merge_conflict(&err),
        "real merge-conflict output must classify, got {err:?}"
    );
}

// A `commit` on a clean tree fails with git's "nothing to commit" — the
// classifier must recognise the real wording.
#[tokio::test]
#[ignore = "requires the git binary"]
async fn classifier_matches_real_nothing_to_commit() {
    let tmp = TempDir::new("cls-nothing");
    let dir = tmp.path();
    let git = Git::new();

    git.init(dir).await.expect("init");
    configure(dir);
    std::fs::write(dir.join("a.txt"), "x\n").expect("write");
    git.add(dir, &[PathBuf::from("a.txt")]).await.expect("add");
    git.commit(dir, "seed").await.expect("commit");

    // Tree is clean now: a second commit has nothing to record.
    let err = git
        .commit(dir, "empty")
        .await
        .expect_err("commit on a clean tree must fail");
    assert!(
        vcs_git::is_nothing_to_commit(&err),
        "real nothing-to-commit output must classify, got {err:?}"
    );
}

// A fetch from an unreachable remote is a transient network failure — the
// classifier must recognise the real connection error. `fetch_from` retries
// transient errors (3 attempts x 500ms backoff), so this takes ~1-1.5s; the
// connection-refused on a closed port is immediate per attempt.
#[tokio::test]
#[ignore = "requires the git binary"]
async fn classifier_matches_real_transient_fetch() {
    let tmp = TempDir::new("cls-fetch");
    let dir = tmp.path();
    let git = Git::new();

    git.init(dir).await.expect("init");
    configure(dir);

    // Port 1 is reserved and never listening, so git's connection is refused
    // immediately ("Connection refused"/"failed to connect" — both in the
    // transient-marker list) rather than waiting on a DNS/connect timeout.
    git.remote_add(dir, "dead", "http://127.0.0.1:1/x.git")
        .await
        .expect("remote add");
    let err = git
        .fetch_from(dir, "dead")
        .await
        .expect_err("fetch from an unreachable remote must fail");
    assert!(
        vcs_git::is_transient_fetch_error(&err),
        "real connection-refused output must classify as transient, got {err:?}"
    );
}

// The typed conflict model round-trips a REAL conflicted file: parse →
// resolve(Theirs) → write back → stage → the conflict is gone.
#[tokio::test]
#[ignore = "requires the git binary"]
async fn conflict_model_resolves_a_real_conflict() {
    use vcs_git::conflict::{ResolutionSide, parse_conflicts, render, resolve};

    let tmp = TempDir::new("conflict-model");
    let dir = tmp.path();
    let git = Git::new();
    git.init(dir).await.expect("init");
    configure(dir);
    std::fs::write(dir.join("a.txt"), "base\n").expect("write");
    git.add(dir, &[PathBuf::from("a.txt")]).await.expect("add");
    git.commit(dir, "base").await.expect("commit");
    git.create_branch(dir, "other").await.expect("branch");
    std::fs::write(dir.join("a.txt"), "ours\n").expect("write");
    git.add(dir, &[PathBuf::from("a.txt")]).await.expect("add");
    git.commit(dir, "ours").await.expect("commit");
    git.checkout(dir, "other").await.expect("checkout");
    std::fs::write(dir.join("a.txt"), "theirs\n").expect("write");
    git.add(dir, &[PathBuf::from("a.txt")]).await.expect("add");
    git.commit(dir, "theirs").await.expect("commit");
    let main = "-"; // previous branch
    let _ = main;
    assert!(
        git.merge_commit(dir, MergeCommit::branch("@{-1}"))
            .await
            .is_err(),
        "conflict expected"
    );

    let content = std::fs::read_to_string(dir.join("a.txt")).expect("read");
    let segments = parse_conflicts(&content).expect("parse real markers");
    assert_eq!(render(&segments), content, "byte-exact roundtrip");
    let resolved = resolve(&segments, ResolutionSide::Theirs).expect("resolve");
    assert!(!resolved.contains("<<<<<<<"), "markers gone");
    std::fs::write(dir.join("a.txt"), &resolved).expect("write resolved");
    git.add(dir, &[PathBuf::from("a.txt")]).await.expect("add");
    assert!(
        git.conflicted_files(dir)
            .await
            .expect("conflicted")
            .is_empty(),
        "conflict cleared after writing the resolution"
    );
}