File: test_view.rs

package info (click to toggle)
jujutsu 0.30.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 9,756 kB
  • sloc: sh: 283; makefile: 31
file content (619 lines) | stat: -rw-r--r-- 22,125 bytes parent folder | download
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
// 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::collections::BTreeMap;

use jj_lib::op_store::BookmarkTarget;
use jj_lib::op_store::RefTarget;
use jj_lib::op_store::RemoteRef;
use jj_lib::op_store::RemoteRefState;
use jj_lib::ref_name::RefName;
use jj_lib::ref_name::RemoteName;
use jj_lib::ref_name::RemoteRefSymbol;
use jj_lib::ref_name::WorkspaceNameBuf;
use jj_lib::repo::Repo as _;
use maplit::btreemap;
use maplit::hashset;
use test_case::test_case;
use testutils::commit_transactions;
use testutils::create_random_commit;
use testutils::write_random_commit;
use testutils::CommitGraphBuilder;
use testutils::TestRepo;

fn remote_symbol<'a, N, M>(name: &'a N, remote: &'a M) -> RemoteRefSymbol<'a>
where
    N: AsRef<RefName> + ?Sized,
    M: AsRef<RemoteName> + ?Sized,
{
    RemoteRefSymbol {
        name: name.as_ref(),
        remote: remote.as_ref(),
    }
}

#[test]
fn test_heads_empty() {
    let test_repo = TestRepo::init();
    let repo = &test_repo.repo;

    assert_eq!(
        *repo.view().heads(),
        hashset! {repo.store().root_commit_id().clone()}
    );
}

#[test]
fn test_heads_fork() {
    let test_repo = TestRepo::init();
    let repo = &test_repo.repo;
    let mut tx = repo.start_transaction();

    let mut graph_builder = CommitGraphBuilder::new(tx.repo_mut());
    let initial = graph_builder.initial_commit();
    let child1 = graph_builder.commit_with_parents(&[&initial]);
    let child2 = graph_builder.commit_with_parents(&[&initial]);
    let repo = tx.commit("test").unwrap();

    assert_eq!(
        *repo.view().heads(),
        hashset! {
            child1.id().clone(),
            child2.id().clone(),
        }
    );
}

#[test]
fn test_heads_merge() {
    let test_repo = TestRepo::init();
    let repo = &test_repo.repo;
    let mut tx = repo.start_transaction();

    let mut graph_builder = CommitGraphBuilder::new(tx.repo_mut());
    let initial = graph_builder.initial_commit();
    let child1 = graph_builder.commit_with_parents(&[&initial]);
    let child2 = graph_builder.commit_with_parents(&[&initial]);
    let merge = graph_builder.commit_with_parents(&[&child1, &child2]);
    let repo = tx.commit("test").unwrap();

    assert_eq!(*repo.view().heads(), hashset! {merge.id().clone()});
}

#[test]
fn test_merge_views_heads() {
    // Tests merging of the view's heads (by performing divergent operations).
    let test_repo = TestRepo::init();
    let repo = &test_repo.repo;

    let mut tx = repo.start_transaction();
    let mut_repo = tx.repo_mut();
    let head_unchanged = write_random_commit(mut_repo);
    let head_remove_tx1 = write_random_commit(mut_repo);
    let head_remove_tx2 = write_random_commit(mut_repo);
    let repo = tx.commit("test").unwrap();

    let mut tx1 = repo.start_transaction();
    tx1.repo_mut().remove_head(head_remove_tx1.id());
    let head_add_tx1 = write_random_commit(tx1.repo_mut());

    let mut tx2 = repo.start_transaction();
    tx2.repo_mut().remove_head(head_remove_tx2.id());
    let head_add_tx2 = write_random_commit(tx2.repo_mut());

    let repo = commit_transactions(vec![tx1, tx2]);

    let expected_heads = hashset! {
        head_unchanged.id().clone(),
        head_add_tx1.id().clone(),
        head_add_tx2.id().clone(),
    };
    assert_eq!(repo.view().heads(), &expected_heads);
}

