git-std 0.11.12

Standard git workflow — commits, versioning, hooks
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
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
use std::path::Path;

use assert_cmd::Command;

/// Helper: run a git command and return stdout.
fn git(dir: &Path, args: &[&str]) -> String {
    let output = std::process::Command::new("git")
        .current_dir(dir)
        .args(args)
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "git {:?} failed: {}",
        args,
        String::from_utf8_lossy(&output.stderr)
    );
    String::from_utf8_lossy(&output.stdout).trim().to_string()
}

/// Helper: get HEAD commit message.
fn head_message(dir: &Path) -> String {
    git(dir, &["log", "-1", "--format=%B"]).trim().to_string()
}

/// Helper: initialise a git repo for hooks tests.
fn init_hooks_repo(dir: &Path) {
    git(dir, &["init"]);
    git(dir, &["config", "user.name", "Test"]);
    git(dir, &["config", "user.email", "test@test.com"]);
}

// --- Hooks run integration tests (#32–#35) ---

/// #32 — Argument passthrough: `{msg}` token gets substituted with the path
/// passed after `--`.
#[test]
fn hooks_run_arg_passthrough_substitutes_msg_token() {
    let dir = tempfile::tempdir().unwrap();
    init_hooks_repo(dir.path());

    let hooks_dir = dir.path().join(".githooks");
    std::fs::create_dir_all(&hooks_dir).unwrap();
    // Use `echo {msg}` so the substituted path appears in output.
    std::fs::write(hooks_dir.join("commit-msg.hooks"), "! echo {msg}\n").unwrap();

    let assert = Command::cargo_bin("git-std")
        .unwrap()
        .args([
            "--color",
            "never",
            "hook",
            "run",
            "commit-msg",
            "--",
            "/tmp/test-msg",
        ])
        .current_dir(dir.path())
        .assert()
        .success();

    // The substituted path should appear in stdout (from echo) or stderr
    // (from the hook runner summary line).
    let stdout = String::from_utf8_lossy(&assert.get_output().stdout);
    let stderr = String::from_utf8_lossy(&assert.get_output().stderr);
    let combined = format!("{stdout}{stderr}");
    assert!(
        combined.contains("/tmp/test-msg"),
        "output should contain the substituted path, got:\nstdout: {stdout}\nstderr: {stderr}"
    );
}

/// #33 — Pre-commit workflow: mix of passing, failing, and advisory commands.
#[test]
fn hooks_run_pre_commit_workflow() {
    let dir = tempfile::tempdir().unwrap();
    init_hooks_repo(dir.path());

    let hooks_dir = dir.path().join(".githooks");
    std::fs::create_dir_all(&hooks_dir).unwrap();
    // The advisory command (`? false`) fails but is advisory, so it gets ⚠.
    std::fs::write(
        hooks_dir.join("pre-commit.hooks"),
        "echo \"lint ok\"\nfalse\n? false\n",
    )
    .unwrap();

    let assert = Command::cargo_bin("git-std")
        .unwrap()
        .args(["--color", "never", "hook", "run", "pre-commit"])
        .current_dir(dir.path())
        .assert()
        .code(1);

    let stderr = String::from_utf8_lossy(&assert.get_output().stderr);
    // Check mark for the passing `echo "lint ok"` command.
    assert!(
        stderr.contains('\u{2713}'),
        "should contain check mark for passing command, got: {stderr}"
    );
    // Cross mark for the failing `false` command.
    assert!(
        stderr.contains('\u{2717}'),
        "should contain cross mark for failing command, got: {stderr}"
    );
    // Warning mark for the advisory `echo "advisory warning"` command.
    assert!(
        stderr.contains('\u{26a0}'),
        "should contain warning mark for advisory command, got: {stderr}"
    );
}

/// #34 — Commit-msg workflow: bad message fails validation.
#[test]
fn hooks_run_commit_msg_bad_message_fails() {
    let dir = tempfile::tempdir().unwrap();
    init_hooks_repo(dir.path());

    // Use the cargo-built binary path so the hook command works in CI
    // where `git std` isn't on PATH.
    let bin = Command::cargo_bin("git-std")
        .unwrap()
        .get_program()
        .to_owned();
    let bin_str = bin.to_string_lossy();

    let hooks_dir = dir.path().join(".githooks");
    std::fs::create_dir_all(&hooks_dir).unwrap();
    std::fs::write(
        hooks_dir.join("commit-msg.hooks"),
        format!("! {bin_str} lint --file {{msg}}\n"),
    )
    .unwrap();

    // Write a bad commit message to a temp file.
    let msg_file = dir.path().join("COMMIT_MSG");
    std::fs::write(&msg_file, "bad message\n").unwrap();

    Command::cargo_bin("git-std")
        .unwrap()
        .args([
            "--color",
            "never",
            "hook",
            "run",
            "commit-msg",
            "--",
            msg_file.to_str().unwrap(),
        ])
        .current_dir(dir.path())
        .assert()
        .code(1);
}

