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.internal.storage.file;
45
46 import static org.eclipse.jgit.lib.Constants.OBJECT_ID_LENGTH;
47 import static org.eclipse.jgit.lib.Constants.OBJECT_ID_STRING_LENGTH;
48 import static org.junit.Assert.assertEquals;
49 import static org.junit.Assert.assertNotNull;
50 import static org.junit.Assert.assertTrue;
51 import static org.junit.Assert.fail;
52
53 import java.io.ByteArrayOutputStream;
54 import java.io.File;
55 import java.io.FileOutputStream;
56 import java.io.IOException;
57 import java.io.OutputStream;
58 import java.util.ArrayList;
59 import java.util.Collection;
60 import java.util.List;
61
62 import org.eclipse.jgit.errors.AmbiguousObjectException;
63 import org.eclipse.jgit.internal.storage.file.FileRepository;
64 import org.eclipse.jgit.internal.storage.file.PackIndexWriter;
65 import org.eclipse.jgit.internal.storage.file.PackIndexWriterV2;
66 import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
67 import org.eclipse.jgit.junit.TestRepository;
68 import org.eclipse.jgit.lib.AbbreviatedObjectId;
69 import org.eclipse.jgit.lib.ObjectId;
70 import org.eclipse.jgit.lib.ObjectReader;
71 import org.eclipse.jgit.lib.Repository;
72 import org.eclipse.jgit.revwalk.RevBlob;
73 import org.eclipse.jgit.transport.PackedObjectInfo;
74 import org.eclipse.jgit.util.FileUtils;
75 import org.eclipse.jgit.util.io.SafeBufferedOutputStream;
76 import org.junit.After;
77 import org.junit.Before;
78 import org.junit.Test;
79
80 public class AbbreviationTest extends LocalDiskRepositoryTestCase {
81 private FileRepository db;
82
83 private ObjectReader reader;
84
85 private TestRepository<Repository> test;
86
87 @Before
88 public void setUp() throws Exception {
89 super.setUp();
90 db = createBareRepository();
91 reader = db.newObjectReader();
92 test = new TestRepository<Repository>(db);
93 }
94
95 @After
96 public void tearDown() throws Exception {
97 if (reader != null) {
98 reader.close();
99 }
100 }
101
102 @Test
103 public void testAbbreviateOnEmptyRepository() throws IOException {
104 ObjectId id = id("9d5b926ed164e8ee88d3b8b1e525d699adda01ba");
105
106 assertEquals(id.abbreviate(2), reader.abbreviate(id, 2));
107 assertEquals(id.abbreviate(7), reader.abbreviate(id, 7));
108 assertEquals(id.abbreviate(8), reader.abbreviate(id, 8));
109 assertEquals(id.abbreviate(10), reader.abbreviate(id, 10));
110 assertEquals(id.abbreviate(16), reader.abbreviate(id, 16));
111
112 assertEquals(AbbreviatedObjectId.fromObjectId(id),
113 reader.abbreviate(id, OBJECT_ID_STRING_LENGTH));
114
115 Collection<ObjectId> matches;
116
117 matches = reader.resolve(reader.abbreviate(id, 8));
118 assertNotNull(matches);
119 assertEquals(0, matches.size());
120
121 matches = reader.resolve(AbbreviatedObjectId.fromObjectId(id));
122 assertNotNull(matches);
123 assertEquals(1, matches.size());
124 assertEquals(id, matches.iterator().next());
125 }
126
127 @Test
128 public void testAbbreviateLooseBlob() throws Exception {
129 ObjectId id = test.blob("test");
130
131 assertEquals(id.abbreviate(2), reader.abbreviate(id, 2));
132 assertEquals(id.abbreviate(7), reader.abbreviate(id, 7));
133 assertEquals(id.abbreviate(8), reader.abbreviate(id, 8));
134 assertEquals(id.abbreviate(10), reader.abbreviate(id, 10));
135 assertEquals(id.abbreviate(16), reader.abbreviate(id, 16));
136
137 Collection<ObjectId> matches = reader.resolve(reader.abbreviate(id, 8));
138 assertNotNull(matches);
139 assertEquals(1, matches.size());
140 assertEquals(id, matches.iterator().next());
141
142 assertEquals(id, db.resolve(reader.abbreviate(id, 8).name()));
143 }
144
145 @Test
146 public void testAbbreviatePackedBlob() throws Exception {
147 RevBlob id = test.blob("test");
148 test.branch("master").commit().add("test", id).child();
149 test.packAndPrune();
150 assertTrue(reader.has(id));
151
152 assertEquals(id.abbreviate(7), reader.abbreviate(id, 7));
153 assertEquals(id.abbreviate(8), reader.abbreviate(id, 8));
154 assertEquals(id.abbreviate(10), reader.abbreviate(id, 10));
155 assertEquals(id.abbreviate(16), reader.abbreviate(id, 16));
156
157 Collection<ObjectId> matches = reader.resolve(reader.abbreviate(id, 8));
158 assertNotNull(matches);
159 assertEquals(1, matches.size());
160 assertEquals(id, matches.iterator().next());
161
162 assertEquals(id, db.resolve(reader.abbreviate(id, 8).name()));
163 }
164
165 @Test
166 public void testAbbreviateIsActuallyUnique() throws Exception {
167
168
169
170
171
172
173 ObjectId id = id("9d5b926ed164e8ee88d3b8b1e525d699adda01ba");
174 byte[] idBuf = toByteArray(id);
175 List<PackedObjectInfo> objects = new ArrayList<PackedObjectInfo>();
176 for (int i = 0; i < 256; i++) {
177 idBuf[9] = (byte) i;
178 objects.add(new PackedObjectInfo(ObjectId.fromRaw(idBuf)));
179 }
180
181 String packName = "pack-" + id.name();
182 File packDir = new File(db.getObjectDatabase().getDirectory(), "pack");
183 File idxFile = new File(packDir, packName + ".idx");
184 File packFile = new File(packDir, packName + ".pack");
185 FileUtils.mkdir(packDir, true);
186 OutputStream dst = new SafeBufferedOutputStream(new FileOutputStream(
187 idxFile));
188 try {
189 PackIndexWriter writer = new PackIndexWriterV2(dst);
190 writer.write(objects, new byte[OBJECT_ID_LENGTH]);
191 } finally {
192 dst.close();
193 }
194 new FileOutputStream(packFile).close();
195
196 assertEquals(id.abbreviate(20), reader.abbreviate(id, 2));
197
198 AbbreviatedObjectId abbrev8 = id.abbreviate(8);
199 Collection<ObjectId> matches = reader.resolve(abbrev8);
200 assertNotNull(matches);
201 assertEquals(objects.size(), matches.size());
202 for (PackedObjectInfo info : objects)
203 assertTrue("contains " + info.name(), matches.contains(info));
204
205 try {
206 db.resolve(abbrev8.name());
207 fail("did not throw AmbiguousObjectException");
208 } catch (AmbiguousObjectException err) {
209 assertEquals(abbrev8, err.getAbbreviatedObjectId());
210 matches = err.getCandidates();
211 assertNotNull(matches);
212 assertEquals(objects.size(), matches.size());
213 for (PackedObjectInfo info : objects)
214 assertTrue("contains " + info.name(), matches.contains(info));
215 }
216
217 assertEquals(id, db.resolve(id.abbreviate(20).name()));
218 }
219
220 private static ObjectId id(String name) {
221 return ObjectId.fromString(name);
222 }
223
224 private static byte[] toByteArray(ObjectId id) throws IOException {
225 ByteArrayOutputStream buf = new ByteArrayOutputStream(OBJECT_ID_LENGTH);
226 id.copyRawTo(buf);
227 return buf.toByteArray();
228 }
229 }