git-std 0.11.6

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
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
use std::path::Path;

use assert_cmd::Command;
use predicates::prelude::*;

/// 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: check if a tag exists.
fn tag_exists(dir: &Path, tag: &str) -> bool {
    std::process::Command::new("git")
        .current_dir(dir)
        .args(["rev-parse", "--verify", &format!("refs/tags/{tag}")])
        .output()
        .unwrap()
        .status
        .success()
}

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

/// Helper: collect all tag names.
fn collect_tag_names(dir: &Path) -> Vec<String> {
    let output = git(dir, &["tag", "-l"]);
    if output.is_empty() {
        vec![]
    } else {
        output.lines().map(|s| s.to_string()).collect()
    }
}

/// Helper: initialise a git repo with a Cargo.toml and one commit.
fn init_bump_repo(dir: &Path) {
    git(dir, &["init"]);
    git(dir, &["config", "user.name", "Test"]);
    git(dir, &["config", "user.email", "test@test.com"]);

    // Write a minimal Cargo.toml.
    std::fs::write(
        dir.join("Cargo.toml"),
        "[package]\nname = \"test-pkg\"\nversion = \"0.0.0\"\nedition = \"2021\"\n",
    )
    .unwrap();

    git(dir, &["add", "Cargo.toml"]);
    git(dir, &["commit", "-m", "chore: init"]);
}

/// Helper: add a commit to a repo.
fn add_commit(dir: &Path, filename: &str, message: &str) {
    std::fs::write(dir.join(filename), message).unwrap();
    git(dir, &["add", filename]);
    git(dir, &["commit", "-m", message]);
}

/// Helper: create an annotated tag.
fn create_tag(dir: &Path, name: &str) {
    git(dir, &["tag", "-a", name, "-m", name]);
}

#[test]
fn bump_help_shows_flags() {
    let assert = Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump", "--help"])
        .assert()
        .success();

    let stdout = String::from_utf8_lossy(&assert.get_output().stdout);
    for flag in [
        "--dry-run",
        "--prerelease",
        "--release-as",
        "--first-release",
        "--no-tag",
        "--no-commit",
        "--skip-changelog",
        "--sign",
        "--force",
        "--stable",
        "--minor",
        "--push",
    ] {
        assert!(stdout.contains(flag), "bump help should list '{flag}' flag");
    }
}

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

    // Tag v0.1.0 on the init commit.
    create_tag(dir.path(), "v0.1.0");

    // Add a feat commit.
    add_commit(dir.path(), "a.txt", "feat: add feature A");

    // Pre-1.0: feat (Minor) downshifts to Patch → 0.1.0 → 0.1.1.
    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump", "--dry-run"])
        .current_dir(dir.path())
        .assert()
        .success()
        .stderr(predicate::str::contains("0.1.0 → 0.1.1"))
        .stderr(predicate::str::contains("Would commit"))
        .stderr(predicate::str::contains("Would tag"));
}

#[test]
fn bump_dry_run_no_writes() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v0.1.0");
    add_commit(dir.path(), "a.txt", "feat: add feature A");

    // Read Cargo.toml before.
    let before = std::fs::read_to_string(dir.path().join("Cargo.toml")).unwrap();

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump", "--dry-run"])
        .current_dir(dir.path())
        .assert()
        .success();

    // Cargo.toml should be unchanged.
    let after = std::fs::read_to_string(dir.path().join("Cargo.toml")).unwrap();
    assert_eq!(before, after);

    // No CHANGELOG.md should be created.
    assert!(!dir.path().join("CHANGELOG.md").exists());
}

#[test]
fn bump_performs_full_workflow() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v1.0.0");

    // Add a fix commit.
    add_commit(dir.path(), "b.txt", "fix: handle edge case");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump"])
        .current_dir(dir.path())
        .assert()
        .success()
        .stderr(predicate::str::contains("1.0.0 → 1.0.1"))
        .stderr(predicate::str::contains("Committed"))
        .stderr(predicate::str::contains("Tagged"));

    // Verify Cargo.toml was updated.
    let cargo = std::fs::read_to_string(dir.path().join("Cargo.toml")).unwrap();
    assert!(cargo.contains("version = \"1.0.1\""));

    // Verify CHANGELOG.md was created.
    assert!(dir.path().join("CHANGELOG.md").exists());

    // Verify the tag exists.
    assert!(tag_exists(dir.path(), "v1.0.1"), "tag v1.0.1 should exist");

    // Verify commit message.
    assert_eq!(head_message(dir.path()), "chore(release): 1.0.1");
}

#[test]
fn bump_no_bump_worthy_commits() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v1.0.0");

    // Add a non-bump commit.
    add_commit(dir.path(), "c.txt", "chore: update deps");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump"])
        .current_dir(dir.path())
        .assert()
        .success()
        .stderr(predicate::str::contains("no bump-worthy commits"));
}

#[test]
fn bump_major_on_breaking_change() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v1.2.3");

    add_commit(dir.path(), "d.txt", "feat!: remove old API");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump"])
        .current_dir(dir.path())
        .assert()
        .success()
        .stderr(predicate::str::contains("1.2.3 → 2.0.0"));
}

#[test]
fn bump_no_commit_flag() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v1.0.0");
    add_commit(dir.path(), "e.txt", "feat: new thing");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump", "--no-commit"])
        .current_dir(dir.path())
        .assert()
        .success();

    // Cargo.toml should be updated.
    let cargo = std::fs::read_to_string(dir.path().join("Cargo.toml")).unwrap();
    assert!(cargo.contains("version = \"1.1.0\""));

    // No release commit should exist.
    assert_ne!(head_message(dir.path()), "chore(release): 1.1.0");

    // No tag should exist.
    assert!(!tag_exists(dir.path(), "v1.1.0"));
}

#[test]
fn bump_skip_changelog() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v1.0.0");
    add_commit(dir.path(), "f.txt", "fix: a fix");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump", "--skip-changelog"])
        .current_dir(dir.path())
        .assert()
        .success();

    // No CHANGELOG.md should be created.
    assert!(!dir.path().join("CHANGELOG.md").exists());
}