#[test]
fn test_merge_views_checkout() {
    // Tests merging of the view's checkout (by performing divergent operations).
    let test_repo = TestRepo::init();
    let repo = &test_repo.repo;

    // Workspace 1 gets updated in both transactions.
    // Workspace 2 gets updated only in tx1.
    // Workspace 3 gets updated only in tx2.
    // Workspace 4 gets deleted in tx1 and modified in tx2.
    // Workspace 5 gets deleted in tx2 and modified in tx1.
    // Workspace 6 gets added in tx1.
    // Workspace 7 gets added in tx2.
    let mut initial_tx = repo.start_transaction();
    let commit1 = write_random_commit(initial_tx.repo_mut());
    let commit2 = write_random_commit(initial_tx.repo_mut());
    let commit3 = write_random_commit(initial_tx.repo_mut());
    let ws1_name = WorkspaceNameBuf::from("ws1");
    let ws2_name = WorkspaceNameBuf::from("ws2");
    let ws3_name = WorkspaceNameBuf::from("ws3");
    let ws4_name = WorkspaceNameBuf::from("ws4");
    let ws5_name = WorkspaceNameBuf::from("ws5");
    let ws6_name = WorkspaceNameBuf::from("ws6");
    let ws7_name = WorkspaceNameBuf::from("ws7");
    initial_tx
        .repo_mut()
        .set_wc_commit(ws1_name.clone(), commit1.id().clone())
        .unwrap();
    initial_tx
        .repo_mut()
        .set_wc_commit(ws2_name.clone(), commit1.id().clone())
        .unwrap();
    initial_tx
        .repo_mut()
        .set_wc_commit(ws3_name.clone(), commit1.id().clone())
        .unwrap();
    initial_tx
        .repo_mut()
        .set_wc_commit(ws4_name.clone(), commit1.id().clone())
        .unwrap();
    initial_tx
        .repo_mut()
        .set_wc_commit(ws5_name.clone(), commit1.id().clone())
        .unwrap();
    let repo = initial_tx.commit("test").unwrap();

    let mut tx1 = repo.start_transaction();
    tx1.repo_mut()
        .set_wc_commit(ws1_name.clone(), commit2.id().clone())
        .unwrap();
    tx1.repo_mut()
        .set_wc_commit(ws2_name.clone(), commit2.id().clone())
        .unwrap();
    tx1.repo_mut().remove_wc_commit(&ws4_name).unwrap();
    tx1.repo_mut()
        .set_wc_commit(ws5_name.clone(), commit2.id().clone())
        .unwrap();
    tx1.repo_mut()
        .set_wc_commit(ws6_name.clone(), commit2.id().clone())
        .unwrap();

    let mut tx2 = repo.start_transaction();
    tx2.repo_mut()
        .set_wc_commit(ws1_name.clone(), commit3.id().clone())
        .unwrap();
    tx2.repo_mut()
        .set_wc_commit(ws3_name.clone(), commit3.id().clone())
        .unwrap();
    tx2.repo_mut()
        .set_wc_commit(ws4_name.clone(), commit3.id().clone())
        .unwrap();
    tx2.repo_mut().remove_wc_commit(&ws5_name).unwrap();
    tx2.repo_mut()
        .set_wc_commit(ws7_name.clone(), commit3.id().clone())
        .unwrap();

    let repo = commit_transactions(vec![tx1, tx2]);

    // We currently arbitrarily pick the first transaction's working-copy commit
    // (first by transaction end time).
    assert_eq!(repo.view().get_wc_commit_id(&ws1_name), Some(commit2.id()));
    assert_eq!(repo.view().get_wc_commit_id(&ws2_name), Some(commit2.id()));
    assert_eq!(repo.view().get_wc_commit_id(&ws3_name), Some(commit3.id()));
    assert_eq!(repo.view().get_wc_commit_id(&ws4_name), None);
    assert_eq!(repo.view().get_wc_commit_id(&ws5_name), None);
    assert_eq!(repo.view().get_wc_commit_id(&ws6_name), Some(commit2.id()));
    assert_eq!(repo.view().get_wc_commit_id(&ws7_name), Some(commit3.id()));
}

