git-loom 0.25.0

A Git CLI tool that weaves together multiple feature branches into integration branches
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
use crate::core::test_helpers::TestRepo;
use crate::core::ui;
use crate::core::weave::Weave;

// ── Helper: create a woven branch with commits ─────────────────────────

/// Set up a test repo with a woven feature-a branch containing the given
/// number of commits.
///
/// Creates a real merge topology (not fast-forward) by adding a commit on
/// integration before merging feature-a:
///
/// ```text
/// origin/main → Int (integration commit)
///             ↘                  ↘
///              A1 [→ A2] ──────→ merge (HEAD, integration)
///              (feature-a)
/// ```
fn setup_woven_branch(num_commits: usize) -> TestRepo {
    let test_repo = TestRepo::new_with_remote();
    let base_oid = test_repo.find_remote_branch_target("origin/main");

    test_repo.create_branch_at("feature-a", &base_oid.to_string());

    test_repo.switch_branch("feature-a");
    for i in 1..=num_commits {
        test_repo.commit(&format!("A{}", i), &format!("a{}.txt", i));
    }

    test_repo.switch_branch("integration");

    // Add a commit on integration BEFORE merging to prevent fast-forward
    test_repo.commit("Int", "int.txt");

    // Merge feature-a (creates a real merge commit since integration diverged)
    test_repo.merge_no_ff("feature-a");

    test_repo
}

// ── Drop commit tests ───────────────────────────────────────────────────

#[test]
fn drop_commit_removes_it_from_history() {
    let test_repo = TestRepo::new_with_remote();
    let _c1_oid = test_repo.commit("Keep", "keep.txt");
    let c2_oid = test_repo.commit("Drop me", "drop.txt");
    test_repo.commit("Keep2", "keep2.txt");

    let result = super::drop_commit(&test_repo.repo, &c2_oid.to_string(), true);
    assert!(result.is_ok(), "drop_commit failed: {:?}", result);

    assert_eq!(test_repo.get_message(0), "Keep2");
    assert_eq!(test_repo.get_message(1), "Keep");
}

#[test]
fn drop_commit_dirty_tree_autostashed() {
    let test_repo = TestRepo::new_with_remote();
    test_repo.commit("Base", "base.txt");
    let c1_oid = test_repo.commit("Commit", "file.txt");
    test_repo.write_file("base.txt", "dirty");

    let result = super::drop_commit(&test_repo.repo, &c1_oid.to_string(), true);
    assert!(
        result.is_ok(),
        "drop should succeed with autostash: {:?}",
        result
    );

    // Dirty changes should be preserved after autostash
    assert_eq!(test_repo.read_file("base.txt"), "dirty");
}

/// Dropping the only commit of a branch leaves the branch behind, empty, at
/// the base it built on. Removing it too is `loom drop feature-a`.
#[test]
fn drop_last_commit_on_branch_leaves_it_empty() {
    let test_repo = setup_woven_branch(1);
    let base_oid = test_repo.find_remote_branch_target("origin/main");
    let branch_oid = test_repo.get_branch_target("feature-a");

    super::drop_commit(&test_repo.repo, &branch_oid.to_string(), true)
        .expect("dropping the only commit of a branch");

    assert!(
        test_repo.branch_exists("feature-a"),
        "feature-a should survive its commit"
    );
    assert_eq!(test_repo.get_branch_target("feature-a"), base_oid);
    assert!(!test_repo.commit_messages().contains(&"A1".to_string()));
    // The merge went with the emptied section
    assert_eq!(test_repo.head_commit().parent_count(), 1);
}

/// A stacked branch owns no section of its own, so it takes the plain commit
/// path — and survives its only commit the same way.
#[test]
fn drop_sole_commit_of_a_stacked_branch_leaves_it_empty() {
    let test_repo = TestRepo::new_with_remote();
    let base_oid = test_repo.find_remote_branch_target("origin/main");

    test_repo.create_branch_at("inner", &base_oid.to_string());
    test_repo.switch_branch("inner");
    let i1_oid = test_repo.commit("I1", "i1.txt");

    test_repo.create_branch_at("outer", &i1_oid.to_string());
    test_repo.switch_branch("outer");
    test_repo.commit("O1", "o1.txt");

    test_repo.switch_branch("integration");
    test_repo.merge_no_ff("outer");

    super::drop_commit(&test_repo.repo, &i1_oid.to_string(), true)
        .expect("dropping the sole commit of a stacked branch");

    assert!(test_repo.branch_exists("inner"), "inner should survive");
    assert_eq!(test_repo.get_branch_target("inner"), base_oid);
    let outer = test_repo.get_branch_target("outer");
    let o1 = test_repo.find_commit(outer);
    assert_eq!(o1.summary().unwrap().unwrap(), "O1");
    assert_eq!(o1.parent_id(0).unwrap(), base_oid, "I1 is gone");
}

/// The rebase moves an emptied branch's ref like any other, so a checkout of
/// it elsewhere refuses the drop before any history is rewritten.
#[test]
fn drop_refuses_when_an_emptied_branch_is_checked_out_elsewhere() {
    let test_repo = setup_woven_branch(1);
    let a1_oid = test_repo.get_branch_target("feature-a");
    let old_head = test_repo.head_oid();

    let wt = test_repo.workdir().parent().unwrap().join("wt-feature-a");
    crate::git::run_git(
        &test_repo.workdir(),
        &["worktree", "add", wt.to_str().unwrap(), "feature-a"],
    )
    .unwrap();

    let err = super::drop_commit(&test_repo.repo, &a1_oid.to_string(), true)
        .expect_err("feature-a is checked out elsewhere");
    assert!(err.to_string().contains("feature-a"), "{err}");

    assert_eq!(test_repo.head_oid(), old_head, "history must be untouched");
    assert_eq!(test_repo.get_branch_target("feature-a"), a1_oid);
    assert!(
        !crate::core::transaction::state_path(test_repo.repo.path()).exists(),
        "no rebase ever started, so the state file must not be left behind"
    );
}