#[test]
fn bump_release_as() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v1.0.0");
    add_commit(dir.path(), "g.txt", "fix: a fix");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump", "--release-as", "5.0.0"])
        .current_dir(dir.path())
        .assert()
        .success()
        .stderr(predicate::str::contains("1.0.0 → 5.0.0"));

    let cargo = std::fs::read_to_string(dir.path().join("Cargo.toml")).unwrap();
    assert!(cargo.contains("version = \"5.0.0\""));
}

#[test]
fn bump_first_release() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    // No tags — first release. Add a feat commit so the changelog has content.
    add_commit(dir.path(), "init.txt", "feat: initial feature");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump", "--first-release"])
        .current_dir(dir.path())
        .assert()
        .success();

    // CHANGELOG.md should be created.
    assert!(dir.path().join("CHANGELOG.md").exists());

    // Version should stay at 0.0.0 (first-release doesn't bump).
    let cargo = std::fs::read_to_string(dir.path().join("Cargo.toml")).unwrap();
    assert!(cargo.contains("version = \"0.0.0\""));
}

/// Assert that a version string matches calver format: YYYY.MM.PATCH
/// where YYYY is a 4-digit year, MM is a 1-2 digit month (1-12),
/// and PATCH is a non-negative integer.
fn assert_calver_format(ver: &str) {
    let parts: Vec<&str> = ver.split('.').collect();
    assert_eq!(
        parts.len(),
        3,
        "calver should have 3 dot-separated parts, got: {ver}"
    );

    // Year: exactly 4 digits, >= 2020
    let year: u32 = parts[0]
        .parse()
        .unwrap_or_else(|_| panic!("year should be numeric, got: {}", parts[0]));
    assert!(
        (2020..=2100).contains(&year),
        "year should be a plausible 4-digit year, got: {year}"
    );

    // Month: 1-2 digits, 1-12
    let month: u32 = parts[1]
        .parse()
        .unwrap_or_else(|_| panic!("month should be numeric, got: {}", parts[1]));
    assert!(
        (1..=12).contains(&month),
        "month should be 1-12, got: {month}"
    );

    // Patch: non-negative integer
    let _patch: u32 = parts[2]
        .parse()
        .unwrap_or_else(|_| panic!("patch should be numeric, got: {}", parts[2]));
}

/// Full calver release cycle: first release, then a second bump in the same month.
#[test]
fn bump_full_release_cycle() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());

    // Write a .git-std.toml with calver scheme.
    std::fs::write(dir.path().join(".git-std.toml"), "scheme = \"calver\"\n").unwrap();

    // Add a feat commit.
    add_commit(dir.path(), "a.txt", "feat: first feature");

    // First release.
    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump"])
        .current_dir(dir.path())
        .assert()
        .success()
        .stderr(predicate::str::contains("calver"))
        .stderr(predicate::str::contains("Committed"))
        .stderr(predicate::str::contains("Tagged"));

    // Verify a tag was created.
    let tags = collect_tag_names(dir.path());
    assert!(!tags.is_empty(), "at least one tag should exist after bump");

    // Verify the first tag matches calver format: vYYYY.MM.PATCH
    let tag_name = &tags[0];
    assert!(
        tag_name.starts_with('v'),
        "tag should start with 'v', got: {tag_name}"
    );
    let ver = tag_name.strip_prefix('v').unwrap();
    assert_calver_format(ver);

    // The first release should have patch == 0.
    let parts: Vec<&str> = ver.split('.').collect();
    assert_eq!(
        parts[2], "0",
        "first calver release should have patch 0, got: {}",
        parts[2]
    );

    // Second bump: add another commit, should increment patch.
    add_commit(dir.path(), "b.txt", "fix: a bugfix");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump"])
        .current_dir(dir.path())
        .assert()
        .success()
        .stderr(predicate::str::contains("calver"));

    // Should now have two tags.
    let tags2 = collect_tag_names(dir.path());
    assert!(
        tags2.len() >= 2,
        "should have at least 2 tags after second bump, got: {}",
        tags2.len()
    );

    // Verify both tags match calver format.
    for t in &tags2 {
        let v = t.strip_prefix('v').unwrap_or(t);
        assert_calver_format(v);
    }

    // The second tag should share the same YYYY.MM prefix and have patch == 1.
    let first_ver = tags2[0].strip_prefix('v').unwrap();
    let second_ver = tags2[1].strip_prefix('v').unwrap();
    let first_parts: Vec<&str> = first_ver.split('.').collect();
    let second_parts: Vec<&str> = second_ver.split('.').collect();
    assert_eq!(
        first_parts[0], second_parts[0],
        "year should match between bumps: {} vs {}",
        first_parts[0], second_parts[0]
    );
    assert_eq!(
        first_parts[1], second_parts[1],
        "month should match between bumps: {} vs {}",
        first_parts[1], second_parts[1]
    );
    let patch: u32 = second_parts[2].parse().expect("patch should be numeric");
    assert_eq!(
        patch, 1,
        "second calver bump should have patch 1, got: {patch}"
    );
}

/// Pre-release cycle: first bump with --prerelease creates -rc.0 style tag,
/// second bump increments it.
#[test]
fn bump_prerelease_cycle() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v1.0.0");

    // Add a feat commit.
    add_commit(dir.path(), "a.txt", "feat: new feature");

    // First pre-release bump.
    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump", "--prerelease"])
        .current_dir(dir.path())
        .assert()
        .success()
        .stderr(predicate::str::contains("1.0.0 → 1.1.0-rc.0"));

    // Verify tag.
    assert!(
        tag_exists(dir.path(), "v1.1.0-rc.0"),
        "tag v1.1.0-rc.0 should exist"
    );

    // Second pre-release bump.
    add_commit(dir.path(), "b.txt", "fix: a fix");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump", "--prerelease"])
        .current_dir(dir.path())
        .assert()
        .success()
        .stderr(predicate::str::contains("1.1.0-rc.0 → 1.1.0-rc.1"));

    // Verify second tag.
    assert!(
        tag_exists(dir.path(), "v1.1.0-rc.1"),
        "tag v1.1.0-rc.1 should exist"
    );
}

