Browse Source

Don't keep empty ignore rules in the ignore node list

Change-Id: Icd893dfaba06561bbe5cc60ebf866ec5d8301c22
Signed-off-by: Andrey Loskutov <loskutov@gmx.de>
stable-4.1
Andrey Loskutov 10 years ago committed by Matthias Sohn
parent
commit
4e7639bb65
  1. 22
      org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/IgnoreNodeTest.java
  2. 8
      org.eclipse.jgit/src/org/eclipse/jgit/ignore/FastIgnoreRule.java
  3. 5
      org.eclipse.jgit/src/org/eclipse/jgit/ignore/IgnoreNode.java

22
org.eclipse.jgit.test/tst/org/eclipse/jgit/ignore/IgnoreNodeTest.java

@ -44,12 +44,15 @@ package org.eclipse.jgit.ignore;
import static org.eclipse.jgit.junit.Assert.assertEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import org.eclipse.jgit.errors.CorruptObjectException;
@ -323,6 +326,15 @@ public class IgnoreNodeTest extends RepositoryTestCase {
node.isIgnored("", false, true));
}
@Test
public void testEmptyIgnoreRules() throws IOException {
IgnoreNode node = new IgnoreNode();
node.parse(writeToString("", "#", "!", "[[=a=]]"));
assertEquals(new ArrayList<>(), node.getRules());
node.parse(writeToString(" ", " / "));
assertEquals(2, node.getRules().size());
}
@Test
public void testSlashOnlyMatchesDirectory() throws IOException {
writeIgnoreFile(".gitignore", "out/");
@ -472,4 +484,12 @@ public class IgnoreNodeTest extends RepositoryTestCase {
data.append(line + "\n");
writeTrashFile(name, data.toString());
}
private InputStream writeToString(String... rules) throws IOException {
StringBuilder data = new StringBuilder();
for (String line : rules) {
data.append(line + "\n");
}
return new ByteArrayInputStream(data.toString().getBytes("UTF-8"));
}
}

8
org.eclipse.jgit/src/org/eclipse/jgit/ignore/FastIgnoreRule.java

@ -188,6 +188,14 @@ public class FastIgnoreRule {
return !inverse;
}
/**
* @return true if the rule never matches (comment line or broken pattern)
* @since 4.1
*/
public boolean isEmpty() {
return matcher == NO_MATCH;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();

5
org.eclipse.jgit/src/org/eclipse/jgit/ignore/IgnoreNode.java

@ -110,7 +110,10 @@ public class IgnoreNode {
String txt;
while ((txt = br.readLine()) != null) {
if (txt.length() > 0 && !txt.startsWith("#") && !txt.equals("/")) { //$NON-NLS-1$ //$NON-NLS-2$
rules.add(new FastIgnoreRule(txt));
FastIgnoreRule rule = new FastIgnoreRule(txt);
if (!rule.isEmpty()) {
rules.add(rule);
}
}
}
}

Loading…
Cancel
Save