/// #34 — Commit-msg workflow: good message passes validation.
#[test]
fn hooks_run_commit_msg_good_message_passes() {
    let dir = tempfile::tempdir().unwrap();
    init_hooks_repo(dir.path());

    // Use the cargo-built binary path so the hook command works in CI.
    let bin = Command::cargo_bin("git-std")
        .unwrap()
        .get_program()
        .to_owned();
    let bin_str = bin.to_string_lossy();

    let hooks_dir = dir.path().join(".githooks");
    std::fs::create_dir_all(&hooks_dir).unwrap();
    std::fs::write(
        hooks_dir.join("commit-msg.hooks"),
        format!("! {bin_str} lint --file {{msg}}\n"),
    )
    .unwrap();

    // Write a valid conventional commit message to a temp file.
    let msg_file = dir.path().join("COMMIT_MSG");
    std::fs::write(&msg_file, "feat: valid commit\n").unwrap();

    Command::cargo_bin("git-std")
        .unwrap()
        .args([
            "--color",
            "never",
            "hook",
            "run",
            "commit-msg",
            "--",
            msg_file.to_str().unwrap(),
        ])
        .current_dir(dir.path())
        .assert()
        .success();
}

/// #35 — Full install cycle: install hooks, then commit through git which
/// triggers the shims.
#[test]
fn hooks_full_install_cycle() {
    let dir = tempfile::tempdir().unwrap();
    git(dir.path(), &["init"]);
    git(dir.path(), &["config", "user.name", "Test"]);
    git(dir.path(), &["config", "user.email", "test@test.com"]);

    let hooks_dir = dir.path().join(".githooks");
    std::fs::create_dir_all(&hooks_dir).unwrap();
    std::fs::write(
        hooks_dir.join("pre-commit.hooks"),
        "echo \"pre-commit ok\"\n",
    )
    .unwrap();
    // Use the cargo-built binary path for the commit-msg hook so it works
    // in CI where `git std` isn't on PATH.
    let bin = Command::cargo_bin("git-std")
        .unwrap()
        .get_program()
        .to_owned();
    let bin_str = bin.to_string_lossy();
    std::fs::write(
        hooks_dir.join("commit-msg.hooks"),
        format!("! {bin_str} lint --file {{msg}}\n"),
    )
    .unwrap();

    // Run `git std init`.
    Command::cargo_bin("git-std")
        .unwrap()
        .args(["init"])
        .env("GIT_STD_HOOKS_ENABLE", "pre-commit,commit-msg")
        .current_dir(dir.path())
        .assert()
        .success();

    // Verify core.hooksPath is set.
    let hooks_path = git(dir.path(), &["config", "core.hooksPath"]);
    assert_eq!(hooks_path, ".githooks");

    // Verify shims exist and are executable.
    let pre_commit_shim = hooks_dir.join("pre-commit");
    let commit_msg_shim = hooks_dir.join("commit-msg");
    assert!(pre_commit_shim.exists(), "pre-commit shim should exist");
    assert!(commit_msg_shim.exists(), "commit-msg shim should exist");

    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let perms = std::fs::metadata(&pre_commit_shim).unwrap().permissions();
        assert!(
            perms.mode() & 0o111 != 0,
            "pre-commit shim should be executable"
        );
        let perms = std::fs::metadata(&commit_msg_shim).unwrap().permissions();
        assert!(
            perms.mode() & 0o111 != 0,
            "commit-msg shim should be executable"
        );
    }

    // The shims call `git std hook run ...` which invokes `git-std` as a
    // git subcommand. For this to work, the `git-std` binary must be on
    // PATH. Locate the cargo-built binary and prepend its directory.
    let bin_path = Command::cargo_bin("git-std")
        .unwrap()
        .get_program()
        .to_owned();
    let bin_dir = Path::new(&bin_path).parent().unwrap();
    let path_env = format!(
        "{}:{}",
        bin_dir.display(),
        std::env::var("PATH").unwrap_or_default()
    );

    // Stage a file and commit with a valid conventional message.
    // The hooks fire (pre-commit + commit-msg) via the installed shims.
    std::fs::write(dir.path().join("hello.txt"), "hello\n").unwrap();

    let status = std::process::Command::new("git")
        .args(["add", "hello.txt"])
        .current_dir(dir.path())
        .status()
        .unwrap();
    assert!(status.success(), "git add should succeed");

    let output = std::process::Command::new("git")
        .args(["commit", "-m", "feat: add hello"])
        .current_dir(dir.path())
        .env("PATH", &path_env)
        .output()
        .unwrap();
    assert!(
        output.status.success(),
        "git commit with valid message should succeed when hooks are installed.\nstdout: {}\nstderr: {}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr),
    );

    // Verify the commit was created.
    let msg = head_message(dir.path());
    assert!(
        msg.starts_with("feat: add hello"),
        "commit message should start with 'feat: add hello', got: {msg:?}",
    );
}

// --- Fail-fast mode integration test (#114) ---