/// A lock file for a missing tool emits a warning but does not fail the bump.
#[test]
fn bump_missing_tool_lock_file_warns_and_continues() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v1.0.0");

    // Write a pyproject.toml so the trigger for uv.lock is satisfied.
    std::fs::write(
        dir.path().join("pyproject.toml"),
        "[project]\nname = \"test\"\nversion = \"1.0.0\"\n",
    )
    .unwrap();
    std::fs::write(
        dir.path().join(".git-std.toml"),
        "[[version_files]]\npath = \"pyproject.toml\"\nregex = 'version = \"([^\"]+)\"'\n",
    )
    .unwrap();
    git(dir.path(), &["add", "."]);
    git(dir.path(), &["commit", "-m", "chore: add pyproject"]);

    add_commit(dir.path(), "a.txt", "fix: small fix");

    // Write a uv.lock — override PATH to only include git's directory so `uv`
    // cannot be found regardless of the host environment, isolating the
    // "tool not on PATH" warning path.
    std::fs::write(dir.path().join("uv.lock"), "# placeholder\n").unwrap();

    // Build a minimal PATH that contains git (needed for bump internals)
    // but excludes ecosystem tools like uv, npm, etc.
    let git_dir = std::process::Command::new("which")
        .arg("git")
        .output()
        .map(|o| {
            let p = String::from_utf8_lossy(&o.stdout).trim().to_string();
            std::path::PathBuf::from(p)
                .parent()
                .unwrap()
                .to_string_lossy()
                .to_string()
        })
        .unwrap();

    // Bump must still succeed.
    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump"])
        .current_dir(dir.path())
        .env("PATH", &git_dir)
        .assert()
        .success();

    // Cargo.toml was updated — version bump happened.
    let cargo = std::fs::read_to_string(dir.path().join("Cargo.toml")).unwrap();
    assert!(cargo.contains("version = \"1.0.1\""));
}

/// dry-run with a lock file present mentions "Would sync".
#[test]
fn bump_dry_run_shows_would_sync() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v1.0.0");

    // Write a pyproject.toml so the trigger for uv.lock is satisfied.
    std::fs::write(
        dir.path().join("pyproject.toml"),
        "[project]\nname = \"test\"\nversion = \"1.0.0\"\n",
    )
    .unwrap();
    std::fs::write(
        dir.path().join(".git-std.toml"),
        "[[version_files]]\npath = \"pyproject.toml\"\nregex = 'version = \"([^\"]+)\"'\n",
    )
    .unwrap();
    git(dir.path(), &["add", "."]);
    git(dir.path(), &["commit", "-m", "chore: add pyproject"]);

    add_commit(dir.path(), "a.txt", "feat: new feature");

    // Write a uv.lock so the dry-run path has something to report.
    std::fs::write(dir.path().join("uv.lock"), "# placeholder\n").unwrap();

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump", "--dry-run"])
        .current_dir(dir.path())
        .assert()
        .success()
        .stderr(predicate::str::contains("Would sync"))
        .stderr(predicate::str::contains("uv.lock"));
}

/// dry-run with a lock file present does NOT mention "Would sync" when the
/// trigger version file is not in the version_files list.
#[test]
fn bump_dry_run_skips_untriggered_lock_file() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v1.0.0");
    add_commit(dir.path(), "a.txt", "feat: new feature");

    // Write a uv.lock on disk but NO pyproject.toml in version_files.
    std::fs::write(dir.path().join("uv.lock"), "# placeholder\n").unwrap();

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump", "--dry-run"])
        .current_dir(dir.path())
        .assert()
        .success()
        .stderr(predicate::str::contains("Would sync:   uv.lock").not());
}

// ── Pre-1.0 semver convention ──────────────────────────────────

/// Pre-1.0: a breaking change bumps minor, not major.
#[test]
fn bump_pre1_breaking_bumps_minor() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v0.10.2");

    add_commit(dir.path(), "brk.txt", "feat!: remove old API");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump"])
        .current_dir(dir.path())
        .assert()
        .success()
        .stderr(predicate::str::contains("0.10.2 → 0.11.0"));

    let cargo = std::fs::read_to_string(dir.path().join("Cargo.toml")).unwrap();
    assert!(cargo.contains("version = \"0.11.0\""));
    assert!(tag_exists(dir.path(), "v0.11.0"));
}

/// Pre-1.0: a feat bumps patch, not minor.
#[test]
fn bump_pre1_feat_bumps_patch() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v0.10.2");

    add_commit(dir.path(), "ft.txt", "feat: add feature");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump"])
        .current_dir(dir.path())
        .assert()
        .success()
        .stderr(predicate::str::contains("0.10.2 → 0.10.3"));

    let cargo = std::fs::read_to_string(dir.path().join("Cargo.toml")).unwrap();
    assert!(cargo.contains("version = \"0.10.3\""));
}

/// Pre-1.0: a fix bumps patch.
#[test]
fn bump_pre1_fix_bumps_patch() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v0.10.2");

    add_commit(dir.path(), "fx.txt", "fix: handle edge case");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump"])
        .current_dir(dir.path())
        .assert()
        .success()
        .stderr(predicate::str::contains("0.10.2 → 0.10.3"));
}

/// Pre-1.0: --release-as still overrides computed version.
#[test]
fn bump_pre1_release_as_overrides() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v0.10.2");

    add_commit(dir.path(), "ra.txt", "feat: something");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump", "--release-as", "1.0.0"])
        .current_dir(dir.path())
        .assert()
        .success()
        .stderr(predicate::str::contains("0.10.2 → 1.0.0"));

    let cargo = std::fs::read_to_string(dir.path().join("Cargo.toml")).unwrap();
    assert!(cargo.contains("version = \"1.0.0\""));
    assert!(tag_exists(dir.path(), "v1.0.0"));
}

/// Pre-1.0: --dry-run shows the downshifted bump plan.
#[test]
fn bump_pre1_dry_run_shows_correct_plan() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v0.10.2");

    add_commit(dir.path(), "dr.txt", "feat!: breaking API change");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump", "--dry-run"])
        .current_dir(dir.path())
        .assert()
        .success()
        .stderr(predicate::str::contains("0.10.2 → 0.11.0"))
        .stderr(predicate::str::contains("Would commit"))
        .stderr(predicate::str::contains("Would tag"));
}

