-
Notifications
You must be signed in to change notification settings - Fork 349
/
test_git.rs
1560 lines (1441 loc) · 54.4 KB
/
test_git.rs
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
// Copyright 2020 The Jujutsu Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::path::PathBuf;
use std::sync::Arc;
use git2::Oid;
use itertools::Itertools;
use jujutsu_lib::backend::{CommitId, ObjectId};
use jujutsu_lib::commit::Commit;
use jujutsu_lib::git;
use jujutsu_lib::git::{GitFetchError, GitPushError, GitRefUpdate};
use jujutsu_lib::git_backend::GitBackend;
use jujutsu_lib::op_store::{BranchTarget, RefTarget};
use jujutsu_lib::repo::{ReadonlyRepo, Repo};
use jujutsu_lib::settings::{GitSettings, UserSettings};
use maplit::{btreemap, hashset};
use tempfile::TempDir;
use testutils::{create_random_commit, write_random_commit, TestRepo};
fn empty_git_commit<'r>(
git_repo: &'r git2::Repository,
ref_name: &str,
parents: &[&git2::Commit],
) -> git2::Commit<'r> {
let signature = git2::Signature::now("Someone", "[email protected]").unwrap();
let empty_tree_id = Oid::from_str("4b825dc642cb6eb9a060e54bf8d69288fbee4904").unwrap();
let empty_tree = git_repo.find_tree(empty_tree_id).unwrap();
let oid = git_repo
.commit(
Some(ref_name),
&signature,
&signature,
&format!("random commit {}", rand::random::<u32>()),
&empty_tree,
parents,
)
.unwrap();
git_repo.find_commit(oid).unwrap()
}
fn jj_id(commit: &git2::Commit) -> CommitId {
CommitId::from_bytes(commit.id().as_bytes())
}
fn git_id(commit: &Commit) -> Oid {
Oid::from_bytes(commit.id().as_bytes()).unwrap()
}
#[test]
fn test_import_refs() {
let settings = testutils::user_settings();
let git_settings = GitSettings::default();
let test_repo = TestRepo::init(true);
let repo = &test_repo.repo;
let git_repo = repo.store().git_repo().unwrap();
let commit1 = empty_git_commit(&git_repo, "refs/heads/main", &[]);
git_ref(&git_repo, "refs/remotes/origin/main", commit1.id());
let commit2 = empty_git_commit(&git_repo, "refs/heads/main", &[&commit1]);
let commit3 = empty_git_commit(&git_repo, "refs/heads/feature1", &[&commit2]);
let commit4 = empty_git_commit(&git_repo, "refs/heads/feature2", &[&commit2]);
let commit5 = empty_git_commit(&git_repo, "refs/tags/v1.0", &[&commit1]);
let commit6 = empty_git_commit(&git_repo, "refs/remotes/origin/feature3", &[&commit1]);
// Should not be imported
empty_git_commit(&git_repo, "refs/notes/x", &[&commit2]);
empty_git_commit(&git_repo, "refs/remotes/origin/HEAD", &[&commit2]);
git_repo.set_head("refs/heads/main").unwrap();
let git_repo = repo.store().git_repo().unwrap();
let mut tx = repo.start_transaction(&settings, "test");
git::import_refs(tx.mut_repo(), &git_repo, &git_settings).unwrap();
tx.mut_repo().rebase_descendants(&settings).unwrap();
let repo = tx.commit();
let view = repo.view();
let expected_heads = hashset! {
jj_id(&commit3),
jj_id(&commit4),
jj_id(&commit5),
jj_id(&commit6)
};
assert_eq!(*view.heads(), expected_heads);
let expected_main_branch = BranchTarget {
local_target: Some(RefTarget::Normal(jj_id(&commit2))),
remote_targets: btreemap! {
"origin".to_string() => RefTarget::Normal(jj_id(&commit1)),
},
};
assert_eq!(
view.branches().get("main"),
Some(expected_main_branch).as_ref()
);
let expected_feature1_branch = BranchTarget {
local_target: Some(RefTarget::Normal(jj_id(&commit3))),
remote_targets: btreemap! {},
};
assert_eq!(
view.branches().get("feature1"),
Some(expected_feature1_branch).as_ref()
);
let expected_feature2_branch = BranchTarget {
local_target: Some(RefTarget::Normal(jj_id(&commit4))),
remote_targets: btreemap! {},
};
assert_eq!(
view.branches().get("feature2"),
Some(expected_feature2_branch).as_ref()
);
let expected_feature3_branch = BranchTarget {
local_target: Some(RefTarget::Normal(jj_id(&commit6))),
remote_targets: btreemap! {
"origin".to_string() => RefTarget::Normal(jj_id(&commit6)),
},
};
assert_eq!(
view.branches().get("feature3"),
Some(expected_feature3_branch).as_ref()
);
assert_eq!(
view.tags().get("v1.0"),
Some(RefTarget::Normal(jj_id(&commit5))).as_ref()
);
assert_eq!(view.git_refs().len(), 6);
assert_eq!(
view.git_refs().get("refs/heads/main"),
Some(RefTarget::Normal(jj_id(&commit2))).as_ref()
);
assert_eq!(
view.git_refs().get("refs/heads/feature1"),
Some(RefTarget::Normal(jj_id(&commit3))).as_ref()
);
assert_eq!(
view.git_refs().get("refs/heads/feature2"),
Some(RefTarget::Normal(jj_id(&commit4))).as_ref()
);
assert_eq!(
view.git_refs().get("refs/remotes/origin/main"),
Some(RefTarget::Normal(jj_id(&commit1))).as_ref()
);
assert_eq!(
view.git_refs().get("refs/remotes/origin/feature3"),
Some(RefTarget::Normal(jj_id(&commit6))).as_ref()
);
assert_eq!(
view.git_refs().get("refs/tags/v1.0"),
Some(RefTarget::Normal(jj_id(&commit5))).as_ref()
);
assert_eq!(view.git_head(), Some(&RefTarget::Normal(jj_id(&commit2))));
}
#[test]
fn test_import_refs_reimport() {
let settings = testutils::user_settings();
let git_settings = GitSettings::default();
let test_workspace = TestRepo::init(true);
let repo = &test_workspace.repo;
let git_repo = repo.store().git_repo().unwrap();
let commit1 = empty_git_commit(&git_repo, "refs/heads/main", &[]);
git_ref(&git_repo, "refs/remotes/origin/main", commit1.id());
let commit2 = empty_git_commit(&git_repo, "refs/heads/main", &[&commit1]);
let _commit3 = empty_git_commit(&git_repo, "refs/heads/feature1", &[&commit2]);
let commit4 = empty_git_commit(&git_repo, "refs/heads/feature2", &[&commit2]);
let pgp_key_oid = git_repo.blob(b"my PGP key").unwrap();
git_repo
.reference("refs/tags/my-gpg-key", pgp_key_oid, false, "")
.unwrap();
let mut tx = repo.start_transaction(&settings, "test");
git::import_refs(tx.mut_repo(), &git_repo, &git_settings).unwrap();
tx.mut_repo().rebase_descendants(&settings).unwrap();
let repo = tx.commit();
// Delete feature1 and rewrite feature2
delete_git_ref(&git_repo, "refs/heads/feature1");
delete_git_ref(&git_repo, "refs/heads/feature2");
let commit5 = empty_git_commit(&git_repo, "refs/heads/feature2", &[&commit2]);
// Also modify feature2 on the jj side
let mut tx = repo.start_transaction(&settings, "test");
let commit6 = create_random_commit(tx.mut_repo(), &settings)
.set_parents(vec![jj_id(&commit2)])
.write()
.unwrap();
tx.mut_repo().set_local_branch(
"feature2".to_string(),
RefTarget::Normal(commit6.id().clone()),
);
let repo = tx.commit();
let mut tx = repo.start_transaction(&settings, "test");
git::import_refs(tx.mut_repo(), &git_repo, &git_settings).unwrap();
tx.mut_repo().rebase_descendants(&settings).unwrap();
let repo = tx.commit();
let view = repo.view();
let expected_heads = hashset! {
jj_id(&commit5),
commit6.id().clone(),
};
assert_eq!(*view.heads(), expected_heads);
assert_eq!(view.branches().len(), 2);
let commit1_target = RefTarget::Normal(jj_id(&commit1));
let commit2_target = RefTarget::Normal(jj_id(&commit2));
let expected_main_branch = BranchTarget {
local_target: Some(RefTarget::Normal(jj_id(&commit2))),
remote_targets: btreemap! {
"origin".to_string() => commit1_target.clone(),
},
};
assert_eq!(
view.branches().get("main"),
Some(expected_main_branch).as_ref()
);
let expected_feature2_branch = BranchTarget {
local_target: Some(RefTarget::Conflict {
removes: vec![jj_id(&commit4)],
adds: vec![commit6.id().clone(), jj_id(&commit5)],
}),
remote_targets: btreemap! {},
};
assert_eq!(
view.branches().get("feature2"),
Some(expected_feature2_branch).as_ref()
);
assert!(view.tags().is_empty());
assert_eq!(view.git_refs().len(), 3);
assert_eq!(
view.git_refs().get("refs/heads/main"),
Some(commit2_target).as_ref()
);
assert_eq!(
view.git_refs().get("refs/remotes/origin/main"),
Some(commit1_target).as_ref()
);
let commit5_target = RefTarget::Normal(jj_id(&commit5));
assert_eq!(
view.git_refs().get("refs/heads/feature2"),
Some(commit5_target).as_ref()
);
}
#[test]
fn test_import_refs_reimport_head_removed() {
// Test that re-importing refs doesn't cause a deleted head to come back
let settings = testutils::user_settings();
let git_settings = GitSettings::default();
let test_repo = TestRepo::init(true);
let repo = &test_repo.repo;
let git_repo = repo.store().git_repo().unwrap();
let commit = empty_git_commit(&git_repo, "refs/heads/main", &[]);
let mut tx = repo.start_transaction(&settings, "test");
git::import_refs(tx.mut_repo(), &git_repo, &git_settings).unwrap();
tx.mut_repo().rebase_descendants(&settings).unwrap();
let commit_id = jj_id(&commit);
// Test the setup
assert!(tx.mut_repo().view().heads().contains(&commit_id));
// Remove the head and re-import
tx.mut_repo().remove_head(&commit_id);
git::import_refs(tx.mut_repo(), &git_repo, &git_settings).unwrap();
tx.mut_repo().rebase_descendants(&settings).unwrap();
assert!(!tx.mut_repo().view().heads().contains(&commit_id));
}
#[test]
fn test_import_refs_reimport_git_head_counts() {
// Test that if a branch is removed but the Git HEAD points to the commit (or a
// descendant of it), we still keep it alive.
let settings = testutils::user_settings();
let git_settings = GitSettings::default();
let test_repo = TestRepo::init(true);
let repo = &test_repo.repo;
let git_repo = repo.store().git_repo().unwrap();
let commit = empty_git_commit(&git_repo, "refs/heads/main", &[]);
git_repo.set_head_detached(commit.id()).unwrap();
let mut tx = repo.start_transaction(&settings, "test");
git::import_refs(tx.mut_repo(), &git_repo, &git_settings).unwrap();
tx.mut_repo().rebase_descendants(&settings).unwrap();
// Delete the branch and re-import. The commit should still be there since HEAD
// points to it
git_repo
.find_reference("refs/heads/main")
.unwrap()
.delete()
.unwrap();
git::import_refs(tx.mut_repo(), &git_repo, &git_settings).unwrap();
tx.mut_repo().rebase_descendants(&settings).unwrap();
assert!(tx.mut_repo().view().heads().contains(&jj_id(&commit)));
}
#[test]
fn test_import_refs_reimport_all_from_root_removed() {
// Test that if a chain of commits all the way from the root gets unreferenced,
// we abandon the whole stack, but not including the root commit.
let settings = testutils::user_settings();
let git_settings = GitSettings::default();
let test_repo = TestRepo::init(true);
let repo = &test_repo.repo;
let git_repo = repo.store().git_repo().unwrap();
let commit = empty_git_commit(&git_repo, "refs/heads/main", &[]);
let mut tx = repo.start_transaction(&settings, "test");
git::import_refs(tx.mut_repo(), &git_repo, &git_settings).unwrap();
tx.mut_repo().rebase_descendants(&settings).unwrap();
// Test the setup
assert!(tx.mut_repo().view().heads().contains(&jj_id(&commit)));
// Remove all git refs and re-import
git_repo
.find_reference("refs/heads/main")
.unwrap()
.delete()
.unwrap();
git::import_refs(tx.mut_repo(), &git_repo, &git_settings).unwrap();
tx.mut_repo().rebase_descendants(&settings).unwrap();
assert!(!tx.mut_repo().view().heads().contains(&jj_id(&commit)));
}
#[test]
fn test_import_some_refs() {
let settings = testutils::user_settings();
let git_settings = GitSettings::default();
let test_workspace = TestRepo::init(true);
let repo = &test_workspace.repo;
let git_repo = repo.store().git_repo().unwrap();
let commit_main = empty_git_commit(&git_repo, "refs/remotes/origin/main", &[]);
let commit_feat1 = empty_git_commit(&git_repo, "refs/remotes/origin/feature1", &[&commit_main]);
let commit_feat2 =
empty_git_commit(&git_repo, "refs/remotes/origin/feature2", &[&commit_feat1]);
let commit_feat3 =
empty_git_commit(&git_repo, "refs/remotes/origin/feature3", &[&commit_feat1]);
let commit_feat4 =
empty_git_commit(&git_repo, "refs/remotes/origin/feature4", &[&commit_feat3]);
let commit_ign = empty_git_commit(&git_repo, "refs/remotes/origin/ignored", &[]);
// Import branches feature1, feature2, and feature3.
let mut tx = repo.start_transaction(&settings, "test");
git::import_some_refs(tx.mut_repo(), &git_repo, &git_settings, |ref_name| {
ref_name.starts_with("refs/remotes/origin/feature")
})
.unwrap();
tx.mut_repo().rebase_descendants(&settings).unwrap();
let repo = tx.commit();
// There are two heads, feature2 and feature4.
let view = repo.view();
let expected_heads = hashset! {
jj_id(&commit_feat2),
jj_id(&commit_feat4),
};
assert_eq!(*view.heads(), expected_heads);
// Check that branches feature[1-4] have been locally imported and are known to
// be present on origin as well.
assert_eq!(view.branches().len(), 4);
let commit_feat1_target = RefTarget::Normal(jj_id(&commit_feat1));
let commit_feat2_target = RefTarget::Normal(jj_id(&commit_feat2));
let commit_feat3_target = RefTarget::Normal(jj_id(&commit_feat3));
let commit_feat4_target = RefTarget::Normal(jj_id(&commit_feat4));
let expected_feature1_branch = BranchTarget {
local_target: Some(RefTarget::Normal(jj_id(&commit_feat1))),
remote_targets: btreemap! { "origin".to_string() => commit_feat1_target },
};
assert_eq!(
view.branches().get("feature1"),
Some(expected_feature1_branch).as_ref()
);
let expected_feature2_branch = BranchTarget {
local_target: Some(RefTarget::Normal(jj_id(&commit_feat2))),
remote_targets: btreemap! { "origin".to_string() => commit_feat2_target },
};
assert_eq!(
view.branches().get("feature2"),
Some(expected_feature2_branch).as_ref()
);
let expected_feature3_branch = BranchTarget {
local_target: Some(RefTarget::Normal(jj_id(&commit_feat3))),
remote_targets: btreemap! { "origin".to_string() => commit_feat3_target },
};
assert_eq!(
view.branches().get("feature3"),
Some(expected_feature3_branch).as_ref()
);
let expected_feature4_branch = BranchTarget {
local_target: Some(RefTarget::Normal(jj_id(&commit_feat4))),
remote_targets: btreemap! { "origin".to_string() => commit_feat4_target },
};
assert_eq!(
view.branches().get("feature4"),
Some(expected_feature4_branch).as_ref()
);
assert_eq!(view.branches().get("main"), None,);
assert!(!view.heads().contains(&jj_id(&commit_main)));
assert_eq!(view.branches().get("ignored"), None,);
assert!(!view.heads().contains(&jj_id(&commit_ign)));
// Delete branch feature1, feature3 and feature4 in git repository and import
// branch feature2 only. That should have no impact on the jj repository.
delete_git_ref(&git_repo, "refs/remotes/origin/feature1");
delete_git_ref(&git_repo, "refs/remotes/origin/feature3");
delete_git_ref(&git_repo, "refs/remotes/origin/feature4");
let mut tx = repo.start_transaction(&settings, "test");
git::import_some_refs(tx.mut_repo(), &git_repo, &git_settings, |ref_name| {
ref_name == "refs/remotes/origin/feature2"
})
.unwrap();
tx.mut_repo().rebase_descendants(&settings).unwrap();
let repo = tx.commit();
// feature2 and feature4 will still be heads, and all four branches should be
// present.
let view = repo.view();
assert_eq!(view.branches().len(), 4);
assert_eq!(*view.heads(), expected_heads);
// Import feature1: this should cause the branch to be deleted, but the
// corresponding commit should stay because it is reachable from feature2.
let mut tx = repo.start_transaction(&settings, "test");
git::import_some_refs(tx.mut_repo(), &git_repo, &git_settings, |ref_name| {
ref_name == "refs/remotes/origin/feature1"
})
.unwrap();
// No descendant should be rewritten.
assert_eq!(tx.mut_repo().rebase_descendants(&settings).unwrap(), 0);
let repo = tx.commit();
// feature2 and feature4 should still be the heads, and all three branches
// feature2, feature3, and feature3 should exist.
let view = repo.view();
assert_eq!(view.branches().len(), 3);
assert_eq!(*view.heads(), expected_heads);
// Import feature3: this should cause the branch to be deleted, but
// feature4 should be left alone even though it is no longer in git.
let mut tx = repo.start_transaction(&settings, "test");
git::import_some_refs(tx.mut_repo(), &git_repo, &git_settings, |ref_name| {
ref_name == "refs/remotes/origin/feature3"
})
.unwrap();
// No descendant should be rewritten
assert_eq!(tx.mut_repo().rebase_descendants(&settings).unwrap(), 0);
let repo = tx.commit();
// feature2 and feature4 should still be the heads, and both branches
// should exist.
let view = repo.view();
assert_eq!(view.branches().len(), 2);
assert_eq!(*view.heads(), expected_heads);
// Import feature4: both the head and the branch will disappear.
let mut tx = repo.start_transaction(&settings, "test");
git::import_some_refs(tx.mut_repo(), &git_repo, &git_settings, |ref_name| {
ref_name == "refs/remotes/origin/feature4"
})
.unwrap();
// No descendant should be rewritten
assert_eq!(tx.mut_repo().rebase_descendants(&settings).unwrap(), 0);
let repo = tx.commit();
// feature2 should now be the only head and only branch.
let view = repo.view();
assert_eq!(view.branches().len(), 1);
let expected_heads = hashset! {
jj_id(&commit_feat2),
};
assert_eq!(*view.heads(), expected_heads);
}
fn git_ref(git_repo: &git2::Repository, name: &str, target: Oid) {
git_repo.reference(name, target, true, "").unwrap();
}
fn delete_git_ref(git_repo: &git2::Repository, name: &str) {
git_repo.find_reference(name).unwrap().delete().unwrap();
}
struct GitRepoData {
settings: UserSettings,
_temp_dir: TempDir,
origin_repo: git2::Repository,
git_repo: git2::Repository,
repo: Arc<ReadonlyRepo>,
}
impl GitRepoData {
fn create() -> Self {
let settings = testutils::user_settings();
let temp_dir = testutils::new_temp_dir();
let origin_repo_dir = temp_dir.path().join("source");
let origin_repo = git2::Repository::init_bare(&origin_repo_dir).unwrap();
let git_repo_dir = temp_dir.path().join("git");
let git_repo =
git2::Repository::clone(origin_repo_dir.to_str().unwrap(), &git_repo_dir).unwrap();
let jj_repo_dir = temp_dir.path().join("jj");
std::fs::create_dir(&jj_repo_dir).unwrap();
let repo = ReadonlyRepo::init(
&settings,
&jj_repo_dir,
|store_path| Box::new(GitBackend::init_external(store_path, &git_repo_dir)),
ReadonlyRepo::default_op_store_factory(),
ReadonlyRepo::default_op_heads_store_factory(),
ReadonlyRepo::default_index_store_factory(),
)
.unwrap();
Self {
settings,
_temp_dir: temp_dir,
origin_repo,
git_repo,
repo,
}
}
}
#[test]
fn test_import_refs_empty_git_repo() {
let test_data = GitRepoData::create();
let git_settings = GitSettings::default();
let heads_before = test_data.repo.view().heads().clone();
let mut tx = test_data
.repo
.start_transaction(&test_data.settings, "test");
git::import_refs(tx.mut_repo(), &test_data.git_repo, &git_settings).unwrap();
tx.mut_repo()
.rebase_descendants(&test_data.settings)
.unwrap();
let repo = tx.commit();
assert_eq!(*repo.view().heads(), heads_before);
assert_eq!(repo.view().branches().len(), 0);
assert_eq!(repo.view().tags().len(), 0);
assert_eq!(repo.view().git_refs().len(), 0);
assert_eq!(repo.view().git_head(), None);
}
#[test]
fn test_import_refs_detached_head() {
let test_data = GitRepoData::create();
let git_settings = GitSettings::default();
let commit1 = empty_git_commit(&test_data.git_repo, "refs/heads/main", &[]);
// Delete the reference. Check that the detached HEAD commit still gets added to
// the set of heads
test_data
.git_repo
.find_reference("refs/heads/main")
.unwrap()
.delete()
.unwrap();
test_data.git_repo.set_head_detached(commit1.id()).unwrap();
let mut tx = test_data
.repo
.start_transaction(&test_data.settings, "test");
git::import_refs(tx.mut_repo(), &test_data.git_repo, &git_settings).unwrap();
tx.mut_repo()
.rebase_descendants(&test_data.settings)
.unwrap();
let repo = tx.commit();
let expected_heads = hashset! { jj_id(&commit1) };
assert_eq!(*repo.view().heads(), expected_heads);
assert_eq!(repo.view().git_refs().len(), 0);
assert_eq!(
repo.view().git_head(),
Some(&RefTarget::Normal(jj_id(&commit1)))
);
}
#[test]
fn test_export_refs_no_detach() {
// When exporting the branch that's current checked out, don't detach HEAD if
// the target already matches
let test_data = GitRepoData::create();
let git_settings = GitSettings::default();
let git_repo = test_data.git_repo;
let commit1 = empty_git_commit(&git_repo, "refs/heads/main", &[]);
git_repo.set_head("refs/heads/main").unwrap();
let mut tx = test_data
.repo
.start_transaction(&test_data.settings, "test");
let mut_repo = tx.mut_repo();
git::import_refs(mut_repo, &git_repo, &git_settings).unwrap();
mut_repo.rebase_descendants(&test_data.settings).unwrap();
// Do an initial export to make sure `main` is considered
assert_eq!(git::export_refs(mut_repo, &git_repo), Ok(vec![]));
assert_eq!(
mut_repo.get_git_ref("refs/heads/main"),
Some(RefTarget::Normal(jj_id(&commit1)))
);
assert_eq!(git_repo.head().unwrap().name(), Some("refs/heads/main"));
assert_eq!(
git_repo.find_reference("refs/heads/main").unwrap().target(),
Some(commit1.id())
);
}
#[test]
fn test_export_refs_branch_changed() {
// We can export a change to a branch
let test_data = GitRepoData::create();
let git_settings = GitSettings::default();
let git_repo = test_data.git_repo;
let commit = empty_git_commit(&git_repo, "refs/heads/main", &[]);
git_repo
.reference("refs/heads/feature", commit.id(), false, "test")
.unwrap();
git_repo.set_head("refs/heads/feature").unwrap();
let mut tx = test_data
.repo
.start_transaction(&test_data.settings, "test");
let mut_repo = tx.mut_repo();
git::import_refs(mut_repo, &git_repo, &git_settings).unwrap();
mut_repo.rebase_descendants(&test_data.settings).unwrap();
assert_eq!(git::export_refs(mut_repo, &git_repo), Ok(vec![]));
let new_commit = create_random_commit(mut_repo, &test_data.settings)
.set_parents(vec![jj_id(&commit)])
.write()
.unwrap();
mut_repo.set_local_branch(
"main".to_string(),
RefTarget::Normal(new_commit.id().clone()),
);
assert_eq!(git::export_refs(mut_repo, &git_repo), Ok(vec![]));
assert_eq!(
mut_repo.get_git_ref("refs/heads/main"),
Some(RefTarget::Normal(new_commit.id().clone()))
);
assert_eq!(
git_repo
.find_reference("refs/heads/main")
.unwrap()
.peel_to_commit()
.unwrap()
.id(),
git_id(&new_commit)
);
// HEAD should be unchanged since its target branch didn't change
assert_eq!(git_repo.head().unwrap().name(), Some("refs/heads/feature"));
}
#[test]
fn test_export_refs_current_branch_changed() {
// If we update a branch that is checked out in the git repo, HEAD gets detached
let test_data = GitRepoData::create();
let git_settings = GitSettings::default();
let git_repo = test_data.git_repo;
let commit1 = empty_git_commit(&git_repo, "refs/heads/main", &[]);
git_repo.set_head("refs/heads/main").unwrap();
let mut tx = test_data
.repo
.start_transaction(&test_data.settings, "test");
let mut_repo = tx.mut_repo();
git::import_refs(mut_repo, &git_repo, &git_settings).unwrap();
mut_repo.rebase_descendants(&test_data.settings).unwrap();
assert_eq!(git::export_refs(mut_repo, &git_repo), Ok(vec![]));
let new_commit = create_random_commit(mut_repo, &test_data.settings)
.set_parents(vec![jj_id(&commit1)])
.write()
.unwrap();
mut_repo.set_local_branch(
"main".to_string(),
RefTarget::Normal(new_commit.id().clone()),
);
assert_eq!(git::export_refs(mut_repo, &git_repo), Ok(vec![]));
assert_eq!(
mut_repo.get_git_ref("refs/heads/main"),
Some(RefTarget::Normal(new_commit.id().clone()))
);
assert_eq!(
git_repo
.find_reference("refs/heads/main")
.unwrap()
.peel_to_commit()
.unwrap()
.id(),
git_id(&new_commit)
);
assert!(git_repo.head_detached().unwrap());
}
#[test]
fn test_export_refs_unborn_git_branch() {
// Can export to an empty Git repo (we can handle Git's "unborn branch" state)
let test_data = GitRepoData::create();
let git_settings = GitSettings::default();
let git_repo = test_data.git_repo;
git_repo.set_head("refs/heads/main").unwrap();
let mut tx = test_data
.repo
.start_transaction(&test_data.settings, "test");
let mut_repo = tx.mut_repo();
git::import_refs(mut_repo, &git_repo, &git_settings).unwrap();
mut_repo.rebase_descendants(&test_data.settings).unwrap();
assert_eq!(git::export_refs(mut_repo, &git_repo), Ok(vec![]));
let new_commit = write_random_commit(mut_repo, &test_data.settings);
mut_repo.set_local_branch(
"main".to_string(),
RefTarget::Normal(new_commit.id().clone()),
);
assert_eq!(git::export_refs(mut_repo, &git_repo), Ok(vec![]));
assert_eq!(
mut_repo.get_git_ref("refs/heads/main"),
Some(RefTarget::Normal(new_commit.id().clone()))
);
assert_eq!(
git_repo
.find_reference("refs/heads/main")
.unwrap()
.peel_to_commit()
.unwrap()
.id(),
git_id(&new_commit)
);
// It's weird that the head is still pointing to refs/heads/main, but
// it doesn't seem that Git lets you be on an "unborn branch" while
// also being in a "detached HEAD" state.
assert!(!git_repo.head_detached().unwrap());
}
#[test]
fn test_export_import_sequence() {
// Import a branch pointing to A, modify it in jj to point to B, export it,
// modify it in git to point to C, then import it again. There should be no
// conflict.
let test_data = GitRepoData::create();
let git_settings = GitSettings::default();
let git_repo = test_data.git_repo;
let mut tx = test_data
.repo
.start_transaction(&test_data.settings, "test");
let mut_repo = tx.mut_repo();
let commit_a = write_random_commit(mut_repo, &test_data.settings);
let commit_b = write_random_commit(mut_repo, &test_data.settings);
let commit_c = write_random_commit(mut_repo, &test_data.settings);
// Import the branch pointing to A
git_repo
.reference("refs/heads/main", git_id(&commit_a), true, "test")
.unwrap();
git::import_refs(mut_repo, &git_repo, &git_settings).unwrap();
assert_eq!(
mut_repo.get_git_ref("refs/heads/main"),
Some(RefTarget::Normal(commit_a.id().clone()))
);
// Modify the branch in jj to point to B
mut_repo.set_local_branch("main".to_string(), RefTarget::Normal(commit_b.id().clone()));
// Export the branch to git
assert_eq!(git::export_refs(mut_repo, &git_repo), Ok(vec![]));
assert_eq!(
mut_repo.get_git_ref("refs/heads/main"),
Some(RefTarget::Normal(commit_b.id().clone()))
);
// Modify the branch in git to point to C
git_repo
.reference("refs/heads/main", git_id(&commit_c), true, "test")
.unwrap();
// Import from git
git::import_refs(mut_repo, &git_repo, &git_settings).unwrap();
assert_eq!(
mut_repo.get_git_ref("refs/heads/main"),
Some(RefTarget::Normal(commit_c.id().clone()))
);
assert_eq!(
mut_repo.view().get_local_branch("main"),
Some(RefTarget::Normal(commit_c.id().clone()))
);
}
#[test]
fn test_import_export_no_auto_local_branch() {
// Import a remote tracking branch and export it. We should not create a git
// branch.
let test_data = GitRepoData::create();
let git_settings = GitSettings {
auto_local_branch: false,
};
let git_repo = test_data.git_repo;
let git_commit = empty_git_commit(&git_repo, "refs/remotes/origin/main", &[]);
let mut tx = test_data
.repo
.start_transaction(&test_data.settings, "test");
let mut_repo = tx.mut_repo();
git::import_refs(mut_repo, &git_repo, &git_settings).unwrap();
let expected_branch = BranchTarget {
local_target: None,
remote_targets: btreemap! {
"origin".to_string() => RefTarget::Normal(jj_id(&git_commit))
},
};
assert_eq!(
mut_repo.view().branches().get("main"),
Some(expected_branch).as_ref()
);
assert_eq!(
mut_repo.get_git_ref("refs/remotes/origin/main"),
Some(RefTarget::Normal(jj_id(&git_commit)))
);
// Export the branch to git
assert_eq!(git::export_refs(mut_repo, &git_repo), Ok(vec![]));
assert_eq!(mut_repo.get_git_ref("refs/heads/main"), None);
}
#[test]
fn test_export_conflicts() {
// We skip export of conflicted branches
let test_data = GitRepoData::create();
let git_repo = test_data.git_repo;
let mut tx = test_data
.repo
.start_transaction(&test_data.settings, "test");
let mut_repo = tx.mut_repo();
let commit_a = write_random_commit(mut_repo, &test_data.settings);
let commit_b = write_random_commit(mut_repo, &test_data.settings);
let commit_c = write_random_commit(mut_repo, &test_data.settings);
mut_repo.set_local_branch("main".to_string(), RefTarget::Normal(commit_a.id().clone()));
mut_repo.set_local_branch(
"feature".to_string(),
RefTarget::Normal(commit_a.id().clone()),
);
assert_eq!(git::export_refs(mut_repo, &git_repo), Ok(vec![]));
// Create a conflict and export. It should not be exported, but other changes
// should be.
mut_repo.set_local_branch("main".to_string(), RefTarget::Normal(commit_b.id().clone()));
mut_repo.set_local_branch(
"feature".to_string(),
RefTarget::Conflict {
removes: vec![commit_a.id().clone()],
adds: vec![commit_b.id().clone(), commit_c.id().clone()],
},
);
assert_eq!(git::export_refs(mut_repo, &git_repo), Ok(vec![]));
assert_eq!(
git_repo
.find_reference("refs/heads/feature")
.unwrap()
.target()
.unwrap(),
git_id(&commit_a)
);
assert_eq!(
git_repo
.find_reference("refs/heads/main")
.unwrap()
.target()
.unwrap(),
git_id(&commit_b)
);
}
#[test]
fn test_export_partial_failure() {
// Check that we skip branches that fail to export
let test_data = GitRepoData::create();
let git_repo = test_data.git_repo;
let mut tx = test_data
.repo
.start_transaction(&test_data.settings, "test");
let mut_repo = tx.mut_repo();
let commit_a = write_random_commit(mut_repo, &test_data.settings);
let target = RefTarget::Normal(commit_a.id().clone());
// Empty string is disallowed by Git
mut_repo.set_local_branch("".to_string(), target.clone());
mut_repo.set_local_branch("main".to_string(), target.clone());
// `main/sub` will conflict with `main` in Git, at least when using loose ref
// storage
mut_repo.set_local_branch("main/sub".to_string(), target);
assert_eq!(
git::export_refs(mut_repo, &git_repo),
Ok(vec!["".to_string(), "main/sub".to_string()])
);
// The `main` branch should have succeeded but the other should have failed
assert!(git_repo.find_reference("refs/heads/").is_err());
assert_eq!(
git_repo
.find_reference("refs/heads/main")
.unwrap()
.target()
.unwrap(),
git_id(&commit_a)
);
assert!(git_repo.find_reference("refs/heads/main/sub").is_err());
// Now remove the `main` branch and make sure that the `main/sub` gets exported
// even though it didn't change
mut_repo.remove_local_branch("main");
assert_eq!(
git::export_refs(mut_repo, &git_repo),
Ok(vec!["".to_string()])
);
assert!(git_repo.find_reference("refs/heads/").is_err());
assert!(git_repo.find_reference("refs/heads/main").is_err());
assert_eq!(
git_repo
.find_reference("refs/heads/main/sub")
.unwrap()
.target()
.unwrap(),
git_id(&commit_a)
);
}
#[test]
fn test_export_reexport_transitions() {
// Test exporting after making changes on the jj side, or the git side, or both
let test_data = GitRepoData::create();
let git_repo = test_data.git_repo;
let mut tx = test_data
.repo
.start_transaction(&test_data.settings, "test");
let mut_repo = tx.mut_repo();
let commit_a = write_random_commit(mut_repo, &test_data.settings);
let commit_b = write_random_commit(mut_repo, &test_data.settings);
let commit_c = write_random_commit(mut_repo, &test_data.settings);
// Create a few branches whose names indicate how they change in jj in git. The
// first letter represents the branch's target in the last export. The second
// letter represents the branch's target in jj. The third letter represents the
// branch's target in git. "X" means that the branch doesn't exist. "A", "B", or
// "C" means that the branch points to that commit.
//
// AAB: Branch modified in git
// AAX: Branch deleted in git
// ABA: Branch modified in jj
// ABB: Branch modified in both jj and git, pointing to same target
// ABC: Branch modified in both jj and git, pointing to different targets
// ABX: Branch modified in jj, deleted in git
// AXA: Branch deleted in jj
// AXB: Branch deleted in jj, modified in git
// AXX: Branch deleted in both jj and git
// XAA: Branch added in both jj and git, pointing to same target
// XAB: Branch added in both jj and git, pointing to different targets
// XAX: Branch added in jj
// XXA: Branch added in git
// Create initial state and export it
for branch in [
"AAB", "AAX", "ABA", "ABB", "ABC", "ABX", "AXA", "AXB", "AXX",
] {
mut_repo.set_local_branch(branch.to_string(), RefTarget::Normal(commit_a.id().clone()));
}
assert_eq!(git::export_refs(mut_repo, &git_repo), Ok(vec![]));
// Make changes on the jj side
for branch in ["AXA", "AXB", "AXX"] {
mut_repo.remove_local_branch(branch);
}
for branch in ["XAA", "XAB", "XAX"] {
mut_repo.set_local_branch(branch.to_string(), RefTarget::Normal(commit_a.id().clone()));
}
for branch in ["ABA", "ABB", "ABC", "ABX"] {
mut_repo.set_local_branch(branch.to_string(), RefTarget::Normal(commit_b.id().clone()));
}
// Make changes on the git side
for branch in ["AAX", "ABX", "AXX"] {
git_repo
.find_reference(&format!("refs/heads/{branch}"))
.unwrap()
.delete()
.unwrap();
}
for branch in ["XAA", "XXA"] {