rung-core 0.8.0

Core library for Rung - stack model, state management, sync engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
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
//! Absorb command logic for automatic fixup commit creation.
//!
//! Analyzes staged changes and automatically creates fixup commits
//! targeting the appropriate commits in the local history.

use rung_git::{AbsorbOps, BlameResult, Hunk, Oid};

use crate::StateStore;
use crate::error::Result;

/// A planned fixup operation mapping a hunk to its target commit.
#[derive(Debug, Clone)]
pub struct AbsorbAction {
    /// The hunk to be absorbed.
    pub hunk: Hunk,
    /// The target commit to fixup.
    pub target_commit: Oid,
    /// Short commit message of the target.
    pub target_message: String,
}

/// Result of analyzing staged changes for absorption.
#[derive(Debug, Clone)]
pub struct AbsorbPlan {
    /// Actions that can be executed (hunk mapped to valid target).
    pub actions: Vec<AbsorbAction>,
    /// Hunks that couldn't be mapped to a target commit.
    pub unmapped: Vec<UnmappedHunk>,
}

/// A hunk that couldn't be mapped to a target commit.
#[derive(Debug, Clone)]
pub struct UnmappedHunk {
    /// The hunk that couldn't be absorbed.
    pub hunk: Hunk,
    /// Reason why mapping failed.
    pub reason: UnmapReason,
}

/// Reason why a hunk couldn't be mapped.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum UnmapReason {
    /// New file - no blame history.
    NewFile,
    /// Insert-only hunk (no deleted lines to blame).
    InsertOnly,
    /// Lines touched by multiple commits.
    MultipleCommits,
    /// Target commit is not in the rebaseable range.
    CommitNotInStack,
    /// Target commit is already on the base branch.
    CommitOnBaseBranch,
    /// Blame query failed.
    BlameError(String),
}

impl std::fmt::Display for UnmapReason {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::NewFile => write!(f, "new file (no blame history)"),
            Self::InsertOnly => write!(f, "insert-only hunk (no lines to blame)"),
            Self::MultipleCommits => write!(f, "lines touched by multiple commits"),
            Self::CommitNotInStack => write!(f, "target commit not in stack"),
            Self::CommitOnBaseBranch => write!(f, "target commit already on base branch"),
            Self::BlameError(e) => write!(f, "blame error: {e}"),
        }
    }
}

/// Result of executing an absorb plan.
#[derive(Debug)]
pub struct AbsorbResult {
    /// Number of fixup commits created.
    pub fixups_created: usize,
    /// Commits that were targeted.
    pub targeted_commits: Vec<Oid>,
}