/// Post-1.0: breaking change still bumps major (behaviour unchanged).
#[test]
fn bump_post1_breaking_bumps_major() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v1.2.3");

    add_commit(dir.path(), "brk2.txt", "feat!: remove old API");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump"])
        .current_dir(dir.path())
        .assert()
        .success()
        .stderr(predicate::str::contains("1.2.3 → 2.0.0"));
}

// ── Bump lifecycle hooks ───────────────────────────────────────

/// Helper: write a `.githooks/<name>.hooks` file with the given content.
fn write_hooks_file(dir: &std::path::Path, hook_name: &str, content: &str) {
    let hooks_dir = dir.join(".githooks");
    std::fs::create_dir_all(&hooks_dir).unwrap();
    std::fs::write(hooks_dir.join(format!("{hook_name}.hooks")), content).unwrap();
}

/// `pre-bump` with a required command that exits 0 — bump proceeds normally.
#[test]
fn bump_lifecycle_pre_bump_passes() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v1.0.0");
    add_commit(dir.path(), "a.txt", "fix: a fix");

    write_hooks_file(dir.path(), "pre-bump", "! true\n");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump"])
        .current_dir(dir.path())
        .assert()
        .success()
        .stderr(predicate::str::contains("1.0.0 → 1.0.1"));
}

/// `pre-bump` with a required command that exits non-zero — bump is aborted.
#[test]
fn bump_lifecycle_pre_bump_aborts_on_failure() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v1.0.0");
    add_commit(dir.path(), "a.txt", "fix: a fix");

    write_hooks_file(dir.path(), "pre-bump", "! false\n");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump"])
        .current_dir(dir.path())
        .assert()
        .failure();

    // Cargo.toml must not have been updated.
    let cargo = std::fs::read_to_string(dir.path().join("Cargo.toml")).unwrap();
    assert!(
        !cargo.contains("version = \"1.0.1\""),
        "Cargo.toml must not be updated when pre-bump fails"
    );
    // No tag must exist.
    assert!(!tag_exists(dir.path(), "v1.0.1"), "tag must not exist");
}

/// `pre-bump` is skipped when `--dry-run` is used.
#[test]
fn bump_lifecycle_pre_bump_skipped_on_dry_run() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v1.0.0");
    add_commit(dir.path(), "a.txt", "fix: a fix");

    // A failing pre-bump hook must not abort dry-run.
    write_hooks_file(dir.path(), "pre-bump", "! false\n");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump", "--dry-run"])
        .current_dir(dir.path())
        .assert()
        .success();
}

/// `post-version` receives the new version as its first argument.
#[test]
fn bump_lifecycle_post_version_receives_version() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v1.0.0");
    add_commit(dir.path(), "a.txt", "fix: a fix");

    // Write the first positional arg ($1) to a file inside the tempdir.
    let sentinel = dir.path().join("post-version-arg.txt");
    let sentinel_str = sentinel.to_string_lossy();
    write_hooks_file(
        dir.path(),
        "post-version",
        &format!("! echo $1 > {sentinel_str}\n"),
    );

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump"])
        .current_dir(dir.path())
        .assert()
        .success();

    let written = std::fs::read_to_string(&sentinel)
        .unwrap_or_default()
        .trim()
        .to_string();
    assert_eq!(written, "1.0.1", "post-version should receive '1.0.1'");
}

/// `post-version` aborts bump when it exits non-zero.
#[test]
fn bump_lifecycle_post_version_aborts_on_failure() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v1.0.0");
    add_commit(dir.path(), "a.txt", "fix: a fix");

    write_hooks_file(dir.path(), "post-version", "! false\n");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump"])
        .current_dir(dir.path())
        .assert()
        .failure();

    // No tag should have been created.
    assert!(!tag_exists(dir.path(), "v1.0.1"), "tag must not exist");
}

/// `post-changelog` runs after the changelog is written.
#[test]
fn bump_lifecycle_post_changelog_runs() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v1.0.0");
    add_commit(dir.path(), "a.txt", "fix: a fix");

    // Touch a sentinel file inside the tempdir so we know the hook ran.
    let sentinel = dir.path().join("post-changelog-ran.txt");
    let sentinel_str = sentinel.to_string_lossy();
    write_hooks_file(
        dir.path(),
        "post-changelog",
        &format!("! touch {sentinel_str}\n"),
    );

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump"])
        .current_dir(dir.path())
        .assert()
        .success();

    assert!(
        sentinel.exists(),
        "post-changelog sentinel file must exist after bump"
    );
}

/// `post-changelog` aborts bump when it exits non-zero.
#[test]
fn bump_lifecycle_post_changelog_aborts_on_failure() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v1.0.0");
    add_commit(dir.path(), "a.txt", "fix: a fix");

    write_hooks_file(dir.path(), "post-changelog", "! false\n");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump"])
        .current_dir(dir.path())
        .assert()
        .failure();

    // No release commit should have been created.
    assert_ne!(
        head_message(dir.path()),
        "chore(release): 1.0.1",
        "release commit must not exist"
    );
}

/// `post-changelog` is skipped when `--skip-changelog` is used.
#[test]
fn bump_lifecycle_post_changelog_skipped_when_skip_changelog() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v1.0.0");
    add_commit(dir.path(), "a.txt", "fix: a fix");

    // Failing post-changelog hook must not abort bump when --skip-changelog is passed.
    write_hooks_file(dir.path(), "post-changelog", "! false\n");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump", "--skip-changelog"])
        .current_dir(dir.path())
        .assert()
        .success();
}

/// `post-bump` runs after the release tag is created.
#[test]
fn bump_lifecycle_post_bump_runs_after_tag() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v1.0.0");
    add_commit(dir.path(), "a.txt", "fix: a fix");

    // Touch a sentinel file inside the tempdir.
    let sentinel = dir.path().join("post-bump-ran.txt");
    let sentinel_str = sentinel.to_string_lossy();
    write_hooks_file(
        dir.path(),
        "post-bump",
        &format!("! touch {sentinel_str}\n"),
    );

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump"])
        .current_dir(dir.path())
        .assert()
        .success();

    // Tag must already exist when post-bump runs (hook runs after tag).
    assert!(tag_exists(dir.path(), "v1.0.1"), "tag must exist");
    assert!(
        sentinel.exists(),
        "post-bump sentinel file must exist after bump"
    );
}