/// #114 — Fail-fast mode stops on first failure and skips remaining commands.
///
/// Uses `pre-push` which defaults to fail-fast mode. The first command
/// succeeds, the second fails, and the third should be skipped.
#[test]
fn hooks_run_fail_fast_skips_remaining_on_failure() {
    let dir = tempfile::tempdir().unwrap();
    init_hooks_repo(dir.path());

    let hooks_dir = dir.path().join(".githooks");
    std::fs::create_dir_all(&hooks_dir).unwrap();
    // pre-push defaults to fail-fast mode:
    //   1. `true`  — succeeds
    //   2. `false` — fails  (should trigger abort)
    //   3. `echo should-not-run` — should be skipped
    std::fs::write(
        hooks_dir.join("pre-push.hooks"),
        "true\nfalse\necho should-not-run\n",
    )
    .unwrap();

    let assert = Command::cargo_bin("git-std")
        .unwrap()
        .args(["--color", "never", "hook", "run", "pre-push"])
        .current_dir(dir.path())
        .assert()
        .code(1);

    let stderr = String::from_utf8_lossy(&assert.get_output().stderr);
    let stdout = String::from_utf8_lossy(&assert.get_output().stdout);
    let combined = format!("{stdout}{stderr}");

    // The first command should pass.
    assert!(
        combined.contains('\u{2713}'),
        "should contain check mark for passing command, got: {combined}"
    );
    // The second command should fail.
    assert!(
        combined.contains('\u{2717}'),
        "should contain cross mark for failing command, got: {combined}"
    );
    // The runner should report that remaining commands were skipped.
    assert!(
        combined.contains("skipped (fail-fast)"),
        "should report skipped commands, got: {combined}"
    );
    // The skipped command should NOT have run.
    assert!(
        !combined.contains("should-not-run"),
        "skipped command output should not appear, got: {combined}"
    );
}

/// #114 — Fail-fast with explicit `!` prefix on a collect-mode hook.
///
/// Uses `pre-commit` (collect mode by default) but the failing command
/// has a `!` prefix, forcing fail-fast for that command.
#[test]
fn hooks_run_fail_fast_prefix_overrides_collect_mode() {
    let dir = tempfile::tempdir().unwrap();
    init_hooks_repo(dir.path());

    let hooks_dir = dir.path().join(".githooks");
    std::fs::create_dir_all(&hooks_dir).unwrap();
    // pre-commit defaults to collect mode, but `!false` forces fail-fast
    // for that specific command:
    //   1. `true`  — succeeds
    //   2. `!false` — fails with fail-fast prefix (should abort)
    //   3. `echo should-not-run` — should be skipped
    std::fs::write(
        hooks_dir.join("pre-commit.hooks"),
        "true\n!false\necho should-not-run\n",
    )
    .unwrap();

    let assert = Command::cargo_bin("git-std")
        .unwrap()
        .args(["--color", "never", "hook", "run", "pre-commit"])
        .current_dir(dir.path())
        .assert()
        .code(1);

    let stderr = String::from_utf8_lossy(&assert.get_output().stderr);
    let stdout = String::from_utf8_lossy(&assert.get_output().stdout);
    let combined = format!("{stdout}{stderr}");

    // Should report skipped commands due to fail-fast.
    assert!(
        combined.contains("skipped (fail-fast)"),
        "should report skipped commands when ! prefix triggers fail-fast, got: {combined}"
    );
    // The skipped command should NOT have run.
    assert!(
        !combined.contains("should-not-run"),
        "skipped command output should not appear, got: {combined}"
    );
}

// --- Fix-mode (~) integration tests (#197) ---

/// #197 — $@ is populated with staged file paths for pre-commit.
///
/// The `~` hook command runs with staged files as positional parameters.
/// We verify $@ is non-empty by echoing "$@" and checking the file name
/// appears in output.
#[test]
fn hooks_run_fix_mode_staged_files_passed_as_positional_args() {
    let dir = tempfile::tempdir().unwrap();
    init_hooks_repo(dir.path());

    // Create and stage a file so $@ is non-empty.
    std::fs::write(dir.path().join("hello.txt"), "hello\n").unwrap();
    git(dir.path(), &["add", "hello.txt"]);

    let hooks_dir = dir.path().join(".githooks");
    std::fs::create_dir_all(&hooks_dir).unwrap();
    // The ~ command echoes $@ — staged file names should appear in output.
    std::fs::write(
        hooks_dir.join("pre-commit.hooks"),
        "~ echo \"staged: $@\"\n",
    )
    .unwrap();

    let assert = Command::cargo_bin("git-std")
        .unwrap()
        .args(["--color", "never", "hook", "run", "pre-commit"])
        .current_dir(dir.path())
        .assert()
        .success();

    let stdout = String::from_utf8_lossy(&assert.get_output().stdout);
    let stderr = String::from_utf8_lossy(&assert.get_output().stderr);
    let combined = format!("{stdout}{stderr}");
    assert!(
        combined.contains("hello.txt"),
        "$@ should contain staged file names, got:\n{combined}"
    );
}