#[test]
fn drop_one_of_two_commits_preserves_branch() {
    let test_repo = setup_woven_branch(2);

    let a2_oid = test_repo.get_branch_target("feature-a");

    let result = super::drop_commit(&test_repo.repo, &a2_oid.to_string(), true);
    assert!(result.is_ok(), "drop_commit failed: {:?}", result);

    assert!(
        test_repo.branch_exists("feature-a"),
        "feature-a should still exist"
    );
}

#[test]
fn drop_stale_sha_fails_without_touching_history() {
    let test_repo = TestRepo::new_with_remote();
    let b_oid = test_repo.commit("B", "b.txt");
    let stale_oid = test_repo.commit("C", "c.txt");

    // Rewrite history: C's object stays alive in the reflog but is no longer
    // in the upstream..HEAD range
    test_repo.reset_hard(b_oid);
    let new_tip = test_repo.commit("C2", "c2.txt");

    let result = super::drop_commit(&test_repo.repo, &stale_oid.to_string(), true);
    let err = result.expect_err("dropping a stale SHA must fail");
    assert!(
        err.to_string().contains("not in the local commits"),
        "unexpected error: {}",
        err
    );
    assert_eq!(test_repo.head_oid(), new_tip, "history must be unchanged");
}

#[test]
fn drop_upstream_commit_fails_without_touching_history() {
    let test_repo = TestRepo::new_with_remote();
    let local_oid = test_repo.commit("Local", "local.txt");
    let upstream_oid = test_repo.find_remote_branch_target("origin/main");

    let result = super::drop_commit(&test_repo.repo, &upstream_oid.to_string(), true);
    let err = result.expect_err("dropping an upstream commit must fail");
    assert!(
        err.to_string().contains("already in the upstream"),
        "unexpected error: {}",
        err
    );
    assert_eq!(test_repo.head_oid(), local_oid, "history must be unchanged");
}

// ── Drop branch tests ───────────────────────────────────────────────────

#[test]
fn drop_woven_branch_removes_commits_and_ref() {
    let test_repo = setup_woven_branch(2);

    let result = super::drop_branch(&test_repo.repo, "feature-a", true);
    assert!(result.is_ok(), "drop_branch failed: {:?}", result);

    assert!(
        !test_repo.branch_exists("feature-a"),
        "feature-a should be deleted"
    );

    let messages = test_repo.commit_messages();
    assert!(!messages.contains(&"A1".to_string()), "A1 should be gone");
    assert!(!messages.contains(&"A2".to_string()), "A2 should be gone");
    assert!(
        messages.contains(&"Int".to_string()),
        "Int commit should remain"
    );
}

#[test]
fn drop_branch_at_merge_base_just_deletes_ref() {
    let test_repo = TestRepo::new_with_remote();
    let base_oid = test_repo.find_remote_branch_target("origin/main");

    // Create a branch at merge-base (no commits, no weaving)
    test_repo.create_branch_at("empty-branch", &base_oid.to_string());

    test_repo.commit("C1", "c1.txt");

    let result = super::drop_branch(&test_repo.repo, "empty-branch", true);
    assert!(result.is_ok(), "drop_branch failed: {:?}", result);

    assert!(
        !test_repo.branch_exists("empty-branch"),
        "empty-branch should be deleted"
    );

    assert_eq!(test_repo.get_message(0), "C1");
}

#[test]
fn messages_state_how_many_commits_go_with_the_branch() {
    use super::DropScope::Commits;
    assert_eq!(
        super::drop_prompt("feat", &Commits(1)),
        "Drop branch `feat` and its 1 commit?"
    );
    assert_eq!(
        super::drop_prompt("feat", &Commits(3)),
        "Drop branch `feat` and its 3 commits?"
    );
    assert_eq!(
        super::dropped_message("feat", &Commits(3)),
        "Dropped branch `feat` and its 3 commits"
    );
}

#[test]
fn the_empty_branch_message_says_it_was_empty() {
    assert_eq!(
        super::dropped_message("feat", &super::DropScope::Empty),
        "Dropped empty branch `feat`"
    );
}

/// With nothing removed, neither message claims a number.
#[test]
fn messages_claim_no_count_when_nothing_is_removed() {
    use super::DropScope::Commits;
    assert_eq!(
        super::drop_prompt("feat", &Commits(0)),
        "Drop branch `feat`?"
    );
    assert_eq!(
        super::dropped_message("feat", &Commits(0)),
        "Dropped branch `feat`"
    );
}

/// A co-located drop removes no commit, so both messages name the sibling
/// keeping them instead of a count.
#[test]
fn messages_name_the_sibling_that_keeps_the_commits() {
    use super::DropScope::KeptBy;
    assert_eq!(
        super::drop_prompt("feat", &KeptBy("sibling")),
        "Drop branch `feat`, keeping its commits on `sibling`?"
    );
    assert_eq!(
        super::dropped_message("feat", &KeptBy("sibling")),
        "Dropped branch `feat`, its commits stay on `sibling`"
    );
}

#[test]
fn messages_claim_no_keeper_when_no_ref_names_the_section() {
    use super::DropScope::KeptInHistory;
    assert_eq!(
        super::drop_prompt("feat", &KeptInHistory),
        "Drop branch `feat`, keeping its commits in history?"
    );
    assert_eq!(
        super::dropped_message("feat", &KeptInHistory),
        "Dropped branch `feat`, its commits stay in history"
    );
}

#[test]
fn drop_non_woven_branch_removes_commits_and_ref() {
    let test_repo = TestRepo::new_with_remote();
    let base_oid = test_repo.find_remote_branch_target("origin/main");

    test_repo.create_branch_at("feature-a", &base_oid.to_string());
    test_repo.switch_branch("feature-a");
    test_repo.commit("A1", "a1.txt");
    test_repo.commit("A2", "a2.txt");

    // Switch back to integration and fast-forward merge feature-a
    test_repo.switch_branch("integration");
    test_repo.merge_no_ff("feature-a");

    // Force checkout to sync working tree after merge
    test_repo.force_checkout();

    // Add a commit on integration after the merge so feature-a tip != HEAD
    test_repo.commit("Int", "int.txt");

    let result = super::drop_branch(&test_repo.repo, "feature-a", true);
    assert!(result.is_ok(), "drop_branch failed: {:?}", result);

    assert!(
        !test_repo.branch_exists("feature-a"),
        "feature-a should be deleted"
    );

    let messages = test_repo.commit_messages();
    assert!(!messages.contains(&"A1".to_string()), "A1 should be gone");
    assert!(!messages.contains(&"A2".to_string()), "A2 should be gone");
    assert!(
        messages.contains(&"Int".to_string()),
        "Int commit should remain"
    );
}