/// Create an absorb plan by analyzing staged changes.
///
/// For each staged hunk, this function:
/// 1. Queries git blame to find which commit last touched those lines
/// 2. Validates the target commit is within the rebaseable range
/// 3. Creates an action mapping the hunk to its target
///
/// # Arguments
/// * `repo` - The git repository (implementing `AbsorbOps`)
/// * `state` - Rung state for stack information (implementing `StateStore`)
/// * `base_branch` - The base branch name (e.g., "main")
///
/// # Errors
/// Returns error if git operations fail.
pub fn create_absorb_plan<G, S>(repo: &G, state: &S, base_branch: &str) -> Result<AbsorbPlan>
where
    G: AbsorbOps,
    S: StateStore,
{
    let mut actions = Vec::new();
    let mut unmapped = Vec::new();

    // Get staged hunks
    let hunks = repo.staged_diff_hunks()?;

    if hunks.is_empty() {
        return Ok(AbsorbPlan { actions, unmapped });
    }

    // Get the base branch commit for validation
    let base_commit = repo
        .branch_commit(base_branch)
        .or_else(|_| repo.remote_branch_commit(base_branch))?;

    // Get current HEAD
    let current_branch = repo.current_branch()?;
    let head_commit = repo.branch_commit(&current_branch)?;

    // Get commits in the rebaseable range (between base and HEAD)
    let rebaseable_commits: std::collections::HashSet<Oid> = repo
        .commits_between(base_commit, head_commit)?
        .into_iter()
        .collect();

    // Load stack (reserved for future validation enhancements)
    let _stack = state.load_stack()?;

    for hunk in hunks {
        // New files have no blame history
        if hunk.is_new_file {
            unmapped.push(UnmappedHunk {
                hunk,
                reason: UnmapReason::NewFile,
            });
            continue;
        }

        // Determine blame range
        // For insert-only hunks (old_lines == 0), blame an adjacent line instead
        let (blame_start, blame_end) = if hunk.old_lines == 0 {
            // Insert-only hunk: blame the line at old_start (or line 1 if at file start)
            // old_start is the line number where insertion happens (1-indexed)
            // If old_start is 0, the insertion is at the very start; blame line 1
            let line = hunk.old_start.max(1);
            (line, line)
        } else {
            (
                hunk.old_start,
                hunk.old_start
                    .saturating_add(hunk.old_lines)
                    .saturating_sub(1),
            )
        };

        // Query blame for the original lines (or adjacent line for insert-only hunks)
        let blame_result: Vec<BlameResult> =
            match repo.blame_lines(&hunk.file_path, blame_start, blame_end) {
                Ok(results) => results,
                Err(e) => {
                    unmapped.push(UnmappedHunk {
                        hunk,
                        reason: UnmapReason::BlameError(e.to_string()),
                    });
                    continue;
                }
            };

        // Check if all blamed lines point to the same commit
        if blame_result.is_empty() {
            unmapped.push(UnmappedHunk {
                hunk,
                reason: UnmapReason::BlameError("no blame results".to_string()),
            });
            continue;
        }

        if blame_result.len() > 1 {
            unmapped.push(UnmappedHunk {
                hunk,
                reason: UnmapReason::MultipleCommits,
            });
            continue;
        }

        let target = &blame_result[0];

        // Validate target is in rebaseable range
        if !rebaseable_commits.contains(&target.commit) {
            // Check if it's on base branch
            if repo
                .is_ancestor(target.commit, base_commit)
                .unwrap_or(false)
                || target.commit == base_commit
            {
                unmapped.push(UnmappedHunk {
                    hunk,
                    reason: UnmapReason::CommitOnBaseBranch,
                });
            } else {
                unmapped.push(UnmappedHunk {
                    hunk,
                    reason: UnmapReason::CommitNotInStack,
                });
            }
            continue;
        }

        actions.push(AbsorbAction {
            hunk,
            target_commit: target.commit,
            target_message: target.message.clone(),
        });
    }

    Ok(AbsorbPlan { actions, unmapped })
}

/// Execute an absorb plan by creating fixup commits.
///
/// Creates a single fixup commit targeting the identified commit.
/// This modifies the repository by creating new commits.
///
/// # Errors
/// Returns error if commit creation fails or if hunks target multiple commits.
/// Multiple targets are not supported because git commit consumes the entire
/// staging area, making it impossible to create separate fixup commits for
/// different targets without per-hunk staging (a future enhancement).
pub fn execute_absorb<G: AbsorbOps>(repo: &G, plan: &AbsorbPlan) -> Result<AbsorbResult> {
    if plan.actions.is_empty() {
        return Ok(AbsorbResult {
            fixups_created: 0,
            targeted_commits: vec![],
        });
    }

    // Group actions by target commit
    let mut by_target: std::collections::HashMap<Oid, Vec<&AbsorbAction>> =
        std::collections::HashMap::new();
    for action in &plan.actions {
        by_target
            .entry(action.target_commit)
            .or_default()
            .push(action);
    }

    // Reject multi-target plans - git commit consumes the entire index,
    // so we can't create separate fixup commits without per-hunk staging.
    if by_target.len() > 1 {
        let target_descriptions: Vec<String> = by_target
            .iter()
            .map(|(oid, actions)| {
                let oid_str = oid.to_string();
                let short_sha = oid_str.get(..8).unwrap_or(&oid_str);
                let msg = &actions[0].target_message;
                format!("{short_sha} ({msg})")
            })
            .collect();
        return Err(crate::error::Error::Absorb(format!(
            "staged changes target {} different commits; selective hunk staging not supported. \
             Targets: {}. Stage fewer changes so all hunks target the same commit.",
            by_target.len(),
            target_descriptions.join(", ")
        )));
    }

    let mut targeted_commits = Vec::new();

    // Create fixup commit for the single target
    for target in by_target.keys() {
        repo.create_fixup_commit(*target)?;
        targeted_commits.push(*target);
    }

    Ok(AbsorbResult {
        fixups_created: by_target.len(),
        targeted_commits,
    })
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::field_reassign_with_default)]
mod tests {
    use super::*;
    use crate::config::Config;
    use crate::stack::Stack;
    use crate::state::{RestackState, SyncState};
    use rung_git::{GitOps, RemoteDivergence};
    use std::cell::RefCell;
    use std::collections::HashMap;
    use std::path::Path;
    use std::vec;