/// #197 — Normal commands also receive $@ with staged file paths.
#[test]
fn hooks_run_staged_files_passed_to_normal_commands() {
    let dir = tempfile::tempdir().unwrap();
    init_hooks_repo(dir.path());

    std::fs::write(dir.path().join("world.txt"), "world\n").unwrap();
    git(dir.path(), &["add", "world.txt"]);

    let hooks_dir = dir.path().join(".githooks");
    std::fs::create_dir_all(&hooks_dir).unwrap();
    // Plain command (no prefix) also gets $@ with staged files.
    std::fs::write(hooks_dir.join("pre-commit.hooks"), "echo \"files: $@\"\n").unwrap();

    let assert = Command::cargo_bin("git-std")
        .unwrap()
        .args(["--color", "never", "hook", "run", "pre-commit"])
        .current_dir(dir.path())
        .assert()
        .success();

    let stdout = String::from_utf8_lossy(&assert.get_output().stdout);
    let stderr = String::from_utf8_lossy(&assert.get_output().stderr);
    let combined = format!("{stdout}{stderr}");
    assert!(
        combined.contains("world.txt"),
        "$@ should contain staged file names for plain commands, got:\n{combined}"
    );
}

/// #197 — `~` in pre-commit: stash dance runs, staged content re-staged.
///
/// We stage a file, then a `~` formatter appends a line to it.
/// After the hook runs, the staged version should include the formatter's change.
#[test]
fn hooks_run_fix_mode_pre_commit_restages_formatted_content() {
    let dir = tempfile::tempdir().unwrap();
    init_hooks_repo(dir.path());

    // Write and stage the initial file.
    std::fs::write(dir.path().join("fmt.txt"), "line1\n").unwrap();
    git(dir.path(), &["add", "fmt.txt"]);

    let hooks_dir = dir.path().join(".githooks");
    std::fs::create_dir_all(&hooks_dir).unwrap();
    // The formatter appends "formatted" to each staged file passed via $@.
    std::fs::write(
        hooks_dir.join("pre-commit.hooks"),
        "~ for f in \"$@\"; do echo 'formatted' >> \"$f\"; done\n",
    )
    .unwrap();

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["--color", "never", "hook", "run", "pre-commit"])
        .current_dir(dir.path())
        .assert()
        .success();

    // After the hook, the staged version of fmt.txt should contain "formatted".
    let staged_content = std::process::Command::new("git")
        .args(["show", ":fmt.txt"])
        .current_dir(dir.path())
        .output()
        .unwrap();
    let staged_str = String::from_utf8_lossy(&staged_content.stdout);
    assert!(
        staged_str.contains("formatted"),
        "staged content should include formatter output, got: {staged_str}"
    );
}

/// #197 — `~` in a non-pre-commit hook prints a warning and treats as `!`.
#[test]
fn hooks_run_fix_mode_non_pre_commit_warns_and_treats_as_fail_fast() {
    let dir = tempfile::tempdir().unwrap();
    init_hooks_repo(dir.path());

    let hooks_dir = dir.path().join(".githooks");
    std::fs::create_dir_all(&hooks_dir).unwrap();
    // Use commit-msg with ~ prefix on a passing command.
    std::fs::write(hooks_dir.join("commit-msg.hooks"), "~ true\n").unwrap();

    let assert = Command::cargo_bin("git-std")
        .unwrap()
        .args(["--color", "never", "hook", "run", "commit-msg"])
        .current_dir(dir.path())
        .assert()
        .success();

    let stderr = String::from_utf8_lossy(&assert.get_output().stderr);
    assert!(
        stderr.contains("warning:"),
        "should print a warning for ~ in non-pre-commit, got:\n{stderr}"
    );
    assert!(
        stderr.contains("pre-commit"),
        "warning should mention pre-commit, got:\n{stderr}"
    );
}

/// #197 — `~` in a non-pre-commit hook: failing command causes non-zero exit.
#[test]
fn hooks_run_fix_mode_non_pre_commit_failing_command_fails() {
    let dir = tempfile::tempdir().unwrap();
    init_hooks_repo(dir.path());

    let hooks_dir = dir.path().join(".githooks");
    std::fs::create_dir_all(&hooks_dir).unwrap();
    // commit-msg with ~ prefix on a failing command — should fail.
    std::fs::write(hooks_dir.join("commit-msg.hooks"), "~ false\n").unwrap();

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["--color", "never", "hook", "run", "commit-msg"])
        .current_dir(dir.path())
        .assert()
        .code(1);
}