#[test]
fn drop_file_target_fails() {
    let test_repo = TestRepo::new_with_remote();
    test_repo.commit("C1", "c1.txt");

    // "nonexistent" doesn't resolve to anything
    let result = test_repo.in_dir(|| super::run(vec!["nonexistent".to_string()], true));

    assert!(result.is_err());
}

#[test]
fn drop_woven_branch_with_two_branches_preserves_other() {
    let test_repo = TestRepo::new_with_remote();
    let base_oid = test_repo.find_remote_branch_target("origin/main");

    test_repo.create_branch_at("feature-a", &base_oid.to_string());
    test_repo.switch_branch("feature-a");
    test_repo.commit("A1", "a1.txt");
    test_repo.commit("A2", "a2.txt");
    test_repo.switch_branch("integration");

    test_repo.create_branch_at("feature-b", &base_oid.to_string());
    test_repo.switch_branch("feature-b");
    test_repo.commit("B1", "b1.txt");
    test_repo.switch_branch("integration");

    // Add integration commit to prevent fast-forward, then weave both
    test_repo.commit("Int", "int.txt");
    test_repo.merge_no_ff("feature-a");
    test_repo.merge_no_ff("feature-b");

    let result = super::drop_branch(&test_repo.repo, "feature-a", true);
    assert!(result.is_ok(), "drop_branch failed: {:?}", result);

    assert!(!test_repo.branch_exists("feature-a"));
    assert!(test_repo.branch_exists("feature-b"));

    let messages = test_repo.commit_messages();
    assert!(!messages.contains(&"A1".to_string()), "A1 should be gone");
    assert!(!messages.contains(&"A2".to_string()), "A2 should be gone");
    assert!(messages.contains(&"B1".to_string()), "B1 should remain");
}

// ── Co-located branch tests (same tip) ───────────────────────────────────

#[test]
fn drop_colocated_non_woven_preserves_other_branch_and_commits() {
    let test_repo = TestRepo::new_with_remote();
    let base_oid = test_repo.find_remote_branch_target("origin/main");

    test_repo.create_branch_at("feature-a", &base_oid.to_string());
    test_repo.switch_branch("feature-a");
    test_repo.commit("A1", "a1.txt");
    test_repo.switch_branch("integration");

    test_repo.merge_no_ff("feature-a");
    test_repo.force_checkout();

    // Create feature-b at the same tip as feature-a (co-located)
    let fa_tip = test_repo.get_branch_target("feature-a");
    test_repo.create_branch_at("feature-b", &fa_tip.to_string());

    // Add a commit after so branches are not at HEAD
    test_repo.commit("Int", "int.txt");

    // Drop feature-a — feature-b shares the same tip
    let result = super::drop_branch(&test_repo.repo, "feature-a", true);
    assert!(result.is_ok(), "drop_branch failed: {:?}", result);

    assert!(
        !test_repo.branch_exists("feature-a"),
        "feature-a should be deleted"
    );

    assert!(
        test_repo.branch_exists("feature-b"),
        "feature-b should still exist"
    );

    // Commits should still be in history (not dropped)
    let messages = test_repo.commit_messages();
    assert!(
        messages.contains(&"A1".to_string()),
        "A1 should still be in history"
    );
    assert!(
        messages.contains(&"Int".to_string()),
        "Int should still be in history"
    );
}

#[test]
fn drop_colocated_woven_preserves_other_branch_and_commits() {
    let test_repo = TestRepo::new_with_remote();
    let base_oid = test_repo.find_remote_branch_target("origin/main");

    test_repo.create_branch_at("feature-a", &base_oid.to_string());
    test_repo.switch_branch("feature-a");
    test_repo.commit("A1", "a1.txt");
    test_repo.switch_branch("integration");

    // Create feature-b at the same tip as feature-a (co-located)
    let fa_tip = test_repo.get_branch_target("feature-a");
    test_repo.create_branch_at("feature-b", &fa_tip.to_string());

    // Add integration commit and weave feature-a (creates merge topology)
    test_repo.commit("Int", "int.txt");
    test_repo.merge_no_ff("feature-a");

    // Drop feature-a — feature-b shares the same tip
    let result = super::drop_branch(&test_repo.repo, "feature-a", true);
    assert!(result.is_ok(), "drop_branch failed: {:?}", result);

    assert!(
        !test_repo.branch_exists("feature-a"),
        "feature-a should be deleted"
    );

    assert!(
        test_repo.branch_exists("feature-b"),
        "feature-b should still exist"
    );

    // A1 should still be in history (feature-b still needs it)
    let messages = test_repo.commit_messages();
    assert!(
        messages.contains(&"A1".to_string()),
        "A1 should still be in history"
    );
    assert!(
        messages.contains(&"Int".to_string()),
        "Int should still be in history"
    );
}

#[test]
fn drop_stacked_outer_branch_preserves_inner_branch() {
    // Stacked topology: feat2 is stacked on feat1.
    //   origin/main → A1 (feat1) → A2 (feat2)
    //                                         ↘
    //                            Int --------→ merge (HEAD, integration)
    //
    // Dropping feat2 should keep feat1 and its commit A1.
    let test_repo = TestRepo::new_with_remote();
    let base_oid = test_repo.find_remote_branch_target("origin/main");

    test_repo.create_branch_at("feat1", &base_oid.to_string());
    test_repo.switch_branch("feat1");
    test_repo.commit("A1", "a1.txt");
    let feat1_tip = test_repo.head_oid();

    // Create feat2 stacked on feat1 with one commit
    test_repo.create_branch_at("feat2", &feat1_tip.to_string());
    test_repo.switch_branch("feat2");
    test_repo.commit("A2", "a2.txt");
    test_repo.switch_branch("integration");

    // Add integration commit and weave feat2 (which includes feat1)
    test_repo.commit("Int", "int.txt");
    test_repo.merge_no_ff("feat2");

    let result = super::drop_branch(&test_repo.repo, "feat2", true);
    assert!(result.is_ok(), "drop_branch failed: {:?}", result);

    assert!(!test_repo.branch_exists("feat2"), "feat2 should be deleted");

    assert!(test_repo.branch_exists("feat1"), "feat1 should still exist");

    // A1 should remain in history, A2 should be gone
    let messages = test_repo.commit_messages();
    assert!(
        messages.contains(&"A1".to_string()),
        "A1 should still be in history"
    );
    assert!(!messages.contains(&"A2".to_string()), "A2 should be gone");
    assert!(
        messages.contains(&"Int".to_string()),
        "Int should still be in history"
    );
}

