View Javadoc
1   /*
2    * Copyright (C) 2010, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 		// This test is far more difficult. We have to manually craft
168 		// an input that contains collisions at a particular prefix,
169 		// but this is computationally difficult. Instead we force an
170 		// index file to have what we want.
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 }