/// #268 — Fix mode preserves staged deletions.
///
/// When a file is staged for deletion (`git rm`), a `~` fix command must not
/// undo the deletion. After the hook runs, the file should still be staged
/// for deletion in the index.
#[test]
fn hooks_run_fix_mode_preserves_staged_deletions() {
    let dir = tempfile::tempdir().unwrap();
    init_hooks_repo(dir.path());

    // Create and commit a file so we can delete it later.
    std::fs::write(dir.path().join("to-delete.txt"), "content\n").unwrap();
    std::fs::write(dir.path().join("to-keep.txt"), "keep\n").unwrap();
    git(dir.path(), &["add", "to-delete.txt", "to-keep.txt"]);
    git(dir.path(), &["commit", "-m", "initial"]);

    // Stage the file for deletion.
    git(dir.path(), &["rm", "to-delete.txt"]);

    // Also stage a modification so the fix command has something to work with.
    std::fs::write(dir.path().join("to-keep.txt"), "modified\n").unwrap();
    git(dir.path(), &["add", "to-keep.txt"]);

    let hooks_dir = dir.path().join(".githooks");
    std::fs::create_dir_all(&hooks_dir).unwrap();
    // A no-op formatter that succeeds without modifying files.
    std::fs::write(hooks_dir.join("pre-commit.hooks"), "~ true\n").unwrap();

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["--color", "never", "hook", "run", "pre-commit"])
        .current_dir(dir.path())
        .assert()
        .success();

    // Verify the deletion is still staged in the index.
    let status_output = std::process::Command::new("git")
        .args(["diff", "--cached", "--name-only", "--diff-filter=D"])
        .current_dir(dir.path())
        .output()
        .unwrap();
    let deleted = String::from_utf8_lossy(&status_output.stdout);
    assert!(
        deleted.contains("to-delete.txt"),
        "to-delete.txt should still be staged for deletion after fix-mode hook, got:\n{deleted}"
    );

    // Verify the kept file is still staged.
    let status_output = std::process::Command::new("git")
        .args(["diff", "--cached", "--name-only", "--diff-filter=M"])
        .current_dir(dir.path())
        .output()
        .unwrap();
    let modified = String::from_utf8_lossy(&status_output.stdout);
    assert!(
        modified.contains("to-keep.txt"),
        "to-keep.txt should still be staged after fix-mode hook, got:\n{modified}"
    );
}

/// #268 — Deleted files are not passed as $@ to fix commands.
///
/// Files staged for deletion should not appear in the positional parameters
/// passed to fix-mode commands, since formatters cannot operate on deleted files.
#[test]
fn hooks_run_fix_mode_excludes_deleted_files_from_args() {
    let dir = tempfile::tempdir().unwrap();
    init_hooks_repo(dir.path());

    // Create and commit files.
    std::fs::write(dir.path().join("deleted.txt"), "gone\n").unwrap();
    std::fs::write(dir.path().join("kept.txt"), "here\n").unwrap();
    git(dir.path(), &["add", "deleted.txt", "kept.txt"]);
    git(dir.path(), &["commit", "-m", "initial"]);

    // Stage one for deletion, modify the other.
    git(dir.path(), &["rm", "deleted.txt"]);
    std::fs::write(dir.path().join("kept.txt"), "modified\n").unwrap();
    git(dir.path(), &["add", "kept.txt"]);

    let hooks_dir = dir.path().join(".githooks");
    std::fs::create_dir_all(&hooks_dir).unwrap();
    // Echo $@ so we can verify which files are passed.
    std::fs::write(hooks_dir.join("pre-commit.hooks"), "~ echo \"args: $@\"\n").unwrap();

    let assert = Command::cargo_bin("git-std")
        .unwrap()
        .args(["--color", "never", "hook", "run", "pre-commit"])
        .current_dir(dir.path())
        .assert()
        .success();

    let stdout = String::from_utf8_lossy(&assert.get_output().stdout);
    let stderr = String::from_utf8_lossy(&assert.get_output().stderr);
    let combined = format!("{stdout}{stderr}");

    // kept.txt should be in $@.
    assert!(
        combined.contains("kept.txt"),
        "kept.txt should appear in $@, got:\n{combined}"
    );
    // deleted.txt should NOT be in $@ (already excluded by ACMR filter).
    assert!(
        !combined.contains("deleted.txt"),
        "deleted.txt should not appear in $@, got:\n{combined}"
    );
}

/// #197 — No stash dance when no `~` commands are present.
///
/// A plain pre-commit hook with no `~` commands should not attempt any stash
/// operations (the hook runs normally).
#[test]
fn hooks_run_no_stash_dance_without_fix_commands() {
    let dir = tempfile::tempdir().unwrap();
    init_hooks_repo(dir.path());

    std::fs::write(dir.path().join("file.txt"), "content\n").unwrap();
    git(dir.path(), &["add", "file.txt"]);

    let hooks_dir = dir.path().join(".githooks");
    std::fs::create_dir_all(&hooks_dir).unwrap();
    std::fs::write(hooks_dir.join("pre-commit.hooks"), "true\n").unwrap();

    // Should succeed without any stash-related warnings.
    let assert = Command::cargo_bin("git-std")
        .unwrap()
        .args(["--color", "never", "hook", "run", "pre-commit"])
        .current_dir(dir.path())
        .assert()
        .success();

    let stderr = String::from_utf8_lossy(&assert.get_output().stderr);
    assert!(
        !stderr.contains("stash"),
        "no stash messages expected for hook without ~ commands, got:\n{stderr}"
    );
}