/// Two branches at the same sole commit inside an outer branch both survive,
/// parked at the base that commit built on.
#[test]
fn drop_sole_commit_shared_by_two_branches_leaves_them_empty() {
    let test_repo = TestRepo::new_with_remote();
    let base_oid = test_repo.find_remote_branch_target("origin/main");

    test_repo.create_branch_at("inner", &base_oid.to_string());
    test_repo.switch_branch("inner");
    let i1_oid = test_repo.commit("I1", "i1.txt");
    test_repo.create_branch_at("inner-too", &i1_oid.to_string());

    test_repo.create_branch_at("outer", &i1_oid.to_string());
    test_repo.switch_branch("outer");
    test_repo.commit("O1", "o1.txt");

    test_repo.switch_branch("integration");
    test_repo.merge_no_ff("outer");

    super::drop_commit(&test_repo.repo, &i1_oid.to_string(), true)
        .expect("dropping the sole commit of two inner branches");

    for name in ["inner", "inner-too"] {
        assert!(test_repo.branch_exists(name), "{name} should survive");
        assert_eq!(test_repo.get_branch_target(name), base_oid);
    }
    let repo = &test_repo.repo;
    let head = repo.head().unwrap().peel_to_commit().unwrap();
    assert_eq!(head.parent_count(), 2, "HEAD should still merge outer");
    let outer = test_repo.get_branch_target("outer");
    assert_eq!(head.parent_id(1).unwrap(), outer);
    let o1 = repo.find_commit(outer).unwrap();
    assert_eq!(o1.summary().unwrap().unwrap(), "O1");
    assert_eq!(o1.parent_id(0).unwrap(), base_oid, "I1 is gone");
}

#[test]
fn drop_stacked_inner_branch_deletes_only_the_ref() {
    // base -> A1 (feat1) -> A2 (feat2), woven in: feat1 is inside feat2's section.
    let test_repo = TestRepo::new_with_remote();
    let base_oid = test_repo.find_remote_branch_target("origin/main");

    test_repo.create_branch_at("feat1", &base_oid.to_string());
    test_repo.switch_branch("feat1");
    test_repo.commit("A1", "a1.txt");
    let feat1_tip = test_repo.head_oid();

    test_repo.create_branch_at("feat2", &feat1_tip.to_string());
    test_repo.switch_branch("feat2");
    test_repo.commit("A2", "a2.txt");
    test_repo.switch_branch("integration");

    test_repo.commit("Int", "int.txt");
    test_repo.merge_no_ff("feat2");
    let head_before = test_repo.head_oid();
    let feat2_before = test_repo.get_branch_target("feat2");

    super::drop_branch(&test_repo.repo, "feat1", true).expect("dropping an inner branch");

    assert!(!test_repo.branch_exists("feat1"), "feat1 should be deleted");
    assert!(test_repo.branch_exists("feat2"), "feat2 should still exist");
    assert_eq!(test_repo.head_oid(), head_before, "no history rewritten");
    assert_eq!(test_repo.get_branch_target("feat2"), feat2_before);
    let messages = test_repo.commit_messages();
    assert!(messages.contains(&"A1".to_string()));
    assert!(messages.contains(&"A2".to_string()));
}

/// Deleting the outer ref leaves a section carrying a generated `section-<hash>`
/// label; the drop must not offer that label as the branch keeping the commits.
#[test]
fn drop_inner_branch_of_an_unnamed_section_names_no_keeper() {
    // base -> A1 (feat1) -> A2, merged in, then the outer ref is deleted.
    let test_repo = TestRepo::new_with_remote();
    let base_oid = test_repo.find_remote_branch_target("origin/main");

    test_repo.create_branch_at("feat1", &base_oid.to_string());
    test_repo.switch_branch("feat1");
    test_repo.commit("A1", "a1.txt");
    let feat1_tip = test_repo.head_oid();

    test_repo.create_branch_at("feat2", &feat1_tip.to_string());
    test_repo.switch_branch("feat2");
    test_repo.commit("A2", "a2.txt");
    test_repo.switch_branch("integration");
    test_repo.merge_no_ff("feat2");
    test_repo.delete_branch("feat2");
    let head_before = test_repo.head_oid();

    let graph = Weave::from_repo(&test_repo.repo).expect("weave graph");
    assert_eq!(
        graph.inner_branch_keeper("feat1"),
        None,
        "no ref names the section, so it has no keeper to show"
    );

    super::drop_branch(&test_repo.repo, "feat1", true).expect("dropping an inner branch");

    assert!(!test_repo.branch_exists("feat1"), "feat1 should be deleted");
    assert_eq!(test_repo.head_oid(), head_before, "no history rewritten");
    let messages = test_repo.commit_messages();
    assert!(messages.contains(&"A1".to_string()));
    assert!(messages.contains(&"A2".to_string()));
}

/// Guards the inner-branch shortcut against a branch on the integration line
/// that a later section's commits also cover: only the `assigned_branches`
/// ordering in `Weave::from_repo_with_info` keeps it off the shortcut, so a
/// reorder there would silently turn this drop into a bare ref delete.
#[test]
fn non_woven_branch_covered_by_a_woven_section_is_not_inner() {
    // base -> I1 (old) on the line; feat forks at I1 and is woven, so feat's
    // section walks back over I1 too.
    let test_repo = TestRepo::new_with_remote();

    let i1_oid = test_repo.commit("I1", "i1.txt");
    test_repo.create_branch_at("old", &i1_oid.to_string());
    test_repo.create_branch_at("feat", &i1_oid.to_string());
    test_repo.switch_branch("feat");
    test_repo.commit("F1", "f1.txt");
    test_repo.switch_branch("integration");
    test_repo.merge_no_ff("feat");
    let head_before = test_repo.head_oid();

    let graph = Weave::from_repo(&test_repo.repo).expect("weave graph");
    assert!(
        !graph.is_inner_branch("old"),
        "the Pick owns the ref, not feat"
    );
    assert_eq!(graph.inner_branch_keeper("old"), None);

    super::drop_branch(&test_repo.repo, "old", true).expect("dropping a non-woven branch");

    assert!(!test_repo.branch_exists("old"), "old should be deleted");
    assert_ne!(
        test_repo.head_oid(),
        head_before,
        "the non-woven path rebases; the inner shortcut would not"
    );
}