    // Mock implementation for AbsorbOps
    struct MockRepo {
        hunks: Vec<Hunk>,
        blame_results: HashMap<String, Vec<BlameResult>>,
        blame_errors: HashMap<String, String>,
        branch_commits: HashMap<String, Oid>,
        commits_between: Vec<Oid>,
        current_branch: String,
        is_ancestor_results: HashMap<(Oid, Oid), bool>,
        fixup_commits_created: RefCell<Vec<Oid>>,
    }

    impl Default for MockRepo {
        fn default() -> Self {
            Self {
                hunks: vec![],
                blame_results: HashMap::new(),
                blame_errors: HashMap::new(),
                branch_commits: HashMap::new(),
                commits_between: vec![],
                current_branch: "feature".to_string(),
                is_ancestor_results: HashMap::new(),
                fixup_commits_created: RefCell::new(vec![]),
            }
        }
    }

    impl GitOps for MockRepo {
        fn workdir(&self) -> Option<&Path> {
            None
        }
        fn current_branch(&self) -> rung_git::Result<String> {
            Ok(self.current_branch.clone())
        }
        fn head_detached(&self) -> rung_git::Result<bool> {
            Ok(false)
        }
        fn is_rebasing(&self) -> bool {
            false
        }
        fn branch_exists(&self, _name: &str) -> bool {
            true
        }
        fn create_branch(&self, _name: &str) -> rung_git::Result<Oid> {
            unimplemented!()
        }
        fn checkout(&self, _branch: &str) -> rung_git::Result<()> {
            Ok(())
        }
        fn delete_branch(&self, _name: &str) -> rung_git::Result<()> {
            unimplemented!()
        }
        fn list_branches(&self) -> rung_git::Result<Vec<String>> {
            unimplemented!()
        }
        fn branch_commit(&self, branch: &str) -> rung_git::Result<Oid> {
            self.branch_commits
                .get(branch)
                .copied()
                .ok_or_else(|| rung_git::Error::BranchNotFound(branch.to_string()))
        }
        fn remote_branch_commit(&self, branch: &str) -> rung_git::Result<Oid> {
            self.branch_commits
                .get(&format!("origin/{branch}"))
                .copied()
                .ok_or_else(|| rung_git::Error::BranchNotFound(branch.to_string()))
        }
        fn branch_commit_message(&self, _branch: &str) -> rung_git::Result<String> {
            unimplemented!()
        }
        fn merge_base(&self, _one: Oid, _two: Oid) -> rung_git::Result<Oid> {
            unimplemented!()
        }
        fn commits_between(&self, _from: Oid, _to: Oid) -> rung_git::Result<Vec<Oid>> {
            Ok(self.commits_between.clone())
        }
        fn count_commits_between(&self, _from: Oid, _to: Oid) -> rung_git::Result<usize> {
            unimplemented!()
        }
        fn is_clean(&self) -> rung_git::Result<bool> {
            Ok(true)
        }
        fn require_clean(&self) -> rung_git::Result<()> {
            Ok(())
        }
        fn stage_all(&self) -> rung_git::Result<()> {
            unimplemented!()
        }
        fn has_staged_changes(&self) -> rung_git::Result<bool> {
            Ok(!self.hunks.is_empty())
        }
        fn create_commit(&self, _message: &str) -> rung_git::Result<Oid> {
            unimplemented!()
        }
        fn amend_commit(&self, _new_message: Option<&str>) -> rung_git::Result<Oid> {
            unimplemented!()
        }
        fn rebase_onto(&self, _target: Oid) -> rung_git::Result<()> {
            unimplemented!()
        }
        fn rebase_onto_from(&self, _onto: Oid, _from: Oid) -> rung_git::Result<()> {
            unimplemented!()
        }
        fn conflicting_files(&self) -> rung_git::Result<Vec<String>> {
            unimplemented!()
        }
        fn predict_rebase_conflicts(
            &self,
            _branch: &str,
            _onto: Oid,
        ) -> rung_git::Result<Vec<rung_git::ConflictPrediction>> {
            Ok(vec![])
        }
        fn rebase_abort(&self) -> rung_git::Result<()> {
            unimplemented!()
        }
        fn rebase_continue(&self) -> rung_git::Result<()> {
            unimplemented!()
        }
        fn origin_url(&self) -> rung_git::Result<String> {
            unimplemented!()
        }
        fn remote_divergence(&self, _branch: &str) -> rung_git::Result<RemoteDivergence> {
            unimplemented!()
        }
        fn detect_default_branch(&self) -> Option<String> {
            Some("main".to_string())
        }
        fn push(&self, _branch: &str, _force: bool) -> rung_git::Result<()> {
            unimplemented!()
        }
        fn fetch_all(&self) -> rung_git::Result<()> {
            unimplemented!()
        }
        fn fetch(&self, _branch: &str) -> rung_git::Result<()> {
            unimplemented!()
        }
        fn pull_ff(&self) -> rung_git::Result<()> {
            unimplemented!()
        }
        fn reset_branch(&self, _branch: &str, _commit: Oid) -> rung_git::Result<()> {
            unimplemented!()
        }
    }