/// #268 — Fix mode preserves staged deletions on fail-fast early exit.
///
/// When a `~` fix command fails, the hook aborts early (fail-fast). Staged
/// deletions must still be preserved in the index after the early exit.
#[test]
fn hooks_run_fix_mode_preserves_staged_deletions_on_failfast() {
    let dir = tempfile::tempdir().unwrap();
    init_hooks_repo(dir.path());

    // Create and commit two files so we can delete one and modify the other.
    std::fs::write(dir.path().join("to-delete.txt"), "content\n").unwrap();
    std::fs::write(dir.path().join("to-keep.txt"), "keep\n").unwrap();
    git(dir.path(), &["add", "to-delete.txt", "to-keep.txt"]);
    git(dir.path(), &["commit", "-m", "initial"]);

    // Stage the file for deletion.
    git(dir.path(), &["rm", "to-delete.txt"]);

    // Stage a modification so the fix command has something to work with.
    std::fs::write(dir.path().join("to-keep.txt"), "modified\n").unwrap();
    git(dir.path(), &["add", "to-keep.txt"]);

    let hooks_dir = dir.path().join(".githooks");
    std::fs::create_dir_all(&hooks_dir).unwrap();
    // A fix command that always fails — triggers fail-fast early exit.
    std::fs::write(hooks_dir.join("pre-commit.hooks"), "~ false\n").unwrap();

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["--color", "never", "hook", "run", "pre-commit"])
        .current_dir(dir.path())
        .assert()
        .code(1);

    // Verify the deletion is still staged in the index after fail-fast.
    let status_output = std::process::Command::new("git")
        .args(["diff", "--cached", "--name-only", "--diff-filter=D"])
        .current_dir(dir.path())
        .output()
        .unwrap();
    let deleted = String::from_utf8_lossy(&status_output.stdout);
    assert!(
        deleted.contains("to-delete.txt"),
        "to-delete.txt should still be staged for deletion after fail-fast, got:\n{deleted}"
    );
}

// --- Stash dance corner cases (#280–#282) ---

/// #280 — Renamed files survive the stash dance.
///
/// A `git mv` rename should be preserved through the fix-mode stash/unstash
/// cycle. After the hook runs, the index should still show the rename (R status)
/// with the new name staged and the old name removed.
#[test]
fn hooks_run_fix_mode_preserves_renamed_files() {
    let dir = tempfile::tempdir().unwrap();
    init_hooks_repo(dir.path());

    // Create and commit a file so we can rename it.
    std::fs::write(dir.path().join("old.txt"), "hello").unwrap();
    git(dir.path(), &["add", "old.txt"]);
    git(dir.path(), &["commit", "-m", "initial"]);

    // Rename the file (git mv stages the rename automatically).
    git(dir.path(), &["mv", "old.txt", "new.txt"]);

    let hooks_dir = dir.path().join(".githooks");
    std::fs::create_dir_all(&hooks_dir).unwrap();
    // A no-op formatter that succeeds without modifying files.
    std::fs::write(hooks_dir.join("pre-commit.hooks"), "~ true\n").unwrap();

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["--color", "never", "hook", "run", "pre-commit"])
        .current_dir(dir.path())
        .assert()
        .success();

    // Verify the rename is still staged (R status with new.txt present).
    let status_output = std::process::Command::new("git")
        .args(["diff", "--cached", "--name-status"])
        .current_dir(dir.path())
        .output()
        .unwrap();
    let name_status = String::from_utf8_lossy(&status_output.stdout);
    assert!(
        name_status.contains("new.txt"),
        "new.txt should be in the staged files after stash dance, got:\n{name_status}"
    );

    // old.txt should NOT be in staged files (it was renamed away).
    let staged_output = std::process::Command::new("git")
        .args(["diff", "--cached", "--name-only", "--diff-filter=ACMR"])
        .current_dir(dir.path())
        .output()
        .unwrap();
    let staged_names = String::from_utf8_lossy(&staged_output.stdout);
    assert!(
        !staged_names.contains("old.txt"),
        "old.txt should not be in the staged files after rename, got:\n{staged_names}"
    );
}

/// #281 — Formatter-created files are NOT staged.
///
/// When a `~` fix command creates a new file that was not originally staged,
/// the stash dance must not add it to the index. Only the originally-staged
/// files should remain staged.
#[test]
fn hooks_run_fix_mode_does_not_stage_formatter_created_files() {
    let dir = tempfile::tempdir().unwrap();
    init_hooks_repo(dir.path());

    // Create and stage a file.
    std::fs::write(dir.path().join("src.txt"), "source\n").unwrap();
    git(dir.path(), &["add", "src.txt"]);

    let hooks_dir = dir.path().join(".githooks");
    std::fs::create_dir_all(&hooks_dir).unwrap();
    // Formatter that creates a new file "extra.txt" as a side effect.
    std::fs::write(
        hooks_dir.join("pre-commit.hooks"),
        "~ sh -c \"echo created > extra.txt\"\n",
    )
    .unwrap();

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["--color", "never", "hook", "run", "pre-commit"])
        .current_dir(dir.path())
        .assert()
        .success();

    // extra.txt should exist on disk (the formatter created it).
    assert!(
        dir.path().join("extra.txt").exists(),
        "extra.txt should exist on disk after formatter ran"
    );

    // extra.txt should NOT be staged.
    let staged_output = std::process::Command::new("git")
        .args(["diff", "--cached", "--name-only"])
        .current_dir(dir.path())
        .output()
        .unwrap();
    let staged = String::from_utf8_lossy(&staged_output.stdout);
    assert!(
        !staged.contains("extra.txt"),
        "extra.txt should not be staged (formatter side effect), got:\n{staged}"
    );

    // src.txt SHOULD still be staged.
    assert!(
        staged.contains("src.txt"),
        "src.txt should still be staged after hook, got:\n{staged}"
    );
}

