1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.api;
12
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertTrue;
15
16 import java.util.Map;
17
18 import org.eclipse.jgit.junit.RepositoryTestCase;
19 import org.eclipse.jgit.junit.TestRepository;
20 import org.eclipse.jgit.lib.ObjectId;
21 import org.eclipse.jgit.lib.Repository;
22 import org.eclipse.jgit.revwalk.RevCommit;
23 import org.junit.Before;
24 import org.junit.Test;
25
26 public class NameRevCommandTest extends RepositoryTestCase {
27 private TestRepository<Repository> tr;
28 private Git git;
29
30 @Override
31 @Before
32 public void setUp() throws Exception {
33 super.setUp();
34 tr = new TestRepository<>(db);
35 git = new Git(db);
36 }
37
38 @Test
39 public void nameExact() throws Exception {
40 RevCommit c = tr.commit().create();
41 tr.update("master", c);
42 assertOneResult("master", c);
43 }
44
45 @Test
46 public void prefix() throws Exception {
47 RevCommit c = tr.commit().create();
48 tr.update("refs/heads/master", c);
49 tr.update("refs/tags/tag", c);
50 assertOneResult("master", c);
51 assertOneResult("master",
52 git.nameRev().addPrefix("refs/heads/").addPrefix("refs/tags/"),
53 c);
54 assertOneResult("tag",
55 git.nameRev().addPrefix("refs/tags/").addPrefix("refs/heads/"),
56 c);
57 }
58
59 @Test
60 public void ref() throws Exception {
61 RevCommit c = tr.commit().create();
62 tr.update("refs/heads/master", c);
63 tr.update("refs/tags/tag", c);
64 assertOneResult("master",
65 git.nameRev().addRef(db.exactRef("refs/heads/master")), c);
66 assertOneResult("tag",
67 git.nameRev().addRef(db.exactRef("refs/tags/tag")), c);
68 }
69
70 @Test
71 public void annotatedTags() throws Exception {
72 RevCommit c = tr.commit().create();
73 tr.update("refs/heads/master", c);
74 tr.update("refs/tags/tag1", c);
75 tr.update("refs/tags/tag2", tr.tag("tag2", c));
76 assertOneResult("tag2", git.nameRev().addAnnotatedTags(), c);
77 }
78
79 @Test
80 public void annotatedTagsNoResult() throws Exception {
81 RevCommit c = tr.commit().create();
82 tr.update("refs/heads/master", c);
83 tr.update("refs/tags/tag1", c);
84 tr.update("refs/tags/tag2", c);
85 Map<ObjectId, String> result = git.nameRev()
86 .add(c)
87 .addAnnotatedTags()
88 .call();
89 assertTrue(result.toString(), result.isEmpty());
90 }
91
92 @Test
93 public void simpleAncestor() throws Exception {
94
95 RevCommit c0 = tr.commit().create();
96 RevCommit c1 = tr.commit().parent(c0).create();
97 RevCommit c2 = tr.commit().parent(c1).create();
98 tr.update("master", c2);
99 Map<ObjectId, String> result = git.nameRev().add(c0).add(c1).add(c2).call();
100 assertEquals(3, result.size());
101 assertEquals("master~2", result.get(c0));
102 assertEquals("master~1", result.get(c1));
103 assertEquals("master", result.get(c2));
104 }
105
106 @Test
107 public void multiplePathsNoMerge() throws Exception {
108
109
110 RevCommit c0 = tr.commit().create();
111 RevCommit c1 = tr.commit().parent(c0).create();
112 RevCommit c2 = tr.commit().parent(c0).create();
113 RevCommit c3 = tr.commit().parent(c2).create();
114 tr.update("master", c1);
115 tr.update("branch", c3);
116 assertOneResult("master~1", c0);
117 }
118
119 @Test
120 public void onePathMerge() throws Exception {
121
122
123 RevCommit c0 = tr.commit().create();
124 RevCommit c1 = tr.commit().parent(c0).create();
125 RevCommit c2 = tr.commit().parent(c0).create();
126 RevCommit c3 = tr.commit().parent(c1).parent(c2).create();
127 tr.update("master", c3);
128 assertOneResult("master~2", c0);
129 }
130
131 @Test
132 public void onePathMergeSecondParent() throws Exception {
133
134
135 RevCommit c0 = tr.commit().create();
136 RevCommit c1 = tr.commit().parent(c0).create();
137 RevCommit c2 = tr.commit().parent(c0).create();
138 RevCommit c3 = tr.commit().parent(c2).create();
139 RevCommit c4 = tr.commit().parent(c1).parent(c3).create();
140 tr.update("master", c4);
141 assertOneResult("master^2", c3);
142 assertOneResult("master^2~1", c2);
143 }
144
145 @Test
146 public void onePathMergeLongerFirstParentPath() throws Exception {
147
148
149 RevCommit c0 = tr.commit().create();
150 RevCommit c1 = tr.commit().parent(c0).create();
151 RevCommit c2 = tr.commit().parent(c1).create();
152 RevCommit c3 = tr.commit().parent(c0).create();
153 RevCommit c4 = tr.commit().parent(c2).parent(c3).create();
154 tr.update("master", c4);
155 assertOneResult("master^2", c3);
156 assertOneResult("master~3", c0);
157 }
158
159 @Test
160 public void multiplePathsSecondParent() throws Exception {
161
162
163 RevCommit c0 = tr.commit().create();
164 RevCommit c1 = tr.commit().parent(c0).create();
165 RevCommit c = c0;
166 int mergeCost = 5;
167 for (int i = 0; i < mergeCost; i++) {
168 c = tr.commit().parent(c).create();
169 }
170 RevCommit c2 = tr.commit().parent(c).parent(c1).create();
171 tr.update("master", c2);
172 assertOneResult("master^2~1", git.nameRev().setMergeCost(mergeCost), c0);
173 }
174
175 private static void assertOneResult(String expected, NameRevCommand nameRev,
176 ObjectId id) throws Exception {
177 Map<ObjectId, String> result = nameRev.add(id).call();
178 assertEquals(1, result.size());
179 assertEquals(expected, result.get(id));
180 }
181
182 private void assertOneResult(String expected, ObjectId id) throws Exception {
183 assertOneResult(expected, git.nameRev(), id);
184 }
185 }