/// `post-bump` with advisory command (`?`) — failure is tolerated.
#[test]
fn bump_lifecycle_post_bump_advisory_tolerates_failure() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v1.0.0");
    add_commit(dir.path(), "a.txt", "fix: a fix");

    // Advisory command that always fails.
    write_hooks_file(dir.path(), "post-bump", "? false\n");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump"])
        .current_dir(dir.path())
        .assert()
        .success();

    // Tag should still have been created.
    assert!(tag_exists(dir.path(), "v1.0.1"), "tag must exist");
}

/// `post-bump` is skipped when `--no-commit` is used (no commit/tag created).
#[test]
fn bump_lifecycle_post_bump_skipped_on_no_commit() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v1.0.0");
    add_commit(dir.path(), "a.txt", "fix: a fix");

    // Failing post-bump hook must not abort bump when --no-commit is passed.
    write_hooks_file(dir.path(), "post-bump", "! false\n");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump", "--no-commit"])
        .current_dir(dir.path())
        .assert()
        .success();
}

/// `GIT_STD_SKIP_HOOKS=1` skips all lifecycle hooks.
#[test]
fn bump_lifecycle_skip_hooks_env_var() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v1.0.0");
    add_commit(dir.path(), "a.txt", "fix: a fix");

    // All hooks fail — GIT_STD_SKIP_HOOKS=1 must suppress them all.
    for hook in ["pre-bump", "post-version", "post-changelog", "post-bump"] {
        write_hooks_file(dir.path(), hook, "! false\n");
    }

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump"])
        .current_dir(dir.path())
        .env("GIT_STD_SKIP_HOOKS", "1")
        .assert()
        .success();
}

/// `--dry-run` skips all lifecycle hooks (`post-version`, `post-changelog`,
/// `post-bump`).
#[test]
fn bump_lifecycle_dry_run_skips_all_hooks() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v1.0.0");
    add_commit(dir.path(), "a.txt", "fix: a fix");

    // All four hooks fail if executed — dry-run must not run any of them.
    for hook in ["pre-bump", "post-version", "post-changelog", "post-bump"] {
        write_hooks_file(dir.path(), hook, "! false\n");
    }

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump", "--dry-run"])
        .current_dir(dir.path())
        .assert()
        .success();
}

/// Helper: initialise a bare repo and add it as a remote to `local_dir`.
fn init_remote(local_dir: &Path, remote_dir: &Path, remote_name: &str) {
    // Init bare remote.
    std::process::Command::new("git")
        .args(["init", "--bare"])
        .arg(remote_dir)
        .output()
        .unwrap();
    // Add as a named remote.
    git(
        local_dir,
        &[
            "remote",
            "add",
            remote_name,
            &remote_dir.display().to_string(),
        ],
    );
}

/// Helper: check whether a tag exists in a bare remote repo.
fn remote_tag_exists(remote_dir: &Path, tag: &str) -> bool {
    std::process::Command::new("git")
        .current_dir(remote_dir)
        .args(["rev-parse", "--verify", &format!("refs/tags/{tag}")])
        .output()
        .unwrap()
        .status
        .success()
}

// ── --push flag ────────────────────────────────────────────────

/// --push pushes the commit and tag to the default remote (origin).
#[test]
fn bump_push_default_remote() {
    let local = tempfile::tempdir().unwrap();
    let remote = tempfile::tempdir().unwrap();
    init_bump_repo(local.path());
    create_tag(local.path(), "v1.0.0");
    add_commit(local.path(), "a.txt", "fix: a fix");
    init_remote(local.path(), remote.path(), "origin");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump", "--push"])
        .current_dir(local.path())
        .assert()
        .success()
        .stderr(predicate::str::contains("Pushed to origin"));

    // Tag should be present in the remote.
    assert!(
        remote_tag_exists(remote.path(), "v1.0.1"),
        "tag v1.0.1 should be pushed to remote"
    );
}

/// --push <remote> pushes to the specified remote.
#[test]
fn bump_push_named_remote() {
    let local = tempfile::tempdir().unwrap();
    let remote = tempfile::tempdir().unwrap();
    init_bump_repo(local.path());
    create_tag(local.path(), "v1.0.0");
    add_commit(local.path(), "a.txt", "fix: a fix");
    init_remote(local.path(), remote.path(), "upstream");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump", "--push", "upstream"])
        .current_dir(local.path())
        .assert()
        .success()
        .stderr(predicate::str::contains("Pushed to upstream"));

    assert!(
        remote_tag_exists(remote.path(), "v1.0.1"),
        "tag v1.0.1 should be pushed to upstream remote"
    );
}

/// --dry-run --push reports "Would push to <remote>" without actually pushing.
#[test]
fn bump_dry_run_push_reports_would_push() {
    let local = tempfile::tempdir().unwrap();
    let remote = tempfile::tempdir().unwrap();
    init_bump_repo(local.path());
    create_tag(local.path(), "v1.0.0");
    add_commit(local.path(), "a.txt", "fix: a fix");
    init_remote(local.path(), remote.path(), "origin");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump", "--dry-run", "--push"])
        .current_dir(local.path())
        .assert()
        .success()
        .stderr(predicate::str::contains("Would push to origin"));

    // Nothing pushed to remote.
    assert!(
        !remote_tag_exists(remote.path(), "v1.0.1"),
        "dry-run should not push anything"
    );
}

/// --dry-run --push <remote> reports "Would push to <remote>".
#[test]
fn bump_dry_run_push_named_remote() {
    let local = tempfile::tempdir().unwrap();
    let remote = tempfile::tempdir().unwrap();
    init_bump_repo(local.path());
    create_tag(local.path(), "v1.0.0");
    add_commit(local.path(), "a.txt", "fix: a fix");
    init_remote(local.path(), remote.path(), "upstream");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump", "--dry-run", "--push", "upstream"])
        .current_dir(local.path())
        .assert()
        .success()
        .stderr(predicate::str::contains("Would push to upstream"));
}

/// --push to a non-existent remote fails with a non-zero exit code.
#[test]
fn bump_push_failure_exits_nonzero() {
    let local = tempfile::tempdir().unwrap();
    init_bump_repo(local.path());
    create_tag(local.path(), "v1.0.0");
    add_commit(local.path(), "a.txt", "fix: a fix");
    // No remote configured — push should fail.

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump", "--push", "nonexistent-remote"])
        .current_dir(local.path())
        .assert()
        .failure();
}

