Browse Source

Use w1 for hashCode of AbbreviatedObjectId

Very short abbreviations that are under 8 hex digits do not
have values in w2. Use w1 as the Java hashCode() instead, so
that the prefix of the abbreviation is always included in the
hashing function used by any java.util.Collection type.

Change-Id: Idaf69f86b62630ba4a022d31b4c293c6d138f557
stable-4.9
Shawn Pearce 7 years ago
parent
commit
4a00f18e8e
  1. 16
      org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/AbbreviatedObjectIdTest.java
  2. 2
      org.eclipse.jgit/src/org/eclipse/jgit/lib/AbbreviatedObjectId.java

16
org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/AbbreviatedObjectIdTest.java

@ -86,7 +86,6 @@ public class AbbreviatedObjectIdTest {
final ObjectId f = i.toObjectId(); final ObjectId f = i.toObjectId();
assertNotNull(f); assertNotNull(f);
assertEquals(ObjectId.fromString(s), f); assertEquals(ObjectId.fromString(s), f);
assertEquals(f.hashCode(), i.hashCode());
} }
@Test @Test
@ -101,7 +100,6 @@ public class AbbreviatedObjectIdTest {
final ObjectId f = i.toObjectId(); final ObjectId f = i.toObjectId();
assertNotNull(f); assertNotNull(f);
assertEquals(ObjectId.fromString(s), f); assertEquals(ObjectId.fromString(s), f);
assertEquals(f.hashCode(), i.hashCode());
} }
@Test @Test
@ -215,7 +213,7 @@ public class AbbreviatedObjectIdTest {
} }
@Test @Test
public void testEquals_Short() { public void testEquals_Short8() {
final String s = "7b6e8067"; final String s = "7b6e8067";
final AbbreviatedObjectId a = AbbreviatedObjectId.fromString(s); final AbbreviatedObjectId a = AbbreviatedObjectId.fromString(s);
final AbbreviatedObjectId b = AbbreviatedObjectId.fromString(s); final AbbreviatedObjectId b = AbbreviatedObjectId.fromString(s);
@ -225,6 +223,18 @@ public class AbbreviatedObjectIdTest {
assertEquals(a, b); assertEquals(a, b);
} }
@Test
public void testEquals_Short4() {
final String s = "7b6e";
final AbbreviatedObjectId a = AbbreviatedObjectId.fromString(s);
final AbbreviatedObjectId b = AbbreviatedObjectId.fromString(s);
assertNotSame(a, b);
assertTrue(a.hashCode() != 0);
assertTrue(a.hashCode() == b.hashCode());
assertEquals(b, a);
assertEquals(a, b);
}
@Test @Test
public void testEquals_Full() { public void testEquals_Full() {
final String s = "7b6e8067ec96acef9a4184b43210d583b6d2f99a"; final String s = "7b6e8067ec96acef9a4184b43210d583b6d2f99a";

2
org.eclipse.jgit/src/org/eclipse/jgit/lib/AbbreviatedObjectId.java

@ -336,7 +336,7 @@ public final class AbbreviatedObjectId implements Serializable {
@Override @Override
public int hashCode() { public int hashCode() {
return w2; return w1;
} }
@Override @Override

Loading…
Cancel
Save