git-loom 0.18.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
use crate::core::test_helpers::TestRepo;

// ── 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");

    // Create feature-a at merge-base
    test_repo.create_branch_at("feature-a", &base_oid.to_string());

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

    // Switch back to integration
    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");
}

#[test]
fn drop_last_commit_on_branch_auto_deletes_branch() {
    let test_repo = setup_woven_branch(1);
    let branch_oid = test_repo.get_branch_target("feature-a");

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

    // feature-a should have been auto-deleted
    assert!(
        !test_repo.branch_exists("feature-a"),
        "feature-a should have been auto-deleted"
    );
}

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

    // Get the tip commit of feature-a (A2)
    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);

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

// ── 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);

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

    // A1, A2 are gone from history, Int should remain
    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());

    // Add a commit on integration so there's something in the range
    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"
    );

    // C1 should still be there
    assert_eq!(test_repo.get_message(0), "C1");
}

#[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");

    // Create feature-a at merge-base and add commits
    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"
    );

    // A1, A2 should be gone, Int should remain
    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("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");

    // Create feature-a with commits
    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");

    // Create feature-b with commits
    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");

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

    // feature-a gone, feature-b still exists
    assert!(!test_repo.branch_exists("feature-a"));
    assert!(test_repo.branch_exists("feature-b"));

    // B1 should remain, A1 and A2 should be gone
    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");

    // Create feature-a with a commit
    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");

    // Fast-forward integration to feature-a
    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);

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

    // feature-b should still exist and point to the same commit content
    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");

    // Create feature-a with a commit
    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);

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

    // feature-b should still exist
    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"
    );
}

// ── Stacked branch tests ─────────────────────────────────────────────────

#[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");

    // Create feat1 at merge-base with one commit
    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");

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

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

    // feat1 should still exist
    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"
    );
}

// ── 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(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("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");

    // Modify the tracked file
    test_repo.write_file("base.txt", "modified content");

    let result = super::drop_file(&test_repo.repo, "base.txt", 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");

    // Write an untracked file
    test_repo.write_file("untracked.txt", "new content");

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

    // File should be deleted
    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");

    // Write and stage a new file
    test_repo.write_file("new.txt", "new content");
    test_repo.stage_files(&["new.txt"]);

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

    // File should be deleted
    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"
    );
}

// ── 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_file(&test_repo.repo, "newdir", 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();

    // Commit files inside a directory
    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");

    // Modify both tracked files
    test_repo.write_file("src/one.txt", "modified-one");
    test_repo.write_file("src/two.txt", "modified-two");

    let result = super::drop_file(&test_repo.repo, "src", 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();

    // Commit a file inside a directory
    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_file(&test_repo.repo, "mix", 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");

    // Create and stage new files in a directory
    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_file(&test_repo.repo, "staged", 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");

    // Create a merge commit
    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(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("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");
    // Dirty the file
    std::fs::write(sub_dir.join("file.txt"), "modified").unwrap();

    let result = test_repo.in_dir_path(&sub_dir, || crate::drop::run("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");
}