/// Cargo.lock is synced via custom [[version_files]] when root Cargo.toml is a
/// workspace manifest with no [package] section (the ecosystem native_write
/// returns NotDetected, so sync must fire from the custom-files path).
#[test]
fn bump_cargo_lock_sync_via_custom_version_files() {
    let dir = tempfile::tempdir().unwrap();

    // Workspace-style repo: root Cargo.toml has no [package], only [workspace].
    let workspace_toml = r#"[workspace]
members = ["app"]
resolver = "2"
"#;
    std::fs::write(dir.path().join("Cargo.toml"), workspace_toml).unwrap();

    // Member crate.
    std::fs::create_dir_all(dir.path().join("app/src")).unwrap();
    std::fs::write(dir.path().join("app/src/main.rs"), "fn main() {}").unwrap();
    std::fs::write(
        dir.path().join("app/Cargo.toml"),
        "[package]\nname = \"app\"\nversion = \"1.0.0\"\nedition = \"2021\"\n",
    )
    .unwrap();

    // .git-std.toml with custom [[version_files]] pointing at the member.
    std::fs::write(
        dir.path().join(".git-std.toml"),
        "[[version_files]]\npath = \"app/Cargo.toml\"\nregex = 'version = \"([^\"]+)\"'\n",
    )
    .unwrap();

    // Init git repo and generate Cargo.lock.
    git(dir.path(), &["init"]);
    git(dir.path(), &["config", "user.email", "test@example.com"]);
    git(dir.path(), &["config", "user.name", "Test"]);
    std::process::Command::new("cargo")
        .args(["generate-lockfile"])
        .current_dir(dir.path())
        .status()
        .expect("cargo must be available");
    git(dir.path(), &["add", "."]);
    git(dir.path(), &["commit", "-m", "chore: initial"]);
    create_tag(dir.path(), "v1.0.0");
    add_commit(dir.path(), "x.txt", "fix: a bug");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump"])
        .current_dir(dir.path())
        .assert()
        .success()
        .stderr(predicate::str::contains("Synced:"));

    // Cargo.lock must be in the bump commit.
    let files_in_commit = git(
        dir.path(),
        &["diff-tree", "--no-commit-id", "--name-only", "-r", "HEAD"],
    );
    assert!(
        files_in_commit.contains("Cargo.lock"),
        "Cargo.lock should be staged in the bump commit, got: {files_in_commit}"
    );
}

/// Cargo.lock is synced when `cargo` is available and Cargo.toml was updated.
#[test]
fn bump_cargo_lock_sync_happy_path() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v1.0.0");

    // Generate a real Cargo.lock so the lock sync has a file to update.
    std::fs::create_dir_all(dir.path().join("src")).unwrap();
    std::fs::write(dir.path().join("src/lib.rs"), "").unwrap();
    std::process::Command::new("cargo")
        .args(["generate-lockfile"])
        .current_dir(dir.path())
        .status()
        .expect("cargo must be available");
    git(dir.path(), &["add", "."]);
    git(dir.path(), &["commit", "-m", "chore: add lock"]);

    add_commit(dir.path(), "a.txt", "fix: a bug");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump"])
        .current_dir(dir.path())
        .assert()
        .success()
        .stderr(predicate::str::contains("Synced:"));

    // Verify the lock file was staged as part of the bump commit.
    let files_in_commit = git(
        dir.path(),
        &["diff-tree", "--no-commit-id", "--name-only", "-r", "HEAD"],
    );
    assert!(
        files_in_commit.contains("Cargo.lock"),
        "Cargo.lock should be staged in the bump commit, got: {files_in_commit}"
    );
}

/// Workspace where members inherit the version via `version.workspace = true`.
/// Only the root `[workspace.package]` version should be updated; member
/// Cargo.toml files must not be modified.
#[test]
fn bump_workspace_package_inheritance() {
    let dir = tempfile::tempdir().unwrap();

    // Root manifest: declares the version in [workspace.package].
    let workspace_toml = r#"[workspace]
members = ["app"]
resolver = "2"

[workspace.package]
version = "1.0.0"
edition = "2021"
"#;
    std::fs::write(dir.path().join("Cargo.toml"), workspace_toml).unwrap();

    // Member crate: inherits version from workspace.
    std::fs::create_dir_all(dir.path().join("app/src")).unwrap();
    std::fs::write(dir.path().join("app/src/main.rs"), "fn main() {}").unwrap();
    std::fs::write(
        dir.path().join("app/Cargo.toml"),
        "[package]\nname = \"app\"\nversion.workspace = true\nedition.workspace = true\n",
    )
    .unwrap();

    git(dir.path(), &["init"]);
    git(dir.path(), &["config", "user.email", "test@example.com"]);
    git(dir.path(), &["config", "user.name", "Test"]);
    std::process::Command::new("cargo")
        .args(["generate-lockfile"])
        .current_dir(dir.path())
        .status()
        .expect("cargo must be available");
    git(dir.path(), &["add", "."]);
    git(dir.path(), &["commit", "-m", "chore: initial"]);
    create_tag(dir.path(), "v1.0.0");
    add_commit(dir.path(), "x.txt", "fix: a bug");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump"])
        .current_dir(dir.path())
        .assert()
        .success();

    // Root manifest must have the new version.
    let root_content = std::fs::read_to_string(dir.path().join("Cargo.toml")).unwrap();
    assert!(
        root_content.contains("version = \"1.0.1\""),
        "root [workspace.package] version should be updated"
    );

    // Member manifest must still use workspace inheritance — not a pinned version.
    let member_content = std::fs::read_to_string(dir.path().join("app/Cargo.toml")).unwrap();
    assert!(
        member_content.contains("version.workspace = true"),
        "member must not have its workspace inherit rewritten"
    );
    assert!(
        !member_content.contains("version = \""),
        "member must not gain a pinned version field"
    );
}