/// Guards the inner-branch shortcut against a stack left on the integration
/// line: no weave section covers it, so it takes the non-woven path and its
/// commits do go, rewriting the branch stacked on top.
#[test]
fn drop_non_woven_stacked_inner_branch_drops_its_commits() {
    // base -> A1 (feat1) -> A2 (feat2), never woven: both tips are on the
    // integration first-parent line.
    let test_repo = TestRepo::new_with_remote();
    let base_oid = test_repo.find_remote_branch_target("origin/main");

    let a1_oid = test_repo.commit("A1", "a1.txt");
    test_repo.create_branch_at("feat1", &a1_oid.to_string());
    let a2_oid = test_repo.commit("A2", "a2.txt");
    test_repo.create_branch_at("feat2", &a2_oid.to_string());

    super::drop_branch(&test_repo.repo, "feat1", true).expect("dropping a non-woven inner branch");

    assert!(!test_repo.branch_exists("feat1"), "feat1 should be deleted");
    assert!(test_repo.branch_exists("feat2"), "feat2 should still exist");
    let messages = test_repo.commit_messages();
    assert!(!messages.contains(&"A1".to_string()), "A1 should be gone");
    assert!(messages.contains(&"A2".to_string()), "A2 should survive");
    let feat2 = test_repo.find_commit(test_repo.get_branch_target("feat2"));
    assert_eq!(
        feat2.parent_id(0).unwrap(),
        base_oid,
        "feat2 replayed on base"
    );
}

/// Aborting a drop that conflicted puts the branch it would have emptied back
/// at its original tip, not at the base it was parked on.
#[test]
fn drop_abort_restores_an_emptied_branch() {
    let test_repo = TestRepo::new_with_remote();
    let base_oid = test_repo.find_remote_branch_target("origin/main");

    // feature-a's only commit creates shared.txt
    test_repo.create_branch_at("feature-a", &base_oid.to_string());
    test_repo.switch_branch("feature-a");
    let a1_oid = test_repo.commit("version-a", "shared.txt");

    test_repo.switch_branch("integration");
    test_repo.commit("Int", "int.txt");
    test_repo.merge_no_ff("feature-a");

    // An integration commit modifies shared.txt, so replaying it without A1
    // conflicts and the drop pauses.
    test_repo.write_file("shared.txt", "version-b");
    test_repo.stage_files(&["shared.txt"]);
    test_repo.commit_staged("Commit B");
    let old_head = test_repo.head_oid();

    super::drop_commit(&test_repo.repo, &a1_oid.to_string(), true)
        .expect("drop_commit should pause on conflict");
    let git_dir = test_repo.repo.path().to_path_buf();
    assert!(
        crate::core::transaction::state_path(&git_dir).exists(),
        "loom state must exist while the drop is paused"
    );

    crate::core::transaction::abort_cmd(&test_repo.workdir(), &git_dir).unwrap();

    assert_eq!(test_repo.head_oid(), old_head, "HEAD must be restored");
    assert!(test_repo.branch_exists("feature-a"));
    assert_eq!(
        test_repo.get_branch_target("feature-a"),
        a1_oid,
        "feature-a must be back at its own commit, not parked at the base"
    );
}

// ── Drop via run() (end-to-end) ─────────────────────────────────────────

#[test]
fn run_drop_commit_by_hash() {
    let test_repo = TestRepo::new_with_remote();
    test_repo.commit("Keep", "keep.txt");
    let drop_oid = test_repo.commit("Drop me", "drop.txt");
    test_repo.commit("Keep2", "keep2.txt");

    let result = test_repo.in_dir(|| super::run(vec![drop_oid.to_string()], true));

    assert!(result.is_ok(), "run failed: {:?}", result);
    assert_eq!(test_repo.get_message(0), "Keep2");
    assert_eq!(test_repo.get_message(1), "Keep");
}

#[test]
fn run_drop_branch_by_name() {
    let test_repo = setup_woven_branch(2);

    let result = test_repo.in_dir(|| super::run(vec!["feature-a".to_string()], true));

    assert!(result.is_ok(), "run failed: {:?}", result);
    assert!(!test_repo.branch_exists("feature-a"));
}

// ── Drop file tests ─────────────────────────────────────────────────────

#[test]
fn drop_file_restores_tracked_modifications() {
    let test_repo = TestRepo::new_with_remote();
    test_repo.commit("Base", "base.txt");

    test_repo.write_file("base.txt", "modified content");

    let result = super::drop_files(&test_repo.repo, &["base.txt".to_string()], true);
    assert!(result.is_ok(), "drop_file failed: {:?}", result);

    // File should be restored to its committed state (content == commit message)
    assert_eq!(test_repo.read_file("base.txt"), "Base");
    assert!(
        test_repo.status_porcelain().is_empty(),
        "working tree should be clean"
    );
}

#[test]
fn drop_file_deletes_untracked_file() {
    let test_repo = TestRepo::new_with_remote();
    test_repo.commit("Base", "base.txt");

    test_repo.write_file("untracked.txt", "new content");

    let result = super::drop_files(&test_repo.repo, &["untracked.txt".to_string()], true);
    assert!(result.is_ok(), "drop_file failed: {:?}", result);

    let path = test_repo.workdir().join("untracked.txt");
    assert!(!path.exists(), "untracked.txt should be deleted");
    assert!(
        test_repo.status_porcelain().is_empty(),
        "working tree should be clean"
    );
}