    impl AbsorbOps for MockRepo {
        fn staged_diff_hunks(&self) -> rung_git::Result<Vec<Hunk>> {
            Ok(self.hunks.clone())
        }

        fn blame_lines(
            &self,
            file_path: &str,
            _start: u32,
            _end: u32,
        ) -> rung_git::Result<Vec<BlameResult>> {
            if let Some(err) = self.blame_errors.get(file_path) {
                return Err(rung_git::Error::BlameError(err.clone()));
            }
            Ok(self
                .blame_results
                .get(file_path)
                .cloned()
                .unwrap_or_default())
        }

        fn is_ancestor(&self, ancestor: Oid, descendant: Oid) -> rung_git::Result<bool> {
            Ok(self
                .is_ancestor_results
                .get(&(ancestor, descendant))
                .copied()
                .unwrap_or(false))
        }

        fn create_fixup_commit(&self, target: Oid) -> rung_git::Result<Oid> {
            self.fixup_commits_created.borrow_mut().push(target);
            // Return a new "fixup" commit OID
            Ok(Oid::from_str("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").unwrap())
        }
    }

    // Mock implementation for StateStore
    #[derive(Default)]
    struct MockState {
        stack: Stack,
    }

    impl StateStore for MockState {
        fn is_initialized(&self) -> bool {
            true
        }
        fn init(&self) -> crate::Result<()> {
            Ok(())
        }
        fn rung_dir(&self) -> &Path {
            Path::new(".git/rung")
        }
        fn load_stack(&self) -> crate::Result<Stack> {
            Ok(self.stack.clone())
        }
        fn save_stack(&self, _stack: &Stack) -> crate::Result<()> {
            Ok(())
        }
        fn load_config(&self) -> crate::Result<Config> {
            Ok(Config::default())
        }
        fn save_config(&self, _config: &Config) -> crate::Result<()> {
            Ok(())
        }
        fn default_branch(&self) -> crate::Result<String> {
            Ok("main".to_string())
        }
        fn is_sync_in_progress(&self) -> bool {
            false
        }
        fn load_sync_state(&self) -> crate::Result<SyncState> {
            unimplemented!()
        }
        fn save_sync_state(&self, _state: &SyncState) -> crate::Result<()> {
            unimplemented!()
        }
        fn clear_sync_state(&self) -> crate::Result<()> {
            unimplemented!()
        }
        fn is_restack_in_progress(&self) -> bool {
            false
        }
        fn load_restack_state(&self) -> crate::Result<RestackState> {
            unimplemented!()
        }
        fn save_restack_state(&self, _state: &RestackState) -> crate::Result<()> {
            unimplemented!()
        }
        fn clear_restack_state(&self) -> crate::Result<()> {
            unimplemented!()
        }
        fn is_split_in_progress(&self) -> bool {
            false
        }
        fn load_split_state(&self) -> crate::Result<crate::state::SplitState> {
            Err(crate::Error::NoBackupFound)
        }
        fn save_split_state(&self, _state: &crate::state::SplitState) -> crate::Result<()> {
            Ok(())
        }
        fn clear_split_state(&self) -> crate::Result<()> {
            Ok(())
        }
        fn is_fold_in_progress(&self) -> bool {
            false
        }
        fn load_fold_state(&self) -> crate::Result<crate::state::FoldState> {
            Err(crate::Error::NoBackupFound)
        }
        fn save_fold_state(&self, _state: &crate::state::FoldState) -> crate::Result<()> {
            Ok(())
        }
        fn clear_fold_state(&self) -> crate::Result<()> {
            Ok(())
        }
        fn create_backup(&self, _branches: &[(&str, &str)]) -> crate::Result<String> {
            unimplemented!()
        }
        fn latest_backup(&self) -> crate::Result<String> {
            unimplemented!()
        }
        fn load_backup(&self, _backup_id: &str) -> crate::Result<Vec<(String, String)>> {
            unimplemented!()
        }
        fn delete_backup(&self, _backup_id: &str) -> crate::Result<()> {
            unimplemented!()
        }
        fn cleanup_backups(&self, _keep: usize) -> crate::Result<()> {
            unimplemented!()
        }
    }

