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
//! Sync engine for rebasing stack branches.
//!
//! This module contains the core logic for the `rung sync` command,
//! which recursively rebases all branches in a stack when the base moves.

mod execute;
mod plan;
mod predict;
mod reconcile;
mod types;
mod undo;

// Re-export all public types
pub use types::*;

// Re-export all public functions
pub use execute::{abort_sync, continue_sync, execute_sync};
pub use plan::create_sync_plan;
pub use predict::predict_sync_conflicts;
pub use reconcile::{reconcile_merged, remove_stale_branches};
pub use undo::undo_sync;

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;
    use crate::stack::{Stack, StackBranch};
    use crate::state::State;
    use std::fs;
    use tempfile::TempDir;

    /// Create a test repository with an initial commit
    fn init_test_repo() -> (TempDir, rung_git::Repository, git2::Repository) {
        let temp = TempDir::new().unwrap();
        let git_repo = git2::Repository::init(temp.path()).unwrap();

        // Create initial commit
        let sig = git2::Signature::now("Test", "test@example.com").unwrap();
        fs::write(temp.path().join("README.md"), "# Test").unwrap();

        let mut index = git_repo.index().unwrap();
        index.add_path(std::path::Path::new("README.md")).unwrap();
        index.write().unwrap();

        let tree_id = index.write_tree().unwrap();
        let tree = git_repo.find_tree(tree_id).unwrap();
        git_repo
            .commit(Some("HEAD"), &sig, &sig, "Initial commit", &tree, &[])
            .unwrap();
        drop(tree);

        let rung_repo = rung_git::Repository::open(temp.path()).unwrap();
        (temp, rung_repo, git_repo)
    }

    /// Add a commit to the current branch
    fn add_commit(temp: &TempDir, git_repo: &git2::Repository, filename: &str, message: &str) {
        let sig = git2::Signature::now("Test", "test@example.com").unwrap();
        fs::write(temp.path().join(filename), "content").unwrap();

        let mut index = git_repo.index().unwrap();
        index.add_path(std::path::Path::new(filename)).unwrap();
        index.write().unwrap();

        let tree_id = index.write_tree().unwrap();
        let tree = git_repo.find_tree(tree_id).unwrap();
        let parent = git_repo.head().unwrap().peel_to_commit().unwrap();

        git_repo
            .commit(Some("HEAD"), &sig, &sig, message, &tree, &[&parent])
            .unwrap();
    }

    #[test]
    fn test_sync_plan_empty_when_synced() {
        let (_temp, rung_repo, git_repo) = init_test_repo();

        // Get main branch name
        let main_branch = rung_repo.current_branch().unwrap();

        // Create feature branch at current HEAD
        let head = git_repo.head().unwrap().peel_to_commit().unwrap();
        git_repo.branch("feature-a", &head, false).unwrap();

        // Create stack with feature-a based on main
        let mut stack = Stack::new();
        stack.add_branch(StackBranch::try_new("feature-a", Some(main_branch.clone())).unwrap());

        // Plan should be empty - feature-a is at same commit as main
        let plan = create_sync_plan(&rung_repo, &stack, &main_branch).unwrap();
        assert!(plan.is_empty());
    }

    #[test]
    fn test_sync_plan_detects_divergence() {
        let (temp, rung_repo, git_repo) = init_test_repo();

        // Get main branch name
        let main_branch = rung_repo.current_branch().unwrap();

        // Create feature branch at current HEAD
        let head = git_repo.head().unwrap().peel_to_commit().unwrap();
        git_repo.branch("feature-a", &head, false).unwrap();

        // Add a commit to main (making feature-a diverge)
        add_commit(&temp, &git_repo, "main-update.txt", "Update main");

        // Create stack with feature-a based on main
        let mut stack = Stack::new();
        stack.add_branch(StackBranch::try_new("feature-a", Some(main_branch.clone())).unwrap());

        // Plan should have one action - rebase feature-a
        let plan = create_sync_plan(&rung_repo, &stack, &main_branch).unwrap();
        assert_eq!(plan.branches.len(), 1);
        assert_eq!(plan.branches[0].branch, "feature-a");
    }

    #[test]
    fn test_sync_plan_chain() {
        let (temp, rung_repo, git_repo) = init_test_repo();

        let main_branch = rung_repo.current_branch().unwrap();

        // Create feature-a at current HEAD
        let head = git_repo.head().unwrap().peel_to_commit().unwrap();
        git_repo.branch("feature-a", &head, false).unwrap();

        // Checkout feature-a and add a commit
        git_repo.set_head("refs/heads/feature-a").unwrap();
        git_repo
            .checkout_head(Some(git2::build::CheckoutBuilder::new().force()))
            .unwrap();
        add_commit(&temp, &git_repo, "feature-a.txt", "Feature A commit");

        // Create feature-b based on feature-a
        let head = git_repo.head().unwrap().peel_to_commit().unwrap();
        git_repo.branch("feature-b", &head, false).unwrap();

        // Go back to main and add a commit
        git_repo
            .set_head(&format!("refs/heads/{main_branch}"))
            .unwrap();
        git_repo
            .checkout_head(Some(git2::build::CheckoutBuilder::new().force()))
            .unwrap();
        add_commit(&temp, &git_repo, "main-update.txt", "Update main");

        // Create stack
        let mut stack = Stack::new();
        stack.add_branch(StackBranch::try_new("feature-a", Some(main_branch.clone())).unwrap());
        stack.add_branch(StackBranch::try_new("feature-b", Some("feature-a")).unwrap());

        // Plan should cascade: feature-a needs rebase, so feature-b is also included
        // This ensures one sync handles the entire stack
        let plan = create_sync_plan(&rung_repo, &stack, &main_branch).unwrap();
        assert_eq!(plan.branches.len(), 2);
        assert_eq!(plan.branches[0].branch, "feature-a");
        assert_eq!(plan.branches[1].branch, "feature-b");
    }

    #[test]
    fn test_sync_plan_cascade_deep_stack() {
        let (temp, rung_repo, git_repo) = init_test_repo();

        let main_branch = rung_repo.current_branch().unwrap();

        // Create a 4-branch deep stack: main → A → B → C → D
        let head = git_repo.head().unwrap().peel_to_commit().unwrap();
        git_repo.branch("feature-a", &head, false).unwrap();

        git_repo.set_head("refs/heads/feature-a").unwrap();
        git_repo
            .checkout_head(Some(git2::build::CheckoutBuilder::new().force()))
            .unwrap();
        add_commit(&temp, &git_repo, "a.txt", "A commit");

        let head = git_repo.head().unwrap().peel_to_commit().unwrap();
        git_repo.branch("feature-b", &head, false).unwrap();

        git_repo.set_head("refs/heads/feature-b").unwrap();
        git_repo
            .checkout_head(Some(git2::build::CheckoutBuilder::new().force()))
            .unwrap();
        add_commit(&temp, &git_repo, "b.txt", "B commit");

        let head = git_repo.head().unwrap().peel_to_commit().unwrap();
        git_repo.branch("feature-c", &head, false).unwrap();

        git_repo.set_head("refs/heads/feature-c").unwrap();
        git_repo
            .checkout_head(Some(git2::build::CheckoutBuilder::new().force()))
            .unwrap();
        add_commit(&temp, &git_repo, "c.txt", "C commit");

        let head = git_repo.head().unwrap().peel_to_commit().unwrap();
        git_repo.branch("feature-d", &head, false).unwrap();

        // Go back to main and add a commit (causes cascade)
        git_repo
            .set_head(&format!("refs/heads/{main_branch}"))
            .unwrap();
        git_repo
            .checkout_head(Some(git2::build::CheckoutBuilder::new().force()))
            .unwrap();
        add_commit(&temp, &git_repo, "main-update.txt", "Update main");

        // Create stack
        let mut stack = Stack::new();
        stack.add_branch(StackBranch::try_new("feature-a", Some(main_branch.clone())).unwrap());
        stack.add_branch(StackBranch::try_new("feature-b", Some("feature-a")).unwrap());
        stack.add_branch(StackBranch::try_new("feature-c", Some("feature-b")).unwrap());
        stack.add_branch(StackBranch::try_new("feature-d", Some("feature-c")).unwrap());

        // Plan should cascade through entire stack in one pass
        let plan = create_sync_plan(&rung_repo, &stack, &main_branch).unwrap();
        assert_eq!(plan.branches.len(), 4);
        assert_eq!(plan.branches[0].branch, "feature-a");
        assert_eq!(plan.branches[1].branch, "feature-b");
        assert_eq!(plan.branches[2].branch, "feature-c");
        assert_eq!(plan.branches[3].branch, "feature-d");
    }

    #[test]
    fn test_remove_stale_branches() {
        let (temp, rung_repo, git_repo) = init_test_repo();
        let state = State::new(temp.path()).unwrap();
        state.init().unwrap();

        let main_branch = rung_repo.current_branch().unwrap();

        // Create two branches
        let head = git_repo.head().unwrap().peel_to_commit().unwrap();
        git_repo.branch("feature-a", &head, false).unwrap();
        git_repo.branch("feature-b", &head, false).unwrap();

        // Create stack: main -> feature-a -> feature-b
        let mut stack = Stack::new();
        stack.add_branch(StackBranch::try_new("feature-a", Some(main_branch.clone())).unwrap());
        stack.add_branch(StackBranch::try_new("feature-b", Some("feature-a")).unwrap());
        state.save_stack(&stack).unwrap();

        // Delete feature-a git (making stale)
        rung_repo.delete_branch("feature-a").unwrap();

        // Run stale branch removal
        let result = remove_stale_branches(&rung_repo, &state).unwrap();

        assert_eq!(result.removed.len(), 1);
        assert_eq!(result.removed[0], "feature-a");

        // Verify stack was updated: feature-b should now point to main

        let updated_stack = state.load_stack().unwrap();
        assert_eq!(updated_stack.len(), 1);
        let b = updated_stack.find_branch("feature-b").unwrap();
        assert_eq!(b.parent.as_ref().unwrap().as_str(), main_branch.as_str());
    }

    #[test]
    fn test_execute_sync_with_conflict() {
        let (temp, rung_repo, git_repo) = init_test_repo();
        let state = State::new(temp.path()).unwrap();
        state.init().unwrap();

        let main_branch = rung_repo.current_branch().unwrap();

        // Setup a conflict: modify same line in the main feature-a
        fs::write(temp.path().join("conflict.txt"), "Original\n").unwrap();
        add_commit(&temp, &git_repo, "conflict.txt", "Initial");

        let head = git_repo.head().unwrap().peel_to_commit().unwrap();
        git_repo.branch("feature-a", &head, false).unwrap();

        // Change on main
        {
            let sig = git2::Signature::now("Test", "test@example.com").unwrap();
            fs::write(temp.path().join("conflict.txt"), "Main content\n").unwrap(); // UNIQUE
            let mut index = git_repo.index().unwrap();
            index
                .add_path(std::path::Path::new("conflict.txt"))
                .unwrap();
            index.write().unwrap();
            let tree_id = index.write_tree().unwrap();
            let tree = git_repo.find_tree(tree_id).unwrap();
            let parent = git_repo.head().unwrap().peel_to_commit().unwrap();
            git_repo
                .commit(Some("HEAD"), &sig, &sig, "Main change", &tree, &[&parent])
                .unwrap();
        }

        // Change on feature-a
        git_repo.set_head("refs/heads/feature-a").unwrap();

        git_repo
            .checkout_head(Some(git2::build::CheckoutBuilder::new().force()))
            .unwrap();
        {
            let sig = git2::Signature::now("Test", "test@example.com").unwrap();
            fs::write(temp.path().join("conflict.txt"), "Feature content\n").unwrap(); // UNIQUE
            let mut index = git_repo.index().unwrap();
            index
                .add_path(std::path::Path::new("conflict.txt"))
                .unwrap();
            index.write().unwrap();
            let tree_id = index.write_tree().unwrap();
            let tree = git_repo.find_tree(tree_id).unwrap();
            let parent = git_repo.head().unwrap().peel_to_commit().unwrap();
            git_repo
                .commit(
                    Some("HEAD"),
                    &sig,
                    &sig,
                    "Feature-a change",
                    &tree,
                    &[&parent],
                )
                .unwrap();
        }

        // Create stack
        let mut stack = Stack::new();
        stack.add_branch(StackBranch::try_new("feature-a", Some(main_branch.clone())).unwrap());
        state.save_stack(&stack).unwrap();

        // Plan sync
        let plan = create_sync_plan(&rung_repo, &stack, &main_branch).unwrap();

        // Execute sync - should be paused by conflict
        let result = execute_sync(&rung_repo, &state, plan).unwrap();
        match result {
            SyncResult::Paused {
                at_branch,
                conflict_files,
                ..
            } => {
                assert_eq!(at_branch, "feature-a");
                assert!(conflict_files.contains(&"conflict.txt".to_string()));
            }
            _ => panic!("Expected sync to be paused by conflict, but got {result:?}"),
        }

        assert!(state.is_sync_in_progress());
    }

    #[test]
    fn test_reconcile_merged_empty() {
        let (temp, _rung_repo, _git_repo) = init_test_repo();
        let state = State::new(temp.path()).unwrap();
        state.init().unwrap();

        // Empty merged list should return empty result
        let result = reconcile_merged(&state, &[]).unwrap();
        assert!(result.merged.is_empty());
        assert!(result.reparented.is_empty());
        assert!(result.repaired.is_empty());
    }

    #[test]
    fn test_reconcile_merged_with_children() {
        let (temp, rung_repo, git_repo) = init_test_repo();
        let state = State::new(temp.path()).unwrap();
        state.init().unwrap();

        let main_branch = rung_repo.current_branch().unwrap();

        // Create feature-a at current HEAD
        let head = git_repo.head().unwrap().peel_to_commit().unwrap();
        git_repo.branch("feature-a", &head, false).unwrap();

        // Create stack: main -> feature-a -> feature-b
        let mut stack = Stack::new();
        stack.add_branch(StackBranch::try_new("feature-a", Some(main_branch.as_str())).unwrap());
        let mut branch_b = StackBranch::try_new("feature-b", Some("feature-a")).unwrap();
        branch_b.pr = Some(456);
        stack.add_branch(branch_b);
        state.save_stack(&stack).unwrap();

        // Simulate feature-a being merged into main
        let merged_prs = vec![ExternalMergeInfo {
            branch_name: "feature-a".to_string(),
            pr_number: 123,
            merged_into: main_branch.clone(),
        }];

        let result = reconcile_merged(&state, &merged_prs).unwrap();

        // feature-a should be in merged list
        assert_eq!(result.merged.len(), 1);
        assert_eq!(result.merged[0].name, "feature-a");
        assert_eq!(result.merged[0].pr_number, 123);

        // feature-b should be reparented to main
        assert_eq!(result.reparented.len(), 1);
        assert_eq!(result.reparented[0].name, "feature-b");
        assert_eq!(result.reparented[0].old_parent, "feature-a");
        assert_eq!(result.reparented[0].new_parent, main_branch.as_str());
        assert_eq!(result.reparented[0].pr_number, Some(456));

        // Verify stack was updated
        let updated_stack = state.load_stack().unwrap();
        assert!(updated_stack.find_branch("feature-a").is_none());
        let b = updated_stack.find_branch("feature-b").unwrap();
        assert_eq!(b.parent.as_ref().unwrap().as_str(), main_branch.as_str());
    }

    #[test]
    fn test_undo_sync() {
        let (temp, rung_repo, git_repo) = init_test_repo();
        let state = State::new(temp.path()).unwrap();
        state.init().unwrap();

        let main_branch = rung_repo.current_branch().unwrap();

        // Create feature-a at current HEAD
        let head = git_repo.head().unwrap().peel_to_commit().unwrap();
        git_repo.branch("feature-a", &head, false).unwrap();

        // Get the original SHA of feature-a
        let _original_sha = head.id().to_string();

        // Checkout feature-a and add a commit
        git_repo.set_head("refs/heads/feature-a").unwrap();
        git_repo
            .checkout_head(Some(git2::build::CheckoutBuilder::new().force()))
            .unwrap();
        add_commit(&temp, &git_repo, "feature-a.txt", "Feature A commit");

        // Get the new SHA
        let new_sha = git_repo
            .head()
            .unwrap()
            .peel_to_commit()
            .unwrap()
            .id()
            .to_string();

        // Create a backup (simulating what sync does)
        let backup_refs = vec![("feature-a", new_sha.as_str())];
        let backup_id = state.create_backup(&backup_refs).unwrap();

        // Modify the backup to point to original SHA (simulating pre-sync state)
        // Actually, let's just test that undo_sync works with a valid backup
        // by creating a backup with current state and verifying it can be restored

        // Create stack
        let mut stack = Stack::new();
        stack.add_branch(StackBranch::try_new("feature-a", Some(main_branch)).unwrap());
        state.save_stack(&stack).unwrap();

        // Now undo should restore from the backup
        let result = undo_sync(&rung_repo, &state).unwrap();

        assert_eq!(result.branches_restored, 1);
        assert_eq!(result.backup_id, backup_id);
    }

    #[test]
    fn test_sync_plan_base_branch_not_found() {
        let (_temp, rung_repo, git_repo) = init_test_repo();

        let _main_branch = rung_repo.current_branch().unwrap();

        // Create feature-a
        let head = git_repo.head().unwrap().peel_to_commit().unwrap();
        git_repo.branch("feature-a", &head, false).unwrap();

        // Create stack with feature-a pointing to non-existent base
        let mut stack = Stack::new();
        stack.add_branch(StackBranch::try_new("feature-a", None::<&str>).unwrap());

        // Plan with non-existent base branch should error
        let result = create_sync_plan(&rung_repo, &stack, "nonexistent-branch");
        assert!(result.is_err());
    }

    #[test]
    fn test_sync_plan_skips_stale_branches() {
        let (_temp, rung_repo, git_repo) = init_test_repo();

        let main_branch = rung_repo.current_branch().unwrap();

        // Create only feature-a in git
        let head = git_repo.head().unwrap().peel_to_commit().unwrap();
        git_repo.branch("feature-a", &head, false).unwrap();

        // But stack has both feature-a and feature-b (stale)
        let mut stack = Stack::new();
        stack.add_branch(StackBranch::try_new("feature-a", Some(main_branch.clone())).unwrap());
        stack.add_branch(StackBranch::try_new("feature-b", Some("feature-a")).unwrap());

        // Plan should only include feature-a (feature-b is stale)
        let plan = create_sync_plan(&rung_repo, &stack, &main_branch).unwrap();
        // feature-a is at same commit as main, so plan is empty
        // but importantly, it didn't error on the stale feature-b
        assert!(plan.is_empty());
    }

    #[test]
    fn test_sync_plan_skips_branch_with_stale_parent() {
        let (_temp, rung_repo, git_repo) = init_test_repo();

        let main_branch = rung_repo.current_branch().unwrap();

        // Create only feature-b in git (feature-a is stale/deleted)
        let head = git_repo.head().unwrap().peel_to_commit().unwrap();
        git_repo.branch("feature-b", &head, false).unwrap();

        // Stack has feature-a -> feature-b, but feature-a doesn't exist in git
        let mut stack = Stack::new();
        stack.add_branch(StackBranch::try_new("feature-a", Some(main_branch.clone())).unwrap());
        stack.add_branch(StackBranch::try_new("feature-b", Some("feature-a")).unwrap());

        // Plan should skip feature-b because its parent (feature-a) doesn't exist
        // This hits line 52 in plan.rs: continue when parent is a stale stack branch
        let plan = create_sync_plan(&rung_repo, &stack, &main_branch).unwrap();
        assert!(
            plan.is_empty(),
            "Plan should be empty - both branches are effectively stale"
        );
    }

    #[test]
    fn test_abort_sync_restores_branches() {
        let (temp, rung_repo, git_repo) = init_test_repo();
        let state = State::new(temp.path()).unwrap();
        state.init().unwrap();

        let main_branch = rung_repo.current_branch().unwrap();

        // Setup a conflict scenario
        fs::write(temp.path().join("conflict.txt"), "Original\n").unwrap();
        add_commit(&temp, &git_repo, "conflict.txt", "Initial");

        let head = git_repo.head().unwrap().peel_to_commit().unwrap();
        git_repo.branch("feature-a", &head, false).unwrap();

        // Record feature-a's original commit
        let feature_a_original = git_repo
            .find_branch("feature-a", git2::BranchType::Local)
            .unwrap()
            .get()
            .peel_to_commit()
            .unwrap()
            .id();

        // Change on main
        {
            let sig = git2::Signature::now("Test", "test@example.com").unwrap();
            fs::write(temp.path().join("conflict.txt"), "Main content\n").unwrap();
            let mut index = git_repo.index().unwrap();
            index
                .add_path(std::path::Path::new("conflict.txt"))
                .unwrap();
            index.write().unwrap();
            let tree_id = index.write_tree().unwrap();
            let tree = git_repo.find_tree(tree_id).unwrap();
            let parent = git_repo.head().unwrap().peel_to_commit().unwrap();
            git_repo
                .commit(Some("HEAD"), &sig, &sig, "Main change", &tree, &[&parent])
                .unwrap();
        }

        // Change on feature-a
        git_repo.set_head("refs/heads/feature-a").unwrap();
        git_repo
            .checkout_head(Some(git2::build::CheckoutBuilder::new().force()))
            .unwrap();
        {
            let sig = git2::Signature::now("Test", "test@example.com").unwrap();
            fs::write(temp.path().join("conflict.txt"), "Feature content\n").unwrap();
            let mut index = git_repo.index().unwrap();
            index
                .add_path(std::path::Path::new("conflict.txt"))
                .unwrap();
            index.write().unwrap();
            let tree_id = index.write_tree().unwrap();
            let tree = git_repo.find_tree(tree_id).unwrap();
            let parent = git_repo.head().unwrap().peel_to_commit().unwrap();
            git_repo
                .commit(
                    Some("HEAD"),
                    &sig,
                    &sig,
                    "Feature-a change",
                    &tree,
                    &[&parent],
                )
                .unwrap();
        }

        // Record feature-a's commit before sync (with the new change)
        let feature_a_before_sync = git_repo
            .find_branch("feature-a", git2::BranchType::Local)
            .unwrap()
            .get()
            .peel_to_commit()
            .unwrap()
            .id();

        // Create stack
        let mut stack = Stack::new();
        stack.add_branch(StackBranch::try_new("feature-a", Some(main_branch.clone())).unwrap());
        state.save_stack(&stack).unwrap();

        // Execute sync - should be paused by conflict
        let plan = create_sync_plan(&rung_repo, &stack, &main_branch).unwrap();
        let result = execute_sync(&rung_repo, &state, plan).unwrap();
        assert!(matches!(result, SyncResult::Paused { .. }));
        assert!(state.is_sync_in_progress());

        // Now abort the sync
        abort_sync(&rung_repo, &state).unwrap();

        // Verify sync state is cleared
        assert!(!state.is_sync_in_progress());

        // Verify feature-a is restored to its pre-sync commit
        let feature_a_after_abort = git_repo
            .find_branch("feature-a", git2::BranchType::Local)
            .unwrap()
            .get()
            .peel_to_commit()
            .unwrap()
            .id();

        assert_eq!(
            feature_a_before_sync, feature_a_after_abort,
            "feature-a should be restored to its pre-sync state after abort"
        );
        assert_ne!(
            feature_a_original, feature_a_after_abort,
            "feature-a should have the new commit, not the original"
        );
    }
}