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
|
/*
* Copyright (C) 2020, Google LLC and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
* https://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.eclipse.jgit.internal.revwalk;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Stream;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.junit.TestRepository.CommitBuilder;
import org.eclipse.jgit.revwalk.ObjectReachabilityChecker;
import org.eclipse.jgit.revwalk.RevBlob;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevObject;
import org.junit.Before;
import org.junit.Test;
public abstract class ObjectReachabilityTestCase
extends LocalDiskRepositoryTestCase {
private TestRepository<FileRepository> repo;
private AddressableRevCommit baseCommit;
private AddressableRevCommit branchACommit;
private AddressableRevCommit branchBCommit;
private AddressableRevCommit mergeCommit;
abstract ObjectReachabilityChecker getChecker(
TestRepository<FileRepository> repository) throws Exception;
// Pair of commit and blob inside it
protected static class AddressableRevCommit {
RevCommit commit;
RevBlob blob;
AddressableRevCommit(RevCommit commit, RevBlob blob) {
this.commit = commit;
this.blob = blob;
}
}
@Override
@Before
public void setUp() throws Exception {
super.setUp();
FileRepository db = createWorkRepository();
repo = new TestRepository<>(db);
prepareRepo();
}
@Test
public void blob_in_base_reachable_from_branches() throws Exception {
ObjectReachabilityChecker checker = getChecker(repo);
RevObject baseBlob = baseCommit.blob;
assertReachable("reachable from one branch", checker.areAllReachable(
Arrays.asList(baseBlob), Stream.of(branchACommit.commit)));
assertReachable("reachable from another branch",
checker.areAllReachable(
Arrays.asList(baseBlob),
Stream.of(branchBCommit.commit)));
}
@Test
public void blob_reachable_from_owning_commit() throws Exception {
ObjectReachabilityChecker checker = getChecker(repo);
RevObject branchABlob = branchACommit.blob;
assertReachable("reachable from itself",
checker.areAllReachable(Arrays.asList(branchABlob),
Stream.of(branchACommit.commit)));
}
@Test
public void blob_in_branch_reachable_from_merge() throws Exception {
ObjectReachabilityChecker checker = getChecker(repo);
RevObject branchABlob = branchACommit.blob;
assertReachable("reachable from merge", checker.areAllReachable(
Arrays.asList(branchABlob), Stream.of(mergeCommit.commit)));
}
@Test
public void blob_unreachable_from_earlier_commit() throws Exception {
ObjectReachabilityChecker checker = getChecker(repo);
RevObject branchABlob = branchACommit.blob;
assertUnreachable("unreachable from earlier commit",
checker.areAllReachable(Arrays.asList(branchABlob),
Stream.of(baseCommit.commit)));
}
@Test
public void blob_unreachable_from_parallel_branch() throws Exception {
ObjectReachabilityChecker checker = getChecker(repo);
RevObject branchABlob = branchACommit.blob;
assertUnreachable("unreachable from another branch",
checker.areAllReachable(Arrays.asList(branchABlob),
Stream.of(branchBCommit.commit)));
}
private void prepareRepo() throws Exception {
baseCommit = createCommit("base");
branchACommit = createCommit("branchA", baseCommit);
branchBCommit = createCommit("branchB", baseCommit);
mergeCommit = createCommit("merge", branchACommit, branchBCommit);
// Bitmaps are generated from references
repo.update("refs/heads/a", branchACommit.commit);
repo.update("refs/heads/b", branchBCommit.commit);
repo.update("refs/heads/merge", mergeCommit.commit);
}
private AddressableRevCommit createCommit(String blobPath, AddressableRevCommit... parents) throws Exception {
RevBlob blob = repo.blob(blobPath + " content");
CommitBuilder commitBuilder = repo.commit();
for (int i = 0; i < parents.length; i++) {
commitBuilder.parent(parents[i].commit);
}
commitBuilder.add(blobPath, blob);
RevCommit commit = commitBuilder.create();
return new AddressableRevCommit(commit, blob);
}
private static void assertReachable(String msg, Optional<RevObject> result) {
assertFalse(msg, result.isPresent());
}
private static void assertUnreachable(String msg, Optional<RevObject> result) {
assertTrue(msg, result.isPresent());
}
}
|