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 package org.eclipse.jgit.api;
45
46 import static org.junit.Assert.assertEquals;
47 import static org.junit.Assert.assertTrue;
48
49 import java.util.Map;
50
51 import org.eclipse.jgit.junit.RepositoryTestCase;
52 import org.eclipse.jgit.junit.TestRepository;
53 import org.eclipse.jgit.lib.ObjectId;
54 import org.eclipse.jgit.lib.Repository;
55 import org.eclipse.jgit.revwalk.RevCommit;
56 import org.junit.Before;
57 import org.junit.Test;
58
59 public class NameRevCommandTest extends RepositoryTestCase {
60 private TestRepository<Repository> tr;
61 private Git git;
62
63 @Override
64 @Before
65 public void setUp() throws Exception {
66 super.setUp();
67 tr = new TestRepository<>(db);
68 git = new Git(db);
69 }
70
71 @Test
72 public void nameExact() throws Exception {
73 RevCommit c = tr.commit().create();
74 tr.update("master", c);
75 assertOneResult("master", c);
76 }
77
78 @Test
79 public void prefix() throws Exception {
80 RevCommit c = tr.commit().create();
81 tr.update("refs/heads/master", c);
82 tr.update("refs/tags/tag", c);
83 assertOneResult("master", c);
84 assertOneResult("master",
85 git.nameRev().addPrefix("refs/heads/").addPrefix("refs/tags/"),
86 c);
87 assertOneResult("tag",
88 git.nameRev().addPrefix("refs/tags/").addPrefix("refs/heads/"),
89 c);
90 }
91
92 @Test
93 public void ref() throws Exception {
94 RevCommit c = tr.commit().create();
95 tr.update("refs/heads/master", c);
96 tr.update("refs/tags/tag", c);
97 assertOneResult("master",
98 git.nameRev().addRef(db.exactRef("refs/heads/master")), c);
99 assertOneResult("tag",
100 git.nameRev().addRef(db.exactRef("refs/tags/tag")), c);
101 }
102
103 @Test
104 public void annotatedTags() throws Exception {
105 RevCommit c = tr.commit().create();
106 tr.update("refs/heads/master", c);
107 tr.update("refs/tags/tag1", c);
108 tr.update("refs/tags/tag2", tr.tag("tag2", c));
109 assertOneResult("tag2", git.nameRev().addAnnotatedTags(), c);
110 }
111
112 @Test
113 public void annotatedTagsNoResult() throws Exception {
114 RevCommit c = tr.commit().create();
115 tr.update("refs/heads/master", c);
116 tr.update("refs/tags/tag1", c);
117 tr.update("refs/tags/tag2", c);
118 Map<ObjectId, String> result = git.nameRev()
119 .add(c)
120 .addAnnotatedTags()
121 .call();
122 assertTrue(result.toString(), result.isEmpty());
123 }
124
125 @Test
126 public void simpleAncestor() throws Exception {
127
128 RevCommit c0 = tr.commit().create();
129 RevCommit c1 = tr.commit().parent(c0).create();
130 RevCommit c2 = tr.commit().parent(c1).create();
131 tr.update("master", c2);
132 Map<ObjectId, String> result = git.nameRev().add(c0).add(c1).add(c2).call();
133 assertEquals(3, result.size());
134 assertEquals("master~2", result.get(c0));
135 assertEquals("master~1", result.get(c1));
136 assertEquals("master", result.get(c2));
137 }
138
139 @Test
140 public void multiplePathsNoMerge() throws Exception {
141
142
143 RevCommit c0 = tr.commit().create();
144 RevCommit c1 = tr.commit().parent(c0).create();
145 RevCommit c2 = tr.commit().parent(c0).create();
146 RevCommit c3 = tr.commit().parent(c2).create();
147 tr.update("master", c1);
148 tr.update("branch", c3);
149 assertOneResult("master~1", c0);
150 }
151
152 @Test
153 public void onePathMerge() throws Exception {
154
155
156 RevCommit c0 = tr.commit().create();
157 RevCommit c1 = tr.commit().parent(c0).create();
158 RevCommit c2 = tr.commit().parent(c0).create();
159 RevCommit c3 = tr.commit().parent(c1).parent(c2).create();
160 tr.update("master", c3);
161 assertOneResult("master~2", c0);
162 }
163
164 @Test
165 public void onePathMergeSecondParent() throws Exception {
166
167
168 RevCommit c0 = tr.commit().create();
169 RevCommit c1 = tr.commit().parent(c0).create();
170 RevCommit c2 = tr.commit().parent(c0).create();
171 RevCommit c3 = tr.commit().parent(c2).create();
172 RevCommit c4 = tr.commit().parent(c1).parent(c3).create();
173 tr.update("master", c4);
174 assertOneResult("master^2", c3);
175 assertOneResult("master^2~1", c2);
176 }
177
178 @Test
179 public void onePathMergeLongerFirstParentPath() throws Exception {
180
181
182 RevCommit c0 = tr.commit().create();
183 RevCommit c1 = tr.commit().parent(c0).create();
184 RevCommit c2 = tr.commit().parent(c1).create();
185 RevCommit c3 = tr.commit().parent(c0).create();
186 RevCommit c4 = tr.commit().parent(c2).parent(c3).create();
187 tr.update("master", c4);
188 assertOneResult("master^2", c3);
189 assertOneResult("master~3", c0);
190 }
191
192 @Test
193 public void multiplePathsSecondParent() throws Exception {
194
195
196 RevCommit c0 = tr.commit().create();
197 RevCommit c1 = tr.commit().parent(c0).create();
198 RevCommit c = c0;
199 int mergeCost = 5;
200 for (int i = 0; i < mergeCost; i++) {
201 c = tr.commit().parent(c).create();
202 }
203 RevCommit c2 = tr.commit().parent(c).parent(c1).create();
204 tr.update("master", c2);
205 assertOneResult("master^2~1", git.nameRev().setMergeCost(mergeCost), c0);
206 }
207
208 private static void assertOneResult(String expected, NameRevCommand nameRev,
209 ObjectId id) throws Exception {
210 Map<ObjectId, String> result = nameRev.add(id).call();
211 assertEquals(1, result.size());
212 assertEquals(expected, result.get(id));
213 }
214
215 private void assertOneResult(String expected, ObjectId id) throws Exception {
216 assertOneResult(expected, git.nameRev(), id);
217 }
218 }