#[test]
fn drop_file_deletes_staged_new_file() {
    let test_repo = TestRepo::new_with_remote();
    test_repo.commit("Base", "base.txt");

    test_repo.write_file("new.txt", "new content");
    test_repo.stage_files(&["new.txt"]);

    let result = super::drop_files(&test_repo.repo, &["new.txt".to_string()], true);
    assert!(result.is_ok(), "drop_file failed: {:?}", result);

    let path = test_repo.workdir().join("new.txt");
    assert!(!path.exists(), "new.txt should be deleted");
    assert!(
        test_repo.status_porcelain().is_empty(),
        "working tree should be clean"
    );
}

#[test]
fn several_files_drop_together_after_one_confirmation() {
    let test_repo = TestRepo::new_with_remote();
    test_repo.commit("Base", "base.txt");
    test_repo.write_file("base.txt", "modified content");
    test_repo.write_file("untracked.txt", "new content");

    let result = test_repo.in_dir(|| {
        super::run(
            vec!["base.txt".to_string(), "untracked.txt".to_string()],
            true,
        )
    });
    assert!(result.is_ok(), "drop failed: {:?}", result);

    assert_eq!(test_repo.read_file("base.txt"), "Base");
    assert!(!test_repo.workdir().join("untracked.txt").exists());
    assert!(test_repo.status_porcelain().is_empty());
}

#[test]
fn only_files_drop_together() {
    let test_repo = setup_woven_branch(1);
    test_repo.write_file("extra.txt", "new content");

    for other in ["feature-a", "zz"] {
        let result =
            test_repo.in_dir(|| super::run(vec!["extra.txt".to_string(), other.to_string()], true));
        let err = result.unwrap_err().to_string();
        assert_eq!(err, "Only files can be dropped together");
    }
    assert!(test_repo.branch_exists("feature-a"));
    assert!(test_repo.workdir().join("extra.txt").exists());
}

/// The directory's clean already removes the file; a delete of its own would
/// then fail on a path that is gone.
#[test]
fn a_file_inside_a_dropped_directory_goes_with_it() {
    let test_repo = TestRepo::new_with_remote();
    test_repo.commit("Base", "base.txt");
    std::fs::create_dir_all(test_repo.workdir().join("newdir")).unwrap();
    test_repo.write_file("newdir/a.txt", "new");
    test_repo.write_file("newdir/b.txt", "new");

    let paths = ["newdir/a.txt".to_string(), "newdir".to_string()];
    let result = super::drop_files(&test_repo.repo, &paths, true);
    assert!(result.is_ok(), "drop failed: {:?}", result);
    assert!(!test_repo.workdir().join("newdir").exists());
    assert!(test_repo.status_porcelain().is_empty());
}

/// Spellings shell completion produces (`newdir/`, `./newdir`, `.`) name the
/// same directory; it must neither swallow itself nor miss its files.
#[test]
fn directory_spellings_drop_the_same_directory() {
    for args in [
        vec!["newdir/"],
        vec!["./newdir", "newdir/a.txt"],
        vec!["newdir/a.txt", "newdir/"],
        vec!["."],
        vec![".", "newdir/a.txt"],
    ] {
        let test_repo = TestRepo::new_with_remote();
        test_repo.commit("Base", "base.txt");
        std::fs::create_dir_all(test_repo.workdir().join("newdir")).unwrap();
        test_repo.write_file("newdir/a.txt", "new");
        test_repo.write_file("base.txt", "dirty");

        let targets: Vec<String> = args.iter().map(|a| a.to_string()).collect();
        let result = test_repo.in_dir(|| super::run(targets, true));
        assert!(result.is_ok(), "{args:?}: {:?}", result);
        assert!(
            !test_repo.workdir().join("newdir").exists(),
            "{args:?}: newdir should be gone"
        );
        // The root takes tracked changes too; a subdirectory leaves them.
        let expected = if args.contains(&".") { "Base" } else { "dirty" };
        assert_eq!(test_repo.read_file("base.txt"), expected, "{args:?}");
    }
}

/// A file named twice (here in two spellings) is dropped once.
#[test]
fn a_file_named_twice_is_dropped_once() {
    let test_repo = TestRepo::new_with_remote();
    test_repo.commit("Base", "base.txt");
    test_repo.write_file("untracked.txt", "new content");

    let result = test_repo.in_dir(|| {
        super::run(
            vec!["untracked.txt".to_string(), "./untracked.txt".to_string()],
            true,
        )
    });
    assert!(result.is_ok(), "drop failed: {:?}", result);
    assert!(!test_repo.workdir().join("untracked.txt").exists());
}

#[test]
fn files_prompt_lists_restored_and_deleted_paths() {
    use super::FileOp;
    let plan = |path: &str, op| (path.to_string(), op);
    assert_eq!(
        super::files_prompt(&[plan("a", FileOp::Restore)]),
        "Discard changes to `a`?"
    );
    assert_eq!(
        super::files_prompt(&[plan("d", FileOp::RestoreDir)]),
        "Discard all changes in `d`?"
    );
    assert_eq!(
        super::files_prompt(&[plan("n", FileOp::RmStaged)]),
        "Delete `n`?"
    );
    assert_eq!(
        super::files_prompt(&[plan("a", FileOp::Restore), plan("b", FileOp::RestoreDir)]),
        "Discard all selected changes?\nrestore `a`\nrestore `b`"
    );
    assert_eq!(
        super::files_prompt(&[plan("n", FileOp::Remove), plan("d", FileOp::CleanDir)]),
        "Delete all selected files?\ndelete `n`\ndelete `d`"
    );
    assert_eq!(
        super::files_prompt(&[plan("a", FileOp::Restore), plan("n", FileOp::Remove)]),
        "Discard all selected changes and delete all selected files?\nrestore `a`\ndelete `n`"
    );
}

// ── Drop directory tests ─────────────────────────────────────────────────

#[test]
fn drop_dir_with_only_untracked_files() {
    let test_repo = TestRepo::new_with_remote();
    test_repo.commit("Base", "base.txt");

    // Create a directory with only untracked files
    std::fs::create_dir_all(test_repo.workdir().join("newdir")).unwrap();
    test_repo.write_file("newdir/a.txt", "aaa");
    test_repo.write_file("newdir/b.txt", "bbb");

    let result = super::drop_files(&test_repo.repo, &["newdir".to_string()], true);
    assert!(result.is_ok(), "drop_file (dir) failed: {:?}", result);

    assert!(
        !test_repo.workdir().join("newdir").exists(),
        "newdir should be deleted"
    );
    assert!(
        test_repo.status_porcelain().is_empty(),
        "working tree should be clean"
    );
}