#[test]
fn test_merge_views_bookmarks() {
    // Tests merging of bookmarks (by performing concurrent operations). See
    // test_refs.rs for tests of merging of individual ref targets.
    let test_repo = TestRepo::init();
    let repo = &test_repo.repo;

    let mut tx = repo.start_transaction();
    let mut_repo = tx.repo_mut();
    let main_bookmark_local_tx0 = write_random_commit(mut_repo);
    let main_bookmark_origin_tx0 = write_random_commit(mut_repo);
    let main_bookmark_alternate_tx0 = write_random_commit(mut_repo);
    let main_bookmark_origin_tx0_remote_ref = RemoteRef {
        target: RefTarget::normal(main_bookmark_origin_tx0.id().clone()),
        state: RemoteRefState::New,
    };
    let main_bookmark_alternate_tx0_remote_ref = RemoteRef {
        target: RefTarget::normal(main_bookmark_alternate_tx0.id().clone()),
        state: RemoteRefState::Tracked,
    };
    mut_repo.set_local_bookmark_target(
        "main".as_ref(),
        RefTarget::normal(main_bookmark_local_tx0.id().clone()),
    );
    mut_repo.set_remote_bookmark(
        remote_symbol("main", "origin"),
        main_bookmark_origin_tx0_remote_ref,
    );
    mut_repo.set_remote_bookmark(
        remote_symbol("main", "alternate"),
        main_bookmark_alternate_tx0_remote_ref.clone(),
    );
    let feature_bookmark_local_tx0 = write_random_commit(mut_repo);
    mut_repo.set_local_bookmark_target(
        "feature".as_ref(),
        RefTarget::normal(feature_bookmark_local_tx0.id().clone()),
    );
    let repo = tx.commit("test").unwrap();

    let mut tx1 = repo.start_transaction();
    let main_bookmark_local_tx1 = write_random_commit(tx1.repo_mut());
    tx1.repo_mut().set_local_bookmark_target(
        "main".as_ref(),
        RefTarget::normal(main_bookmark_local_tx1.id().clone()),
    );
    let feature_bookmark_tx1 = write_random_commit(tx1.repo_mut());
    tx1.repo_mut().set_local_bookmark_target(
        "feature".as_ref(),
        RefTarget::normal(feature_bookmark_tx1.id().clone()),
    );

    let mut tx2 = repo.start_transaction();
    let main_bookmark_local_tx2 = write_random_commit(tx2.repo_mut());
    let main_bookmark_origin_tx2 = write_random_commit(tx2.repo_mut());
    let main_bookmark_origin_tx2_remote_ref = RemoteRef {
        target: RefTarget::normal(main_bookmark_origin_tx2.id().clone()),
        state: RemoteRefState::Tracked,
    };
    tx2.repo_mut().set_local_bookmark_target(
        "main".as_ref(),
        RefTarget::normal(main_bookmark_local_tx2.id().clone()),
    );
    tx2.repo_mut().set_remote_bookmark(
        remote_symbol("main", "origin"),
        main_bookmark_origin_tx2_remote_ref.clone(),
    );

    let repo = commit_transactions(vec![tx1, tx2]);
    let expected_main_bookmark = BookmarkTarget {
        local_target: &RefTarget::from_legacy_form(
            [main_bookmark_local_tx0.id().clone()],
            [
                main_bookmark_local_tx1.id().clone(),
                main_bookmark_local_tx2.id().clone(),
            ],
        ),
        remote_refs: vec![
            (
                "alternate".as_ref(),
                &main_bookmark_alternate_tx0_remote_ref,
            ),
            // tx1: unchanged, tx2: new -> tracking
            ("origin".as_ref(), &main_bookmark_origin_tx2_remote_ref),
        ],
    };
    let expected_feature_bookmark = BookmarkTarget {
        local_target: &RefTarget::normal(feature_bookmark_tx1.id().clone()),
        remote_refs: vec![],
    };
    assert_eq!(
        repo.view().bookmarks().collect::<BTreeMap<_, _>>(),
        btreemap! {
            "main".as_ref() => expected_main_bookmark,
            "feature".as_ref() => expected_feature_bookmark,
        }
    );
}