/// #282 — Binary files survive the stash dance.
///
/// Binary file content must be preserved byte-for-byte through the
/// stash/unstash cycle. We stage a file with raw bytes (PNG header) and
/// verify the staged content is identical after the hook runs.
#[test]
fn hooks_run_fix_mode_preserves_binary_files() {
    let dir = tempfile::tempdir().unwrap();
    init_hooks_repo(dir.path());

    // Create a binary file with a PNG header signature.
    let png_header: &[u8] = &[0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];
    std::fs::write(dir.path().join("image.bin"), png_header).unwrap();
    git(dir.path(), &["add", "image.bin"]);

    let hooks_dir = dir.path().join(".githooks");
    std::fs::create_dir_all(&hooks_dir).unwrap();
    // A no-op formatter that succeeds without modifying files.
    std::fs::write(hooks_dir.join("pre-commit.hooks"), "~ true\n").unwrap();

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["--color", "never", "hook", "run", "pre-commit"])
        .current_dir(dir.path())
        .assert()
        .success();

    // The staged content of image.bin should be byte-identical to the original.
    let show_output = std::process::Command::new("git")
        .args(["show", ":image.bin"])
        .current_dir(dir.path())
        .output()
        .unwrap();
    assert!(
        show_output.status.success(),
        "git show :image.bin should succeed"
    );
    assert_eq!(
        show_output.stdout, png_header,
        "staged binary content should be byte-identical after stash dance"
    );
}

/// #277 + #278 — Fix-mode failure prints actionable hints.
///
/// When a `~` fix command fails, the output must include hint lines telling
/// the user how to skip the hook, skip all hooks, and disable the command.
#[test]
fn hooks_run_fix_mode_failure_prints_hints() {
    let dir = tempfile::tempdir().unwrap();
    init_hooks_repo(dir.path());

    std::fs::write(dir.path().join("file.txt"), "content\n").unwrap();
    git(dir.path(), &["add", "file.txt"]);

    let hooks_dir = dir.path().join(".githooks");
    std::fs::create_dir_all(&hooks_dir).unwrap();
    std::fs::write(hooks_dir.join("pre-commit.hooks"), "~ false\n").unwrap();

    let assert = Command::cargo_bin("git-std")
        .unwrap()
        .args(["--color", "never", "hook", "run", "pre-commit"])
        .current_dir(dir.path())
        .assert()
        .code(1);

    let stderr = String::from_utf8_lossy(&assert.get_output().stderr);

    assert!(
        stderr.contains("hint: to skip this hook:"),
        "should print skip-hook hint, got:\n{stderr}"
    );
    assert!(
        stderr.contains("--no-verify"),
        "should mention --no-verify, got:\n{stderr}"
    );
    assert!(
        stderr.contains("GIT_STD_SKIP_HOOKS=1"),
        "should mention GIT_STD_SKIP_HOOKS, got:\n{stderr}"
    );
    assert!(
        stderr.contains(".githooks/pre-commit.hooks"),
        "should reference the hooks file, got:\n{stderr}"
    );
}

/// #279 — Formatter-deleted files are not re-staged as deletions.
///
/// If a fix-mode formatter deletes a file that was staged for modification,
/// `restage_files()` should skip it with a warning instead of silently
/// staging a deletion.
#[test]
fn hooks_run_fix_mode_skips_restage_of_formatter_deleted_files() {
    let dir = tempfile::tempdir().unwrap();
    init_hooks_repo(dir.path());

    // Create and commit a file.
    std::fs::write(dir.path().join("victim.txt"), "original\n").unwrap();
    std::fs::write(dir.path().join("survivor.txt"), "keep\n").unwrap();
    git(dir.path(), &["add", "victim.txt", "survivor.txt"]);
    git(dir.path(), &["commit", "-m", "initial"]);

    // Stage modifications.
    std::fs::write(dir.path().join("victim.txt"), "modified\n").unwrap();
    std::fs::write(dir.path().join("survivor.txt"), "also modified\n").unwrap();
    git(dir.path(), &["add", "victim.txt", "survivor.txt"]);

    let hooks_dir = dir.path().join(".githooks");
    std::fs::create_dir_all(&hooks_dir).unwrap();
    // Formatter that deletes victim.txt.
    std::fs::write(hooks_dir.join("pre-commit.hooks"), "~ rm -f victim.txt\n").unwrap();

    let assert = Command::cargo_bin("git-std")
        .unwrap()
        .args(["--color", "never", "hook", "run", "pre-commit"])
        .current_dir(dir.path())
        .assert()
        .success();

    let stderr = String::from_utf8_lossy(&assert.get_output().stderr);

    // Warning about the deleted file.
    assert!(
        stderr.contains("victim.txt") && stderr.contains("deleted by formatter"),
        "should warn about formatter-deleted file, got:\n{stderr}"
    );

    // victim.txt should NOT be staged as a deletion.
    let deletions = std::process::Command::new("git")
        .args(["diff", "--cached", "--name-only", "--diff-filter=D"])
        .current_dir(dir.path())
        .output()
        .unwrap();
    let deleted = String::from_utf8_lossy(&deletions.stdout);
    assert!(
        !deleted.contains("victim.txt"),
        "victim.txt should not be staged as deletion, got:\n{deleted}"
    );

    // survivor.txt should still be staged.
    let staged = std::process::Command::new("git")
        .args(["diff", "--cached", "--name-only", "--diff-filter=ACMR"])
        .current_dir(dir.path())
        .output()
        .unwrap();
    let staged_files = String::from_utf8_lossy(&staged.stdout);
    assert!(
        staged_files.contains("survivor.txt"),
        "survivor.txt should still be staged, got:\n{staged_files}"
    );
}

