From c053900c5bc89d2b55c563e04bc1edab53cdc143 Mon Sep 17 00:00:00 2001 From: Andrey Loskutov Date: Sun, 14 Dec 2014 18:07:59 +0100 Subject: PathMatcher should respect "assumeDirectory" flag The path matcher should not fail if the rule ends with trailing slash, target pattern does not ends with the slash and the "assumeDirectory" flag is set. E.g. */bin/ should also match a/bin if this pattern is threated as directory by WorkingTreeIterator (FileMode.TREE). The old code/tests have never tested directory rules with patterns *without* trailing slashes but with the "assumeDirectory" flag set. Unfortunately this is exactly what WorkingTreeIterator does... The tests are changed to test *both* cases now (with trailing slash and without) if the target pattern has trailing slash (represents directory). Bug: 454672 Change-Id: I621c1644d9e94df3eb9f6f09c6de0fe51f0950a4 Also-by: Markus Duft Signed-off-by: Andrey Loskutov --- org.eclipse.jgit/src/org/eclipse/jgit/ignore/internal/PathMatcher.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'org.eclipse.jgit/src') diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/ignore/internal/PathMatcher.java b/org.eclipse.jgit/src/org/eclipse/jgit/ignore/internal/PathMatcher.java index dcecf303c4..d3e5f6a053 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/ignore/internal/PathMatcher.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/ignore/internal/PathMatcher.java @@ -217,7 +217,8 @@ public class PathMatcher extends AbstractMatcher { matcher++; match = matches(matcher, path, left, endExcl, assumeDirectory); - } else if (dirOnly) + } else if (dirOnly && !assumeDirectory) + // Directory expectations not met return false; } return match && matcher + 1 == matchers.size(); -- cgit v1.2.3 From 1cb566844116fc280969e4a89be8fa1c6e3a7d72 Mon Sep 17 00:00:00 2001 From: Marc Strapetz Date: Thu, 30 Oct 2014 19:36:49 +0100 Subject: Rename detection should canonicalize line endings Native Git canonicalizes line endings when detecting renames, more specifically it replaces CRLF by LF. See: hash_chars in diffcore-delta.c Bug: 449545 Change-Id: Iec2aab12ae9e67074cccb7fbd4d9defe176a0130 Signed-off-by: Marc Strapetz Signed-off-by: Matthias Sohn --- .../org/eclipse/jgit/diff/SimilarityIndexTest.java | 62 +++++++++++++++------ .../src/org/eclipse/jgit/diff/SimilarityIndex.java | 64 ++++++++++++++-------- 2 files changed, 86 insertions(+), 40 deletions(-) (limited to 'org.eclipse.jgit/src') diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/SimilarityIndexTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/SimilarityIndexTest.java index 95423609a9..4724677bb8 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/SimilarityIndexTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/SimilarityIndexTest.java @@ -83,7 +83,7 @@ public class SimilarityIndexTest { + "B\n" // + "B\n").getBytes("UTF-8"); SimilarityIndex si = new SimilarityIndex(); - si.hash(new ByteArrayInputStream(in), in.length); + si.hash(new ByteArrayInputStream(in), in.length, false); assertEquals(2, si.size()); } @@ -103,6 +103,48 @@ public class SimilarityIndexTest { assertEquals(100, dst.score(src, 100)); } + @Test + public void testCommonScore_SameFiles_CR_canonicalization() + throws TableFullException { + String text = "" // + + "A\r\n" // + + "B\r\n" // + + "D\r\n" // + + "B\r\n"; + SimilarityIndex src = hash(text); + SimilarityIndex dst = hash(text.replace("\r", "")); + assertEquals(8, src.common(dst)); + assertEquals(8, dst.common(src)); + + assertEquals(100, src.score(dst, 100)); + assertEquals(100, dst.score(src, 100)); + } + + @Test + public void testCommonScoreLargeObject_SameFiles_CR_canonicalization() + throws TableFullException, IOException { + String text = "" // + + "A\r\n" // + + "B\r\n" // + + "D\r\n" // + + "B\r\n"; + SimilarityIndex src = new SimilarityIndex(); + byte[] bytes1 = text.getBytes("UTF-8"); + src.hash(new ByteArrayInputStream(bytes1), bytes1.length, true); + src.sort(); + + SimilarityIndex dst = new SimilarityIndex(); + byte[] bytes2 = text.replace("\r", "").getBytes("UTF-8"); + dst.hash(new ByteArrayInputStream(bytes2), bytes2.length, true); + dst.sort(); + + assertEquals(8, src.common(dst)); + assertEquals(8, dst.common(src)); + + assertEquals(100, src.score(dst, 100)); + assertEquals(100, dst.score(src, 100)); + } + @Test public void testCommonScore_EmptyFiles() throws TableFullException { SimilarityIndex src = hash(""); @@ -132,24 +174,8 @@ public class SimilarityIndexTest { } private static SimilarityIndex hash(String text) throws TableFullException { - SimilarityIndex src = new SimilarityIndex() { - @Override - void hash(byte[] raw, int ptr, final int end) - throws TableFullException { - while (ptr < end) { - int hash = raw[ptr] & 0xff; - int start = ptr; - do { - int c = raw[ptr++] & 0xff; - if (c == '\n') - break; - } while (ptr < end && ptr - start < 64); - add(hash, ptr - start); - } - } - }; + SimilarityIndex src = new SimilarityIndex(); byte[] raw = Constants.encode(text); - src.setFileSize(raw.length); src.hash(raw, 0, raw.length); src.sort(); return src; diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/diff/SimilarityIndex.java b/org.eclipse.jgit/src/org/eclipse/jgit/diff/SimilarityIndex.java index 17ccb9726f..f376b8e36e 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/diff/SimilarityIndex.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/diff/SimilarityIndex.java @@ -79,8 +79,11 @@ class SimilarityIndex { /** Maximum value of the count field, also mask to extract the count. */ private static final long MAX_COUNT = (1L << KEY_SHIFT) - 1; - /** Total size of the file we hashed into the structure. */ - private long fileSize; + /** + * Total amount of bytes hashed into the structure, including \n. This is + * usually the size of the file minus number of CRLF encounters. + */ + private long hashedCnt; /** Number of non-zero entries in {@link #idHash}. */ private int idSize; @@ -108,48 +111,59 @@ class SimilarityIndex { idGrowAt = growAt(idHashBits); } - long getFileSize() { - return fileSize; - } - - void setFileSize(long size) { - fileSize = size; - } - void hash(ObjectLoader obj) throws MissingObjectException, IOException, TableFullException { if (obj.isLarge()) { - ObjectStream in = obj.openStream(); - try { - setFileSize(in.getSize()); - hash(in, fileSize); - } finally { - in.close(); - } + hashLargeObject(obj); } else { byte[] raw = obj.getCachedBytes(); - setFileSize(raw.length); hash(raw, 0, raw.length); } } + private void hashLargeObject(ObjectLoader obj) throws IOException, + TableFullException { + ObjectStream in1 = obj.openStream(); + boolean text; + try { + text = !RawText.isBinary(in1); + } finally { + in1.close(); + } + + ObjectStream in2 = obj.openStream(); + try { + hash(in2, in2.getSize(), text); + } finally { + in2.close(); + } + } + void hash(byte[] raw, int ptr, final int end) throws TableFullException { + final boolean text = !RawText.isBinary(raw); + hashedCnt = 0; while (ptr < end) { int hash = 5381; + int blockHashedCnt = 0; int start = ptr; // Hash one line, or one block, whichever occurs first. do { int c = raw[ptr++] & 0xff; + // Ignore CR in CRLF sequence if text + if (text && c == '\r' && ptr < end && raw[ptr] == '\n') + continue; + blockHashedCnt++; if (c == '\n') break; hash = (hash << 5) + hash + c; } while (ptr < end && ptr - start < 64); - add(hash, ptr - start); + hashedCnt += blockHashedCnt; + add(hash, blockHashedCnt); } } - void hash(InputStream in, long remaining) throws IOException, + void hash(InputStream in, long remaining, boolean text) throws IOException, TableFullException { byte[] buf = new byte[4096]; int ptr = 0; @@ -157,6 +171,7 @@ class SimilarityIndex { while (0 < remaining) { int hash = 5381; + int blockHashedCnt = 0; // Hash one line, or one block, whichever occurs first. int n = 0; @@ -170,11 +185,16 @@ class SimilarityIndex { n++; int c = buf[ptr++] & 0xff; + // Ignore CR in CRLF sequence if text + if (text && c == '\r' && ptr < cnt && buf[ptr] == '\n') + continue; + blockHashedCnt++; if (c == '\n') break; hash = (hash << 5) + hash + c; } while (n < 64 && n < remaining); - add(hash, n); + hashedCnt += blockHashedCnt; + add(hash, blockHashedCnt); remaining -= n; } } @@ -193,7 +213,7 @@ class SimilarityIndex { } int score(SimilarityIndex dst, int maxScore) { - long max = Math.max(fileSize, dst.fileSize); + long max = Math.max(hashedCnt, dst.hashedCnt); if (max == 0) return maxScore; return (int) ((common(dst) * maxScore) / max); -- cgit v1.2.3 From a4c2c8a3eea8a28b34e298f5ef3ccbd56dd87fac Mon Sep 17 00:00:00 2001 From: RĂ¼diger Herrmann Date: Fri, 17 Oct 2014 13:05:41 +0200 Subject: Trim author/committer name and email in commit header MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit C Git trims name and email before inserting them into the commit header so that " A U Thor " and " author@example.com " becomes "A U Thor " with a single separating space. This changes PersonIdent#toExternalString() to trim name and email before concatenating them. Change-Id: Idd77b659d0db957626824f6632e2da38d7731625 Signed-off-by: RĂ¼diger Herrmann --- .../tst/org/eclipse/jgit/lib/T0001_PersonIdentTest.java | 12 ++++++++++++ org.eclipse.jgit/src/org/eclipse/jgit/lib/PersonIdent.java | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-) (limited to 'org.eclipse.jgit/src') diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/T0001_PersonIdentTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/T0001_PersonIdentTest.java index 1b7276bf77..315c495606 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/T0001_PersonIdentTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/T0001_PersonIdentTest.java @@ -44,6 +44,7 @@ package org.eclipse.jgit.lib; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import java.util.Date; import java.util.TimeZone; @@ -83,4 +84,15 @@ public class T0001_PersonIdentTest { public void nullForEmailShouldThrowIllegalArgumentException() { new PersonIdent("A U Thor", null); } + + @Test + public void testToExternalStringTrimsNameAndEmail() throws Exception { + PersonIdent personIdent = new PersonIdent(" A U Thor ", + " author@example.com "); + + String externalString = personIdent.toExternalString(); + + assertTrue(externalString.startsWith("A U Thor ")); + } + } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/PersonIdent.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/PersonIdent.java index 69f7fd4404..8f7e3eff7c 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/PersonIdent.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/PersonIdent.java @@ -254,9 +254,9 @@ public class PersonIdent implements Serializable { */ public String toExternalString() { final StringBuilder r = new StringBuilder(); - r.append(getName()); + r.append(getName().trim()); r.append(" <"); //$NON-NLS-1$ - r.append(getEmailAddress()); + r.append(getEmailAddress().trim()); r.append("> "); //$NON-NLS-1$ r.append(when / 1000); r.append(' '); -- cgit v1.2.3