#[test]
fn test_merge_views_tags() {
    // Tests merging of tags (by performing divergent operations). See
    // test_refs.rs for tests of merging of individual ref targets.
    let test_repo = TestRepo::init();
    let repo = &test_repo.repo;

    let mut tx = repo.start_transaction();
    let mut_repo = tx.repo_mut();
    let v1_tx0 = write_random_commit(mut_repo);
    mut_repo.set_tag_target("v1.0".as_ref(), RefTarget::normal(v1_tx0.id().clone()));
    let v2_tx0 = write_random_commit(mut_repo);
    mut_repo.set_tag_target("v2.0".as_ref(), RefTarget::normal(v2_tx0.id().clone()));
    let repo = tx.commit("test").unwrap();

    let mut tx1 = repo.start_transaction();
    let v1_tx1 = write_random_commit(tx1.repo_mut());
    tx1.repo_mut()
        .set_tag_target("v1.0".as_ref(), RefTarget::normal(v1_tx1.id().clone()));
    let v2_tx1 = write_random_commit(tx1.repo_mut());
    tx1.repo_mut()
        .set_tag_target("v2.0".as_ref(), RefTarget::normal(v2_tx1.id().clone()));

    let mut tx2 = repo.start_transaction();
    let v1_tx2 = write_random_commit(tx2.repo_mut());
    tx2.repo_mut()
        .set_tag_target("v1.0".as_ref(), RefTarget::normal(v1_tx2.id().clone()));

    let repo = commit_transactions(vec![tx1, tx2]);
    let expected_v1 = RefTarget::from_legacy_form(
        [v1_tx0.id().clone()],
        [v1_tx1.id().clone(), v1_tx2.id().clone()],
    );
    let expected_v2 = RefTarget::normal(v2_tx1.id().clone());
    assert_eq!(
        repo.view().tags(),
        &btreemap! {
            "v1.0".into() => expected_v1,
            "v2.0".into() => expected_v2,
        }
    );
}

#[test]
fn test_merge_views_git_refs() {
    // Tests merging of git refs (by performing divergent operations). See
    // test_refs.rs for tests of merging of individual ref targets.
    let test_repo = TestRepo::init();
    let repo = &test_repo.repo;

    let mut tx = repo.start_transaction();
    let mut_repo = tx.repo_mut();
    let main_bookmark_tx0 = write_random_commit(mut_repo);
    mut_repo.set_git_ref_target(
        "refs/heads/main".as_ref(),
        RefTarget::normal(main_bookmark_tx0.id().clone()),
    );
    let feature_bookmark_tx0 = write_random_commit(mut_repo);
    mut_repo.set_git_ref_target(
        "refs/heads/feature".as_ref(),
        RefTarget::normal(feature_bookmark_tx0.id().clone()),
    );
    let repo = tx.commit("test").unwrap();

    let mut tx1 = repo.start_transaction();
    let main_bookmark_tx1 = write_random_commit(tx1.repo_mut());
    tx1.repo_mut().set_git_ref_target(
        "refs/heads/main".as_ref(),
        RefTarget::normal(main_bookmark_tx1.id().clone()),
    );
    let feature_bookmark_tx1 = write_random_commit(tx1.repo_mut());
    tx1.repo_mut().set_git_ref_target(
        "refs/heads/feature".as_ref(),
        RefTarget::normal(feature_bookmark_tx1.id().clone()),
    );

    let mut tx2 = repo.start_transaction();
    let main_bookmark_tx2 = write_random_commit(tx2.repo_mut());
    tx2.repo_mut().set_git_ref_target(
        "refs/heads/main".as_ref(),
        RefTarget::normal(main_bookmark_tx2.id().clone()),
    );

    let repo = commit_transactions(vec![tx1, tx2]);
    let expected_main_bookmark = RefTarget::from_legacy_form(
        [main_bookmark_tx0.id().clone()],
        [
            main_bookmark_tx1.id().clone(),
            main_bookmark_tx2.id().clone(),
        ],
    );
    let expected_feature_bookmark = RefTarget::normal(feature_bookmark_tx1.id().clone());
    assert_eq!(
        repo.view().git_refs(),
        &btreemap! {
            "refs/heads/main".into() => expected_main_bookmark,
            "refs/heads/feature".into() => expected_feature_bookmark,
        }
    );
}

