Browse Source
Core classes to parse and process .gitattributes files including support for reading attributes in WorkingTreeIterator and the dirCacheIterator. The implementation follows the git ignore implementation. It supports lazy reading attributes while walking the working tree. Bug: 342372 CQ: 9078 Change-Id: I05f3ce1861fbf9896b1bcb7816ba78af35f3ad3d Also-by: Marc Strapetz <marc.strapetz@syntevo.com> Also-by: Gunnar Wagenknecht <gunnar@wagenknecht.org> Also-by: Arthur Daussy <arthur.daussy@obeo.fr> Signed-off-by: Gunnar Wagenknecht <gunnar@wagenknecht.org> Signed-off-by: Marc Strapetz <marc.strapetz@syntevo.com> Signed-off-by: Arthur Daussy <arthur.daussy@obeo.fr> Signed-off-by: Chris Aniszczyk <caniszczyk@gmail.com> Signed-off-by: Matthias Sohn <matthias.sohn@sap.com> Signed-off-by: Chris Aniszczyk <caniszczyk@gmail.com>stable-3.7
Arthur Daussy
12 years ago
committed by
Chris Aniszczyk
18 changed files with 2090 additions and 23 deletions
@ -0,0 +1,182 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2014, Obeo. |
||||||
|
* and other copyright owners as documented in the project's IP log. |
||||||
|
* |
||||||
|
* This program and the accompanying materials are made available |
||||||
|
* under the terms of the Eclipse Distribution License v1.0 which |
||||||
|
* accompanies this distribution, is reproduced below, and is |
||||||
|
* available at http://www.eclipse.org/org/documents/edl-v10.php
|
||||||
|
* |
||||||
|
* All rights reserved. |
||||||
|
* |
||||||
|
* Redistribution and use in source and binary forms, with or |
||||||
|
* without modification, are permitted provided that the following |
||||||
|
* conditions are met: |
||||||
|
* |
||||||
|
* - Redistributions of source code must retain the above copyright |
||||||
|
* notice, this list of conditions and the following disclaimer. |
||||||
|
* |
||||||
|
* - Redistributions in binary form must reproduce the above |
||||||
|
* copyright notice, this list of conditions and the following |
||||||
|
* disclaimer in the documentation and/or other materials provided |
||||||
|
* with the distribution. |
||||||
|
* |
||||||
|
* - Neither the name of the Eclipse Foundation, Inc. nor the |
||||||
|
* names of its contributors may be used to endorse or promote |
||||||
|
* products derived from this software without specific prior |
||||||
|
* written permission. |
||||||
|
* |
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
||||||
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
||||||
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||||||
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||||
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||||||
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||||
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
||||||
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||||
|
*/ |
||||||
|
package org.eclipse.jgit.attributes; |
||||||
|
|
||||||
|
import static org.eclipse.jgit.attributes.Attribute.State.SET; |
||||||
|
import static org.eclipse.jgit.attributes.Attribute.State.UNSET; |
||||||
|
import static org.junit.Assert.assertEquals; |
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream; |
||||||
|
import java.io.IOException; |
||||||
|
import java.io.InputStream; |
||||||
|
import java.util.Collections; |
||||||
|
import java.util.HashMap; |
||||||
|
import java.util.HashSet; |
||||||
|
import java.util.Set; |
||||||
|
|
||||||
|
import org.junit.After; |
||||||
|
import org.junit.Test; |
||||||
|
|
||||||
|
/** |
||||||
|
* Test {@link AttributesNode} |
||||||
|
*/ |
||||||
|
public class AttributeNodeTest { |
||||||
|
|
||||||
|
private static final Attribute A_SET_ATTR = new Attribute("A", SET); |
||||||
|
|
||||||
|
private static final Attribute A_UNSET_ATTR = new Attribute("A", UNSET); |
||||||
|
|
||||||
|
private static final Attribute B_SET_ATTR = new Attribute("B", SET); |
||||||
|
|
||||||
|
private static final Attribute B_UNSET_ATTR = new Attribute("B", UNSET); |
||||||
|
|
||||||
|
private static final Attribute C_VALUE_ATTR = new Attribute("C", "value"); |
||||||
|
|
||||||
|
private static final Attribute C_VALUE2_ATTR = new Attribute("C", "value2"); |
||||||
|
|
||||||
|
private InputStream is; |
||||||
|
|
||||||
|
@After |
||||||
|
public void after() throws IOException { |
||||||
|
if (is != null) |
||||||
|
is.close(); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testBasic() throws IOException { |
||||||
|
String attributeFileContent = "*.type1 A -B C=value\n" |
||||||
|
+ "*.type2 -A B C=value2"; |
||||||
|
|
||||||
|
is = new ByteArrayInputStream(attributeFileContent.getBytes()); |
||||||
|
AttributesNode node = new AttributesNode(); |
||||||
|
node.parse(is); |
||||||
|
assertAttribute("file.type1", node, |
||||||
|
asSet(A_SET_ATTR, B_UNSET_ATTR, C_VALUE_ATTR)); |
||||||
|
assertAttribute("file.type2", node, |
||||||
|
asSet(A_UNSET_ATTR, B_SET_ATTR, C_VALUE2_ATTR)); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testNegativePattern() throws IOException { |
||||||
|
String attributeFileContent = "!*.type1 A -B C=value\n" |
||||||
|
+ "!*.type2 -A B C=value2"; |
||||||
|
|
||||||
|
is = new ByteArrayInputStream(attributeFileContent.getBytes()); |
||||||
|
AttributesNode node = new AttributesNode(); |
||||||
|
node.parse(is); |
||||||
|
assertAttribute("file.type1", node, Collections.<Attribute> emptySet()); |
||||||
|
assertAttribute("file.type2", node, Collections.<Attribute> emptySet()); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testEmptyNegativeAttributeKey() throws IOException { |
||||||
|
String attributeFileContent = "*.type1 - \n" //
|
||||||
|
+ "*.type2 - -A"; |
||||||
|
is = new ByteArrayInputStream(attributeFileContent.getBytes()); |
||||||
|
AttributesNode node = new AttributesNode(); |
||||||
|
node.parse(is); |
||||||
|
assertAttribute("file.type1", node, Collections.<Attribute> emptySet()); |
||||||
|
assertAttribute("file.type2", node, asSet(A_UNSET_ATTR)); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testEmptyValueKey() throws IOException { |
||||||
|
String attributeFileContent = "*.type1 = \n" //
|
||||||
|
+ "*.type2 =value\n"//
|
||||||
|
+ "*.type3 attr=\n"; |
||||||
|
is = new ByteArrayInputStream(attributeFileContent.getBytes()); |
||||||
|
AttributesNode node = new AttributesNode(); |
||||||
|
node.parse(is); |
||||||
|
assertAttribute("file.type1", node, Collections.<Attribute> emptySet()); |
||||||
|
assertAttribute("file.type2", node, Collections.<Attribute> emptySet()); |
||||||
|
assertAttribute("file.type3", node, asSet(new Attribute("attr", ""))); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testEmptyLine() throws IOException { |
||||||
|
String attributeFileContent = "*.type1 A -B C=value\n" //
|
||||||
|
+ "\n" //
|
||||||
|
+ " \n" //
|
||||||
|
+ "*.type2 -A B C=value2"; |
||||||
|
|
||||||
|
is = new ByteArrayInputStream(attributeFileContent.getBytes()); |
||||||
|
AttributesNode node = new AttributesNode(); |
||||||
|
node.parse(is); |
||||||
|
assertAttribute("file.type1", node, |
||||||
|
asSet(A_SET_ATTR, B_UNSET_ATTR, C_VALUE_ATTR)); |
||||||
|
assertAttribute("file.type2", node, |
||||||
|
asSet(A_UNSET_ATTR, B_SET_ATTR, C_VALUE2_ATTR)); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testTabSeparator() throws IOException { |
||||||
|
String attributeFileContent = "*.type1 \tA -B\tC=value\n" |
||||||
|
+ "*.type2\t -A\tB C=value2\n" //
|
||||||
|
+ "*.type3 \t\t B\n" //
|
||||||
|
+ "*.type3\t-A";//
|
||||||
|
|
||||||
|
is = new ByteArrayInputStream(attributeFileContent.getBytes()); |
||||||
|
AttributesNode node = new AttributesNode(); |
||||||
|
node.parse(is); |
||||||
|
assertAttribute("file.type1", node, |
||||||
|
asSet(A_SET_ATTR, B_UNSET_ATTR, C_VALUE_ATTR)); |
||||||
|
assertAttribute("file.type2", node, |
||||||
|
asSet(A_UNSET_ATTR, B_SET_ATTR, C_VALUE2_ATTR)); |
||||||
|
assertAttribute("file.type3", node, asSet(A_UNSET_ATTR, B_SET_ATTR)); |
||||||
|
} |
||||||
|
|
||||||
|
private void assertAttribute(String path, AttributesNode node, |
||||||
|
Set<Attribute> attrs) { |
||||||
|
HashMap<String, Attribute> attributes = new HashMap<String, Attribute>(); |
||||||
|
node.getAttributes(path, false, attributes); |
||||||
|
assertEquals(attrs, new HashSet<Attribute>(attributes.values())); |
||||||
|
} |
||||||
|
|
||||||
|
static Set<Attribute> asSet(Attribute... attrs) { |
||||||
|
Set<Attribute> result = new HashSet<Attribute>(); |
||||||
|
for (Attribute attr : attrs) |
||||||
|
result.add(attr); |
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,77 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2010, Marc Strapetz <marc.strapetz@syntevo.com> |
||||||
|
* Copyright (C) 2013, Gunnar Wagenknecht |
||||||
|
* and other copyright owners as documented in the project's IP log. |
||||||
|
* |
||||||
|
* This program and the accompanying materials are made available |
||||||
|
* under the terms of the Eclipse Distribution License v1.0 which |
||||||
|
* accompanies this distribution, is reproduced below, and is |
||||||
|
* available at http://www.eclipse.org/org/documents/edl-v10.php
|
||||||
|
* |
||||||
|
* All rights reserved. |
||||||
|
* |
||||||
|
* Redistribution and use in source and binary forms, with or |
||||||
|
* without modification, are permitted provided that the following |
||||||
|
* conditions are met: |
||||||
|
* |
||||||
|
* - Redistributions of source code must retain the above copyright |
||||||
|
* notice, this list of conditions and the following disclaimer. |
||||||
|
* |
||||||
|
* - Redistributions in binary form must reproduce the above |
||||||
|
* copyright notice, this list of conditions and the following |
||||||
|
* disclaimer in the documentation and/or other materials provided |
||||||
|
* with the distribution. |
||||||
|
* |
||||||
|
* - Neither the name of the Eclipse Foundation, Inc. nor the |
||||||
|
* names of its contributors may be used to endorse or promote |
||||||
|
* products derived from this software without specific prior |
||||||
|
* written permission. |
||||||
|
* |
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
||||||
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
||||||
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||||||
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||||
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||||||
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||||
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
||||||
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||||
|
*/ |
||||||
|
package org.eclipse.jgit.attributes; |
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals; |
||||||
|
import static org.junit.Assert.assertNull; |
||||||
|
|
||||||
|
import org.eclipse.jgit.attributes.Attribute.State; |
||||||
|
import org.junit.Test; |
||||||
|
|
||||||
|
/** |
||||||
|
* Tests {@link Attribute} |
||||||
|
*/ |
||||||
|
public class AttributeTest { |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testBasic() { |
||||||
|
Attribute a = new Attribute("delta", State.SET); |
||||||
|
assertEquals(a.getKey(), "delta"); |
||||||
|
assertEquals(a.getState(), State.SET); |
||||||
|
assertNull(a.getValue()); |
||||||
|
assertEquals(a.toString(), "delta"); |
||||||
|
|
||||||
|
a = new Attribute("delta", State.UNSET); |
||||||
|
assertEquals(a.getKey(), "delta"); |
||||||
|
assertEquals(a.getState(), State.UNSET); |
||||||
|
assertNull(a.getValue()); |
||||||
|
assertEquals(a.toString(), "-delta"); |
||||||
|
|
||||||
|
a = new Attribute("delta", "value"); |
||||||
|
assertEquals(a.getKey(), "delta"); |
||||||
|
assertEquals(a.getState(), State.CUSTOM); |
||||||
|
assertEquals(a.getValue(), "value"); |
||||||
|
assertEquals(a.toString(), "delta=value"); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,412 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2010, Red Hat Inc. |
||||||
|
* and other copyright owners as documented in the project's IP log. |
||||||
|
* |
||||||
|
* This program and the accompanying materials are made available |
||||||
|
* under the terms of the Eclipse Distribution License v1.0 which |
||||||
|
* accompanies this distribution, is reproduced below, and is |
||||||
|
* available at http://www.eclipse.org/org/documents/edl-v10.php
|
||||||
|
* |
||||||
|
* All rights reserved. |
||||||
|
* |
||||||
|
* Redistribution and use in source and binary forms, with or |
||||||
|
* without modification, are permitted provided that the following |
||||||
|
* conditions are met: |
||||||
|
* |
||||||
|
* - Redistributions of source code must retain the above copyright |
||||||
|
* notice, this list of conditions and the following disclaimer. |
||||||
|
* |
||||||
|
* - Redistributions in binary form must reproduce the above |
||||||
|
* copyright notice, this list of conditions and the following |
||||||
|
* disclaimer in the documentation and/or other materials provided |
||||||
|
* with the distribution. |
||||||
|
* |
||||||
|
* - Neither the name of the Eclipse Foundation, Inc. nor the |
||||||
|
* names of its contributors may be used to endorse or promote |
||||||
|
* products derived from this software without specific prior |
||||||
|
* written permission. |
||||||
|
* |
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
||||||
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
||||||
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||||||
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||||
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||||||
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||||
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
||||||
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||||
|
*/ |
||||||
|
package org.eclipse.jgit.attributes; |
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals; |
||||||
|
import static org.junit.Assert.assertFalse; |
||||||
|
import static org.junit.Assert.assertNotNull; |
||||||
|
import static org.junit.Assert.assertTrue; |
||||||
|
|
||||||
|
import org.junit.Test; |
||||||
|
|
||||||
|
/** |
||||||
|
* Tests git attributes pattern matches |
||||||
|
* <p> |
||||||
|
* Inspired by {@link org.eclipse.jgit.ignore.IgnoreMatcherTest} |
||||||
|
* </p> |
||||||
|
*/ |
||||||
|
public class AttributesMatcherTest { |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testBasic() { |
||||||
|
String pattern = "/test.stp"; |
||||||
|
assertMatched(pattern, "/test.stp"); |
||||||
|
|
||||||
|
pattern = "#/test.stp"; |
||||||
|
assertNotMatched(pattern, "/test.stp"); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testFileNameWildcards() { |
||||||
|
//Test basic * and ? for any pattern + any character
|
||||||
|
String pattern = "*.st?"; |
||||||
|
assertMatched(pattern, "/test.stp"); |
||||||
|
assertMatched(pattern, "/anothertest.stg"); |
||||||
|
assertMatched(pattern, "/anothertest.st0"); |
||||||
|
assertNotMatched(pattern, "/anothertest.sta1"); |
||||||
|
//Check that asterisk does not expand to "/"
|
||||||
|
assertNotMatched(pattern, "/another/test.sta1"); |
||||||
|
|
||||||
|
//Same as above, with a leading slash to ensure that doesn't cause problems
|
||||||
|
pattern = "/*.st?"; |
||||||
|
assertMatched(pattern, "/test.stp"); |
||||||
|
assertMatched(pattern, "/anothertest.stg"); |
||||||
|
assertMatched(pattern, "/anothertest.st0"); |
||||||
|
assertNotMatched(pattern, "/anothertest.sta1"); |
||||||
|
//Check that asterisk does not expand to "/"
|
||||||
|
assertNotMatched(pattern, "/another/test.sta1"); |
||||||
|
|
||||||
|
//Test for numbers
|
||||||
|
pattern = "*.sta[0-5]"; |
||||||
|
assertMatched(pattern, "/test.sta5"); |
||||||
|
assertMatched(pattern, "/test.sta4"); |
||||||
|
assertMatched(pattern, "/test.sta3"); |
||||||
|
assertMatched(pattern, "/test.sta2"); |
||||||
|
assertMatched(pattern, "/test.sta1"); |
||||||
|
assertMatched(pattern, "/test.sta0"); |
||||||
|
assertMatched(pattern, "/anothertest.sta2"); |
||||||
|
assertNotMatched(pattern, "test.stag"); |
||||||
|
assertNotMatched(pattern, "test.sta6"); |
||||||
|
|
||||||
|
//Test for letters
|
||||||
|
pattern = "/[tv]est.sta[a-d]"; |
||||||
|
assertMatched(pattern, "/test.staa"); |
||||||
|
assertMatched(pattern, "/test.stab"); |
||||||
|
assertMatched(pattern, "/test.stac"); |
||||||
|
assertMatched(pattern, "/test.stad"); |
||||||
|
assertMatched(pattern, "/vest.stac"); |
||||||
|
assertNotMatched(pattern, "test.stae"); |
||||||
|
assertNotMatched(pattern, "test.sta9"); |
||||||
|
|
||||||
|
//Test child directory/file is matched
|
||||||
|
pattern = "/src/ne?"; |
||||||
|
assertMatched(pattern, "/src/new/"); |
||||||
|
assertMatched(pattern, "/src/new"); |
||||||
|
assertMatched(pattern, "/src/new/a.c"); |
||||||
|
assertMatched(pattern, "/src/new/a/a.c"); |
||||||
|
assertNotMatched(pattern, "/src/new.c"); |
||||||
|
|
||||||
|
//Test name-only fnmatcher matches
|
||||||
|
pattern = "ne?"; |
||||||
|
assertMatched(pattern, "/src/new/"); |
||||||
|
assertMatched(pattern, "/src/new"); |
||||||
|
assertMatched(pattern, "/src/new/a.c"); |
||||||
|
assertMatched(pattern, "/src/new/a/a.c"); |
||||||
|
assertMatched(pattern, "/neb"); |
||||||
|
assertNotMatched(pattern, "/src/new.c"); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testTargetWithoutLeadingSlash() { |
||||||
|
//Test basic * and ? for any pattern + any character
|
||||||
|
String pattern = "/*.st?"; |
||||||
|
assertMatched(pattern, "test.stp"); |
||||||
|
assertMatched(pattern, "anothertest.stg"); |
||||||
|
assertMatched(pattern, "anothertest.st0"); |
||||||
|
assertNotMatched(pattern, "anothertest.sta1"); |
||||||
|
//Check that asterisk does not expand to ""
|
||||||
|
assertNotMatched(pattern, "another/test.sta1"); |
||||||
|
|
||||||
|
//Same as above, with a leading slash to ensure that doesn't cause problems
|
||||||
|
pattern = "/*.st?"; |
||||||
|
assertMatched(pattern, "test.stp"); |
||||||
|
assertMatched(pattern, "anothertest.stg"); |
||||||
|
assertMatched(pattern, "anothertest.st0"); |
||||||
|
assertNotMatched(pattern, "anothertest.sta1"); |
||||||
|
//Check that asterisk does not expand to ""
|
||||||
|
assertNotMatched(pattern, "another/test.sta1"); |
||||||
|
|
||||||
|
//Test for numbers
|
||||||
|
pattern = "/*.sta[0-5]"; |
||||||
|
assertMatched(pattern, "test.sta5"); |
||||||
|
assertMatched(pattern, "test.sta4"); |
||||||
|
assertMatched(pattern, "test.sta3"); |
||||||
|
assertMatched(pattern, "test.sta2"); |
||||||
|
assertMatched(pattern, "test.sta1"); |
||||||
|
assertMatched(pattern, "test.sta0"); |
||||||
|
assertMatched(pattern, "anothertest.sta2"); |
||||||
|
assertNotMatched(pattern, "test.stag"); |
||||||
|
assertNotMatched(pattern, "test.sta6"); |
||||||
|
|
||||||
|
//Test for letters
|
||||||
|
pattern = "/[tv]est.sta[a-d]"; |
||||||
|
assertMatched(pattern, "test.staa"); |
||||||
|
assertMatched(pattern, "test.stab"); |
||||||
|
assertMatched(pattern, "test.stac"); |
||||||
|
assertMatched(pattern, "test.stad"); |
||||||
|
assertMatched(pattern, "vest.stac"); |
||||||
|
assertNotMatched(pattern, "test.stae"); |
||||||
|
assertNotMatched(pattern, "test.sta9"); |
||||||
|
|
||||||
|
//Test child directory/file is matched
|
||||||
|
pattern = "/src/ne?"; |
||||||
|
assertMatched(pattern, "src/new/"); |
||||||
|
assertMatched(pattern, "src/new"); |
||||||
|
assertMatched(pattern, "src/new/a.c"); |
||||||
|
assertMatched(pattern, "src/new/a/a.c"); |
||||||
|
assertNotMatched(pattern, "src/new.c"); |
||||||
|
|
||||||
|
//Test name-only fnmatcher matches
|
||||||
|
pattern = "ne?"; |
||||||
|
assertMatched(pattern, "src/new/"); |
||||||
|
assertMatched(pattern, "src/new"); |
||||||
|
assertMatched(pattern, "src/new/a.c"); |
||||||
|
assertMatched(pattern, "src/new/a/a.c"); |
||||||
|
assertMatched(pattern, "neb"); |
||||||
|
assertNotMatched(pattern, "src/new.c"); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testParentDirectoryGitAttributes() { |
||||||
|
//Contains git attribute patterns such as might be seen in a parent directory
|
||||||
|
|
||||||
|
//Test for wildcards
|
||||||
|
String pattern = "/*/*.c"; |
||||||
|
assertMatched(pattern, "/file/a.c"); |
||||||
|
assertMatched(pattern, "/src/a.c"); |
||||||
|
assertNotMatched(pattern, "/src/new/a.c"); |
||||||
|
|
||||||
|
//Test child directory/file is matched
|
||||||
|
pattern = "/src/new"; |
||||||
|
assertMatched(pattern, "/src/new/"); |
||||||
|
assertMatched(pattern, "/src/new"); |
||||||
|
assertMatched(pattern, "/src/new/a.c"); |
||||||
|
assertMatched(pattern, "/src/new/a/a.c"); |
||||||
|
assertNotMatched(pattern, "/src/new.c"); |
||||||
|
|
||||||
|
//Test child directory is matched, slash after name
|
||||||
|
pattern = "/src/new/"; |
||||||
|
assertMatched(pattern, "/src/new/"); |
||||||
|
assertMatched(pattern, "/src/new/a.c"); |
||||||
|
assertMatched(pattern, "/src/new/a/a.c"); |
||||||
|
assertNotMatched(pattern, "/src/new"); |
||||||
|
assertNotMatched(pattern, "/src/new.c"); |
||||||
|
|
||||||
|
//Test directory is matched by name only
|
||||||
|
pattern = "b1"; |
||||||
|
assertMatched(pattern, "/src/new/a/b1/a.c"); |
||||||
|
assertNotMatched(pattern, "/src/new/a/b2/file.c"); |
||||||
|
assertNotMatched(pattern, "/src/new/a/bb1/file.c"); |
||||||
|
assertNotMatched(pattern, "/src/new/a/file.c"); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testTrailingSlash() { |
||||||
|
String pattern = "/src/"; |
||||||
|
assertMatched(pattern, "/src/"); |
||||||
|
assertMatched(pattern, "/src/new"); |
||||||
|
assertMatched(pattern, "/src/new/a.c"); |
||||||
|
assertMatched(pattern, "/src/a.c"); |
||||||
|
assertNotMatched(pattern, "/src"); |
||||||
|
assertNotMatched(pattern, "/srcA/"); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testNameOnlyMatches() { |
||||||
|
/* |
||||||
|
* Name-only matches do not contain any path separators |
||||||
|
*/ |
||||||
|
//Test matches for file extension
|
||||||
|
String pattern = "*.stp"; |
||||||
|
assertMatched(pattern, "/test.stp"); |
||||||
|
assertMatched(pattern, "/src/test.stp"); |
||||||
|
assertNotMatched(pattern, "/test.stp1"); |
||||||
|
assertNotMatched(pattern, "/test.astp"); |
||||||
|
|
||||||
|
//Test matches for name-only, applies to file name or folder name
|
||||||
|
pattern = "src"; |
||||||
|
assertMatched(pattern, "/src"); |
||||||
|
assertMatched(pattern, "/src/"); |
||||||
|
assertMatched(pattern, "/src/a.c"); |
||||||
|
assertMatched(pattern, "/src/new/a.c"); |
||||||
|
assertMatched(pattern, "/new/src/a.c"); |
||||||
|
assertMatched(pattern, "/file/src"); |
||||||
|
|
||||||
|
//Test matches for name-only, applies only to folder names
|
||||||
|
pattern = "src/"; |
||||||
|
assertMatched(pattern, "/src/"); |
||||||
|
assertMatched(pattern, "/src/a.c"); |
||||||
|
assertMatched(pattern, "/src/new/a.c"); |
||||||
|
assertMatched(pattern, "/new/src/a.c"); |
||||||
|
assertNotMatched(pattern, "/src"); |
||||||
|
assertNotMatched(pattern, "/file/src"); |
||||||
|
|
||||||
|
//Test matches for name-only, applies to file name or folder name
|
||||||
|
//With a small wildcard
|
||||||
|
pattern = "?rc"; |
||||||
|
assertMatched(pattern, "/src/a.c"); |
||||||
|
assertMatched(pattern, "/src/new/a.c"); |
||||||
|
assertMatched(pattern, "/new/src/a.c"); |
||||||
|
assertMatched(pattern, "/file/src"); |
||||||
|
assertMatched(pattern, "/src/"); |
||||||
|
|
||||||
|
//Test matches for name-only, applies to file name or folder name
|
||||||
|
//With a small wildcard
|
||||||
|
pattern = "?r[a-c]"; |
||||||
|
assertMatched(pattern, "/src/a.c"); |
||||||
|
assertMatched(pattern, "/src/new/a.c"); |
||||||
|
assertMatched(pattern, "/new/src/a.c"); |
||||||
|
assertMatched(pattern, "/file/src"); |
||||||
|
assertMatched(pattern, "/src/"); |
||||||
|
assertMatched(pattern, "/srb/a.c"); |
||||||
|
assertMatched(pattern, "/grb/new/a.c"); |
||||||
|
assertMatched(pattern, "/new/crb/a.c"); |
||||||
|
assertMatched(pattern, "/file/3rb"); |
||||||
|
assertMatched(pattern, "/xrb/"); |
||||||
|
assertMatched(pattern, "/3ra/a.c"); |
||||||
|
assertMatched(pattern, "/5ra/new/a.c"); |
||||||
|
assertMatched(pattern, "/new/1ra/a.c"); |
||||||
|
assertMatched(pattern, "/file/dra"); |
||||||
|
assertMatched(pattern, "/era/"); |
||||||
|
assertNotMatched(pattern, "/crg"); |
||||||
|
assertNotMatched(pattern, "/cr3"); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testGetters() { |
||||||
|
AttributesRule r = new AttributesRule("/pattern/", ""); |
||||||
|
assertFalse(r.isNameOnly()); |
||||||
|
assertTrue(r.dirOnly()); |
||||||
|
assertNotNull(r.getAttributes()); |
||||||
|
assertTrue(r.getAttributes().isEmpty()); |
||||||
|
assertEquals(r.getPattern(), "/pattern"); |
||||||
|
|
||||||
|
r = new AttributesRule("/patter?/", ""); |
||||||
|
assertFalse(r.isNameOnly()); |
||||||
|
assertTrue(r.dirOnly()); |
||||||
|
assertNotNull(r.getAttributes()); |
||||||
|
assertTrue(r.getAttributes().isEmpty()); |
||||||
|
assertEquals(r.getPattern(), "/patter?"); |
||||||
|
|
||||||
|
r = new AttributesRule("patt*", ""); |
||||||
|
assertTrue(r.isNameOnly()); |
||||||
|
assertFalse(r.dirOnly()); |
||||||
|
assertNotNull(r.getAttributes()); |
||||||
|
assertTrue(r.getAttributes().isEmpty()); |
||||||
|
assertEquals(r.getPattern(), "patt*"); |
||||||
|
|
||||||
|
r = new AttributesRule("pattern", "attribute1"); |
||||||
|
assertTrue(r.isNameOnly()); |
||||||
|
assertFalse(r.dirOnly()); |
||||||
|
assertNotNull(r.getAttributes()); |
||||||
|
assertFalse(r.getAttributes().isEmpty()); |
||||||
|
assertEquals(r.getAttributes().size(), 1); |
||||||
|
assertEquals(r.getPattern(), "pattern"); |
||||||
|
|
||||||
|
r = new AttributesRule("pattern", "attribute1 -attribute2"); |
||||||
|
assertTrue(r.isNameOnly()); |
||||||
|
assertFalse(r.dirOnly()); |
||||||
|
assertNotNull(r.getAttributes()); |
||||||
|
assertEquals(r.getAttributes().size(), 2); |
||||||
|
assertEquals(r.getPattern(), "pattern"); |
||||||
|
|
||||||
|
r = new AttributesRule("pattern", "attribute1 \t-attribute2 \t"); |
||||||
|
assertTrue(r.isNameOnly()); |
||||||
|
assertFalse(r.dirOnly()); |
||||||
|
assertNotNull(r.getAttributes()); |
||||||
|
assertEquals(r.getAttributes().size(), 2); |
||||||
|
assertEquals(r.getPattern(), "pattern"); |
||||||
|
|
||||||
|
r = new AttributesRule("pattern", "attribute1\t-attribute2\t"); |
||||||
|
assertTrue(r.isNameOnly()); |
||||||
|
assertFalse(r.dirOnly()); |
||||||
|
assertNotNull(r.getAttributes()); |
||||||
|
assertEquals(r.getAttributes().size(), 2); |
||||||
|
assertEquals(r.getPattern(), "pattern"); |
||||||
|
|
||||||
|
r = new AttributesRule("pattern", "attribute1\t -attribute2\t "); |
||||||
|
assertTrue(r.isNameOnly()); |
||||||
|
assertFalse(r.dirOnly()); |
||||||
|
assertNotNull(r.getAttributes()); |
||||||
|
assertEquals(r.getAttributes().size(), 2); |
||||||
|
assertEquals(r.getPattern(), "pattern"); |
||||||
|
|
||||||
|
r = new AttributesRule("pattern", |
||||||
|
"attribute1 -attribute2 attribute3=value "); |
||||||
|
assertTrue(r.isNameOnly()); |
||||||
|
assertFalse(r.dirOnly()); |
||||||
|
assertNotNull(r.getAttributes()); |
||||||
|
assertEquals(r.getAttributes().size(), 3); |
||||||
|
assertEquals(r.getPattern(), "pattern"); |
||||||
|
assertEquals(r.getAttributes().get(0).toString(), "attribute1"); |
||||||
|
assertEquals(r.getAttributes().get(1).toString(), "-attribute2"); |
||||||
|
assertEquals(r.getAttributes().get(2).toString(), "attribute3=value"); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Check for a match. If target ends with "/", match will assume that the |
||||||
|
* target is meant to be a directory. |
||||||
|
* |
||||||
|
* @param pattern |
||||||
|
* Pattern as it would appear in a .gitattributes file |
||||||
|
* @param target |
||||||
|
* Target file path relative to repository's GIT_DIR |
||||||
|
*/ |
||||||
|
public void assertMatched(String pattern, String target) { |
||||||
|
boolean value = match(pattern, target); |
||||||
|
assertTrue("Expected a match for: " + pattern + " with: " + target, |
||||||
|
value); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Check for a match. If target ends with "/", match will assume that the |
||||||
|
* target is meant to be a directory. |
||||||
|
* |
||||||
|
* @param pattern |
||||||
|
* Pattern as it would appear in a .gitattributes file |
||||||
|
* @param target |
||||||
|
* Target file path relative to repository's GIT_DIR |
||||||
|
*/ |
||||||
|
public void assertNotMatched(String pattern, String target) { |
||||||
|
boolean value = match(pattern, target); |
||||||
|
assertFalse("Expected no match for: " + pattern + " with: " + target, |
||||||
|
value); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Check for a match. If target ends with "/", match will assume that the |
||||||
|
* target is meant to be a directory. |
||||||
|
* |
||||||
|
* @param pattern |
||||||
|
* Pattern as it would appear in a .gitattributes file |
||||||
|
* @param target |
||||||
|
* Target file path relative to repository's GIT_DIR |
||||||
|
* @return Result of {@link AttributesRule#isMatch(String, boolean)} |
||||||
|
*/ |
||||||
|
private static boolean match(String pattern, String target) { |
||||||
|
AttributesRule r = new AttributesRule(pattern, ""); |
||||||
|
//If speed of this test is ever an issue, we can use a presetRule field
|
||||||
|
//to avoid recompiling a pattern each time.
|
||||||
|
return r.isMatch(target, target.endsWith("/")); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,296 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2010, Red Hat Inc. |
||||||
|
* and other copyright owners as documented in the project's IP log. |
||||||
|
* |
||||||
|
* This program and the accompanying materials are made available |
||||||
|
* under the terms of the Eclipse Distribution License v1.0 which |
||||||
|
* accompanies this distribution, is reproduced below, and is |
||||||
|
* available at http://www.eclipse.org/org/documents/edl-v10.php
|
||||||
|
* |
||||||
|
* All rights reserved. |
||||||
|
* |
||||||
|
* Redistribution and use in source and binary forms, with or |
||||||
|
* without modification, are permitted provided that the following |
||||||
|
* conditions are met: |
||||||
|
* |
||||||
|
* - Redistributions of source code must retain the above copyright |
||||||
|
* notice, this list of conditions and the following disclaimer. |
||||||
|
* |
||||||
|
* - Redistributions in binary form must reproduce the above |
||||||
|
* copyright notice, this list of conditions and the following |
||||||
|
* disclaimer in the documentation and/or other materials provided |
||||||
|
* with the distribution. |
||||||
|
* |
||||||
|
* - Neither the name of the Eclipse Foundation, Inc. nor the |
||||||
|
* names of its contributors may be used to endorse or promote |
||||||
|
* products derived from this software without specific prior |
||||||
|
* written permission. |
||||||
|
* |
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
||||||
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
||||||
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||||||
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||||
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||||||
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||||
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
||||||
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||||
|
*/ |
||||||
|
package org.eclipse.jgit.attributes; |
||||||
|
|
||||||
|
import static java.util.Arrays.asList; |
||||||
|
import static org.hamcrest.CoreMatchers.hasItem; |
||||||
|
import static org.hamcrest.MatcherAssert.assertThat; |
||||||
|
import static org.junit.Assert.assertEquals; |
||||||
|
import static org.junit.Assert.assertFalse; |
||||||
|
import static org.junit.Assert.assertNotNull; |
||||||
|
import static org.junit.Assert.assertTrue; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
import java.util.Collections; |
||||||
|
import java.util.LinkedHashMap; |
||||||
|
import java.util.List; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
import org.eclipse.jgit.api.Git; |
||||||
|
import org.eclipse.jgit.attributes.Attribute.State; |
||||||
|
import org.eclipse.jgit.dircache.DirCacheIterator; |
||||||
|
import org.eclipse.jgit.junit.RepositoryTestCase; |
||||||
|
import org.eclipse.jgit.lib.FileMode; |
||||||
|
import org.eclipse.jgit.treewalk.TreeWalk; |
||||||
|
import org.junit.Before; |
||||||
|
import org.junit.Test; |
||||||
|
|
||||||
|
/** |
||||||
|
* Tests attributes node behavior on the the index. |
||||||
|
*/ |
||||||
|
public class AttributesNodeDirCacheIteratorTest extends RepositoryTestCase { |
||||||
|
|
||||||
|
private static final FileMode D = FileMode.TREE; |
||||||
|
|
||||||
|
private static final FileMode F = FileMode.REGULAR_FILE; |
||||||
|
|
||||||
|
private static Attribute EOL_LF = new Attribute("eol", "lf"); |
||||||
|
|
||||||
|
private static Attribute DELTA_UNSET = new Attribute("delta", State.UNSET); |
||||||
|
|
||||||
|
private Git git; |
||||||
|
|
||||||
|
private TreeWalk walk; |
||||||
|
|
||||||
|
@Override |
||||||
|
@Before |
||||||
|
public void setUp() throws Exception { |
||||||
|
super.setUp(); |
||||||
|
git = new Git(db); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testRules() throws Exception { |
||||||
|
writeAttributesFile(".git/info/attributes", "windows* eol=crlf"); |
||||||
|
|
||||||
|
writeAttributesFile(".gitattributes", "*.txt eol=lf"); |
||||||
|
writeTrashFile("windows.file", ""); |
||||||
|
writeTrashFile("windows.txt", ""); |
||||||
|
writeTrashFile("readme.txt", ""); |
||||||
|
|
||||||
|
writeAttributesFile("src/config/.gitattributes", "*.txt -delta"); |
||||||
|
writeTrashFile("src/config/readme.txt", ""); |
||||||
|
writeTrashFile("src/config/windows.file", ""); |
||||||
|
writeTrashFile("src/config/windows.txt", ""); |
||||||
|
|
||||||
|
// Adds file to index
|
||||||
|
git.add().addFilepattern(".").call(); |
||||||
|
|
||||||
|
walk = beginWalk(); |
||||||
|
|
||||||
|
assertIteration(F, ".gitattributes"); |
||||||
|
assertIteration(F, "readme.txt", asList(EOL_LF)); |
||||||
|
|
||||||
|
assertIteration(D, "src"); |
||||||
|
|
||||||
|
assertIteration(D, "src/config"); |
||||||
|
assertIteration(F, "src/config/.gitattributes"); |
||||||
|
assertIteration(F, "src/config/readme.txt", asList(DELTA_UNSET)); |
||||||
|
assertIteration(F, "src/config/windows.file", null); |
||||||
|
assertIteration(F, "src/config/windows.txt", asList(DELTA_UNSET)); |
||||||
|
|
||||||
|
assertIteration(F, "windows.file", null); |
||||||
|
assertIteration(F, "windows.txt", asList(EOL_LF)); |
||||||
|
|
||||||
|
endWalk(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Checks that if there is no .gitattributes file in the repository |
||||||
|
* everything still work fine. |
||||||
|
* |
||||||
|
* @throws Exception |
||||||
|
*/ |
||||||
|
@Test |
||||||
|
public void testNoAttributes() throws Exception { |
||||||
|
writeTrashFile("l0.txt", ""); |
||||||
|
writeTrashFile("level1/l1.txt", ""); |
||||||
|
writeTrashFile("level1/level2/l2.txt", ""); |
||||||
|
|
||||||
|
// Adds file to index
|
||||||
|
git.add().addFilepattern(".").call(); |
||||||
|
walk = beginWalk(); |
||||||
|
|
||||||
|
assertIteration(F, "l0.txt"); |
||||||
|
|
||||||
|
assertIteration(D, "level1"); |
||||||
|
assertIteration(F, "level1/l1.txt"); |
||||||
|
|
||||||
|
assertIteration(D, "level1/level2"); |
||||||
|
assertIteration(F, "level1/level2/l2.txt"); |
||||||
|
|
||||||
|
endWalk(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Checks that empty .gitattribute files do not return incorrect value. |
||||||
|
* |
||||||
|
* @throws Exception |
||||||
|
*/ |
||||||
|
@Test |
||||||
|
public void testEmptyGitAttributeFile() throws Exception { |
||||||
|
writeAttributesFile(".git/info/attributes", ""); |
||||||
|
writeTrashFile("l0.txt", ""); |
||||||
|
writeAttributesFile(".gitattributes", ""); |
||||||
|
writeTrashFile("level1/l1.txt", ""); |
||||||
|
writeTrashFile("level1/level2/l2.txt", ""); |
||||||
|
|
||||||
|
// Adds file to index
|
||||||
|
git.add().addFilepattern(".").call(); |
||||||
|
walk = beginWalk(); |
||||||
|
|
||||||
|
assertIteration(F, ".gitattributes"); |
||||||
|
assertIteration(F, "l0.txt"); |
||||||
|
|
||||||
|
assertIteration(D, "level1"); |
||||||
|
assertIteration(F, "level1/l1.txt"); |
||||||
|
|
||||||
|
assertIteration(D, "level1/level2"); |
||||||
|
assertIteration(F, "level1/level2/l2.txt"); |
||||||
|
|
||||||
|
endWalk(); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testNoMatchingAttributes() throws Exception { |
||||||
|
writeAttributesFile(".git/info/attributes", "*.java delta"); |
||||||
|
writeAttributesFile(".gitattributes", "*.java -delta"); |
||||||
|
writeAttributesFile("levelA/.gitattributes", "*.java eol=lf"); |
||||||
|
writeAttributesFile("levelB/.gitattributes", "*.txt eol=lf"); |
||||||
|
|
||||||
|
writeTrashFile("levelA/lA.txt", ""); |
||||||
|
|
||||||
|
// Adds file to index
|
||||||
|
git.add().addFilepattern(".").call(); |
||||||
|
walk = beginWalk(); |
||||||
|
|
||||||
|
assertIteration(F, ".gitattributes"); |
||||||
|
|
||||||
|
assertIteration(D, "levelA"); |
||||||
|
assertIteration(F, "levelA/.gitattributes"); |
||||||
|
assertIteration(F, "levelA/lA.txt"); |
||||||
|
|
||||||
|
assertIteration(D, "levelB"); |
||||||
|
assertIteration(F, "levelB/.gitattributes"); |
||||||
|
|
||||||
|
endWalk(); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testIncorrectAttributeFileName() throws Exception { |
||||||
|
writeAttributesFile("levelA/file.gitattributes", "*.txt -delta"); |
||||||
|
writeAttributesFile("gitattributes", "*.txt eol=lf"); |
||||||
|
|
||||||
|
writeTrashFile("l0.txt", ""); |
||||||
|
writeTrashFile("levelA/lA.txt", ""); |
||||||
|
|
||||||
|
// Adds file to index
|
||||||
|
git.add().addFilepattern(".").call(); |
||||||
|
walk = beginWalk(); |
||||||
|
|
||||||
|
assertIteration(F, "gitattributes"); |
||||||
|
|
||||||
|
assertIteration(F, "l0.txt"); |
||||||
|
|
||||||
|
assertIteration(D, "levelA"); |
||||||
|
assertIteration(F, "levelA/file.gitattributes"); |
||||||
|
assertIteration(F, "levelA/lA.txt"); |
||||||
|
|
||||||
|
endWalk(); |
||||||
|
} |
||||||
|
|
||||||
|
private void assertIteration(FileMode type, String pathName) |
||||||
|
throws IOException { |
||||||
|
assertIteration(type, pathName, Collections.<Attribute> emptyList()); |
||||||
|
} |
||||||
|
|
||||||
|
private void assertIteration(FileMode type, String pathName, |
||||||
|
List<Attribute> nodeAttrs) throws IOException { |
||||||
|
assertTrue("walk has entry", walk.next()); |
||||||
|
assertEquals(pathName, walk.getPathString()); |
||||||
|
assertEquals(type, walk.getFileMode(0)); |
||||||
|
DirCacheIterator itr = walk.getTree(0, DirCacheIterator.class); |
||||||
|
assertNotNull("has tree", itr); |
||||||
|
|
||||||
|
AttributesNode attributeNode = itr.getEntryAttributesNode(db |
||||||
|
.newObjectReader()); |
||||||
|
assertAttributeNode(pathName, attributeNode, nodeAttrs); |
||||||
|
|
||||||
|
if (D.equals(type)) |
||||||
|
walk.enterSubtree(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
private void assertAttributeNode(String pathName, |
||||||
|
AttributesNode attributeNode, List<Attribute> nodeAttrs) { |
||||||
|
if (attributeNode == null) |
||||||
|
assertTrue(nodeAttrs == null || nodeAttrs.isEmpty()); |
||||||
|
else { |
||||||
|
|
||||||
|
Map<String, Attribute> entryAttributes = new LinkedHashMap<String, Attribute>(); |
||||||
|
attributeNode.getAttributes(pathName, false, entryAttributes); |
||||||
|
|
||||||
|
if (nodeAttrs != null && !nodeAttrs.isEmpty()) { |
||||||
|
for (Attribute attribute : nodeAttrs) { |
||||||
|
assertThat(entryAttributes.values(), hasItem(attribute)); |
||||||
|
} |
||||||
|
} else { |
||||||
|
assertTrue( |
||||||
|
"The entry " |
||||||
|
+ pathName |
||||||
|
+ " should not have any attributes. Instead, the following attributes are applied to this file " |
||||||
|
+ entryAttributes.toString(), |
||||||
|
entryAttributes.isEmpty()); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private void writeAttributesFile(String name, String... rules) |
||||||
|
throws IOException { |
||||||
|
StringBuilder data = new StringBuilder(); |
||||||
|
for (String line : rules) |
||||||
|
data.append(line + "\n"); |
||||||
|
writeTrashFile(name, data.toString()); |
||||||
|
} |
||||||
|
|
||||||
|
private TreeWalk beginWalk() throws Exception { |
||||||
|
TreeWalk newWalk = new TreeWalk(db); |
||||||
|
newWalk.addTree(new DirCacheIterator(db.readDirCache())); |
||||||
|
return newWalk; |
||||||
|
} |
||||||
|
|
||||||
|
private void endWalk() throws IOException { |
||||||
|
assertFalse("Not all files tested", walk.next()); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,282 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2014, Obeo. |
||||||
|
* and other copyright owners as documented in the project's IP log. |
||||||
|
* |
||||||
|
* This program and the accompanying materials are made available |
||||||
|
* under the terms of the Eclipse Distribution License v1.0 which |
||||||
|
* accompanies this distribution, is reproduced below, and is |
||||||
|
* available at http://www.eclipse.org/org/documents/edl-v10.php
|
||||||
|
* |
||||||
|
* All rights reserved. |
||||||
|
* |
||||||
|
* Redistribution and use in source and binary forms, with or |
||||||
|
* without modification, are permitted provided that the following |
||||||
|
* conditions are met: |
||||||
|
* |
||||||
|
* - Redistributions of source code must retain the above copyright |
||||||
|
* notice, this list of conditions and the following disclaimer. |
||||||
|
* |
||||||
|
* - Redistributions in binary form must reproduce the above |
||||||
|
* copyright notice, this list of conditions and the following |
||||||
|
* disclaimer in the documentation and/or other materials provided |
||||||
|
* with the distribution. |
||||||
|
* |
||||||
|
* - Neither the name of the Eclipse Foundation, Inc. nor the |
||||||
|
* names of its contributors may be used to endorse or promote |
||||||
|
* products derived from this software without specific prior |
||||||
|
* written permission. |
||||||
|
* |
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
||||||
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
||||||
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||||||
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||||
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||||||
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||||
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
||||||
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||||
|
*/ |
||||||
|
package org.eclipse.jgit.attributes; |
||||||
|
|
||||||
|
import static java.util.Arrays.asList; |
||||||
|
import static org.hamcrest.CoreMatchers.hasItem; |
||||||
|
import static org.hamcrest.MatcherAssert.assertThat; |
||||||
|
import static org.junit.Assert.assertEquals; |
||||||
|
import static org.junit.Assert.assertFalse; |
||||||
|
import static org.junit.Assert.assertNotNull; |
||||||
|
import static org.junit.Assert.assertTrue; |
||||||
|
|
||||||
|
import java.io.File; |
||||||
|
import java.io.IOException; |
||||||
|
import java.util.Collections; |
||||||
|
import java.util.LinkedHashMap; |
||||||
|
import java.util.List; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
import org.eclipse.jgit.attributes.Attribute.State; |
||||||
|
import org.eclipse.jgit.errors.CorruptObjectException; |
||||||
|
import org.eclipse.jgit.junit.JGitTestUtil; |
||||||
|
import org.eclipse.jgit.junit.RepositoryTestCase; |
||||||
|
import org.eclipse.jgit.lib.FileMode; |
||||||
|
import org.eclipse.jgit.treewalk.FileTreeIterator; |
||||||
|
import org.eclipse.jgit.treewalk.TreeWalk; |
||||||
|
import org.eclipse.jgit.treewalk.WorkingTreeIterator; |
||||||
|
import org.junit.Test; |
||||||
|
|
||||||
|
/** |
||||||
|
* Tests attributes node behavior on the local filesystem. |
||||||
|
*/ |
||||||
|
public class AttributesNodeWorkingTreeIteratorTest extends RepositoryTestCase { |
||||||
|
|
||||||
|
private static final FileMode D = FileMode.TREE; |
||||||
|
|
||||||
|
private static final FileMode F = FileMode.REGULAR_FILE; |
||||||
|
|
||||||
|
private static Attribute EOL_CRLF = new Attribute("eol", "crlf"); |
||||||
|
|
||||||
|
private static Attribute EOL_LF = new Attribute("eol", "lf"); |
||||||
|
|
||||||
|
private static Attribute DELTA_UNSET = new Attribute("delta", State.UNSET); |
||||||
|
|
||||||
|
private static Attribute CUSTOM_VALUE = new Attribute("custom", "value"); |
||||||
|
|
||||||
|
private TreeWalk walk; |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testRules() throws Exception { |
||||||
|
|
||||||
|
File customAttributeFile = File.createTempFile("tmp_", |
||||||
|
"customAttributeFile", null); |
||||||
|
customAttributeFile.deleteOnExit(); |
||||||
|
|
||||||
|
JGitTestUtil.write(customAttributeFile, "*.txt custom=value"); |
||||||
|
db.getConfig().setString("core", null, "attributesfile", |
||||||
|
customAttributeFile.getAbsolutePath()); |
||||||
|
writeAttributesFile(".git/info/attributes", "windows* eol=crlf"); |
||||||
|
|
||||||
|
writeAttributesFile(".gitattributes", "*.txt eol=lf"); |
||||||
|
writeTrashFile("windows.file", ""); |
||||||
|
writeTrashFile("windows.txt", ""); |
||||||
|
writeTrashFile("global.txt", ""); |
||||||
|
writeTrashFile("readme.txt", ""); |
||||||
|
|
||||||
|
writeAttributesFile("src/config/.gitattributes", "*.txt -delta"); |
||||||
|
writeTrashFile("src/config/readme.txt", ""); |
||||||
|
writeTrashFile("src/config/windows.file", ""); |
||||||
|
writeTrashFile("src/config/windows.txt", ""); |
||||||
|
|
||||||
|
walk = beginWalk(); |
||||||
|
|
||||||
|
assertIteration(F, ".gitattributes"); |
||||||
|
assertIteration(F, "global.txt", asList(EOL_LF), null, |
||||||
|
asList(CUSTOM_VALUE)); |
||||||
|
assertIteration(F, "readme.txt", asList(EOL_LF), null, |
||||||
|
asList(CUSTOM_VALUE)); |
||||||
|
|
||||||
|
assertIteration(D, "src"); |
||||||
|
|
||||||
|
assertIteration(D, "src/config"); |
||||||
|
assertIteration(F, "src/config/.gitattributes"); |
||||||
|
assertIteration(F, "src/config/readme.txt", asList(DELTA_UNSET), null, |
||||||
|
asList(CUSTOM_VALUE)); |
||||||
|
assertIteration(F, "src/config/windows.file", null, asList(EOL_CRLF), |
||||||
|
null); |
||||||
|
assertIteration(F, "src/config/windows.txt", asList(DELTA_UNSET), |
||||||
|
asList(EOL_CRLF), asList(CUSTOM_VALUE)); |
||||||
|
|
||||||
|
assertIteration(F, "windows.file", null, asList(EOL_CRLF), null); |
||||||
|
assertIteration(F, "windows.txt", asList(EOL_LF), asList(EOL_CRLF), |
||||||
|
asList(CUSTOM_VALUE)); |
||||||
|
|
||||||
|
endWalk(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Checks that if there is no .gitattributes file in the repository |
||||||
|
* everything still work fine. |
||||||
|
* |
||||||
|
* @throws Exception |
||||||
|
*/ |
||||||
|
@Test |
||||||
|
public void testNoAttributes() throws Exception { |
||||||
|
writeTrashFile("l0.txt", ""); |
||||||
|
writeTrashFile("level1/l1.txt", ""); |
||||||
|
writeTrashFile("level1/level2/l2.txt", ""); |
||||||
|
|
||||||
|
walk = beginWalk(); |
||||||
|
|
||||||
|
assertIteration(F, "l0.txt"); |
||||||
|
|
||||||
|
assertIteration(D, "level1"); |
||||||
|
assertIteration(F, "level1/l1.txt"); |
||||||
|
|
||||||
|
assertIteration(D, "level1/level2"); |
||||||
|
assertIteration(F, "level1/level2/l2.txt"); |
||||||
|
|
||||||
|
endWalk(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Checks that empty .gitattribute files do not return incorrect value. |
||||||
|
* |
||||||
|
* @throws Exception |
||||||
|
*/ |
||||||
|
@Test |
||||||
|
public void testEmptyGitAttributeFile() throws Exception { |
||||||
|
writeAttributesFile(".git/info/attributes", ""); |
||||||
|
writeTrashFile("l0.txt", ""); |
||||||
|
writeAttributesFile(".gitattributes", ""); |
||||||
|
writeTrashFile("level1/l1.txt", ""); |
||||||
|
writeTrashFile("level1/level2/l2.txt", ""); |
||||||
|
|
||||||
|
walk = beginWalk(); |
||||||
|
|
||||||
|
assertIteration(F, ".gitattributes"); |
||||||
|
assertIteration(F, "l0.txt"); |
||||||
|
|
||||||
|
assertIteration(D, "level1"); |
||||||
|
assertIteration(F, "level1/l1.txt"); |
||||||
|
|
||||||
|
assertIteration(D, "level1/level2"); |
||||||
|
assertIteration(F, "level1/level2/l2.txt"); |
||||||
|
|
||||||
|
endWalk(); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testNoMatchingAttributes() throws Exception { |
||||||
|
writeAttributesFile(".git/info/attributes", "*.java delta"); |
||||||
|
writeAttributesFile(".gitattributes", "*.java -delta"); |
||||||
|
writeAttributesFile("levelA/.gitattributes", "*.java eol=lf"); |
||||||
|
writeAttributesFile("levelB/.gitattributes", "*.txt eol=lf"); |
||||||
|
|
||||||
|
writeTrashFile("levelA/lA.txt", ""); |
||||||
|
|
||||||
|
walk = beginWalk(); |
||||||
|
|
||||||
|
assertIteration(F, ".gitattributes"); |
||||||
|
|
||||||
|
assertIteration(D, "levelA"); |
||||||
|
assertIteration(F, "levelA/.gitattributes"); |
||||||
|
assertIteration(F, "levelA/lA.txt"); |
||||||
|
|
||||||
|
assertIteration(D, "levelB"); |
||||||
|
assertIteration(F, "levelB/.gitattributes"); |
||||||
|
|
||||||
|
endWalk(); |
||||||
|
} |
||||||
|
|
||||||
|
private void assertIteration(FileMode type, String pathName) |
||||||
|
throws IOException { |
||||||
|
assertIteration(type, pathName, Collections.<Attribute> emptyList(), |
||||||
|
Collections.<Attribute> emptyList(), |
||||||
|
Collections.<Attribute> emptyList()); |
||||||
|
} |
||||||
|
|
||||||
|
private void assertIteration(FileMode type, String pathName, |
||||||
|
List<Attribute> nodeAttrs, List<Attribute> infoAttrs, |
||||||
|
List<Attribute> globalAttrs) |
||||||
|
throws IOException { |
||||||
|
assertTrue("walk has entry", walk.next()); |
||||||
|
assertEquals(pathName, walk.getPathString()); |
||||||
|
assertEquals(type, walk.getFileMode(0)); |
||||||
|
WorkingTreeIterator itr = walk.getTree(0, WorkingTreeIterator.class); |
||||||
|
assertNotNull("has tree", itr); |
||||||
|
|
||||||
|
AttributesNode attributeNode = itr.getEntryAttributesNode(); |
||||||
|
assertAttributeNode(pathName, attributeNode, nodeAttrs); |
||||||
|
AttributesNode infoAttributeNode = itr.getInfoAttributesNode(); |
||||||
|
assertAttributeNode(pathName, infoAttributeNode, infoAttrs); |
||||||
|
AttributesNode globalAttributeNode = itr.getGlobalAttributesNode(); |
||||||
|
assertAttributeNode(pathName, globalAttributeNode, globalAttrs); |
||||||
|
if (D.equals(type)) |
||||||
|
walk.enterSubtree(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
private void assertAttributeNode(String pathName, |
||||||
|
AttributesNode attributeNode, List<Attribute> nodeAttrs) { |
||||||
|
if (attributeNode == null) |
||||||
|
assertTrue(nodeAttrs == null || nodeAttrs.isEmpty()); |
||||||
|
else { |
||||||
|
|
||||||
|
Map<String, Attribute> entryAttributes = new LinkedHashMap<String, Attribute>(); |
||||||
|
attributeNode.getAttributes(pathName, false, entryAttributes); |
||||||
|
|
||||||
|
if (nodeAttrs != null && !nodeAttrs.isEmpty()) { |
||||||
|
for (Attribute attribute : nodeAttrs) { |
||||||
|
assertThat(entryAttributes.values(), hasItem(attribute)); |
||||||
|
} |
||||||
|
} else { |
||||||
|
assertTrue( |
||||||
|
"The entry " |
||||||
|
+ pathName |
||||||
|
+ " should not have any attributes. Instead, the following attributes are applied to this file " |
||||||
|
+ entryAttributes.toString(), |
||||||
|
entryAttributes.isEmpty()); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private void writeAttributesFile(String name, String... rules) |
||||||
|
throws IOException { |
||||||
|
StringBuilder data = new StringBuilder(); |
||||||
|
for (String line : rules) |
||||||
|
data.append(line + "\n"); |
||||||
|
writeTrashFile(name, data.toString()); |
||||||
|
} |
||||||
|
|
||||||
|
private TreeWalk beginWalk() throws CorruptObjectException { |
||||||
|
TreeWalk newWalk = new TreeWalk(db); |
||||||
|
newWalk.addTree(new FileTreeIterator(db)); |
||||||
|
return newWalk; |
||||||
|
} |
||||||
|
|
||||||
|
private void endWalk() throws IOException { |
||||||
|
assertFalse("Not all files tested", walk.next()); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,184 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2010, Marc Strapetz <marc.strapetz@syntevo.com> |
||||||
|
* and other copyright owners as documented in the project's IP log. |
||||||
|
* |
||||||
|
* This program and the accompanying materials are made available |
||||||
|
* under the terms of the Eclipse Distribution License v1.0 which |
||||||
|
* accompanies this distribution, is reproduced below, and is |
||||||
|
* available at http://www.eclipse.org/org/documents/edl-v10.php
|
||||||
|
* |
||||||
|
* All rights reserved. |
||||||
|
* |
||||||
|
* Redistribution and use in source and binary forms, with or |
||||||
|
* without modification, are permitted provided that the following |
||||||
|
* conditions are met: |
||||||
|
* |
||||||
|
* - Redistributions of source code must retain the above copyright |
||||||
|
* notice, this list of conditions and the following disclaimer. |
||||||
|
* |
||||||
|
* - Redistributions in binary form must reproduce the above |
||||||
|
* copyright notice, this list of conditions and the following |
||||||
|
* disclaimer in the documentation and/or other materials provided |
||||||
|
* with the distribution. |
||||||
|
* |
||||||
|
* - Neither the name of the Eclipse Foundation, Inc. nor the |
||||||
|
* names of its contributors may be used to endorse or promote |
||||||
|
* products derived from this software without specific prior |
||||||
|
* written permission. |
||||||
|
* |
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
||||||
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
||||||
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||||||
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||||
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||||||
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||||
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
||||||
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||||
|
*/ |
||||||
|
package org.eclipse.jgit.attributes; |
||||||
|
|
||||||
|
/** |
||||||
|
* Represents an attribute. |
||||||
|
* <p> |
||||||
|
* According to the man page, an attribute can have the following states: |
||||||
|
* <ul> |
||||||
|
* <li>Set - represented by {@link State#SET}</li> |
||||||
|
* <li>Unset - represented by {@link State#UNSET}</li> |
||||||
|
* <li>Set to a value - represented by {@link State#CUSTOM}</li> |
||||||
|
* <li>Unspecified - <code>null</code> is used instead of an instance of this |
||||||
|
* class</li> |
||||||
|
* </ul> |
||||||
|
* </p> |
||||||
|
* |
||||||
|
* @since 3.7 |
||||||
|
*/ |
||||||
|
public final class Attribute { |
||||||
|
|
||||||
|
/** |
||||||
|
* The attribute value state |
||||||
|
*/ |
||||||
|
public static enum State { |
||||||
|
/** the attribute is set */ |
||||||
|
SET, |
||||||
|
|
||||||
|
/** the attribute is unset */ |
||||||
|
UNSET, |
||||||
|
|
||||||
|
/** the attribute is set to a custom value */ |
||||||
|
CUSTOM |
||||||
|
} |
||||||
|
|
||||||
|
private final String key; |
||||||
|
private final State state; |
||||||
|
private final String value; |
||||||
|
|
||||||
|
/** |
||||||
|
* Creates a new instance |
||||||
|
* |
||||||
|
* @param key |
||||||
|
* the attribute key. Should not be <code>null</code>. |
||||||
|
* @param state |
||||||
|
* the attribute state. It should be either {@link State#SET} or |
||||||
|
* {@link State#UNSET}. In order to create a custom value |
||||||
|
* attribute prefer the use of {@link #Attribute(String, String)} |
||||||
|
* constructor. |
||||||
|
*/ |
||||||
|
public Attribute(String key, State state) { |
||||||
|
this(key, state, null); |
||||||
|
} |
||||||
|
|
||||||
|
private Attribute(String key, State state, String value) { |
||||||
|
if (key == null) |
||||||
|
throw new NullPointerException( |
||||||
|
"The key of an attribute should not be null"); //$NON-NLS-1$
|
||||||
|
if (state == null) |
||||||
|
throw new NullPointerException( |
||||||
|
"The state of an attribute should not be null"); //$NON-NLS-1$
|
||||||
|
|
||||||
|
this.key = key; |
||||||
|
this.state = state; |
||||||
|
this.value = value; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Creates a new instance. |
||||||
|
* |
||||||
|
* @param key |
||||||
|
* the attribute key. Should not be <code>null</code>. |
||||||
|
* @param value |
||||||
|
* the custom attribute value |
||||||
|
*/ |
||||||
|
public Attribute(String key, String value) { |
||||||
|
this(key, State.CUSTOM, value); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean equals(Object obj) { |
||||||
|
if (this == obj) |
||||||
|
return true; |
||||||
|
if (!(obj instanceof Attribute)) |
||||||
|
return false; |
||||||
|
Attribute other = (Attribute) obj; |
||||||
|
if (!key.equals(other.key)) |
||||||
|
return false; |
||||||
|
if (state != other.state) |
||||||
|
return false; |
||||||
|
if (value == null) { |
||||||
|
if (other.value != null) |
||||||
|
return false; |
||||||
|
} else if (!value.equals(other.value)) |
||||||
|
return false; |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @return the attribute key (never returns <code>null</code>) |
||||||
|
*/ |
||||||
|
public String getKey() { |
||||||
|
return key; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the state. |
||||||
|
* |
||||||
|
* @return the state (never returns <code>null</code>) |
||||||
|
*/ |
||||||
|
public State getState() { |
||||||
|
return state; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @return the attribute value (may be <code>null</code>) |
||||||
|
*/ |
||||||
|
public String getValue() { |
||||||
|
return value; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public int hashCode() { |
||||||
|
final int prime = 31; |
||||||
|
int result = 1; |
||||||
|
result = prime * result + key.hashCode(); |
||||||
|
result = prime * result + state.hashCode(); |
||||||
|
result = prime * result + ((value == null) ? 0 : value.hashCode()); |
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public String toString() { |
||||||
|
switch (state) { |
||||||
|
case SET: |
||||||
|
return key; |
||||||
|
case UNSET: |
||||||
|
return "-" + key; //$NON-NLS-1$
|
||||||
|
case CUSTOM: |
||||||
|
default: |
||||||
|
return key + "=" + value; //$NON-NLS-1$
|
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,161 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2010, Red Hat Inc. |
||||||
|
* and other copyright owners as documented in the project's IP log. |
||||||
|
* |
||||||
|
* This program and the accompanying materials are made available |
||||||
|
* under the terms of the Eclipse Distribution License v1.0 which |
||||||
|
* accompanies this distribution, is reproduced below, and is |
||||||
|
* available at http://www.eclipse.org/org/documents/edl-v10.php
|
||||||
|
* |
||||||
|
* All rights reserved. |
||||||
|
* |
||||||
|
* Redistribution and use in source and binary forms, with or |
||||||
|
* without modification, are permitted provided that the following |
||||||
|
* conditions are met: |
||||||
|
* |
||||||
|
* - Redistributions of source code must retain the above copyright |
||||||
|
* notice, this list of conditions and the following disclaimer. |
||||||
|
* |
||||||
|
* - Redistributions in binary form must reproduce the above |
||||||
|
* copyright notice, this list of conditions and the following |
||||||
|
* disclaimer in the documentation and/or other materials provided |
||||||
|
* with the distribution. |
||||||
|
* |
||||||
|
* - Neither the name of the Eclipse Foundation, Inc. nor the |
||||||
|
* names of its contributors may be used to endorse or promote |
||||||
|
* products derived from this software without specific prior |
||||||
|
* written permission. |
||||||
|
* |
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
||||||
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
||||||
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||||||
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||||
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||||||
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||||
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
||||||
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||||
|
*/ |
||||||
|
package org.eclipse.jgit.attributes; |
||||||
|
|
||||||
|
import java.io.BufferedReader; |
||||||
|
import java.io.IOException; |
||||||
|
import java.io.InputStream; |
||||||
|
import java.io.InputStreamReader; |
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.Collections; |
||||||
|
import java.util.List; |
||||||
|
import java.util.ListIterator; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
import org.eclipse.jgit.lib.Constants; |
||||||
|
|
||||||
|
/** |
||||||
|
* Represents a bundle of attributes inherited from a base directory. |
||||||
|
* |
||||||
|
* This class is not thread safe, it maintains state about the last match. |
||||||
|
* |
||||||
|
* @since 3.7 |
||||||
|
*/ |
||||||
|
public class AttributesNode { |
||||||
|
/** The rules that have been parsed into this node. */ |
||||||
|
private final List<AttributesRule> rules; |
||||||
|
|
||||||
|
/** Create an empty ignore node with no rules. */ |
||||||
|
public AttributesNode() { |
||||||
|
rules = new ArrayList<AttributesRule>(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Create an ignore node with given rules. |
||||||
|
* |
||||||
|
* @param rules |
||||||
|
* list of rules. |
||||||
|
**/ |
||||||
|
public AttributesNode(List<AttributesRule> rules) { |
||||||
|
this.rules = rules; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Parse files according to gitattribute standards. |
||||||
|
* |
||||||
|
* @param in |
||||||
|
* input stream holding the standard ignore format. The caller is |
||||||
|
* responsible for closing the stream. |
||||||
|
* @throws IOException |
||||||
|
* Error thrown when reading an ignore file. |
||||||
|
*/ |
||||||
|
public void parse(InputStream in) throws IOException { |
||||||
|
BufferedReader br = asReader(in); |
||||||
|
String txt; |
||||||
|
while ((txt = br.readLine()) != null) { |
||||||
|
txt = txt.trim(); |
||||||
|
if (txt.length() > 0 && !txt.startsWith("#") /* Comments *///$NON-NLS-1$
|
||||||
|
&& !txt.startsWith("!") /* Negative pattern forbidden for attributes */) { //$NON-NLS-1$
|
||||||
|
int patternEndSpace = txt.indexOf(' '); |
||||||
|
int patternEndTab = txt.indexOf('\t'); |
||||||
|
|
||||||
|
final int patternEnd; |
||||||
|
if (patternEndSpace == -1) |
||||||
|
patternEnd = patternEndTab; |
||||||
|
else if (patternEndTab == -1) |
||||||
|
patternEnd = patternEndSpace; |
||||||
|
else |
||||||
|
patternEnd = Math.min(patternEndSpace, patternEndTab); |
||||||
|
|
||||||
|
if (patternEnd > -1) |
||||||
|
rules.add(new AttributesRule(txt.substring(0, patternEnd), |
||||||
|
txt.substring(patternEnd + 1).trim())); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private static BufferedReader asReader(InputStream in) { |
||||||
|
return new BufferedReader(new InputStreamReader(in, Constants.CHARSET)); |
||||||
|
} |
||||||
|
|
||||||
|
/** @return list of all ignore rules held by this node. */ |
||||||
|
public List<AttributesRule> getRules() { |
||||||
|
return Collections.unmodifiableList(rules); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the matching attributes for an entry path. |
||||||
|
* |
||||||
|
* @param entryPath |
||||||
|
* the path to test. The path must be relative to this attribute |
||||||
|
* node's own repository path, and in repository path format |
||||||
|
* (uses '/' and not '\'). |
||||||
|
* @param isDirectory |
||||||
|
* true if the target item is a directory. |
||||||
|
* @param attributes |
||||||
|
* Map that will hold the attributes matching this entry path. If |
||||||
|
* it is not empty, this method will NOT override any |
||||||
|
* existing entry. |
||||||
|
*/ |
||||||
|
public void getAttributes(String entryPath, boolean isDirectory, |
||||||
|
Map<String, Attribute> attributes) { |
||||||
|
// Parse rules in the reverse order that they were read since the last
|
||||||
|
// entry should be used
|
||||||
|
ListIterator<AttributesRule> ruleIterator = rules.listIterator(rules |
||||||
|
.size()); |
||||||
|
while (ruleIterator.hasPrevious()) { |
||||||
|
AttributesRule rule = ruleIterator.previous(); |
||||||
|
if (rule.isMatch(entryPath, isDirectory)) { |
||||||
|
ListIterator<Attribute> attributeIte = rule.getAttributes() |
||||||
|
.listIterator(rule.getAttributes().size()); |
||||||
|
// Parses the attributes in the reverse order that they were
|
||||||
|
// read since the last entry should be used
|
||||||
|
while (attributeIte.hasPrevious()) { |
||||||
|
Attribute attr = attributeIte.previous(); |
||||||
|
if (!attributes.containsKey(attr.getKey())) |
||||||
|
attributes.put(attr.getKey(), attr); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,203 @@ |
|||||||
|
/* |
||||||
|
* Copyright (C) 2010, Red Hat Inc. |
||||||
|
* and other copyright owners as documented in the project's IP log. |
||||||
|
* |
||||||
|
* This program and the accompanying materials are made available |
||||||
|
* under the terms of the Eclipse Distribution License v1.0 which |
||||||
|
* accompanies this distribution, is reproduced below, and is |
||||||
|
* available at http://www.eclipse.org/org/documents/edl-v10.php
|
||||||
|
* |
||||||
|
* All rights reserved. |
||||||
|
* |
||||||
|
* Redistribution and use in source and binary forms, with or |
||||||
|
* without modification, are permitted provided that the following |
||||||
|
* conditions are met: |
||||||
|
* |
||||||
|
* - Redistributions of source code must retain the above copyright |
||||||
|
* notice, this list of conditions and the following disclaimer. |
||||||
|
* |
||||||
|
* - Redistributions in binary form must reproduce the above |
||||||
|
* copyright notice, this list of conditions and the following |
||||||
|
* disclaimer in the documentation and/or other materials provided |
||||||
|
* with the distribution. |
||||||
|
* |
||||||
|
* - Neither the name of the Eclipse Foundation, Inc. nor the |
||||||
|
* names of its contributors may be used to endorse or promote |
||||||
|
* products derived from this software without specific prior |
||||||
|
* written permission. |
||||||
|
* |
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
||||||
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
||||||
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||||
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||||||
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||||
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||||||
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||||
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
||||||
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||||
|
*/ |
||||||
|
package org.eclipse.jgit.attributes; |
||||||
|
|
||||||
|
import static org.eclipse.jgit.ignore.internal.IMatcher.NO_MATCH; |
||||||
|
|
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.Collections; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
import org.eclipse.jgit.attributes.Attribute.State; |
||||||
|
import org.eclipse.jgit.errors.InvalidPatternException; |
||||||
|
import org.eclipse.jgit.ignore.FastIgnoreRule; |
||||||
|
import org.eclipse.jgit.ignore.internal.IMatcher; |
||||||
|
import org.eclipse.jgit.ignore.internal.PathMatcher; |
||||||
|
|
||||||
|
/** |
||||||
|
* A single attributes rule corresponding to one line in a .gitattributes file. |
||||||
|
* |
||||||
|
* Inspiration from: {@link FastIgnoreRule} |
||||||
|
* |
||||||
|
* @since 3.7 |
||||||
|
*/ |
||||||
|
public class AttributesRule { |
||||||
|
|
||||||
|
/** |
||||||
|
* regular expression for splitting attributes - space, tab and \r (the C |
||||||
|
* implementation oddly enough allows \r between attributes) |
||||||
|
* */ |
||||||
|
private static final String ATTRIBUTES_SPLIT_REGEX = "[ \t\r]"; //$NON-NLS-1$
|
||||||
|
|
||||||
|
private static List<Attribute> parseAttributes(String attributesLine) { |
||||||
|
// the C implementation oddly enough allows \r between attributes too.
|
||||||
|
ArrayList<Attribute> result = new ArrayList<Attribute>(); |
||||||
|
for (String attribute : attributesLine.split(ATTRIBUTES_SPLIT_REGEX)) { |
||||||
|
attribute = attribute.trim(); |
||||||
|
if (attribute.length() == 0) |
||||||
|
continue; |
||||||
|
|
||||||
|
if (attribute.startsWith("-")) {//$NON-NLS-1$
|
||||||
|
if (attribute.length() > 1) |
||||||
|
result.add(new Attribute(attribute.substring(1), |
||||||
|
State.UNSET)); |
||||||
|
continue; |
||||||
|
} |
||||||
|
|
||||||
|
final int equalsIndex = attribute.indexOf("="); //$NON-NLS-1$
|
||||||
|
if (equalsIndex == -1) |
||||||
|
result.add(new Attribute(attribute, State.SET)); |
||||||
|
else { |
||||||
|
String attributeKey = attribute.substring(0, equalsIndex); |
||||||
|
if (attributeKey.length() > 0) { |
||||||
|
String attributeValue = attribute |
||||||
|
.substring(equalsIndex + 1); |
||||||
|
result.add(new Attribute(attributeKey, attributeValue)); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
private final String pattern; |
||||||
|
private final List<Attribute> attributes; |
||||||
|
|
||||||
|
private boolean nameOnly; |
||||||
|
private boolean dirOnly; |
||||||
|
|
||||||
|
private IMatcher matcher; |
||||||
|
|
||||||
|
/** |
||||||
|
* Create a new attribute rule with the given pattern. Assumes that the |
||||||
|
* pattern is already trimmed. |
||||||
|
* |
||||||
|
* @param pattern |
||||||
|
* Base pattern for the attributes rule. This pattern will be |
||||||
|
* parsed to generate rule parameters. It can not be |
||||||
|
* <code>null</code>. |
||||||
|
* @param attributes |
||||||
|
* the rule attributes. This string will be parsed to read the |
||||||
|
* attributes. |
||||||
|
*/ |
||||||
|
public AttributesRule(String pattern, String attributes) { |
||||||
|
this.attributes = parseAttributes(attributes); |
||||||
|
nameOnly = false; |
||||||
|
dirOnly = false; |
||||||
|
|
||||||
|
if (pattern.endsWith("/")) { //$NON-NLS-1$
|
||||||
|
pattern = pattern.substring(0, pattern.length() - 1); |
||||||
|
dirOnly = true; |
||||||
|
} |
||||||
|
|
||||||
|
boolean hasSlash = pattern.contains("/"); //$NON-NLS-1$
|
||||||
|
|
||||||
|
if (!hasSlash) |
||||||
|
nameOnly = true; |
||||||
|
else if (!pattern.startsWith("/")) { //$NON-NLS-1$
|
||||||
|
// Contains "/" but does not start with one
|
||||||
|
// Adding / to the start should not interfere with matching
|
||||||
|
pattern = "/" + pattern; //$NON-NLS-1$
|
||||||
|
} |
||||||
|
|
||||||
|
try { |
||||||
|
matcher = PathMatcher.createPathMatcher(pattern, |
||||||
|
Character.valueOf(FastIgnoreRule.PATH_SEPARATOR), dirOnly); |
||||||
|
} catch (InvalidPatternException e) { |
||||||
|
matcher = NO_MATCH; |
||||||
|
} |
||||||
|
|
||||||
|
this.pattern = pattern; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @return True if the pattern should match directories only |
||||||
|
*/ |
||||||
|
public boolean dirOnly() { |
||||||
|
return dirOnly; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the attributes. |
||||||
|
* |
||||||
|
* @return an unmodifiable list of attributes (never returns |
||||||
|
* <code>null</code>) |
||||||
|
*/ |
||||||
|
public List<Attribute> getAttributes() { |
||||||
|
return Collections.unmodifiableList(attributes); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @return <code>true</code> if the pattern is just a file name and not a |
||||||
|
* path |
||||||
|
*/ |
||||||
|
public boolean isNameOnly() { |
||||||
|
return nameOnly; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @return The blob pattern to be used as a matcher (never returns |
||||||
|
* <code>null</code>) |
||||||
|
*/ |
||||||
|
public String getPattern() { |
||||||
|
return pattern; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns <code>true</code> if a match was made. |
||||||
|
* |
||||||
|
* @param relativeTarget |
||||||
|
* Name pattern of the file, relative to the base directory of |
||||||
|
* this rule |
||||||
|
* @param isDirectory |
||||||
|
* Whether the target file is a directory or not |
||||||
|
* @return True if a match was made. |
||||||
|
*/ |
||||||
|
public boolean isMatch(String relativeTarget, boolean isDirectory) { |
||||||
|
if (relativeTarget == null) |
||||||
|
return false; |
||||||
|
if (relativeTarget.length() == 0) |
||||||
|
return false; |
||||||
|
boolean match = matcher.matches(relativeTarget, isDirectory); |
||||||
|
return match; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,4 @@ |
|||||||
|
/** |
||||||
|
* Support for reading .gitattributes. |
||||||
|
*/ |
||||||
|
package org.eclipse.jgit.attributes; |
Loading…
Reference in new issue