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.internal.storage.pack.PackExt;
  28. import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
  29. import org.eclipse.jgit.junit.TestRepository;
  30. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  31. import org.eclipse.jgit.lib.ObjectId;
  32. import org.eclipse.jgit.lib.ObjectReader;
  33. import org.eclipse.jgit.lib.Repository;
  34. import org.eclipse.jgit.revwalk.RevBlob;
  35. import org.eclipse.jgit.transport.PackedObjectInfo;
  36. import org.eclipse.jgit.util.FileUtils;
  37. import org.junit.After;
  38. import org.junit.Before;
  39. import org.junit.Test;
  40. public class AbbreviationTest extends LocalDiskRepositoryTestCase {
  41. private FileRepository db;
  42. private ObjectReader reader;
  43. private TestRepository<Repository> test;
  44. @Override
  45. @Before
  46. public void setUp() throws Exception {
  47. super.setUp();
  48. db = createBareRepository();
  49. reader = db.newObjectReader();
  50. test = new TestRepository<>(db);
  51. }
  52. @Override
  53. @After
  54. public void tearDown() throws Exception {
  55. if (reader != null) {
  56. reader.close();
  57. }
  58. }
  59. @Test
  60. public void testAbbreviateOnEmptyRepository() throws IOException {
  61. ObjectId id = id("9d5b926ed164e8ee88d3b8b1e525d699adda01ba");
  62. assertEquals(id.abbreviate(2), reader.abbreviate(id, 2));
  63. assertEquals(id.abbreviate(7), reader.abbreviate(id, 7));
  64. assertEquals(id.abbreviate(8), reader.abbreviate(id, 8));
  65. assertEquals(id.abbreviate(10), reader.abbreviate(id, 10));
  66. assertEquals(id.abbreviate(16), reader.abbreviate(id, 16));
  67. assertEquals(AbbreviatedObjectId.fromObjectId(id), //
  68. reader.abbreviate(id, OBJECT_ID_STRING_LENGTH));
  69. Collection<ObjectId> matches;
  70. matches = reader.resolve(reader.abbreviate(id, 8));
  71. assertNotNull(matches);
  72. assertEquals(0, matches.size());
  73. matches = reader.resolve(AbbreviatedObjectId.fromObjectId(id));
  74. assertNotNull(matches);
  75. assertEquals(1, matches.size());
  76. assertEquals(id, matches.iterator().next());
  77. }
  78. @Test
  79. public void testAbbreviateLooseBlob() throws Exception {
  80. ObjectId id = test.blob("test");
  81. assertEquals(id.abbreviate(2), reader.abbreviate(id, 2));
  82. assertEquals(id.abbreviate(7), reader.abbreviate(id, 7));
  83. assertEquals(id.abbreviate(8), reader.abbreviate(id, 8));
  84. assertEquals(id.abbreviate(10), reader.abbreviate(id, 10));
  85. assertEquals(id.abbreviate(16), reader.abbreviate(id, 16));
  86. Collection<ObjectId> matches = reader.resolve(reader.abbreviate(id, 8));
  87. assertNotNull(matches);
  88. assertEquals(1, matches.size());
  89. assertEquals(id, matches.iterator().next());
  90. assertEquals(id, db.resolve(reader.abbreviate(id, 8).name()));
  91. }
  92. @Test
  93. public void testAbbreviatePackedBlob() throws Exception {
  94. RevBlob id = test.blob("test");
  95. test.branch("master").commit().add("test", id).child();
  96. test.packAndPrune();
  97. assertTrue(reader.has(id));
  98. assertEquals(id.abbreviate(7), reader.abbreviate(id, 7));
  99. assertEquals(id.abbreviate(8), reader.abbreviate(id, 8));
  100. assertEquals(id.abbreviate(10), reader.abbreviate(id, 10));
  101. assertEquals(id.abbreviate(16), reader.abbreviate(id, 16));
  102. Collection<ObjectId> matches = reader.resolve(reader.abbreviate(id, 8));
  103. assertNotNull(matches);
  104. assertEquals(1, matches.size());
  105. assertEquals(id, matches.iterator().next());
  106. assertEquals(id, db.resolve(reader.abbreviate(id, 8).name()));
  107. }
  108. @Test
  109. public void testAbbreviateIsActuallyUnique() throws Exception {
  110. // This test is far more difficult. We have to manually craft
  111. // an input that contains collisions at a particular prefix,
  112. // but this is computationally difficult. Instead we force an
  113. // index file to have what we want.
  114. //
  115. ObjectId id = id("9d5b926ed164e8ee88d3b8b1e525d699adda01ba");
  116. byte[] idBuf = toByteArray(id);
  117. List<PackedObjectInfo> objects = new ArrayList<>();
  118. for (int i = 0; i < 256; i++) {
  119. idBuf[9] = (byte) i;
  120. objects.add(new PackedObjectInfo(ObjectId.fromRaw(idBuf)));
  121. }
  122. File packDir = db.getObjectDatabase().getPackDirectory();
  123. PackFile idxFile = new PackFile(packDir, id, PackExt.INDEX);
  124. PackFile packFile = idxFile.create(PackExt.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. }