/// Workspace where member crates carry their own pinned versions (not
/// inheriting from `[workspace.package]`). Each member must be updated by the
/// native walker when `cargo set-version` is unavailable.
#[test]
fn bump_workspace_pinned_members() {
    let dir = tempfile::tempdir().unwrap();

    // Root manifest: no [workspace.package] version.
    let workspace_toml = "[workspace]\nmembers = [\"alpha\", \"beta\"]\nresolver = \"2\"\n";
    std::fs::write(dir.path().join("Cargo.toml"), workspace_toml).unwrap();

    for name in ["alpha", "beta"] {
        std::fs::create_dir_all(dir.path().join(name).join("src")).unwrap();
        std::fs::write(dir.path().join(name).join("src/lib.rs"), "").unwrap();
        std::fs::write(
            dir.path().join(name).join("Cargo.toml"),
            format!("[package]\nname = \"{name}\"\nversion = \"2.0.0\"\nedition = \"2021\"\n"),
        )
        .unwrap();
    }

    git(dir.path(), &["init"]);
    git(dir.path(), &["config", "user.email", "test@example.com"]);
    git(dir.path(), &["config", "user.name", "Test"]);
    std::process::Command::new("cargo")
        .args(["generate-lockfile"])
        .current_dir(dir.path())
        .status()
        .expect("cargo must be available");
    git(dir.path(), &["add", "."]);
    git(dir.path(), &["commit", "-m", "chore: initial"]);
    create_tag(dir.path(), "v2.0.0");
    add_commit(dir.path(), "x.txt", "fix: patch");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump"])
        .current_dir(dir.path())
        .assert()
        .success()
        .stderr(predicate::str::contains("Synced:"));

    for name in ["alpha", "beta"] {
        let content = std::fs::read_to_string(dir.path().join(name).join("Cargo.toml")).unwrap();
        assert!(
            content.contains("version = \"2.0.1\""),
            "{name}/Cargo.toml should be updated to 2.0.1, got:\n{content}"
        );
    }

    // Both updated members must be in the bump commit.
    let files_in_commit = git(
        dir.path(),
        &["diff-tree", "--no-commit-id", "--name-only", "-r", "HEAD"],
    );
    assert!(
        files_in_commit.contains("alpha/Cargo.toml"),
        "alpha/Cargo.toml must be staged"
    );
    assert!(
        files_in_commit.contains("beta/Cargo.toml"),
        "beta/Cargo.toml must be staged"
    );
}

/// Workspace with [workspace.dependencies] local path deps — version
/// constraints in [workspace.dependencies] must be updated to match the bump.
#[test]
fn bump_workspace_deps_version_updated() {
    let dir = tempfile::tempdir().unwrap();

    // Root manifest with [workspace.package] version and two local path deps.
    let workspace_toml = r#"[workspace]
members = ["alpha", "beta"]
resolver = "2"

[workspace.package]
version = "1.0.0"
edition = "2021"

[workspace.dependencies]
alpha = { version = "1.0.0", path = "alpha" }
beta = { version = "1.0.0", path = "beta" }
serde = "1"
"#;
    std::fs::write(dir.path().join("Cargo.toml"), workspace_toml).unwrap();

    for name in ["alpha", "beta"] {
        std::fs::create_dir_all(dir.path().join(name).join("src")).unwrap();
        std::fs::write(dir.path().join(name).join("src/lib.rs"), "").unwrap();
        std::fs::write(
            dir.path().join(name).join("Cargo.toml"),
            format!(
                "[package]\nname = \"{name}\"\nversion.workspace = true\nedition.workspace = true\n"
            ),
        )
        .unwrap();
    }

    git(dir.path(), &["init"]);
    git(dir.path(), &["config", "user.email", "test@example.com"]);
    git(dir.path(), &["config", "user.name", "Test"]);
    std::process::Command::new("cargo")
        .args(["generate-lockfile"])
        .current_dir(dir.path())
        .status()
        .expect("cargo must be available");
    git(dir.path(), &["add", "."]);
    git(dir.path(), &["commit", "-m", "chore: initial"]);
    create_tag(dir.path(), "v1.0.0");
    add_commit(dir.path(), "x.txt", "fix: a bug");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump"])
        .current_dir(dir.path())
        .assert()
        .success();

    let root = std::fs::read_to_string(dir.path().join("Cargo.toml")).unwrap();

    // [workspace.package] version updated.
    assert!(
        root.contains("version = \"1.0.1\""),
        "[workspace.package] version should be 1.0.1"
    );

    // Local path dep constraints updated.
    assert!(
        root.contains("alpha = { version = \"1.0.1\", path = \"alpha\" }"),
        "alpha workspace dep version should be 1.0.1, got:\n{root}"
    );
    assert!(
        root.contains("beta = { version = \"1.0.1\", path = \"beta\" }"),
        "beta workspace dep version should be 1.0.1, got:\n{root}"
    );

    // Non-path dep must be untouched.
    assert!(
        root.contains("serde = \"1\""),
        "serde (non-path dep) must not be modified"
    );
}

// ── --release-as with level names ─────────────────────────────

/// `--release-as patch` forces a patch bump from the current version.
#[test]
fn release_as_patch_forces_patch_bump() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v1.2.3");

    // Add a feat commit (would normally be a minor bump).
    add_commit(dir.path(), "a.txt", "feat: new feature");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump", "--dry-run", "--release-as", "patch"])
        .current_dir(dir.path())
        .assert()
        .success()
        .stderr(predicate::str::contains("1.2.3 → 1.2.4"));
}

/// `--release-as minor` forces a minor bump from the current version.
#[test]
fn release_as_minor_forces_minor_bump() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v1.2.3");

    // Add a fix commit (would normally be a patch bump).
    add_commit(dir.path(), "b.txt", "fix: small fix");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump", "--dry-run", "--release-as", "minor"])
        .current_dir(dir.path())
        .assert()
        .success()
        .stderr(predicate::str::contains("1.2.3 → 1.3.0"));
}

/// `--release-as major` forces a major bump from the current version.
#[test]
fn release_as_major_forces_major_bump() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v1.2.3");

    // Add a fix commit (would normally be a patch bump).
    add_commit(dir.path(), "c.txt", "fix: a fix");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump", "--dry-run", "--release-as", "major"])
        .current_dir(dir.path())
        .assert()
        .success()
        .stderr(predicate::str::contains("1.2.3 → 2.0.0"));
}