    fn test_oid(n: u8) -> Oid {
        let hex = format!("{n:0>40}");
        Oid::from_str(&hex).unwrap()
    }

    #[test]
    fn test_unmap_reason_display() {
        assert_eq!(
            UnmapReason::NewFile.to_string(),
            "new file (no blame history)"
        );
        assert_eq!(
            UnmapReason::InsertOnly.to_string(),
            "insert-only hunk (no lines to blame)"
        );
        assert_eq!(
            UnmapReason::MultipleCommits.to_string(),
            "lines touched by multiple commits"
        );
        assert_eq!(
            UnmapReason::CommitNotInStack.to_string(),
            "target commit not in stack"
        );
        assert_eq!(
            UnmapReason::CommitOnBaseBranch.to_string(),
            "target commit already on base branch"
        );
        assert_eq!(
            UnmapReason::BlameError("test".to_string()).to_string(),
            "blame error: test"
        );
    }

    #[test]
    fn test_absorb_plan_empty() {
        let plan = AbsorbPlan {
            actions: vec![],
            unmapped: vec![],
        };
        assert!(plan.actions.is_empty());
        assert!(plan.unmapped.is_empty());
    }

    #[test]
    fn test_create_plan_no_staged_changes() {
        let repo = MockRepo::default();
        let state = MockState::default();

        let plan = create_absorb_plan(&repo, &state, "main").unwrap();

        assert!(plan.actions.is_empty());
        assert!(plan.unmapped.is_empty());
    }

    #[test]
    fn test_create_plan_new_file_unmapped() {
        let mut repo = MockRepo::default();
        repo.hunks = vec![Hunk {
            file_path: "new_file.rs".to_string(),
            old_start: 0,
            old_lines: 0,
            new_start: 1,
            new_lines: 10,
            content: String::new(),
            is_new_file: true,
        }];
        repo.branch_commits.insert("main".to_string(), test_oid(1));
        repo.branch_commits
            .insert("origin/main".to_string(), test_oid(1));
        repo.branch_commits
            .insert("feature".to_string(), test_oid(2));

        let state = MockState::default();

        let plan = create_absorb_plan(&repo, &state, "main").unwrap();

        assert!(plan.actions.is_empty());
        assert_eq!(plan.unmapped.len(), 1);
        assert_eq!(plan.unmapped[0].reason, UnmapReason::NewFile);
    }