#[test]
fn drop_dir_with_only_tracked_modifications() {
    let test_repo = TestRepo::new_with_remote();

    std::fs::create_dir_all(test_repo.workdir().join("src")).unwrap();
    test_repo.write_file("src/one.txt", "original-one");
    test_repo.write_file("src/two.txt", "original-two");
    test_repo.stage_files(&["src/one.txt", "src/two.txt"]);
    test_repo.commit_staged("Initial src files");

    test_repo.write_file("src/one.txt", "modified-one");
    test_repo.write_file("src/two.txt", "modified-two");

    let result = super::drop_files(&test_repo.repo, &["src".to_string()], true);
    assert!(result.is_ok(), "drop_file (dir) failed: {:?}", result);

    assert_eq!(test_repo.read_file("src/one.txt"), "original-one");
    assert_eq!(test_repo.read_file("src/two.txt"), "original-two");
    assert!(
        test_repo.status_porcelain().is_empty(),
        "working tree should be clean"
    );
}

#[test]
fn drop_dir_with_mixed_tracked_and_untracked() {
    let test_repo = TestRepo::new_with_remote();

    std::fs::create_dir_all(test_repo.workdir().join("mix")).unwrap();
    test_repo.write_file("mix/tracked.txt", "original");
    test_repo.stage_files(&["mix/tracked.txt"]);
    test_repo.commit_staged("Initial mix file");

    // Modify tracked file + add untracked file
    test_repo.write_file("mix/tracked.txt", "modified");
    test_repo.write_file("mix/untracked.txt", "new");

    let result = super::drop_files(&test_repo.repo, &["mix".to_string()], true);
    assert!(result.is_ok(), "drop_file (dir) failed: {:?}", result);

    assert_eq!(test_repo.read_file("mix/tracked.txt"), "original");
    assert!(
        !test_repo.workdir().join("mix/untracked.txt").exists(),
        "untracked file should be deleted"
    );
    assert!(
        test_repo.status_porcelain().is_empty(),
        "working tree should be clean"
    );
}

#[test]
fn drop_dir_with_staged_new_files() {
    let test_repo = TestRepo::new_with_remote();
    test_repo.commit("Base", "base.txt");

    std::fs::create_dir_all(test_repo.workdir().join("staged")).unwrap();
    test_repo.write_file("staged/a.txt", "aaa");
    test_repo.write_file("staged/b.txt", "bbb");
    test_repo.stage_files(&["staged/a.txt", "staged/b.txt"]);

    let result = super::drop_files(&test_repo.repo, &["staged".to_string()], true);
    assert!(result.is_ok(), "drop_file (dir) failed: {:?}", result);

    assert!(
        !test_repo.workdir().join("staged").exists(),
        "staged dir should be deleted"
    );
    assert!(
        test_repo.status_porcelain().is_empty(),
        "working tree should be clean"
    );
}

// ── Drop all (zz) tests ─────────────────────────────────────────────────

#[test]
fn drop_all_discards_tracked_and_untracked_changes() {
    let test_repo = TestRepo::new_with_remote();
    test_repo.commit("Base", "base.txt");

    // Mix of changes: modify tracked file + create untracked file
    test_repo.write_file("base.txt", "modified");
    test_repo.write_file("untracked.txt", "new");

    let result = super::drop_all(&test_repo.repo, true);
    assert!(result.is_ok(), "drop_all failed: {:?}", result);

    assert!(
        test_repo.status_porcelain().is_empty(),
        "working tree should be clean"
    );
    let path = test_repo.workdir().join("untracked.txt");
    assert!(!path.exists(), "untracked.txt should be deleted");
    assert_eq!(test_repo.read_file("base.txt"), "Base");
}

#[test]
fn drop_all_fails_when_no_changes() {
    let test_repo = TestRepo::new_with_remote();
    test_repo.commit("Base", "base.txt");

    let result = super::drop_all(&test_repo.repo, true);
    assert!(result.is_err(), "drop_all should fail with no changes");
    assert!(
        result.unwrap_err().to_string().contains("No local changes"),
        "error should mention no changes"
    );
}

// ── Integration tests for centralized resolver ───────────────────────────

#[test]
fn drop_merge_commit_fails() {
    let test_repo = TestRepo::new_with_remote();
    test_repo.commit("a.txt", "a.txt");
    let a_oid = test_repo.head_oid();
    let upstream_oid = test_repo.find_remote_branch_target("origin/main");

    test_repo.commit_merge("Merge side", a_oid, upstream_oid);
    let merge_oid = test_repo.head_oid();

    let result = test_repo.in_dir(|| crate::drop::run(vec![merge_oid.to_string()], true));
    assert!(result.is_err());
    assert!(
        result.unwrap_err().to_string().contains("merge commit"),
        "Error should mention merge commit"
    );
}

#[test]
fn drop_prefers_file_over_branch_name_collision() {
    let test_repo = TestRepo::new_with_remote();
    test_repo.commit_empty("A1");
    let a1_oid = test_repo.head_oid();
    test_repo.create_branch_at_commit("collision", a1_oid);
    test_repo.write_file("collision", "dirty data");

    let result = test_repo.in_dir(|| crate::drop::run(vec!["collision".to_string()], true));
    assert!(result.is_ok(), "Expected ok, got: {:?}", result);
    // Branch should still exist (file was dropped, not the branch)
    assert!(test_repo.branch_exists("collision"));
}

#[test]
fn drop_file_resolves_from_nested_cwd() {
    let test_repo = TestRepo::new_with_remote();
    let sub_dir = test_repo.workdir().join("sub");
    std::fs::create_dir_all(&sub_dir).unwrap();
    std::fs::write(sub_dir.join("file.txt"), "content").unwrap();
    test_repo.stage_files(&["sub/file.txt"]);
    test_repo.commit_staged("add sub/file.txt");
    std::fs::write(sub_dir.join("file.txt"), "modified").unwrap();

    let result = test_repo.in_dir_path(&sub_dir, || {
        crate::drop::run(vec!["file.txt".to_string()], true)
    });
    assert!(result.is_ok(), "Expected ok, got: {:?}", result);
}

