@@ -478,44 +478,12 @@ fn compute_resolutions(
478
478
479
479
#[ cfg( test) ]
480
480
mod test {
481
- use std:: fs;
482
-
483
481
use gitbutler_branch:: BranchOwnershipClaims ;
484
- use tempfile :: tempdir ;
482
+ use gitbutler_testsupport :: testing_repository :: TestingRepository ;
485
483
use uuid:: Uuid ;
486
484
487
485
use super :: * ;
488
486
489
- fn commit_file < ' a > (
490
- repository : & ' a git2:: Repository ,
491
- parent : Option < & git2:: Commit > ,
492
- files : & [ ( & str , & str ) ] ,
493
- ) -> git2:: Commit < ' a > {
494
- for ( file_name, contents) in files {
495
- fs:: write ( repository. path ( ) . join ( ".." ) . join ( file_name) , contents) . unwrap ( ) ;
496
- }
497
- let mut index = repository. index ( ) . unwrap ( ) ;
498
- // Make sure we're not having weird cached state
499
- index. read ( true ) . unwrap ( ) ;
500
- index
501
- . add_all ( [ "*" ] , git2:: IndexAddOption :: DEFAULT , None )
502
- . unwrap ( ) ;
503
-
504
- let signature = git2
:: Signature :: now ( "Caleb" , "[email protected] " ) . unwrap ( ) ;
505
- let commit = repository
506
- . commit (
507
- None ,
508
- & signature,
509
- & signature,
510
- "Committee" ,
511
- & repository. find_tree ( index. write_tree ( ) . unwrap ( ) ) . unwrap ( ) ,
512
- parent. map ( |c| vec ! [ c] ) . unwrap_or_default ( ) . as_slice ( ) ,
513
- )
514
- . unwrap ( ) ;
515
-
516
- repository. find_commit ( commit) . unwrap ( )
517
- }
518
-
519
487
fn make_branch ( head : git2:: Oid , tree : git2:: Oid ) -> Branch {
520
488
Branch {
521
489
id : Uuid :: new_v4 ( ) . into ( ) ,
@@ -540,16 +508,15 @@ mod test {
540
508
541
509
#[ test]
542
510
fn test_up_to_date_if_head_commits_equivalent ( ) {
543
- let tempdir = tempdir ( ) . unwrap ( ) ;
544
- let repository = git2:: Repository :: init ( tempdir. path ( ) ) . unwrap ( ) ;
545
- let initial_commit = commit_file ( & repository, None , & [ ( "foo.txt" , "bar" ) ] ) ;
546
- let head_commit = commit_file ( & repository, Some ( & initial_commit) , & [ ( "foo.txt" , "baz" ) ] ) ;
511
+ let test_repository = TestingRepository :: open ( ) ;
512
+ let initial_commit = test_repository. commit_tree ( None , & [ ( "foo.txt" , "bar" ) ] ) ;
513
+ let head_commit = test_repository. commit_tree ( Some ( & initial_commit) , & [ ( "foo.txt" , "baz" ) ] ) ;
547
514
548
515
let context = UpstreamIntegrationContext {
549
516
_permission : None ,
550
517
old_target : head_commit. clone ( ) ,
551
518
new_target : head_commit,
552
- repository : & repository,
519
+ repository : & test_repository . repository ,
553
520
virtual_branches_in_workspace : vec ! [ ] ,
554
521
target_branch_name : "main" . to_string ( ) ,
555
522
} ;
@@ -562,17 +529,16 @@ mod test {
562
529
563
530
#[ test]
564
531
fn test_updates_required_if_new_head_ahead ( ) {
565
- let tempdir = tempdir ( ) . unwrap ( ) ;
566
- let repository = git2:: Repository :: init ( tempdir. path ( ) ) . unwrap ( ) ;
567
- let initial_commit = commit_file ( & repository, None , & [ ( "foo.txt" , "bar" ) ] ) ;
568
- let old_target = commit_file ( & repository, Some ( & initial_commit) , & [ ( "foo.txt" , "baz" ) ] ) ;
569
- let new_target = commit_file ( & repository, Some ( & old_target) , & [ ( "foo.txt" , "qux" ) ] ) ;
532
+ let test_repository = TestingRepository :: open ( ) ;
533
+ let initial_commit = test_repository. commit_tree ( None , & [ ( "foo.txt" , "bar" ) ] ) ;
534
+ let old_target = test_repository. commit_tree ( Some ( & initial_commit) , & [ ( "foo.txt" , "baz" ) ] ) ;
535
+ let new_target = test_repository. commit_tree ( Some ( & old_target) , & [ ( "foo.txt" , "qux" ) ] ) ;
570
536
571
537
let context = UpstreamIntegrationContext {
572
538
_permission : None ,
573
539
old_target,
574
540
new_target,
575
- repository : & repository,
541
+ repository : & test_repository . repository ,
576
542
virtual_branches_in_workspace : vec ! [ ] ,
577
543
target_branch_name : "main" . to_string ( ) ,
578
544
} ;
@@ -585,19 +551,18 @@ mod test {
585
551
586
552
#[ test]
587
553
fn test_empty_branch ( ) {
588
- let tempdir = tempdir ( ) . unwrap ( ) ;
589
- let repository = git2:: Repository :: init ( tempdir. path ( ) ) . unwrap ( ) ;
590
- let initial_commit = commit_file ( & repository, None , & [ ( "foo.txt" , "bar" ) ] ) ;
591
- let old_target = commit_file ( & repository, Some ( & initial_commit) , & [ ( "foo.txt" , "baz" ) ] ) ;
592
- let new_target = commit_file ( & repository, Some ( & old_target) , & [ ( "foo.txt" , "qux" ) ] ) ;
554
+ let test_repository = TestingRepository :: open ( ) ;
555
+ let initial_commit = test_repository. commit_tree ( None , & [ ( "foo.txt" , "bar" ) ] ) ;
556
+ let old_target = test_repository. commit_tree ( Some ( & initial_commit) , & [ ( "foo.txt" , "baz" ) ] ) ;
557
+ let new_target = test_repository. commit_tree ( Some ( & old_target) , & [ ( "foo.txt" , "qux" ) ] ) ;
593
558
594
559
let branch = make_branch ( old_target. id ( ) , old_target. tree_id ( ) ) ;
595
560
596
561
let context = UpstreamIntegrationContext {
597
562
_permission : None ,
598
563
old_target,
599
564
new_target,
600
- repository : & repository,
565
+ repository : & test_repository . repository ,
601
566
virtual_branches_in_workspace : vec ! [ branch. clone( ) ] ,
602
567
target_branch_name : "main" . to_string ( ) ,
603
568
} ;
@@ -610,22 +575,24 @@ mod test {
610
575
611
576
#[ test]
612
577
fn test_conflicted_head_branch ( ) {
613
- let tempdir = tempdir ( ) . unwrap ( ) ;
614
- let repository = git2:: Repository :: init ( tempdir. path ( ) ) . unwrap ( ) ;
615
- let initial_commit = commit_file ( & repository, None , & [ ( "foo.txt" , "bar" ) ] ) ;
578
+ let test_repository = TestingRepository :: open ( ) ;
579
+ let initial_commit = test_repository. commit_tree ( None , & [ ( "foo.txt" , "bar" ) ] ) ;
616
580
// Create refs/heads/master
617
- repository. branch ( "master" , & initial_commit, false ) . unwrap ( ) ;
618
- let old_target = commit_file ( & repository, Some ( & initial_commit) , & [ ( "foo.txt" , "baz" ) ] ) ;
619
- let branch_head = commit_file ( & repository, Some ( & old_target) , & [ ( "foo.txt" , "fux" ) ] ) ;
620
- let new_target = commit_file ( & repository, Some ( & old_target) , & [ ( "foo.txt" , "qux" ) ] ) ;
581
+ test_repository
582
+ . repository
583
+ . branch ( "master" , & initial_commit, false )
584
+ . unwrap ( ) ;
585
+ let old_target = test_repository. commit_tree ( Some ( & initial_commit) , & [ ( "foo.txt" , "baz" ) ] ) ;
586
+ let branch_head = test_repository. commit_tree ( Some ( & old_target) , & [ ( "foo.txt" , "fux" ) ] ) ;
587
+ let new_target = test_repository. commit_tree ( Some ( & old_target) , & [ ( "foo.txt" , "qux" ) ] ) ;
621
588
622
589
let branch = make_branch ( branch_head. id ( ) , branch_head. tree_id ( ) ) ;
623
590
624
591
let context = UpstreamIntegrationContext {
625
592
_permission : None ,
626
593
old_target,
627
594
new_target : new_target. clone ( ) ,
628
- repository : & repository,
595
+ repository : & test_repository . repository ,
629
596
virtual_branches_in_workspace : vec ! [ branch. clone( ) ] ,
630
597
target_branch_name : "main" . to_string ( ) ,
631
598
} ;
@@ -655,32 +622,32 @@ mod test {
655
622
panic ! ( "Should be variant UpdatedObjects" )
656
623
} ;
657
624
658
- let head_commit = repository. find_commit ( head) . unwrap ( ) ;
625
+ let head_commit = test_repository . repository . find_commit ( head) . unwrap ( ) ;
659
626
assert_eq ! ( head_commit. parent( 0 ) . unwrap( ) . id( ) , new_target. id( ) ) ;
660
627
assert ! ( head_commit. is_conflicted( ) ) ;
661
628
662
- let head_tree = repository
629
+ let head_tree = test_repository
630
+ . repository
663
631
. find_real_tree ( & head_commit, Default :: default ( ) )
664
632
. unwrap ( ) ;
665
633
assert_eq ! ( head_tree. id( ) , tree)
666
634
}
667
635
668
636
#[ test]
669
637
fn test_conflicted_tree_branch ( ) {
670
- let tempdir = tempdir ( ) . unwrap ( ) ;
671
- let repository = git2:: Repository :: init ( tempdir. path ( ) ) . unwrap ( ) ;
672
- let initial_commit = commit_file ( & repository, None , & [ ( "foo.txt" , "bar" ) ] ) ;
673
- let old_target = commit_file ( & repository, Some ( & initial_commit) , & [ ( "foo.txt" , "baz" ) ] ) ;
674
- let branch_head = commit_file ( & repository, Some ( & old_target) , & [ ( "foo.txt" , "fux" ) ] ) ;
675
- let new_target = commit_file ( & repository, Some ( & old_target) , & [ ( "foo.txt" , "qux" ) ] ) ;
638
+ let test_repository = TestingRepository :: open ( ) ;
639
+ let initial_commit = test_repository. commit_tree ( None , & [ ( "foo.txt" , "bar" ) ] ) ;
640
+ let old_target = test_repository. commit_tree ( Some ( & initial_commit) , & [ ( "foo.txt" , "baz" ) ] ) ;
641
+ let branch_head = test_repository. commit_tree ( Some ( & old_target) , & [ ( "foo.txt" , "fux" ) ] ) ;
642
+ let new_target = test_repository. commit_tree ( Some ( & old_target) , & [ ( "foo.txt" , "qux" ) ] ) ;
676
643
677
644
let branch = make_branch ( old_target. id ( ) , branch_head. tree_id ( ) ) ;
678
645
679
646
let context = UpstreamIntegrationContext {
680
647
_permission : None ,
681
648
old_target,
682
649
new_target,
683
- repository : & repository,
650
+ repository : & test_repository . repository ,
684
651
virtual_branches_in_workspace : vec ! [ branch. clone( ) ] ,
685
652
target_branch_name : "main" . to_string ( ) ,
686
653
} ;
@@ -698,21 +665,20 @@ mod test {
698
665
699
666
#[ test]
700
667
fn test_conflicted_head_and_tree_branch ( ) {
701
- let tempdir = tempdir ( ) . unwrap ( ) ;
702
- let repository = git2:: Repository :: init ( tempdir. path ( ) ) . unwrap ( ) ;
703
- let initial_commit = commit_file ( & repository, None , & [ ( "foo.txt" , "bar" ) ] ) ;
704
- let old_target = commit_file ( & repository, Some ( & initial_commit) , & [ ( "foo.txt" , "baz" ) ] ) ;
705
- let branch_head = commit_file ( & repository, Some ( & old_target) , & [ ( "foo.txt" , "fux" ) ] ) ;
706
- let branch_tree = commit_file ( & repository, Some ( & old_target) , & [ ( "foo.txt" , "bax" ) ] ) ;
707
- let new_target = commit_file ( & repository, Some ( & old_target) , & [ ( "foo.txt" , "qux" ) ] ) ;
668
+ let test_repository = TestingRepository :: open ( ) ;
669
+ let initial_commit = test_repository. commit_tree ( None , & [ ( "foo.txt" , "bar" ) ] ) ;
670
+ let old_target = test_repository. commit_tree ( Some ( & initial_commit) , & [ ( "foo.txt" , "baz" ) ] ) ;
671
+ let branch_head = test_repository. commit_tree ( Some ( & old_target) , & [ ( "foo.txt" , "fux" ) ] ) ;
672
+ let branch_tree = test_repository. commit_tree ( Some ( & old_target) , & [ ( "foo.txt" , "bax" ) ] ) ;
673
+ let new_target = test_repository. commit_tree ( Some ( & old_target) , & [ ( "foo.txt" , "qux" ) ] ) ;
708
674
709
675
let branch = make_branch ( branch_head. id ( ) , branch_tree. tree_id ( ) ) ;
710
676
711
677
let context = UpstreamIntegrationContext {
712
678
_permission : None ,
713
679
old_target,
714
680
new_target,
715
- repository : & repository,
681
+ repository : & test_repository . repository ,
716
682
virtual_branches_in_workspace : vec ! [ branch. clone( ) ] ,
717
683
target_branch_name : "main" . to_string ( ) ,
718
684
} ;
@@ -730,19 +696,18 @@ mod test {
730
696
731
697
#[ test]
732
698
fn test_integrated ( ) {
733
- let tempdir = tempdir ( ) . unwrap ( ) ;
734
- let repository = git2:: Repository :: init ( tempdir. path ( ) ) . unwrap ( ) ;
735
- let initial_commit = commit_file ( & repository, None , & [ ( "foo.txt" , "bar" ) ] ) ;
736
- let old_target = commit_file ( & repository, Some ( & initial_commit) , & [ ( "foo.txt" , "baz" ) ] ) ;
737
- let new_target = commit_file ( & repository, Some ( & old_target) , & [ ( "foo.txt" , "qux" ) ] ) ;
699
+ let test_repository = TestingRepository :: open ( ) ;
700
+ let initial_commit = test_repository. commit_tree ( None , & [ ( "foo.txt" , "bar" ) ] ) ;
701
+ let old_target = test_repository. commit_tree ( Some ( & initial_commit) , & [ ( "foo.txt" , "baz" ) ] ) ;
702
+ let new_target = test_repository. commit_tree ( Some ( & old_target) , & [ ( "foo.txt" , "qux" ) ] ) ;
738
703
739
704
let branch = make_branch ( new_target. id ( ) , new_target. tree_id ( ) ) ;
740
705
741
706
let context = UpstreamIntegrationContext {
742
707
_permission : None ,
743
708
old_target,
744
709
new_target,
745
- repository : & repository,
710
+ repository : & test_repository . repository ,
746
711
virtual_branches_in_workspace : vec ! [ branch. clone( ) ] ,
747
712
target_branch_name : "main" . to_string ( ) ,
748
713
} ;
@@ -755,33 +720,25 @@ mod test {
755
720
756
721
#[ test]
757
722
fn test_integrated_commit_with_uncommited_changes ( ) {
758
- let tempdir = tempdir ( ) . unwrap ( ) ;
759
- let repository = git2:: Repository :: init ( tempdir. path ( ) ) . unwrap ( ) ;
723
+ let test_repository = TestingRepository :: open ( ) ;
760
724
let initial_commit =
761
- commit_file ( & repository, None , & [ ( "foo.txt" , "bar" ) , ( "bar.txt" , "bar" ) ] ) ;
762
- let old_target = commit_file (
763
- & repository,
725
+ test_repository. commit_tree ( None , & [ ( "foo.txt" , "bar" ) , ( "bar.txt" , "bar" ) ] ) ;
726
+ let old_target = test_repository. commit_tree (
764
727
Some ( & initial_commit) ,
765
728
& [ ( "foo.txt" , "baz" ) , ( "bar.txt" , "bar" ) ] ,
766
729
) ;
767
- let new_target = commit_file (
768
- & repository,
769
- Some ( & old_target) ,
770
- & [ ( "foo.txt" , "qux" ) , ( "bar.txt" , "bar" ) ] ,
771
- ) ;
772
- let tree = commit_file (
773
- & repository,
774
- Some ( & old_target) ,
775
- & [ ( "foo.txt" , "baz" ) , ( "bar.txt" , "qux" ) ] ,
776
- ) ;
730
+ let new_target = test_repository
731
+ . commit_tree ( Some ( & old_target) , & [ ( "foo.txt" , "qux" ) , ( "bar.txt" , "bar" ) ] ) ;
732
+ let tree = test_repository
733
+ . commit_tree ( Some ( & old_target) , & [ ( "foo.txt" , "baz" ) , ( "bar.txt" , "qux" ) ] ) ;
777
734
778
735
let branch = make_branch ( new_target. id ( ) , tree. tree_id ( ) ) ;
779
736
780
737
let context = UpstreamIntegrationContext {
781
738
_permission : None ,
782
739
old_target,
783
740
new_target,
784
- repository : & repository,
741
+ repository : & test_repository . repository ,
785
742
virtual_branches_in_workspace : vec ! [ branch. clone( ) ] ,
786
743
target_branch_name : "main" . to_string ( ) ,
787
744
} ;
@@ -794,31 +751,23 @@ mod test {
794
751
795
752
#[ test]
796
753
fn test_safly_updatable ( ) {
797
- let tempdir = tempdir ( ) . unwrap ( ) ;
798
- let repository = git2:: Repository :: init ( tempdir. path ( ) ) . unwrap ( ) ;
799
- let initial_commit = commit_file (
800
- & repository,
801
- None ,
802
- & [ ( "files-one.txt" , "foo" ) , ( "file-two.txt" , "foo" ) ] ,
803
- ) ;
804
- let old_target = commit_file (
805
- & repository,
754
+ let test_repository = TestingRepository :: open ( ) ;
755
+ let initial_commit =
756
+ test_repository. commit_tree ( None , & [ ( "files-one.txt" , "foo" ) , ( "file-two.txt" , "foo" ) ] ) ;
757
+ let old_target = test_repository. commit_tree (
806
758
Some ( & initial_commit) ,
807
759
& [ ( "file-one.txt" , "bar" ) , ( "file-two.txt" , "foo" ) ] ,
808
760
) ;
809
- let new_target = commit_file (
810
- & repository,
761
+ let new_target = test_repository. commit_tree (
811
762
Some ( & old_target) ,
812
763
& [ ( "file-one.txt" , "baz" ) , ( "file-two.txt" , "foo" ) ] ,
813
764
) ;
814
765
815
- let branch_head = commit_file (
816
- & repository,
766
+ let branch_head = test_repository. commit_tree (
817
767
Some ( & old_target) ,
818
768
& [ ( "file-one.txt" , "bar" ) , ( "file-two.txt" , "bar" ) ] ,
819
769
) ;
820
- let branch_tree = commit_file (
821
- & repository,
770
+ let branch_tree = test_repository. commit_tree (
822
771
Some ( & branch_head) ,
823
772
& [ ( "file-one.txt" , "bar" ) , ( "file-two.txt" , "baz" ) ] ,
824
773
) ;
@@ -829,7 +778,7 @@ mod test {
829
778
_permission : None ,
830
779
old_target,
831
780
new_target,
832
- repository : & repository,
781
+ repository : & test_repository . repository ,
833
782
virtual_branches_in_workspace : vec ! [ branch. clone( ) ] ,
834
783
target_branch_name : "main" . to_string ( ) ,
835
784
} ;
0 commit comments