/// `--dry-run` with `--release-as minor` shows version but makes no changes.
#[test]
fn release_as_level_respects_dry_run() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v1.0.0");

    add_commit(dir.path(), "d.txt", "fix: a fix");

    let before = std::fs::read_to_string(dir.path().join("Cargo.toml")).unwrap();

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump", "--dry-run", "--release-as", "minor"])
        .current_dir(dir.path())
        .assert()
        .success()
        .stderr(predicate::str::contains("1.0.0 → 1.1.0"))
        .stderr(predicate::str::contains("Would commit"))
        .stderr(predicate::str::contains("Would tag"));

    // No files changed.
    let after = std::fs::read_to_string(dir.path().join("Cargo.toml")).unwrap();
    assert_eq!(before, after);
    assert!(!dir.path().join("CHANGELOG.md").exists());
}

/// `--release-as minor` is rejected on the patch-only scheme.
#[test]
fn release_as_minor_rejected_on_patch_scheme() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v1.0.0");

    std::fs::write(dir.path().join(".git-std.toml"), "scheme = \"patch\"\n").unwrap();
    git(dir.path(), &["add", ".git-std.toml"]);
    git(dir.path(), &["commit", "-m", "chore: add config"]);

    add_commit(dir.path(), "e.txt", "fix: a fix");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump", "--dry-run", "--release-as", "minor"])
        .current_dir(dir.path())
        .assert()
        .failure()
        .stderr(predicate::str::contains(
            "patch-only scheme does not support --release-as minor or --release-as major",
        ));
}

/// `--release-as major` is rejected on the patch-only scheme.
#[test]
fn release_as_major_rejected_on_patch_scheme() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path());
    create_tag(dir.path(), "v1.0.0");

    std::fs::write(dir.path().join(".git-std.toml"), "scheme = \"patch\"\n").unwrap();
    git(dir.path(), &["add", ".git-std.toml"]);
    git(dir.path(), &["commit", "-m", "chore: add config"]);

    add_commit(dir.path(), "f.txt", "fix: a fix");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump", "--dry-run", "--release-as", "major"])
        .current_dir(dir.path())
        .assert()
        .failure()
        .stderr(predicate::str::contains(
            "patch-only scheme does not support --release-as minor or --release-as major",
        ));
}

// --- Branch guard tests ---

/// Helper: init a repo on a named branch (not main).
fn init_bump_repo_on_branch(dir: &std::path::Path, branch: &str) {
    git(dir, &["init", "--initial-branch", branch]);
    git(dir, &["config", "user.name", "Test"]);
    git(dir, &["config", "user.email", "test@test.com"]);
    std::fs::write(
        dir.join("Cargo.toml"),
        "[package]\nname = \"test-pkg\"\nversion = \"1.0.0\"\nedition = \"2021\"\n",
    )
    .unwrap();
    git(dir, &["add", "Cargo.toml"]);
    git(dir, &["commit", "-m", "chore: init"]);
}

#[test]
fn bump_on_non_main_branch_rejected_in_non_tty() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo_on_branch(dir.path(), "feat/my-feature");
    create_tag(dir.path(), "v1.0.0");
    add_commit(dir.path(), "a.txt", "feat: add feature");

    // Non-TTY stdin → error without prompt.
    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump", "--skip-changelog"])
        .current_dir(dir.path())
        .assert()
        .failure()
        .stderr(predicate::str::contains("feat/my-feature"))
        .stderr(predicate::str::contains("main"));
}

#[test]
fn bump_on_non_main_branch_bypassed_with_yes_flag() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo_on_branch(dir.path(), "feat/my-feature");
    create_tag(dir.path(), "v1.0.0");
    add_commit(dir.path(), "a.txt", "feat: add feature");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump", "--skip-changelog", "--yes"])
        .current_dir(dir.path())
        .assert()
        .success();
}

#[test]
fn bump_on_non_main_branch_bypassed_with_env_var() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo_on_branch(dir.path(), "feat/my-feature");
    create_tag(dir.path(), "v1.0.0");
    add_commit(dir.path(), "a.txt", "feat: add feature");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump", "--skip-changelog"])
        .env("GIT_STD_YES", "1")
        .current_dir(dir.path())
        .assert()
        .success();
}

#[test]
fn bump_dry_run_skips_branch_guard() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo_on_branch(dir.path(), "feat/my-feature");
    create_tag(dir.path(), "v1.0.0");
    add_commit(dir.path(), "a.txt", "feat: add feature");

    // --dry-run must NOT trigger the branch guard.
    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump", "--dry-run"])
        .current_dir(dir.path())
        .assert()
        .success();
}

#[test]
fn bump_on_main_branch_no_prompt() {
    let dir = tempfile::tempdir().unwrap();
    init_bump_repo(dir.path()); // uses main branch by default
    create_tag(dir.path(), "v1.0.0");

    // Patch Cargo.toml to match tag version.
    std::fs::write(
        dir.path().join("Cargo.toml"),
        "[package]\nname = \"test-pkg\"\nversion = \"1.0.0\"\nedition = \"2021\"\n",
    )
    .unwrap();
    git(dir.path(), &["add", "Cargo.toml"]);
    git(dir.path(), &["commit", "-m", "chore: set version"]);

    add_commit(dir.path(), "a.txt", "feat: add feature");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump", "--skip-changelog"])
        .current_dir(dir.path())
        .assert()
        .success();
}

#[test]
fn bump_release_branch_config_respected() {
    let dir = tempfile::tempdir().unwrap();
    // Use "release" as the branch name.
    init_bump_repo_on_branch(dir.path(), "release");
    create_tag(dir.path(), "v1.0.0");

    // Config declares "release" as the release branch.
    std::fs::write(
        dir.path().join(".git-std.toml"),
        "release_branch = \"release\"\n",
    )
    .unwrap();
    git(dir.path(), &["add", ".git-std.toml"]);
    git(dir.path(), &["commit", "-m", "chore: config"]);

    add_commit(dir.path(), "a.txt", "feat: add feature");

    Command::cargo_bin("git-std")
        .unwrap()
        .args(["bump", "--skip-changelog"])
        .current_dir(dir.path())
        .assert()
        .success();
}