// ── Abort preserves working state ────────────────────────────────────────

/// Regression: loom abort after a drop conflict must preserve staged changes,
/// unstaged changes on other files, and new untracked files.
///
/// Conflict setup: Commit A creates `shared.txt`; Commit B modifies it.
/// Dropping A forces B to be replayed without A — B's diff expects `shared.txt`
/// to exist with A's content, but the file no longer exists → conflict.
#[test]
fn drop_abort_preserves_working_state() {
    let test_repo = TestRepo::new_with_remote();

    // Commit A creates shared.txt; Commit B modifies it.
    let a_oid = test_repo.commit("version-a", "shared.txt");
    test_repo.write_file("shared.txt", "version-b");
    test_repo.stage_files(&["shared.txt"]);
    test_repo.commit_staged("Commit B");

    // Working state set up before running drop.
    test_repo.write_file("shared.txt", "working-edit"); // change to the conflicting file
    test_repo.write_file("other-staged.txt", "staged-content");
    test_repo.stage_files(&["other-staged.txt"]);
    test_repo.write_file("other-unstaged.txt", "unstaged-content");
    test_repo.write_file("new-file.txt", "new-content");

    // Drop A — B cannot be replayed without A's file → conflict → loom pauses.
    let result = super::drop_commit(&test_repo.repo, &a_oid.to_string(), true);
    assert!(
        result.is_ok(),
        "drop_commit should pause on conflict: {:?}",
        result
    );

    let state_path = test_repo.repo.path().join("loom").join("state.json");
    assert!(
        state_path.exists(),
        "loom state must exist when drop is paused on conflict"
    );

    let workdir = test_repo.workdir();
    let git_dir = test_repo.repo.path().to_path_buf();
    crate::core::transaction::abort_cmd(&workdir, &git_dir).unwrap();

    // All working state preserved after abort.
    assert_eq!(test_repo.read_file("shared.txt"), "working-edit");
    assert_eq!(test_repo.read_file("other-staged.txt"), "staged-content");
    assert_eq!(
        test_repo.read_file("other-unstaged.txt"),
        "unstaged-content"
    );
    assert!(
        workdir.join("new-file.txt").exists(),
        "new untracked file must survive abort"
    );
    assert_eq!(test_repo.read_file("new-file.txt"), "new-content");
}

// ── Confirmation ─────────────────────────────────────────────────────────

/// Declining must raise the marker a dismissed prompt raises, or the TUI
/// reports a routine "no" as a failed command.
#[test]
fn declining_the_confirm_is_cancelled() {
    let (tx, rx) = std::sync::mpsc::channel();
    let worker = std::thread::spawn(move || {
        ui::install(tx);
        let result = super::confirm_or_bail(false, "Drop commit `a1`?");
        ui::uninstall();
        result
    });

    let ui::Request::Prompt { reply, .. } = rx.recv().unwrap() else {
        panic!("expected a prompt");
    };
    reply.send(Some(ui::Answer::Bool(false))).unwrap();

    let err = worker.join().unwrap().unwrap_err();
    assert!(err.downcast_ref::<ui::Cancelled>().is_some());
    assert_eq!(err.to_string(), "Cancelled");
}

// ── Staging survives a rebase that completed ─────────────────────────────

/// Regression: the autostash replay unstages a staged *modification*, so a
/// drop that succeeded has to put the index back too.
#[test]
fn drop_commit_keeps_staging_on_success() {
    let t = TestRepo::new_with_remote();
    t.commit("First", "first.txt");
    let c2 = t.commit("Second", "second.txt");
    t.commit("Third", "third.txt");
    t.write_file("first.txt", "staged edit\n");
    t.write_file("brand-new.txt", "new\n");
    t.stage_files(&["first.txt", "brand-new.txt"]);
    let before = t.status_porcelain();

    super::drop_commit(&t.repo, &c2.to_string(), true).unwrap();

    assert_eq!(t.status_porcelain(), before);
    // The letters alone would pass on a restore that staged the wrong bytes.
    assert_eq!(
        crate::git::run_git_stdout(&t.workdir(), &["show", ":first.txt"]).unwrap(),
        "staged edit\n"
    );
}

#[test]
fn drop_commit_keeps_staging_across_continue() {
    let t = TestRepo::new_with_remote();

    // Dropping A leaves B replaying onto a tree without A's `shared.txt`.
    let a_oid = t.commit("version-a", "shared.txt");
    t.write_file("shared.txt", "version-b");
    t.stage_files(&["shared.txt"]);
    t.commit_staged("Commit B");
    t.write_file("bystander.txt", "bystander\n");
    t.stage_files(&["bystander.txt"]);
    t.commit_staged("Commit C");

    t.write_file("bystander.txt", "staged edit\n");
    t.stage_files(&["bystander.txt"]);
    let before = t.status_porcelain();

    super::drop_commit(&t.repo, &a_oid.to_string(), true).unwrap();
    assert!(crate::git::rebase_is_in_progress(t.repo.path()));

    t.write_file("shared.txt", "version-b");
    t.stage_files(&["shared.txt"]);
    let workdir = t.workdir();
    crate::core::transaction::continue_cmd(&workdir, t.repo.path()).unwrap();

    assert!(!crate::git::rebase_is_in_progress(t.repo.path()));
    assert_eq!(t.status_porcelain(), before);
}

#[test]
fn drop_branch_keeps_staging_on_success() {
    let t = setup_woven_branch(2);
    t.write_file("int.txt", "staged edit\n");
    t.write_file("brand-new.txt", "new\n");
    t.stage_files(&["int.txt", "brand-new.txt"]);
    let before = t.status_porcelain();

    super::drop_branch(&t.repo, "feature-a", true).unwrap();

    assert_eq!(t.status_porcelain(), before);
    // The letters alone would pass on a restore that staged the wrong bytes.
    assert_eq!(
        crate::git::run_git_stdout(&t.workdir(), &["show", ":int.txt"]).unwrap(),
        "staged edit\n"
    );
}