/// #283 — Fix mode rejects submodule entries.
///
/// When a submodule is staged and fix-mode commands exist, the hook
/// should reject execution with a clear error and hint.
#[test]
fn hooks_run_fix_mode_rejects_staged_submodules() {
    let dir = tempfile::tempdir().unwrap();
    init_hooks_repo(dir.path());

    // Create a bare repo to use as a submodule source.
    let sub_source = tempfile::tempdir().unwrap();
    git(sub_source.path(), &["init", "--bare"]);

    // We need a commit in the bare repo for submodule add to work.
    let sub_work = tempfile::tempdir().unwrap();
    git(sub_work.path(), &["init"]);
    git(sub_work.path(), &["config", "user.name", "Test"]);
    git(sub_work.path(), &["config", "user.email", "test@test.com"]);
    std::fs::write(sub_work.path().join("readme.txt"), "sub\n").unwrap();
    git(sub_work.path(), &["add", "readme.txt"]);
    git(sub_work.path(), &["commit", "-m", "init sub"]);
    git(
        sub_work.path(),
        &[
            "remote",
            "add",
            "origin",
            sub_source.path().to_str().unwrap(),
        ],
    );
    git(sub_work.path(), &["push", "origin", "HEAD"]);

    // Add the submodule to the main repo.
    let sub_url = sub_source.path().to_str().unwrap();
    let output = std::process::Command::new("git")
        .args(["submodule", "add", sub_url, "submod"])
        .current_dir(dir.path())
        .env("GIT_ALLOW_PROTOCOL", "file")
        .output()
        .unwrap();
    if !output.status.success() {
        // If submodule add is unsupported in this git version, skip the test.
        eprintln!(
            "skipping: git submodule add failed: {}",
            String::from_utf8_lossy(&output.stderr)
        );
        return;
    }

    let hooks_dir = dir.path().join(".githooks");
    std::fs::create_dir_all(&hooks_dir).unwrap();
    std::fs::write(hooks_dir.join("pre-commit.hooks"), "~ true\n").unwrap();

    let assert = Command::cargo_bin("git-std")
        .unwrap()
        .args(["--color", "never", "hook", "run", "pre-commit"])
        .current_dir(dir.path())
        .assert()
        .code(1);

    let stderr = String::from_utf8_lossy(&assert.get_output().stderr);

    // Should report that fix mode doesn't support submodules.
    assert!(
        stderr.contains("submodule"),
        "should mention submodules in error, got:\n{stderr}"
    );
}

// ── repo-root resolution (#320) ─────────────────────────────────

#[test]
fn hooks_run_from_subdirectory() {
    let dir = tempfile::tempdir().unwrap();
    init_hooks_repo(dir.path());

    let hooks_dir = dir.path().join(".githooks");
    std::fs::create_dir_all(&hooks_dir).unwrap();
    std::fs::write(hooks_dir.join("pre-commit.hooks"), "echo ok\n").unwrap();

    // Create an active shim for pre-commit so `hooks run` finds it.
    Command::cargo_bin("git-std")
        .unwrap()
        .args(["init"])
        .env("GIT_STD_HOOKS_ENABLE", "pre-commit")
        .current_dir(dir.path())
        .assert()
        .success();

    let subdir = dir.path().join("src").join("nested");
    std::fs::create_dir_all(&subdir).unwrap();

    let assert = Command::cargo_bin("git-std")
        .unwrap()
        .args(["--color", "never", "hook", "run", "pre-commit"])
        .current_dir(&subdir)
        .assert()
        .success();

    let stdout = String::from_utf8_lossy(&assert.get_output().stdout);
    let stderr = String::from_utf8_lossy(&assert.get_output().stderr);
    let combined = format!("{stdout}{stderr}");
    assert!(
        combined.contains('\u{2713}'),
        "hooks run from subdirectory should succeed and show check mark, got:\n{combined}"
    );
}