#[test]
fn test_merge_views_git_heads() {
    // Tests merging of git heads (by performing divergent operations). See
    // test_refs.rs for tests of merging of individual ref targets.
    let test_repo = TestRepo::init();
    let repo = &test_repo.repo;

    let mut tx0 = repo.start_transaction();
    let tx0_head = write_random_commit(tx0.repo_mut());
    tx0.repo_mut()
        .set_git_head_target(RefTarget::normal(tx0_head.id().clone()));
    let repo = tx0.commit("test").unwrap();

    let mut tx1 = repo.start_transaction();
    let tx1_head = write_random_commit(tx1.repo_mut());
    tx1.repo_mut()
        .set_git_head_target(RefTarget::normal(tx1_head.id().clone()));

    let mut tx2 = repo.start_transaction();
    let tx2_head = write_random_commit(tx2.repo_mut());
    tx2.repo_mut()
        .set_git_head_target(RefTarget::normal(tx2_head.id().clone()));

    let repo = commit_transactions(vec![tx1, tx2]);
    let expected_git_head = RefTarget::from_legacy_form(
        [tx0_head.id().clone()],
        [tx1_head.id().clone(), tx2_head.id().clone()],
    );
    assert_eq!(repo.view().git_head(), &expected_git_head);
}

#[test]
fn test_merge_views_divergent() {
    // We start with just commit A. Operation 1 rewrites it as A2. Operation 2
    // rewrites it as A3.
    let test_repo = TestRepo::init();

    let mut tx = test_repo.repo.start_transaction();
    let commit_a = write_random_commit(tx.repo_mut());
    let repo = tx.commit("test").unwrap();

    let mut tx1 = repo.start_transaction();
    let commit_a2 = tx1
        .repo_mut()
        .rewrite_commit(&commit_a)
        .set_description("A2")
        .write()
        .unwrap();
    tx1.repo_mut().rebase_descendants().unwrap();

    let mut tx2 = repo.start_transaction();
    let commit_a3 = tx2
        .repo_mut()
        .rewrite_commit(&commit_a)
        .set_description("A3")
        .write()
        .unwrap();
    tx2.repo_mut().rebase_descendants().unwrap();

    let repo = commit_transactions(vec![tx1, tx2]);

    // A2 and A3 should be heads.
    assert_eq!(
        *repo.view().heads(),
        hashset! {commit_a2.id().clone(), commit_a3.id().clone()}
    );
}

#[test_case(false ; "rewrite first")]
#[test_case(true ; "add child first")]
fn test_merge_views_child_on_rewritten(child_first: bool) {
    // We start with just commit A. Operation 1 adds commit B on top. Operation 2
    // rewrites A as A2.
    let test_repo = TestRepo::init();

    let mut tx = test_repo.repo.start_transaction();
    let commit_a = write_random_commit(tx.repo_mut());
    let repo = tx.commit("test").unwrap();

    let mut tx1 = repo.start_transaction();
    let commit_b = create_random_commit(tx1.repo_mut())
        .set_parents(vec![commit_a.id().clone()])
        .write()
        .unwrap();

    let mut tx2 = repo.start_transaction();
    let commit_a2 = tx2
        .repo_mut()
        .rewrite_commit(&commit_a)
        .set_description("A2")
        .write()
        .unwrap();
    tx2.repo_mut().rebase_descendants().unwrap();

    let repo = if child_first {
        commit_transactions(vec![tx1, tx2])
    } else {
        commit_transactions(vec![tx2, tx1])
    };

    // A new B2 commit (B rebased onto A2) should be the only head.
    let heads = repo.view().heads();
    assert_eq!(heads.len(), 1);
    let b2_id = heads.iter().next().unwrap();
    let commit_b2 = repo.store().get_commit(b2_id).unwrap();
    assert_eq!(commit_b2.change_id(), commit_b.change_id());
    assert_eq!(commit_b2.parent_ids(), vec![commit_a2.id().clone()]);
}

