You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbbreviationTest.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (C) 2010, Google Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.internal.storage.file;
  11. import static org.eclipse.jgit.lib.Constants.OBJECT_ID_LENGTH;
  12. import static org.eclipse.jgit.lib.Constants.OBJECT_ID_STRING_LENGTH;
  13. import static org.junit.Assert.assertEquals;
  14. import static org.junit.Assert.assertNotNull;
  15. import static org.junit.Assert.assertTrue;
  16. import static org.junit.Assert.fail;
  17. import java.io.BufferedOutputStream;
  18. import java.io.ByteArrayOutputStream;
  19. import java.io.File;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import java.io.OutputStream;
  23. import java.util.ArrayList;
  24. import java.util.Collection;
  25. import java.util.List;
  26. import org.eclipse.jgit.errors.AmbiguousObjectException;
  27. import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
  28. import org.eclipse.jgit.junit.TestRepository;
  29. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  30. import org.eclipse.jgit.lib.ObjectId;
  31. import org.eclipse.jgit.lib.ObjectReader;
  32. import org.eclipse.jgit.lib.Repository;
  33. import org.eclipse.jgit.revwalk.RevBlob;
  34. import org.eclipse.jgit.transport.PackedObjectInfo;
  35. import org.eclipse.jgit.util.FileUtils;
  36. import org.junit.After;
  37. import org.junit.Before;
  38. import org.junit.Test;
  39. public class AbbreviationTest extends LocalDiskRepositoryTestCase {
  40. private FileRepository db;
  41. private ObjectReader reader;
  42. private TestRepository<Repository> test;
  43. @Override
  44. @Before
  45. public void setUp() throws Exception {
  46. super.setUp();
  47. db = createBareRepository();
  48. reader = db.newObjectReader();
  49. test = new TestRepository<>(db);
  50. }
  51. @Override
  52. @After
  53. public void tearDown() throws Exception {
  54. if (reader != null) {
  55. reader.close();
  56. }
  57. }
  58. @Test
  59. public void testAbbreviateOnEmptyRepository() throws IOException {
  60. ObjectId id = id("9d5b926ed164e8ee88d3b8b1e525d699adda01ba");
  61. assertEquals(id.abbreviate(2), reader.abbreviate(id, 2));
  62. assertEquals(id.abbreviate(7), reader.abbreviate(id, 7));
  63. assertEquals(id.abbreviate(8), reader.abbreviate(id, 8));
  64. assertEquals(id.abbreviate(10), reader.abbreviate(id, 10));
  65. assertEquals(id.abbreviate(16), reader.abbreviate(id, 16));
  66. assertEquals(AbbreviatedObjectId.fromObjectId(id), //
  67. reader.abbreviate(id, OBJECT_ID_STRING_LENGTH));
  68. Collection<ObjectId> matches;
  69. matches = reader.resolve(reader.abbreviate(id, 8));
  70. assertNotNull(matches);
  71. assertEquals(0, matches.size());
  72. matches = reader.resolve(AbbreviatedObjectId.fromObjectId(id));
  73. assertNotNull(matches);
  74. assertEquals(1, matches.size());
  75. assertEquals(id, matches.iterator().next());
  76. }
  77. @Test
  78. public void testAbbreviateLooseBlob() throws Exception {
  79. ObjectId id = test.blob("test");
  80. assertEquals(id.abbreviate(2), reader.abbreviate(id, 2));
  81. assertEquals(id.abbreviate(7), reader.abbreviate(id, 7));
  82. assertEquals(id.abbreviate(8), reader.abbreviate(id, 8));
  83. assertEquals(id.abbreviate(10), reader.abbreviate(id, 10));
  84. assertEquals(id.abbreviate(16), reader.abbreviate(id, 16));
  85. Collection<ObjectId> matches = reader.resolve(reader.abbreviate(id, 8));
  86. assertNotNull(matches);
  87. assertEquals(1, matches.size());
  88. assertEquals(id, matches.iterator().next());
  89. assertEquals(id, db.resolve(reader.abbreviate(id, 8).name()));
  90. }
  91. @Test
  92. public void testAbbreviatePackedBlob() throws Exception {
  93. RevBlob id = test.blob("test");
  94. test.branch("master").commit().add("test", id).child();
  95. test.packAndPrune();
  96. assertTrue(reader.has(id));
  97. assertEquals(id.abbreviate(7), reader.abbreviate(id, 7));
  98. assertEquals(id.abbreviate(8), reader.abbreviate(id, 8));
  99. assertEquals(id.abbreviate(10), reader.abbreviate(id, 10));
  100. assertEquals(id.abbreviate(16), reader.abbreviate(id, 16));
  101. Collection<ObjectId> matches = reader.resolve(reader.abbreviate(id, 8));
  102. assertNotNull(matches);
  103. assertEquals(1, matches.size());
  104. assertEquals(id, matches.iterator().next());
  105. assertEquals(id, db.resolve(reader.abbreviate(id, 8).name()));
  106. }
  107. @Test
  108. public void testAbbreviateIsActuallyUnique() throws Exception {
  109. // This test is far more difficult. We have to manually craft
  110. // an input that contains collisions at a particular prefix,
  111. // but this is computationally difficult. Instead we force an
  112. // index file to have what we want.
  113. //
  114. ObjectId id = id("9d5b926ed164e8ee88d3b8b1e525d699adda01ba");
  115. byte[] idBuf = toByteArray(id);
  116. List<PackedObjectInfo> objects = new ArrayList<>();
  117. for (int i = 0; i < 256; i++) {
  118. idBuf[9] = (byte) i;
  119. objects.add(new PackedObjectInfo(ObjectId.fromRaw(idBuf)));
  120. }
  121. String packName = "pack-" + id.name();
  122. File packDir = db.getObjectDatabase().getPackDirectory();
  123. File idxFile = new File(packDir, packName + ".idx");
  124. File packFile = new File(packDir, packName + ".pack");
  125. FileUtils.mkdir(packDir, true);
  126. try (OutputStream dst = new BufferedOutputStream(
  127. new FileOutputStream(idxFile))) {
  128. PackIndexWriter writer = new PackIndexWriterV2(dst);
  129. writer.write(objects, new byte[OBJECT_ID_LENGTH]);
  130. }
  131. try (FileOutputStream unused = new FileOutputStream(packFile)) {
  132. // unused
  133. }
  134. assertEquals(id.abbreviate(20), reader.abbreviate(id, 2));
  135. AbbreviatedObjectId abbrev8 = id.abbreviate(8);
  136. Collection<ObjectId> matches = reader.resolve(abbrev8);
  137. assertNotNull(matches);
  138. assertEquals(objects.size(), matches.size());
  139. for (PackedObjectInfo info : objects)
  140. assertTrue("contains " + info.name(), matches.contains(info));
  141. try {
  142. db.resolve(abbrev8.name());
  143. fail("did not throw AmbiguousObjectException");
  144. } catch (AmbiguousObjectException err) {
  145. assertEquals(abbrev8, err.getAbbreviatedObjectId());
  146. matches = err.getCandidates();
  147. assertNotNull(matches);
  148. assertEquals(objects.size(), matches.size());
  149. for (PackedObjectInfo info : objects)
  150. assertTrue("contains " + info.name(), matches.contains(info));
  151. }
  152. assertEquals(id, db.resolve(id.abbreviate(20).name()));
  153. }
  154. private static ObjectId id(String name) {
  155. return ObjectId.fromString(name);
  156. }
  157. private static byte[] toByteArray(ObjectId id) throws IOException {
  158. ByteArrayOutputStream buf = new ByteArrayOutputStream(OBJECT_ID_LENGTH);
  159. id.copyRawTo(buf);
  160. return buf.toByteArray();
  161. }
  162. }