    #[test]
    fn test_create_plan_successful_mapping() {
        let target_commit = test_oid(3);

        let mut repo = MockRepo::default();
        repo.hunks = vec![Hunk {
            file_path: "src/lib.rs".to_string(),
            old_start: 10,
            old_lines: 5,
            new_start: 10,
            new_lines: 7,
            content: String::new(),
            is_new_file: false,
        }];
        repo.branch_commits.insert("main".to_string(), test_oid(1));
        repo.branch_commits
            .insert("origin/main".to_string(), test_oid(1));
        repo.branch_commits
            .insert("feature".to_string(), test_oid(2));
        repo.commits_between = vec![target_commit];
        repo.blame_results.insert(
            "src/lib.rs".to_string(),
            vec![BlameResult {
                commit: target_commit,
                message: "Add feature".to_string(),
            }],
        );

        let state = MockState::default();

        let plan = create_absorb_plan(&repo, &state, "main").unwrap();

        assert_eq!(plan.actions.len(), 1);
        assert!(plan.unmapped.is_empty());
        assert_eq!(plan.actions[0].target_commit, target_commit);
        assert_eq!(plan.actions[0].target_message, "Add feature");
    }

    #[test]
    fn test_create_plan_multiple_commits_unmapped() {
        let commit1 = test_oid(3);
        let commit2 = test_oid(4);

        let mut repo = MockRepo::default();
        repo.hunks = vec![Hunk {
            file_path: "src/lib.rs".to_string(),
            old_start: 10,
            old_lines: 5,
            new_start: 10,
            new_lines: 7,
            content: String::new(),
            is_new_file: false,
        }];
        repo.branch_commits.insert("main".to_string(), test_oid(1));
        repo.branch_commits
            .insert("origin/main".to_string(), test_oid(1));
        repo.branch_commits
            .insert("feature".to_string(), test_oid(2));
        repo.commits_between = vec![commit1, commit2];
        repo.blame_results.insert(
            "src/lib.rs".to_string(),
            vec![
                BlameResult {
                    commit: commit1,
                    message: "First commit".to_string(),
                },
                BlameResult {
                    commit: commit2,
                    message: "Second commit".to_string(),
                },
            ],
        );

        let state = MockState::default();

        let plan = create_absorb_plan(&repo, &state, "main").unwrap();

        assert!(plan.actions.is_empty());
        assert_eq!(plan.unmapped.len(), 1);
        assert_eq!(plan.unmapped[0].reason, UnmapReason::MultipleCommits);
    }

    #[test]
    fn test_create_plan_commit_not_in_stack() {
        let target_commit = test_oid(99); // Not in commits_between

        let mut repo = MockRepo::default();
        repo.hunks = vec![Hunk {
            file_path: "src/lib.rs".to_string(),
            old_start: 10,
            old_lines: 5,
            new_start: 10,
            new_lines: 7,
            content: String::new(),
            is_new_file: false,
        }];
        repo.branch_commits.insert("main".to_string(), test_oid(1));
        repo.branch_commits
            .insert("origin/main".to_string(), test_oid(1));
        repo.branch_commits
            .insert("feature".to_string(), test_oid(2));
        repo.commits_between = vec![test_oid(3)]; // Different commit
        repo.blame_results.insert(
            "src/lib.rs".to_string(),
            vec![BlameResult {
                commit: target_commit,
                message: "Old commit".to_string(),
            }],
        );

        let state = MockState::default();

        let plan = create_absorb_plan(&repo, &state, "main").unwrap();

        assert!(plan.actions.is_empty());
        assert_eq!(plan.unmapped.len(), 1);
        assert_eq!(plan.unmapped[0].reason, UnmapReason::CommitNotInStack);
    }

