aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib
diff options
context:
space:
mode:
authorMatthias Sohn <matthias.sohn@sap.com>2022-02-21 02:29:27 +0100
committerMatthias Sohn <matthias.sohn@sap.com>2022-03-02 19:29:48 +0100
commit6f175ea6c46488d7d301a74ccc87d7472c314c1a (patch)
treec14132e05dd80ce3922e116360f3a53f532046a7 /org.eclipse.jgit.test/tst/org/eclipse/jgit/lib
parent9244c07d73968f668e7579a6c8ff626982229b3f (diff)
downloadjgit-6f175ea6c46488d7d301a74ccc87d7472c314c1a.tar.gz
jgit-6f175ea6c46488d7d301a74ccc87d7472c314c1a.zip
Describe: add support for core.abbrev config option
If core.abbrev is unset or "auto" estimate abbreviation length like C git does: - Estimate repository's object count by only considering packed objects, round up to next power of 2 - With the order of 2^len objects, we expect a collision at 2^(len/2). But we also care about hex chars, not bits, and there are 4 bits per hex. So all together we need to divide by 2; but we also want to round odd numbers up, hence adding one before dividing. - For small repos use at least 7 hexdigits - If object database fails to determine object count use 7 hexdigits as fallback If it is set to "no" do not abbreviate object-ids. Otherwise set it to the configured value capped to the range between 4 and length of an unabbreviated object-id. Change-Id: I425f9724b69813dbb57872466bf2d2e1d6dc72c6
Diffstat (limited to 'org.eclipse.jgit.test/tst/org/eclipse/jgit/lib')
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/AbbrevConfigTest.java128
1 files changed, 128 insertions, 0 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/AbbrevConfigTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/AbbrevConfigTest.java
new file mode 100644
index 0000000000..96ace08dd1
--- /dev/null
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/AbbrevConfigTest.java
@@ -0,0 +1,128 @@
+/*
+ * Copyright (C) 2022, Matthias Sohn <matthias.sohn@sap.com> and others
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Distribution License v. 1.0 which is available at
+ * https://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+package org.eclipse.jgit.lib;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
+
+import java.io.IOException;
+
+import org.eclipse.jgit.api.errors.InvalidConfigurationException;
+import org.eclipse.jgit.junit.RepositoryTestCase;
+import org.eclipse.jgit.storage.file.FileBasedConfig;
+import org.junit.Test;
+
+public class AbbrevConfigTest extends RepositoryTestCase {
+
+ @Test
+ public void testDefault() throws Exception {
+ assertEquals(7, testCoreAbbrev(null));
+ }
+
+ @Test
+ public void testAuto() throws Exception {
+ assertEquals(7, testCoreAbbrev("auto"));
+ }
+
+ @Test
+ public void testNo() throws Exception {
+ assertEquals(40, testCoreAbbrev("no"));
+ }
+
+ @Test
+ public void testValidMin() throws Exception {
+ assertEquals(4, testCoreAbbrev("4"));
+ }
+
+ @Test
+ public void testValid() throws Exception {
+ assertEquals(22, testCoreAbbrev("22"));
+ }
+
+ @Test
+ public void testValidMax() throws Exception {
+ assertEquals(40, testCoreAbbrev("40"));
+ }
+
+ @Test
+ public void testInvalid() {
+ assertThrows(InvalidConfigurationException.class,
+ () -> testCoreAbbrev("foo"));
+ }
+
+ @Test
+ public void testInvalid2() {
+ assertThrows(InvalidConfigurationException.class,
+ () -> testCoreAbbrev("2k"));
+ }
+
+ @Test
+ public void testInvalidNegative() {
+ assertThrows(InvalidConfigurationException.class,
+ () -> testCoreAbbrev("-1000"));
+ }
+
+ @Test
+ public void testInvalidBelowRange() {
+ assertThrows(InvalidConfigurationException.class,
+ () -> testCoreAbbrev("3"));
+ }
+
+ @Test
+ public void testInvalidBelowRange2() {
+ assertThrows(InvalidConfigurationException.class,
+ () -> testCoreAbbrev("-1"));
+ }
+
+ @Test
+ public void testInvalidAboveRange() {
+ assertThrows(InvalidConfigurationException.class,
+ () -> testCoreAbbrev("41"));
+ }
+
+ @Test
+ public void testInvalidAboveRange2() {
+ assertThrows(InvalidConfigurationException.class,
+ () -> testCoreAbbrev("100000"));
+ }
+
+ @Test
+ public void testToStringNo()
+ throws InvalidConfigurationException, IOException {
+ assertEquals("40", setCoreAbbrev("no").toString());
+ }
+
+ @Test
+ public void testToString()
+ throws InvalidConfigurationException, IOException {
+ assertEquals("7", setCoreAbbrev("auto").toString());
+ }
+
+ @Test
+ public void testToString12()
+ throws InvalidConfigurationException, IOException {
+ assertEquals("12", setCoreAbbrev("12").toString());
+ }
+
+ private int testCoreAbbrev(String value)
+ throws InvalidConfigurationException, IOException {
+ return setCoreAbbrev(value).get();
+ }
+
+ private AbbrevConfig setCoreAbbrev(String value)
+ throws IOException, InvalidConfigurationException {
+ FileBasedConfig config = db.getConfig();
+ config.setString(ConfigConstants.CONFIG_CORE_SECTION, null,
+ ConfigConstants.CONFIG_KEY_ABBREV, value);
+ config.save();
+ return AbbrevConfig.parseFromConfig(db);
+ }
+
+}