#[test_case(false, false ; "add child on unchanged, rewrite first")]
#[test_case(false, true ; "add child on unchanged, add child first")]
#[test_case(true, false ; "add child on rewritten, rewrite first")]
#[test_case(true, true ; "add child on rewritten, add child first")]
fn test_merge_views_child_on_rewritten_divergent(on_rewritten: bool, child_first: bool) {
    // We start with divergent commits A2 and A3. Operation 1 adds commit B on top
    // of A2 or A3. Operation 2 rewrites A2 as A4. The result should be that B
    // gets rebased onto A4 if it was based on A2 before, but if it was based on
    // A3, it should remain there.
    let test_repo = TestRepo::init();

    let mut tx = test_repo.repo.start_transaction();
    let commit_a2 = write_random_commit(tx.repo_mut());
    let commit_a3 = create_random_commit(tx.repo_mut())
        .set_change_id(commit_a2.change_id().clone())
        .write()
        .unwrap();
    let repo = tx.commit("test").unwrap();

    let mut tx1 = repo.start_transaction();
    let parent = if on_rewritten { &commit_a2 } else { &commit_a3 };
    let commit_b = create_random_commit(tx1.repo_mut())
        .set_parents(vec![parent.id().clone()])
        .write()
        .unwrap();

    let mut tx2 = repo.start_transaction();
    let commit_a4 = tx2
        .repo_mut()
        .rewrite_commit(&commit_a2)
        .set_description("A4")
        .write()
        .unwrap();
    tx2.repo_mut().rebase_descendants().unwrap();

    let repo = if child_first {
        commit_transactions(vec![tx1, tx2])
    } else {
        commit_transactions(vec![tx2, tx1])
    };

    if on_rewritten {
        // A3 should remain as a head. The other head should be B2 (B rebased onto A4).
        let mut heads = repo.view().heads().clone();
        assert_eq!(heads.len(), 2);
        assert!(heads.remove(commit_a3.id()));
        let b2_id = heads.iter().next().unwrap();
        let commit_b2 = repo.store().get_commit(b2_id).unwrap();
        assert_eq!(commit_b2.change_id(), commit_b.change_id());
        assert_eq!(commit_b2.parent_ids(), vec![commit_a4.id().clone()]);
    } else {
        // No rebases should happen, so B and A4 should be the heads.
        let mut heads = repo.view().heads().clone();
        assert_eq!(heads.len(), 2);
        assert!(heads.remove(commit_b.id()));
        assert!(heads.remove(commit_a4.id()));
    }
}

#[test_case(false ; "abandon first")]
#[test_case(true ; "add child first")]
fn test_merge_views_child_on_abandoned(child_first: bool) {
    // We start with commit B on top of commit A. Operation 1 adds commit C on top.
    // Operation 2 abandons B.
    let test_repo = TestRepo::init();

    let mut tx = test_repo.repo.start_transaction();
    let commit_a = write_random_commit(tx.repo_mut());
    let commit_b = create_random_commit(tx.repo_mut())
        .set_parents(vec![commit_a.id().clone()])
        .write()
        .unwrap();
    let repo = tx.commit("test").unwrap();

    let mut tx1 = repo.start_transaction();
    let commit_c = create_random_commit(tx1.repo_mut())
        .set_parents(vec![commit_b.id().clone()])
        .write()
        .unwrap();

    let mut tx2 = repo.start_transaction();
    tx2.repo_mut().record_abandoned_commit(&commit_b);
    tx2.repo_mut().rebase_descendants().unwrap();

    let repo = if child_first {
        commit_transactions(vec![tx1, tx2])
    } else {
        commit_transactions(vec![tx2, tx1])
    };

    // A new C2 commit (C rebased onto A) should be the only head.
    let heads = repo.view().heads();
    assert_eq!(heads.len(), 1);
    let id_c2 = heads.iter().next().unwrap();
    let commit_c2 = repo.store().get_commit(id_c2).unwrap();
    assert_eq!(commit_c2.change_id(), commit_c.change_id());
    assert_eq!(commit_c2.parent_ids(), vec![commit_a.id().clone()]);
}