    #[test]
    fn test_create_plan_commit_on_base_branch() {
        let base_commit = test_oid(1);
        let target_commit = test_oid(99);

        let mut repo = MockRepo::default();
        repo.hunks = vec![Hunk {
            file_path: "src/lib.rs".to_string(),
            old_start: 10,
            old_lines: 5,
            new_start: 10,
            new_lines: 7,
            content: String::new(),
            is_new_file: false,
        }];
        repo.branch_commits.insert("main".to_string(), base_commit);
        repo.branch_commits
            .insert("origin/main".to_string(), base_commit);
        repo.branch_commits
            .insert("feature".to_string(), test_oid(2));
        repo.commits_between = vec![test_oid(3)];
        repo.blame_results.insert(
            "src/lib.rs".to_string(),
            vec![BlameResult {
                commit: target_commit,
                message: "Base commit".to_string(),
            }],
        );
        // Mark target as ancestor of base (on base branch)
        repo.is_ancestor_results
            .insert((target_commit, base_commit), true);

        let state = MockState::default();

        let plan = create_absorb_plan(&repo, &state, "main").unwrap();

        assert!(plan.actions.is_empty());
        assert_eq!(plan.unmapped.len(), 1);
        assert_eq!(plan.unmapped[0].reason, UnmapReason::CommitOnBaseBranch);
    }

    #[test]
    fn test_create_plan_blame_error() {
        let mut repo = MockRepo::default();
        repo.hunks = vec![Hunk {
            file_path: "src/lib.rs".to_string(),
            old_start: 10,
            old_lines: 5,
            new_start: 10,
            new_lines: 7,
            content: String::new(),
            is_new_file: false,
        }];
        repo.branch_commits.insert("main".to_string(), test_oid(1));
        repo.branch_commits
            .insert("origin/main".to_string(), test_oid(1));
        repo.branch_commits
            .insert("feature".to_string(), test_oid(2));
        repo.blame_errors
            .insert("src/lib.rs".to_string(), "file not found".to_string());

        let state = MockState::default();

        let plan = create_absorb_plan(&repo, &state, "main").unwrap();

        assert!(plan.actions.is_empty());
        assert_eq!(plan.unmapped.len(), 1);
        match &plan.unmapped[0].reason {
            UnmapReason::BlameError(msg) => assert!(msg.contains("file not found")),
            _ => panic!("Expected BlameError"),
        }
    }

    #[test]
    fn test_create_plan_empty_blame_result() {
        let mut repo = MockRepo::default();
        repo.hunks = vec![Hunk {
            file_path: "src/lib.rs".to_string(),
            old_start: 10,
            old_lines: 5,
            new_start: 10,
            new_lines: 7,
            content: String::new(),
            is_new_file: false,
        }];
        repo.branch_commits.insert("main".to_string(), test_oid(1));
        repo.branch_commits
            .insert("origin/main".to_string(), test_oid(1));
        repo.branch_commits
            .insert("feature".to_string(), test_oid(2));
        repo.blame_results.insert("src/lib.rs".to_string(), vec![]); // Empty blame results

        let state = MockState::default();

        let plan = create_absorb_plan(&repo, &state, "main").unwrap();

        assert!(plan.actions.is_empty());
        assert_eq!(plan.unmapped.len(), 1);
        match &plan.unmapped[0].reason {
            UnmapReason::BlameError(msg) => assert!(msg.contains("no blame results")),
            _ => panic!("Expected BlameError with 'no blame results'"),
        }
    }

    #[test]
    fn test_create_plan_insert_only_hunk() {
        // Insert-only hunks have old_lines = 0
        // They should still work by blaming the adjacent line
        let target_commit = test_oid(3);

        let mut repo = MockRepo::default();
        repo.hunks = vec![Hunk {
            file_path: "src/lib.rs".to_string(),
            old_start: 10, // Line where insertion happens
            old_lines: 0,  // Insert-only: no lines deleted
            new_start: 10,
            new_lines: 5, // 5 new lines inserted
            content: String::new(),
            is_new_file: false,
        }];
        repo.branch_commits.insert("main".to_string(), test_oid(1));
        repo.branch_commits
            .insert("origin/main".to_string(), test_oid(1));
        repo.branch_commits
            .insert("feature".to_string(), test_oid(2));
        repo.commits_between = vec![target_commit];
        repo.blame_results.insert(
            "src/lib.rs".to_string(),
            vec![BlameResult {
                commit: target_commit,
                message: "Target commit".to_string(),
            }],
        );

        let state = MockState::default();

        let plan = create_absorb_plan(&repo, &state, "main").unwrap();

        // Insert-only hunks should be mappable if adjacent line points to valid target
        assert_eq!(plan.actions.len(), 1);
        assert!(plan.unmapped.is_empty());
        assert_eq!(plan.actions[0].target_commit, target_commit);
    }

    #[test]
    fn test_execute_absorb_empty_plan() {
        let repo = MockRepo::default();
        let plan = AbsorbPlan {
            actions: vec![],
            unmapped: vec![],
        };

        let result = execute_absorb(&repo, &plan).unwrap();

        assert_eq!(result.fixups_created, 0);
        assert!(result.targeted_commits.is_empty());
    }

    #[test]
    fn test_execute_absorb_single_target() {
        let target_commit = test_oid(3);
        let repo = MockRepo::default();

        let plan = AbsorbPlan {
            actions: vec![AbsorbAction {
                hunk: Hunk {
                    file_path: "src/lib.rs".to_string(),
                    old_start: 10,
                    old_lines: 5,
                    new_start: 10,
                    new_lines: 7,
                    content: String::new(),
                    is_new_file: false,
                },
                target_commit,
                target_message: "Feature commit".to_string(),
            }],
            unmapped: vec![],
        };

        let result = execute_absorb(&repo, &plan).unwrap();

        assert_eq!(result.fixups_created, 1);
        assert_eq!(result.targeted_commits.len(), 1);
        assert_eq!(result.targeted_commits[0], target_commit);

        // Verify fixup commit was created
        let created = repo.fixup_commits_created.borrow();
        assert_eq!(created.len(), 1);
        assert_eq!(created[0], target_commit);
    }

    #[test]
    fn test_execute_absorb_multiple_hunks_same_target() {
        let target_commit = test_oid(3);
        let repo = MockRepo::default();

        let plan = AbsorbPlan {
            actions: vec![
                AbsorbAction {
                    hunk: Hunk {
                        file_path: "src/lib.rs".to_string(),
                        old_start: 10,
                        old_lines: 5,
                        new_start: 10,
                        new_lines: 7,
                        content: String::new(),
                        is_new_file: false,
                    },
                    target_commit,
                    target_message: "Feature commit".to_string(),
                },
                AbsorbAction {
                    hunk: Hunk {
                        file_path: "src/main.rs".to_string(),
                        old_start: 20,
                        old_lines: 3,
                        new_start: 20,
                        new_lines: 5,
                        content: String::new(),
                        is_new_file: false,
                    },
                    target_commit,
                    target_message: "Feature commit".to_string(),
                },
            ],
            unmapped: vec![],
        };

        let result = execute_absorb(&repo, &plan).unwrap();

        // Multiple hunks targeting the same commit should create only one fixup
        assert_eq!(result.fixups_created, 1);
        assert_eq!(result.targeted_commits.len(), 1);
    }

    #[test]
    fn test_execute_absorb_multiple_targets_error() {
        let target1 = test_oid(3);
        let target2 = test_oid(4);
        let repo = MockRepo::default();

        let plan = AbsorbPlan {
            actions: vec![
                AbsorbAction {
                    hunk: Hunk {
                        file_path: "src/lib.rs".to_string(),
                        old_start: 10,
                        old_lines: 5,
                        new_start: 10,
                        new_lines: 7,
                        content: String::new(),
                        is_new_file: false,
                    },
                    target_commit: target1,
                    target_message: "First commit".to_string(),
                },
                AbsorbAction {
                    hunk: Hunk {
                        file_path: "src/main.rs".to_string(),
                        old_start: 20,
                        old_lines: 3,
                        new_start: 20,
                        new_lines: 5,
                        content: String::new(),
                        is_new_file: false,
                    },
                    target_commit: target2,
                    target_message: "Second commit".to_string(),
                },
            ],
            unmapped: vec![],
        };

        let result = execute_absorb(&repo, &plan);

        // Should error when hunks target different commits
        assert!(result.is_err());
        let err = result.unwrap_err();
        let err_msg = err.to_string();
        assert!(err_msg.contains("2 different commits"));
        assert!(err_msg.contains("selective hunk staging not supported"));
    }
}