* [JGIT PATCH v4] Implementation of fnmatch and the ignore rules
@ 2008-06-13 18:30 Florian Köberle
2008-06-13 18:34 ` [JGIT PATCH v4 01/24] Added the package fnmatch and two exceptions Florian Koeberle
` (24 more replies)
0 siblings, 25 replies; 26+ messages in thread
From: Florian Köberle @ 2008-06-13 18:30 UTC (permalink / raw)
To: git; +Cc: Robin Rosenberg, Shawn O. Pearce
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi
Thanks for the suggestions and comments, the new patches are available here:
http://repo.or.cz/w/egit/florian.git?a=shortlog;h=refs/heads/mailinglist-patches-4
I will send them as a reply to this email soon.
The most noticeable change is that I improved the fnmatch functionality.
All character classes of the type [:class:] work now.
Other character classes, including the character classes [=class=] and
[.class.] will cause a InvalidPatternException. The last two aren't
supported at my bash neighter, but are defined in the POSIX standard.
Two other implementation differences are:
The bash shell doesn't support the full range of digits, but my
implementatiton does. e.g. ۹ (U+06F9 EXTENDED ARABIC-INDIC DIGIT NINE)
A pattern like [[:] results in a InvalidPatternException in my
implementation. The shell has there a strange behavior: if the files "["
and ":" exists then "ls [[:]" shows only ":".
The other main change is that I introduced a AbstractTestCase class
which which offers functionality to create temporary folders.
RepositoryTestCase extends now from this new class.
An other patch reducing the duplication between AbstractTestCase and
RepositoryTestCase class will follow soon, if you like the change.
I also updated all license statements to the 3-clause BSD.
Even if you don't like the last change (the AbstractTestCase change) it
would be cool if you would accept at least some patches.
Best regards,
Florian
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFIUrzK59ca4mzhfxMRAg+XAKDUoEdpil0+qqZ0pgMx/yJYKd2ztwCfe4aI
fqJ25VXnX2RWEay9aH4Np6Y=
=4i44
-----END PGP SIGNATURE-----
^ permalink raw reply [flat|nested] 26+ messages in thread
* [JGIT PATCH v4 01/24] Added the package fnmatch and two exceptions.
2008-06-13 18:30 [JGIT PATCH v4] Implementation of fnmatch and the ignore rules Florian Köberle
@ 2008-06-13 18:34 ` Florian Koeberle
2008-06-13 18:34 ` [JGIT PATCH v4 02/24] Added the interface FilePattern Florian Koeberle
` (23 subsequent siblings)
24 siblings, 0 replies; 26+ messages in thread
From: Florian Koeberle @ 2008-06-13 18:34 UTC (permalink / raw)
To: git; +Cc: Florian Koeberle
Signed-off-by: Florian Koeberle <florianskarten@web.de>
---
.../spearce/jgit/fnmatch/FileNameMatcherTest.java | 678 ++++++++++++++++++++
.../jgit/errors/InvalidPatternException.java | 65 ++
.../jgit/errors/NoClosingBracketException.java | 69 ++
.../src/org/spearce/jgit/fnmatch/AbstractHead.java | 74 +++
.../org/spearce/jgit/fnmatch/CharacterHead.java | 53 ++
.../org/spearce/jgit/fnmatch/FileNameMatcher.java | 327 ++++++++++
.../src/org/spearce/jgit/fnmatch/GroupHead.java | 220 +++++++
.../src/org/spearce/jgit/fnmatch/Head.java | 50 ++
.../src/org/spearce/jgit/fnmatch/LastHead.java | 56 ++
.../jgit/fnmatch/RestrictedWildCardHead.java | 52 ++
.../src/org/spearce/jgit/fnmatch/WildCardHead.java | 49 ++
11 files changed, 1693 insertions(+), 0 deletions(-)
create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/fnmatch/FileNameMatcherTest.java
create mode 100644 org.spearce.jgit/src/org/spearce/jgit/errors/InvalidPatternException.java
create mode 100644 org.spearce.jgit/src/org/spearce/jgit/errors/NoClosingBracketException.java
create mode 100644 org.spearce.jgit/src/org/spearce/jgit/fnmatch/AbstractHead.java
create mode 100644 org.spearce.jgit/src/org/spearce/jgit/fnmatch/CharacterHead.java
create mode 100644 org.spearce.jgit/src/org/spearce/jgit/fnmatch/FileNameMatcher.java
create mode 100644 org.spearce.jgit/src/org/spearce/jgit/fnmatch/GroupHead.java
create mode 100644 org.spearce.jgit/src/org/spearce/jgit/fnmatch/Head.java
create mode 100644 org.spearce.jgit/src/org/spearce/jgit/fnmatch/LastHead.java
create mode 100644 org.spearce.jgit/src/org/spearce/jgit/fnmatch/RestrictedWildCardHead.java
create mode 100644 org.spearce.jgit/src/org/spearce/jgit/fnmatch/WildCardHead.java
diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/fnmatch/FileNameMatcherTest.java b/org.spearce.jgit.test/tst/org/spearce/jgit/fnmatch/FileNameMatcherTest.java
new file mode 100644
index 0000000..74e88fa
--- /dev/null
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/fnmatch/FileNameMatcherTest.java
@@ -0,0 +1,678 @@
+/*
+ * Copyright (C) 2008, Florian Köberle <florianskarten@web.de>
+ *
+ * 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 Git Development Community 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.spearce.jgit.fnmatch;
+
+import org.spearce.jgit.errors.InvalidPatternException;
+import org.spearce.jgit.fnmatch.FileNameMatcher;
+
+import junit.framework.TestCase;
+
+public class FileNameMatcherTest extends TestCase {
+
+ private void assertMatch(final String pattern, final String input,
+ final boolean matchExpected, final boolean appendCanMatchExpected)
+ throws InvalidPatternException {
+ final FileNameMatcher matcher = new FileNameMatcher(pattern, null);
+ matcher.append(input);
+ assertEquals(matchExpected, matcher.isMatch());
+ assertEquals(appendCanMatchExpected, matcher.canAppendMatch());
+ }
+
+ private void assertFileNameMatch(final String pattern, final String input,
+ final char excludedCharacter, final boolean matchExpected,
+ final boolean appendCanMatchExpected)
+ throws InvalidPatternException {
+ final FileNameMatcher matcher = new FileNameMatcher(pattern,
+ new Character(excludedCharacter));
+ matcher.append(input);
+ assertEquals(matchExpected, matcher.isMatch());
+ assertEquals(appendCanMatchExpected, matcher.canAppendMatch());
+ }
+
+ public void testVerySimplePatternCase0() throws Exception {
+ assertMatch("", "", true, false);
+ }
+
+ public void testVerySimplePatternCase1() throws Exception {
+ assertMatch("ab", "a", false, true);
+ }
+
+ public void testVerySimplePatternCase2() throws Exception {
+ assertMatch("ab", "ab", true, false);
+ }
+
+ public void testVerySimplePatternCase3() throws Exception {
+ assertMatch("ab", "ac", false, false);
+ }
+
+ public void testVerySimplePatternCase4() throws Exception {
+ assertMatch("ab", "abc", false, false);
+ }
+
+ public void testVerySimpleWirdcardCase0() throws Exception {
+ assertMatch("?", "a", true, false);
+ }
+
+ public void testVerySimpleWildCardCase1() throws Exception {
+ assertMatch("??", "a", false, true);
+ }
+
+ public void testVerySimpleWildCardCase2() throws Exception {
+ assertMatch("??", "ab", true, false);
+ }
+
+ public void testVerySimpleWildCardCase3() throws Exception {
+ assertMatch("??", "abc", false, false);
+ }
+
+ public void testVerySimpleStarCase0() throws Exception {
+ assertMatch("*", "", true, true);
+ }
+
+ public void testVerySimpleStarCase1() throws Exception {
+ assertMatch("*", "a", true, true);
+ }
+
+ public void testVerySimpleStarCase2() throws Exception {
+ assertMatch("*", "ab", true, true);
+ }
+
+ public void testSimpleStarCase0() throws Exception {
+ assertMatch("a*b", "a", false, true);
+ }
+
+ public void testSimpleStarCase1() throws Exception {
+ assertMatch("a*c", "ac", true, true);
+ }
+
+ public void testSimpleStarCase2() throws Exception {
+ assertMatch("a*c", "ab", false, true);
+ }
+
+ public void testSimpleStarCase3() throws Exception {
+ assertMatch("a*c", "abc", true, true);
+ }
+
+ public void testManySolutionsCase0() throws Exception {
+ assertMatch("a*a*a", "aaa", true, true);
+ }
+
+ public void testManySolutionsCase1() throws Exception {
+ assertMatch("a*a*a", "aaaa", true, true);
+ }
+
+ public void testManySolutionsCase2() throws Exception {
+ assertMatch("a*a*a", "ababa", true, true);
+ }
+
+ public void testManySolutionsCase3() throws Exception {
+ assertMatch("a*a*a", "aaaaaaaa", true, true);
+ }
+
+ public void testManySolutionsCase4() throws Exception {
+ assertMatch("a*a*a", "aaaaaaab", false, true);
+ }
+
+ public void testVerySimpleGroupCase0() throws Exception {
+ assertMatch("[ab]", "a", true, false);
+ }
+
+ public void testVerySimpleGroupCase1() throws Exception {
+ assertMatch("[ab]", "b", true, false);
+ }
+
+ public void testVerySimpleGroupCase2() throws Exception {
+ assertMatch("[ab]", "ab", false, false);
+ }
+
+ public void testVerySimpleGroupRangeCase0() throws Exception {
+ assertMatch("[b-d]", "a", false, false);
+ }
+
+ public void testVerySimpleGroupRangeCase1() throws Exception {
+ assertMatch("[b-d]", "b", true, false);
+ }
+
+ public void testVerySimpleGroupRangeCase2() throws Exception {
+ assertMatch("[b-d]", "c", true, false);
+ }
+
+ public void testVerySimpleGroupRangeCase3() throws Exception {
+ assertMatch("[b-d]", "d", true, false);
+ }
+
+ public void testVerySimpleGroupRangeCase4() throws Exception {
+ assertMatch("[b-d]", "e", false, false);
+ }
+
+ public void testVerySimpleGroupRangeCase5() throws Exception {
+ assertMatch("[b-d]", "-", false, false);
+ }
+
+ public void testTwoGroupsCase0() throws Exception {
+ assertMatch("[b-d][ab]", "bb", true, false);
+ }
+
+ public void testTwoGroupsCase1() throws Exception {
+ assertMatch("[b-d][ab]", "ca", true, false);
+ }
+
+ public void testTwoGroupsCase2() throws Exception {
+ assertMatch("[b-d][ab]", "fa", false, false);
+ }
+
+ public void testTwoGroupsCase3() throws Exception {
+ assertMatch("[b-d][ab]", "bc", false, false);
+ }
+
+ public void testTwoRangesInOneGroupCase0() throws Exception {
+ assertMatch("[b-ce-e]", "a", false, false);
+ }
+
+ public void testTwoRangesInOneGroupCase1() throws Exception {
+ assertMatch("[b-ce-e]", "b", true, false);
+ }
+
+ public void testTwoRangesInOneGroupCase2() throws Exception {
+ assertMatch("[b-ce-e]", "c", true, false);
+ }
+
+ public void testTwoRangesInOneGroupCase3() throws Exception {
+ assertMatch("[b-ce-e]", "d", false, false);
+ }
+
+ public void testTwoRangesInOneGroupCase4() throws Exception {
+ assertMatch("[b-ce-e]", "e", true, false);
+ }
+
+ public void testTwoRangesInOneGroupCase5() throws Exception {
+ assertMatch("[b-ce-e]", "f", false, false);
+ }
+
+ public void testIncompleteRangesInOneGroupCase0() throws Exception {
+ assertMatch("a[b-]", "ab", true, false);
+ }
+
+ public void testIncompleteRangesInOneGroupCase1() throws Exception {
+ assertMatch("a[b-]", "ac", false, false);
+ }
+
+ public void testIncompleteRangesInOneGroupCase2() throws Exception {
+ assertMatch("a[b-]", "a-", true, false);
+ }
+
+ public void testCombinedRangesInOneGroupCase0() throws Exception {
+ assertMatch("[a-c-e]", "b", true, false);
+ }
+
+ /**
+ * The c belongs to the range a-c. "-e" is no valid range so d should not
+ * match.
+ *
+ * @throws Exception
+ * for some reasons
+ */
+ public void testCombinedRangesInOneGroupCase1() throws Exception {
+ assertMatch("[a-c-e]", "d", false, false);
+ }
+
+ public void testCombinedRangesInOneGroupCase2() throws Exception {
+ assertMatch("[a-c-e]", "e", true, false);
+ }
+
+ public void testInversedGroupCase0() throws Exception {
+ assertMatch("[!b-c]", "a", true, false);
+ }
+
+ public void testInversedGroupCase1() throws Exception {
+ assertMatch("[!b-c]", "b", false, false);
+ }
+
+ public void testInversedGroupCase2() throws Exception {
+ assertMatch("[!b-c]", "c", false, false);
+ }
+
+ public void testInversedGroupCase3() throws Exception {
+ assertMatch("[!b-c]", "d", true, false);
+ }
+
+ public void testAlphaGroupCase0() throws Exception {
+ assertMatch("[[:alpha:]]", "d", true, false);
+ }
+
+ public void testAlphaGroupCase1() throws Exception {
+ assertMatch("[[:alpha:]]", ":", false, false);
+ }
+
+ public void testAlphaGroupCase2() throws Exception {
+ assertMatch("[[:alpha:]]", "ö", true, false);
+ }
+
+ public void test2AlphaGroupsCase0() throws Exception {
+ assertMatch("[[:alpha:]][[:alpha:]]", "aö", true, false);
+ assertMatch("[[:alpha:]][[:alpha:]]", "a1", false, false);
+ }
+
+ public void testAlnumGroupCase0() throws Exception {
+ assertMatch("[[:alnum:]]", "a", true, false);
+ }
+
+ public void testAlnumGroupCase1() throws Exception {
+ assertMatch("[[:alnum:]]", "1", true, false);
+ }
+
+ public void testAlnumGroupCase2() throws Exception {
+ assertMatch("[[:alnum:]]", ":", false, false);
+ }
+
+ public void testBlankGroupCase0() throws Exception {
+ assertMatch("[[:blank:]]", " ", true, false);
+ }
+
+ public void testBlankGroupCase1() throws Exception {
+ assertMatch("[[:blank:]]", "\t", true, false);
+ }
+
+ public void testBlankGroupCase2() throws Exception {
+ assertMatch("[[:blank:]]", "\r", false, false);
+ }
+
+ public void testBlankGroupCase3() throws Exception {
+ assertMatch("[[:blank:]]", "\n", false, false);
+ }
+
+ public void testBlankGroupCase4() throws Exception {
+ assertMatch("[[:blank:]]", "a", false, false);
+ }
+
+ public void testCntrlGroupCase0() throws Exception {
+ assertMatch("[[:cntrl:]]", "a", false, false);
+ }
+
+ public void testCntrlGroupCase1() throws Exception {
+ assertMatch("[[:cntrl:]]", String.valueOf((char) 7), true, false);
+ }
+
+ public void testDigitGroupCase0() throws Exception {
+ assertMatch("[[:digit:]]", "0", true, false);
+ }
+
+ public void testDigitGroupCase1() throws Exception {
+ assertMatch("[[:digit:]]", "5", true, false);
+ }
+
+ public void testDigitGroupCase2() throws Exception {
+ assertMatch("[[:digit:]]", "9", true, false);
+ }
+
+ public void testDigitGroupCase3() throws Exception {
+ assertMatch("[[:digit:]]", "Û¹", true, false);
+ }
+
+ public void testDigitGroupCase4() throws Exception {
+ assertMatch("[[:digit:]]", "a", false, false);
+ }
+
+ public void testDigitGroupCase5() throws Exception {
+ assertMatch("[[:digit:]]", "]", false, false);
+ }
+
+ public void testGraphGroupCase0() throws Exception {
+ assertMatch("[[:graph:]]", "]", true, false);
+ }
+
+ public void testGraphGroupCase1() throws Exception {
+ assertMatch("[[:graph:]]", "a", true, false);
+ }
+
+ public void testGraphGroupCase2() throws Exception {
+ assertMatch("[[:graph:]]", ".", true, false);
+ }
+
+ public void testGraphGroupCase3() throws Exception {
+ assertMatch("[[:graph:]]", "0", true, false);
+ }
+
+ public void testGraphGroupCase4() throws Exception {
+ assertMatch("[[:graph:]]", " ", false, false);
+ }
+
+ public void testGraphGroupCase5() throws Exception {
+ assertMatch("[[:graph:]]", "ö", true, false);
+ }
+
+ public void testLowerGroupCase0() throws Exception {
+ assertMatch("[[:lower:]]", "a", true, false);
+ }
+
+ public void testLowerGroupCase1() throws Exception {
+ assertMatch("[[:lower:]]", "h", true, false);
+ }
+
+ public void testLowerGroupCase2() throws Exception {
+ assertMatch("[[:lower:]]", "A", false, false);
+ }
+
+ public void testLowerGroupCase3() throws Exception {
+ assertMatch("[[:lower:]]", "H", false, false);
+ }
+
+ public void testLowerGroupCase4() throws Exception {
+ assertMatch("[[:lower:]]", "ä", true, false);
+ }
+
+ public void testLowerGroupCase5() throws Exception {
+ assertMatch("[[:lower:]]", ".", false, false);
+ }
+
+ public void testPrintGroupCase0() throws Exception {
+ assertMatch("[[:print:]]", "]", true, false);
+ }
+
+ public void testPrintGroupCase1() throws Exception {
+ assertMatch("[[:print:]]", "a", true, false);
+ }
+
+ public void testPrintGroupCase2() throws Exception {
+ assertMatch("[[:print:]]", ".", true, false);
+ }
+
+ public void testPrintGroupCase3() throws Exception {
+ assertMatch("[[:print:]]", "0", true, false);
+ }
+
+ public void testPrintGroupCase4() throws Exception {
+ assertMatch("[[:print:]]", " ", true, false);
+ }
+
+ public void testPrintGroupCase5() throws Exception {
+ assertMatch("[[:print:]]", "ö", true, false);
+ }
+
+ public void testPunctGroupCase0() throws Exception {
+ assertMatch("[[:punct:]]", ".", true, false);
+ }
+
+ public void testPunctGroupCase1() throws Exception {
+ assertMatch("[[:punct:]]", "@", true, false);
+ }
+
+ public void testPunctGroupCase2() throws Exception {
+ assertMatch("[[:punct:]]", " ", false, false);
+ }
+
+ public void testPunctGroupCase3() throws Exception {
+ assertMatch("[[:punct:]]", "a", false, false);
+ }
+
+ public void testSpaceGroupCase0() throws Exception {
+ assertMatch("[[:space:]]", " ", true, false);
+ }
+
+ public void testSpaceGroupCase1() throws Exception {
+ assertMatch("[[:space:]]", "\t", true, false);
+ }
+
+ public void testSpaceGroupCase2() throws Exception {
+ assertMatch("[[:space:]]", "\r", true, false);
+ }
+
+ public void testSpaceGroupCase3() throws Exception {
+ assertMatch("[[:space:]]", "\n", true, false);
+ }
+
+ public void testSpaceGroupCase4() throws Exception {
+ assertMatch("[[:space:]]", "a", false, false);
+ }
+
+ public void testUpperGroupCase0() throws Exception {
+ assertMatch("[[:upper:]]", "a", false, false);
+ }
+
+ public void testUpperGroupCase1() throws Exception {
+ assertMatch("[[:upper:]]", "h", false, false);
+ }
+
+ public void testUpperGroupCase2() throws Exception {
+ assertMatch("[[:upper:]]", "A", true, false);
+ }
+
+ public void testUpperGroupCase3() throws Exception {
+ assertMatch("[[:upper:]]", "H", true, false);
+ }
+
+ public void testUpperGroupCase4() throws Exception {
+ assertMatch("[[:upper:]]", "Ã", true, false);
+ }
+
+ public void testUpperGroupCase5() throws Exception {
+ assertMatch("[[:upper:]]", ".", false, false);
+ }
+
+ public void testXDigitGroupCase0() throws Exception {
+ assertMatch("[[:xdigit:]]", "a", true, false);
+ }
+
+ public void testXDigitGroupCase1() throws Exception {
+ assertMatch("[[:xdigit:]]", "d", true, false);
+ }
+
+ public void testXDigitGroupCase2() throws Exception {
+ assertMatch("[[:xdigit:]]", "f", true, false);
+ }
+
+ public void testXDigitGroupCase3() throws Exception {
+ assertMatch("[[:xdigit:]]", "0", true, false);
+ }
+
+ public void testXDigitGroupCase4() throws Exception {
+ assertMatch("[[:xdigit:]]", "5", true, false);
+ }
+
+ public void testXDigitGroupCase5() throws Exception {
+ assertMatch("[[:xdigit:]]", "9", true, false);
+ }
+
+ public void testXDigitGroupCase6() throws Exception {
+ assertMatch("[[:xdigit:]]", "Û¹", false, false);
+ }
+
+ public void testXDigitGroupCase7() throws Exception {
+ assertMatch("[[:xdigit:]]", ".", false, false);
+ }
+
+ public void testWordroupCase0() throws Exception {
+ assertMatch("[[:word:]]", "g", true, false);
+ }
+
+ public void testWordroupCase1() throws Exception {
+ assertMatch("[[:word:]]", "ö", true, false);
+ }
+
+ public void testWordroupCase2() throws Exception {
+ assertMatch("[[:word:]]", "5", true, false);
+ }
+
+ public void testWordroupCase3() throws Exception {
+ assertMatch("[[:word:]]", "_", true, false);
+ }
+
+ public void testWordroupCase4() throws Exception {
+ assertMatch("[[:word:]]", " ", false, false);
+ }
+
+ public void testWordroupCase5() throws Exception {
+ assertMatch("[[:word:]]", ".", false, false);
+ }
+
+ public void testSpecialGroupCase0() throws Exception {
+ assertMatch("[[]", "[", true, false);
+ }
+
+ public void testSpecialGroupCase1() throws Exception {
+ assertMatch("[]]", "]", true, false);
+ }
+
+ public void testSpecialGroupCase2() throws Exception {
+ assertMatch("[]a]", "]", true, false);
+ }
+
+ public void testSpecialGroupCase3() throws Exception {
+ assertMatch("[a[]", "[", true, false);
+ }
+
+ public void testSpecialGroupCase4() throws Exception {
+ assertMatch("[a[]", "a", true, false);
+ }
+
+ public void testSpecialGroupCase5() throws Exception {
+ assertMatch("[!]]", "]", false, false);
+ }
+
+ public void testSpecialGroupCase6() throws Exception {
+ assertMatch("[!]]", "x", true, false);
+ }
+
+ public void testSpecialGroupCase7() throws Exception {
+ assertMatch("[:]]", ":]", true, false);
+ }
+
+ public void testSpecialGroupCase8() throws Exception {
+ assertMatch("[:]]", ":", false, true);
+ }
+
+ public void testSpecialGroupCase9() throws Exception {
+ try {
+ assertMatch("[[:]", ":", true, true);
+ fail("InvalidPatternException expected");
+ } catch (InvalidPatternException e) {
+ // expected
+ }
+ }
+
+ public void testUnsupportedGroupCase0() throws Exception {
+ try {
+ assertMatch("[[=a=]]", "b", false, false);
+ fail("InvalidPatternException expected");
+ } catch (InvalidPatternException e) {
+ assertTrue(e.getMessage().contains("[=a=]"));
+ }
+ }
+
+ public void testUnsupportedGroupCase1() throws Exception {
+ try {
+ assertMatch("[[.a.]]", "b", false, false);
+ fail("InvalidPatternException expected");
+ } catch (InvalidPatternException e) {
+ assertTrue(e.getMessage().contains("[.a.]"));
+ }
+ }
+
+ public void testFilePathSimpleCase() throws Exception {
+ assertFileNameMatch("a/b", "a/b", '/', true, false);
+ }
+
+ public void testFilePathCase0() throws Exception {
+ assertFileNameMatch("a*b", "a/b", '/', false, false);
+ }
+
+ public void testFilePathCase1() throws Exception {
+ assertFileNameMatch("a?b", "a/b", '/', false, false);
+ }
+
+ public void testFilePathCase2() throws Exception {
+ assertFileNameMatch("a*b", "a\\b", '\\', false, false);
+ }
+
+ public void testFilePathCase3() throws Exception {
+ assertFileNameMatch("a?b", "a\\b", '\\', false, false);
+ }
+
+ public void testReset() throws Exception {
+ final String pattern = "helloworld";
+ final FileNameMatcher matcher = new FileNameMatcher(pattern, null);
+ matcher.append("helloworld");
+ assertEquals(true, matcher.isMatch());
+ assertEquals(false, matcher.canAppendMatch());
+ matcher.reset();
+ matcher.append("hello");
+ assertEquals(false, matcher.isMatch());
+ assertEquals(true, matcher.canAppendMatch());
+ matcher.append("world");
+ assertEquals(true, matcher.isMatch());
+ assertEquals(false, matcher.canAppendMatch());
+ matcher.append("to much");
+ assertEquals(false, matcher.isMatch());
+ assertEquals(false, matcher.canAppendMatch());
+ matcher.reset();
+ matcher.append("helloworld");
+ assertEquals(true, matcher.isMatch());
+ assertEquals(false, matcher.canAppendMatch());
+ }
+
+ public void testCreateMatcherForSuffix() throws Exception {
+ final String pattern = "helloworld";
+ final FileNameMatcher matcher = new FileNameMatcher(pattern, null);
+ matcher.append("hello");
+ final FileNameMatcher childMatcher = matcher.createMatcherForSuffix();
+ assertEquals(false, matcher.isMatch());
+ assertEquals(true, matcher.canAppendMatch());
+ assertEquals(false, childMatcher.isMatch());
+ assertEquals(true, childMatcher.canAppendMatch());
+ matcher.append("world");
+ assertEquals(true, matcher.isMatch());
+ assertEquals(false, matcher.canAppendMatch());
+ assertEquals(false, childMatcher.isMatch());
+ assertEquals(true, childMatcher.canAppendMatch());
+ childMatcher.append("world");
+ assertEquals(true, matcher.isMatch());
+ assertEquals(false, matcher.canAppendMatch());
+ assertEquals(true, childMatcher.isMatch());
+ assertEquals(false, childMatcher.canAppendMatch());
+ childMatcher.reset();
+ assertEquals(true, matcher.isMatch());
+ assertEquals(false, matcher.canAppendMatch());
+ assertEquals(false, childMatcher.isMatch());
+ assertEquals(true, childMatcher.canAppendMatch());
+ childMatcher.append("world");
+ assertEquals(true, matcher.isMatch());
+ assertEquals(false, matcher.canAppendMatch());
+ assertEquals(true, childMatcher.isMatch());
+ assertEquals(false, childMatcher.canAppendMatch());
+ }
+}
diff --git a/org.spearce.jgit/src/org/spearce/jgit/errors/InvalidPatternException.java b/org.spearce.jgit/src/org/spearce/jgit/errors/InvalidPatternException.java
new file mode 100644
index 0000000..02f67fe
--- /dev/null
+++ b/org.spearce.jgit/src/org/spearce/jgit/errors/InvalidPatternException.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2008, Florian Köberle <florianskarten@web.de>
+ *
+ * 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 Git Development Community 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.spearce.jgit.errors;
+
+/**
+ * Thrown when a pattern passed in an argument was wrong.
+ *
+ */
+public class InvalidPatternException extends Exception {
+ private final String pattern;
+
+ /**
+ * @param message
+ * explains what was wrong with the pattern.
+ * @param pattern
+ * the invalid pattern.
+ */
+ public InvalidPatternException(String message, String pattern) {
+ super(message);
+ this.pattern = pattern;
+ }
+
+ /**
+ * @return the invalid pattern.
+ */
+ public String getPattern() {
+ return pattern;
+ }
+
+}
diff --git a/org.spearce.jgit/src/org/spearce/jgit/errors/NoClosingBracketException.java b/org.spearce.jgit/src/org/spearce/jgit/errors/NoClosingBracketException.java
new file mode 100644
index 0000000..1a93906
--- /dev/null
+++ b/org.spearce.jgit/src/org/spearce/jgit/errors/NoClosingBracketException.java
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2008, Florian Köberle <florianskarten@web.de>
+ *
+ * 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 Git Development Community 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.spearce.jgit.errors;
+
+/**
+ * Thrown when a pattern contains a character group which is open to the right
+ * side or a character class which is open to the right side.
+ */
+public class NoClosingBracketException extends InvalidPatternException {
+
+ /**
+ * @param indexOfOpeningBracket
+ * the position of the [ character which has no ] character.
+ * @param openingBracket
+ * the unclosed bracket.
+ * @param closingBracket
+ * the missing closing bracket.
+ * @param pattern
+ * the invalid pattern.
+ */
+ public NoClosingBracketException(final int indexOfOpeningBracket,
+ final String openingBracket, final String closingBracket,
+ final String pattern) {
+ super(createMessage(indexOfOpeningBracket, openingBracket,
+ closingBracket), pattern);
+ }
+
+ private static String createMessage(final int indexOfOpeningBracket,
+ final String openingBracket, final String closingBracket) {
+ return String.format("No closing %s found for %s at index %s.",
+ closingBracket, openingBracket, new Integer(
+ indexOfOpeningBracket));
+ }
+}
diff --git a/org.spearce.jgit/src/org/spearce/jgit/fnmatch/AbstractHead.java b/org.spearce.jgit/src/org/spearce/jgit/fnmatch/AbstractHead.java
new file mode 100644
index 0000000..1e9a0ca
--- /dev/null
+++ b/org.spearce.jgit/src/org/spearce/jgit/fnmatch/AbstractHead.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2008, Florian Köberle <florianskarten@web.de>
+ *
+ * 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 Git Development Community 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.spearce.jgit.fnmatch;
+
+import java.util.List;
+
+abstract class AbstractHead implements Head {
+ private List<Head> newHeads = null;
+
+ private final boolean star;
+
+ protected abstract boolean matches(char c);
+
+ AbstractHead(boolean star) {
+ this.star = star;
+ }
+
+ /**
+ *
+ * @param newHeads
+ * a list of {@link Head}s which will not be modified.
+ */
+ public final void setNewHeads(List<Head> newHeads) {
+ if (this.newHeads != null)
+ throw new IllegalStateException("Property is already non null");
+ this.newHeads = newHeads;
+ }
+
+ public List<Head> getNextHeads(char c) {
+ if (matches(c))
+ return newHeads;
+ else
+ return FileNameMatcher.EMPTY_HEAD_LIST;
+ }
+
+ boolean isStar() {
+ return star;
+ }
+}
diff --git a/org.spearce.jgit/src/org/spearce/jgit/fnmatch/CharacterHead.java b/org.spearce.jgit/src/org/spearce/jgit/fnmatch/CharacterHead.java
new file mode 100644
index 0000000..01c3403
--- /dev/null
+++ b/org.spearce.jgit/src/org/spearce/jgit/fnmatch/CharacterHead.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2008, Florian Köberle <florianskarten@web.de>
+ *
+ * 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 Git Development Community 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.spearce.jgit.fnmatch;
+
+final class CharacterHead extends AbstractHead {
+ private final char expectedCharacter;
+
+ protected CharacterHead(final char expectedCharacter) {
+ super(false);
+ this.expectedCharacter = expectedCharacter;
+ }
+
+ @Override
+ protected final boolean matches(final char c) {
+ return c == expectedCharacter;
+ }
+
+}
diff --git a/org.spearce.jgit/src/org/spearce/jgit/fnmatch/FileNameMatcher.java b/org.spearce.jgit/src/org/spearce/jgit/fnmatch/FileNameMatcher.java
new file mode 100644
index 0000000..21fbf77
--- /dev/null
+++ b/org.spearce.jgit/src/org/spearce/jgit/fnmatch/FileNameMatcher.java
@@ -0,0 +1,327 @@
+/*
+ * Copyright (C) 2008, Florian Köberle <florianskarten@web.de>
+ *
+ * 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 Git Development Community 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.spearce.jgit.fnmatch;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.spearce.jgit.errors.InvalidPatternException;
+import org.spearce.jgit.errors.NoClosingBracketException;
+
+/**
+ * This class can be used to match filenames against fnmatch like patterns. It
+ * is not thread save.
+ */
+public class FileNameMatcher {
+ static final List<Head> EMPTY_HEAD_LIST = Collections.emptyList();
+
+ private static final Pattern characterClassStartPattern = Pattern
+ .compile("\\[[.:=]");
+
+ private List<Head> headsStartValue;
+
+ private List<Head> heads;
+
+ /**
+ * {{@link #extendStringToMatchByOneCharacter(char)} needs a list for the
+ * new heads, allocating a new array would be bad for the performance, as
+ * the method gets called very often.
+ *
+ */
+ private List<Head> listForLocalUseage;
+
+ /**
+ *
+ * @param headsStartValue
+ * must be a list which will never be modified.
+ */
+ private FileNameMatcher(final List<Head> headsStartValue) {
+ this.headsStartValue = headsStartValue;
+ this.heads = new ArrayList<Head>(headsStartValue.size());
+ this.heads.addAll(this.headsStartValue);
+ this.listForLocalUseage = new ArrayList<Head>(headsStartValue.size());
+ }
+
+ /**
+ * @param patternString
+ * must contain a pattern which fnmatch would accept.
+ * @param invalidWildgetCharacter
+ * if this parameter isn't null then this character will not
+ * match at wildcards(* and ? are wildcards).
+ * @throws InvalidPatternException
+ * if the patternString contains a invalid fnmatch pattern.
+ */
+ public FileNameMatcher(final String patternString,
+ final Character invalidWildgetCharacter)
+ throws InvalidPatternException {
+ this(createHeadsStartValues(patternString, invalidWildgetCharacter));
+ }
+
+ private static List<Head> createHeadsStartValues(
+ final String patternString, final Character invalidWildgetCharacter)
+ throws InvalidPatternException {
+
+ final List<AbstractHead> allHeads = parseHeads(patternString,
+ invalidWildgetCharacter);
+
+ List<Head> nextHeadsSuggestion = new ArrayList<Head>(2);
+ nextHeadsSuggestion.add(LastHead.INSTANCE);
+ for (int i = allHeads.size() - 1; i >= 0; i--) {
+ final AbstractHead head = allHeads.get(i);
+
+ // explanation:
+ // a and * of the pattern "a*b"
+ // need *b as newHeads
+ // that's why * extends the list for it self and it's left neighbor.
+ if (head.isStar()) {
+ nextHeadsSuggestion.add(head);
+ head.setNewHeads(nextHeadsSuggestion);
+ } else {
+ head.setNewHeads(nextHeadsSuggestion);
+ nextHeadsSuggestion = new ArrayList<Head>(2);
+ nextHeadsSuggestion.add(head);
+ }
+ }
+ return nextHeadsSuggestion;
+ }
+
+ private static int findGroupEnd(final int indexOfStartBracket,
+ final String pattern) throws InvalidPatternException {
+ int firstValidCharClassIndex = indexOfStartBracket + 1;
+ int firstValidEndBracketIndex = indexOfStartBracket + 2;
+
+ if (indexOfStartBracket + 1 >= pattern.length())
+ throw new NoClosingBracketException(indexOfStartBracket, "[", "]",
+ pattern);
+
+ if (pattern.charAt(firstValidCharClassIndex) == '!') {
+ firstValidCharClassIndex++;
+ firstValidEndBracketIndex++;
+ }
+
+ final Matcher charClassStartMatcher = characterClassStartPattern
+ .matcher(pattern);
+
+ int groupEnd = -1;
+ while (groupEnd == -1) {
+
+ final int possibleGroupEnd = pattern.indexOf(']',
+ firstValidEndBracketIndex);
+ if (possibleGroupEnd == -1)
+ throw new NoClosingBracketException(indexOfStartBracket, "[",
+ "]", pattern);
+
+ final boolean foundCharClass = charClassStartMatcher
+ .find(firstValidCharClassIndex);
+
+ if (foundCharClass
+ && charClassStartMatcher.start() < possibleGroupEnd) {
+
+ final String classStart = charClassStartMatcher.group(0);
+ final String classEnd = classStart.charAt(1) + "]";
+
+ final int classStartIndex = charClassStartMatcher.start();
+ final int classEndIndex = pattern.indexOf(classEnd,
+ classStartIndex + 2);
+
+ if (classEndIndex == -1)
+ throw new NoClosingBracketException(classStartIndex,
+ classStart, classEnd, pattern);
+
+ firstValidCharClassIndex = classEndIndex + 2;
+ firstValidEndBracketIndex = firstValidCharClassIndex;
+ } else {
+ groupEnd = possibleGroupEnd;
+ }
+ }
+ return groupEnd;
+ }
+
+ private static List<AbstractHead> parseHeads(final String pattern,
+ final Character invalidWildgetCharacter)
+ throws InvalidPatternException {
+
+ int currentIndex = 0;
+ List<AbstractHead> heads = new ArrayList<AbstractHead>();
+ while (currentIndex < pattern.length()) {
+ final int groupStart = pattern.indexOf('[', currentIndex);
+ if (groupStart == -1) {
+ final String patternPart = pattern.substring(currentIndex);
+ heads.addAll(createSimpleHeads(patternPart,
+ invalidWildgetCharacter));
+ currentIndex = pattern.length();
+ } else {
+ final String patternPart = pattern.substring(currentIndex,
+ groupStart);
+ heads.addAll(createSimpleHeads(patternPart,
+ invalidWildgetCharacter));
+
+ final int groupEnd = findGroupEnd(groupStart, pattern);
+ final String groupPart = pattern.substring(groupStart + 1,
+ groupEnd);
+ heads.add(new GroupHead(groupPart, pattern));
+ currentIndex = groupEnd + 1;
+ }
+ }
+ return heads;
+ }
+
+ private static List<AbstractHead> createSimpleHeads(
+ final String patternPart, final Character invalidWildgetCharacter) {
+ final List<AbstractHead> heads = new ArrayList<AbstractHead>(
+ patternPart.length());
+ for (int i = 0; i < patternPart.length(); i++) {
+ final char c = patternPart.charAt(i);
+ switch (c) {
+ case '*': {
+ final AbstractHead head = createWildCardHead(
+ invalidWildgetCharacter, true);
+ heads.add(head);
+ break;
+ }
+ case '?': {
+ final AbstractHead head = createWildCardHead(
+ invalidWildgetCharacter, false);
+ heads.add(head);
+ break;
+ }
+ default:
+ final CharacterHead head = new CharacterHead(c);
+ heads.add(head);
+ }
+ }
+ return heads;
+ }
+
+ private static AbstractHead createWildCardHead(
+ final Character invalidWildgetCharacter, final boolean star) {
+ if (invalidWildgetCharacter != null)
+ return new RestrictedWildCardHead(invalidWildgetCharacter
+ .charValue(), star);
+ else
+ return new WildCardHead(star);
+ }
+
+ private void extendStringToMatchByOneCharacter(final char c) {
+ final List<Head> newHeads = listForLocalUseage;
+ newHeads.clear();
+ List<Head> lastAddedHeads = null;
+ for (int i = 0; i < heads.size(); i++) {
+ final Head head = heads.get(i);
+ final List<Head> headsToAdd = head.getNextHeads(c);
+ // Why the next performance optimization isn't wrong:
+ // Some times two heads return the very same list.
+ // We save future effort if we don't add these heads again.
+ // This is the case with the heads "a" and "*" of "a*b" which
+ // both can return the list ["*","b"]
+ if (headsToAdd != lastAddedHeads) {
+ newHeads.addAll(headsToAdd);
+ lastAddedHeads = headsToAdd;
+ }
+ }
+ listForLocalUseage = heads;
+ heads = newHeads;
+ }
+
+ /**
+ *
+ * @param stringToMatch
+ * extends the string which is matched against the patterns of
+ * this class.
+ */
+ public void append(final String stringToMatch) {
+ for (int i = 0; i < stringToMatch.length(); i++) {
+ final char c = stringToMatch.charAt(i);
+ extendStringToMatchByOneCharacter(c);
+ }
+ }
+
+ /**
+ * Resets this matcher to it's state right after construction.
+ */
+ public void reset() {
+ heads.clear();
+ heads.addAll(headsStartValue);
+ }
+
+ /**
+ *
+ * @return a {@link FileNameMatcher} instance which uses the same pattern
+ * like this matcher, but has the current state of this matcher as
+ * reset and start point.
+ */
+ public FileNameMatcher createMatcherForSuffix() {
+ final List<Head> copyOfHeads = new ArrayList<Head>(heads.size());
+ copyOfHeads.addAll(heads);
+ return new FileNameMatcher(copyOfHeads);
+ }
+
+ /**
+ *
+ * @return true, if the string currently being matched does match.
+ */
+ public boolean isMatch() {
+ final ListIterator<Head> headIterator = heads
+ .listIterator(heads.size());
+ while (headIterator.hasPrevious()) {
+ final Head head = headIterator.previous();
+ if (head == LastHead.INSTANCE) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ *
+ * @return false, if the string being matched will not match when the string
+ * gets extended.
+ */
+ public boolean canAppendMatch() {
+ for (int i = 0; i < heads.size(); i++) {
+ if (heads.get(i) != LastHead.INSTANCE) {
+ return true;
+ }
+ }
+ return false;
+ }
+}
diff --git a/org.spearce.jgit/src/org/spearce/jgit/fnmatch/GroupHead.java b/org.spearce.jgit/src/org/spearce/jgit/fnmatch/GroupHead.java
new file mode 100644
index 0000000..9f72010
--- /dev/null
+++ b/org.spearce.jgit/src/org/spearce/jgit/fnmatch/GroupHead.java
@@ -0,0 +1,220 @@
+/*
+ * Copyright (C) 2008, Florian Köberle <florianskarten@web.de>
+ *
+ * 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 Git Development Community 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.spearce.jgit.fnmatch;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.spearce.jgit.errors.InvalidPatternException;
+
+final class GroupHead extends AbstractHead {
+ private final List<CharacterPattern> characterClasses;
+
+ private static final Pattern REGEX_PATTERN = Pattern
+ .compile("([^-][-][^-]|\\[[.:=].*?[.:=]\\])");
+
+ private final boolean inverse;
+
+ GroupHead(String pattern, final String wholePattern)
+ throws InvalidPatternException {
+ super(false);
+ this.characterClasses = new ArrayList<CharacterPattern>();
+ this.inverse = pattern.startsWith("!");
+ if (inverse) {
+ pattern = pattern.substring(1);
+ }
+ final Matcher matcher = REGEX_PATTERN.matcher(pattern);
+ while (matcher.find()) {
+ final String characterClass = matcher.group(0);
+ if (characterClass.length() == 3 && characterClass.charAt(1) == '-') {
+ final char start = characterClass.charAt(0);
+ final char end = characterClass.charAt(2);
+ characterClasses.add(new CharacterRange(start, end));
+ } else if (characterClass.equals("[:alnum:]")) {
+ characterClasses.add(LetterPattern.INSTANCE);
+ characterClasses.add(DigitPattern.INSTANCE);
+ } else if (characterClass.equals("[:alpha:]")) {
+ characterClasses.add(LetterPattern.INSTANCE);
+ } else if (characterClass.equals("[:blank:]")) {
+ characterClasses.add(new OneCharacterPattern(' '));
+ characterClasses.add(new OneCharacterPattern('\t'));
+ } else if (characterClass.equals("[:cntrl:]")) {
+ characterClasses.add(new CharacterRange('\u0000', '\u001F'));
+ characterClasses.add(new OneCharacterPattern('\u007F'));
+ } else if (characterClass.equals("[:digit:]")) {
+ characterClasses.add(DigitPattern.INSTANCE);
+ } else if (characterClass.equals("[:graph:]")) {
+ characterClasses.add(new CharacterRange('\u0021', '\u007E'));
+ characterClasses.add(LetterPattern.INSTANCE);
+ characterClasses.add(DigitPattern.INSTANCE);
+ } else if (characterClass.equals("[:lower:]")) {
+ characterClasses.add(LowerPattern.INSTANCE);
+ } else if (characterClass.equals("[:print:]")) {
+ characterClasses.add(new CharacterRange('\u0020', '\u007E'));
+ characterClasses.add(LetterPattern.INSTANCE);
+ characterClasses.add(DigitPattern.INSTANCE);
+ } else if (characterClass.equals("[:punct:]")) {
+ characterClasses.add(PunctPattern.INSTANCE);
+ } else if (characterClass.equals("[:space:]")) {
+ characterClasses.add(WhitespacePattern.INSTANCE);
+ } else if (characterClass.equals("[:upper:]")) {
+ characterClasses.add(UpperPattern.INSTANCE);
+ } else if (characterClass.equals("[:xdigit:]")) {
+ characterClasses.add(new CharacterRange('0', '9'));
+ characterClasses.add(new CharacterRange('a', 'f'));
+ characterClasses.add(new CharacterRange('A', 'F'));
+ } else if (characterClass.equals("[:word:]")) {
+ characterClasses.add(new OneCharacterPattern('_'));
+ characterClasses.add(LetterPattern.INSTANCE);
+ characterClasses.add(DigitPattern.INSTANCE);
+ } else {
+ final String message = String.format(
+ "The character class %s is not supported.",
+ characterClass);
+ throw new InvalidPatternException(message, wholePattern);
+ }
+
+ pattern = matcher.replaceFirst("");
+ matcher.reset(pattern);
+ }
+ // pattern contains now no ranges
+ for (int i = 0; i < pattern.length(); i++) {
+ final char c = pattern.charAt(i);
+ characterClasses.add(new OneCharacterPattern(c));
+ }
+ }
+
+ @Override
+ protected final boolean matches(final char c) {
+ for (CharacterPattern pattern : characterClasses) {
+ if (pattern.matches(c)) {
+ return !inverse;
+ }
+ }
+ return inverse;
+ }
+
+ private interface CharacterPattern {
+ /**
+ * @param c
+ * the character to test
+ * @return returns true if the character matches a pattern.
+ */
+ boolean matches(char c);
+ }
+
+ private static final class CharacterRange implements CharacterPattern {
+ private final char start;
+
+ private final char end;
+
+ CharacterRange(char start, char end) {
+ this.start = start;
+ this.end = end;
+ }
+
+ public final boolean matches(char c) {
+ return start <= c && c <= end;
+ }
+ }
+
+ private static final class DigitPattern implements CharacterPattern {
+ static final GroupHead.DigitPattern INSTANCE = new DigitPattern();
+
+ public final boolean matches(char c) {
+ return Character.isDigit(c);
+ }
+ }
+
+ private static final class LetterPattern implements CharacterPattern {
+ static final GroupHead.LetterPattern INSTANCE = new LetterPattern();
+
+ public final boolean matches(char c) {
+ return Character.isLetter(c);
+ }
+ }
+
+ private static final class LowerPattern implements CharacterPattern {
+ static final GroupHead.LowerPattern INSTANCE = new LowerPattern();
+
+ public final boolean matches(char c) {
+ return Character.isLowerCase(c);
+ }
+ }
+
+ private static final class UpperPattern implements CharacterPattern {
+ static final GroupHead.UpperPattern INSTANCE = new UpperPattern();
+
+ public final boolean matches(char c) {
+ return Character.isUpperCase(c);
+ }
+ }
+
+ private static final class WhitespacePattern implements CharacterPattern {
+ static final GroupHead.WhitespacePattern INSTANCE = new WhitespacePattern();
+
+ public final boolean matches(char c) {
+ return Character.isWhitespace(c);
+ }
+ }
+
+ private static final class OneCharacterPattern implements CharacterPattern {
+ private char expectedCharacter;
+
+ OneCharacterPattern(final char c) {
+ this.expectedCharacter = c;
+ }
+
+ public final boolean matches(char c) {
+ return this.expectedCharacter == c;
+ }
+ }
+
+ private static final class PunctPattern implements CharacterPattern {
+ static final GroupHead.PunctPattern INSTANCE = new PunctPattern();
+
+ private static String punctCharacters = "-!\"#$%&'()*+,./:;<=>?@[\\]_`{|}~";
+
+ public boolean matches(char c) {
+ return punctCharacters.indexOf(c) != -1;
+ }
+ }
+
+}
diff --git a/org.spearce.jgit/src/org/spearce/jgit/fnmatch/Head.java b/org.spearce.jgit/src/org/spearce/jgit/fnmatch/Head.java
new file mode 100644
index 0000000..498f96c
--- /dev/null
+++ b/org.spearce.jgit/src/org/spearce/jgit/fnmatch/Head.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2008, Florian Köberle <florianskarten@web.de>
+ *
+ * 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 Git Development Community 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.spearce.jgit.fnmatch;
+
+import java.util.List;
+
+interface Head {
+ /**
+ *
+ * @param c
+ * the character which decides which heads are returned.
+ * @return a list of heads based on the input.
+ */
+ public abstract List<Head> getNextHeads(char c);
+}
diff --git a/org.spearce.jgit/src/org/spearce/jgit/fnmatch/LastHead.java b/org.spearce.jgit/src/org/spearce/jgit/fnmatch/LastHead.java
new file mode 100644
index 0000000..d3c9813
--- /dev/null
+++ b/org.spearce.jgit/src/org/spearce/jgit/fnmatch/LastHead.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2008, Florian Köberle <florianskarten@web.de>
+ *
+ * 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 Git Development Community 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.spearce.jgit.fnmatch;
+
+import java.util.List;
+
+final class LastHead implements Head {
+ static final Head INSTANCE = new LastHead();
+
+ /**
+ * Don't call this constructor, use {@link #INSTANCE}
+ */
+ private LastHead() {
+ // defined because of javadoc and visibility modifier.
+ }
+
+ public List<Head> getNextHeads(char c) {
+ return FileNameMatcher.EMPTY_HEAD_LIST;
+ }
+
+}
diff --git a/org.spearce.jgit/src/org/spearce/jgit/fnmatch/RestrictedWildCardHead.java b/org.spearce.jgit/src/org/spearce/jgit/fnmatch/RestrictedWildCardHead.java
new file mode 100644
index 0000000..9d8d277
--- /dev/null
+++ b/org.spearce.jgit/src/org/spearce/jgit/fnmatch/RestrictedWildCardHead.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2008, Florian Köberle <florianskarten@web.de>
+ *
+ * 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 Git Development Community 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.spearce.jgit.fnmatch;
+
+final class RestrictedWildCardHead extends AbstractHead {
+ private final char excludedCharacter;
+
+ RestrictedWildCardHead(final char excludedCharacter, final boolean star) {
+ super(star);
+ this.excludedCharacter = excludedCharacter;
+ }
+
+ @Override
+ protected final boolean matches(final char c) {
+ return c != excludedCharacter;
+ }
+}
diff --git a/org.spearce.jgit/src/org/spearce/jgit/fnmatch/WildCardHead.java b/org.spearce.jgit/src/org/spearce/jgit/fnmatch/WildCardHead.java
new file mode 100644
index 0000000..570e374
--- /dev/null
+++ b/org.spearce.jgit/src/org/spearce/jgit/fnmatch/WildCardHead.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2008, Florian Köberle <florianskarten@web.de>
+ *
+ * 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 Git Development Community 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.spearce.jgit.fnmatch;
+
+final class WildCardHead extends AbstractHead {
+ WildCardHead(boolean star) {
+ super(star);
+ }
+
+ @Override
+ protected final boolean matches(final char c) {
+ return true;
+ }
+}
--
1.5.4.3
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [JGIT PATCH v4 02/24] Added the interface FilePattern.
2008-06-13 18:30 [JGIT PATCH v4] Implementation of fnmatch and the ignore rules Florian Köberle
2008-06-13 18:34 ` [JGIT PATCH v4 01/24] Added the package fnmatch and two exceptions Florian Koeberle
@ 2008-06-13 18:34 ` Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 03/24] Added the class Rule Florian Koeberle
` (22 subsequent siblings)
24 siblings, 0 replies; 26+ messages in thread
From: Florian Koeberle @ 2008-06-13 18:34 UTC (permalink / raw)
To: git; +Cc: Florian Koeberle
Signed-off-by: Florian Koeberle <florianskarten@web.de>
---
.../spearce/jgit/treewalk/rules/FilePattern.java | 128 ++++++++++++++++++++
1 files changed, 128 insertions(+), 0 deletions(-)
create mode 100644 org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/FilePattern.java
diff --git a/org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/FilePattern.java b/org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/FilePattern.java
new file mode 100644
index 0000000..86cba6b
--- /dev/null
+++ b/org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/FilePattern.java
@@ -0,0 +1,128 @@
+/*
+ * Copyright (C) 2008, Florian Köberle <florianskarten@web.de>
+ *
+ * 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 Git Development Community 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.spearce.jgit.treewalk.rules;
+
+/**
+ * A {@link FilePattern} can be used to check if files in a directory matches a
+ * pattern. It provides with the {@link #getPatternForSubDirectory(String)}
+ * method {@link FilePattern}s for sub directories.
+ *
+ * Implementations of this interface should be immutable.
+ *
+ */
+interface FilePattern {
+ /**
+ * Contains the only instance of {@link FilePatternMatchAlways}.
+ */
+ public static final FilePattern MATCH_ALWAYS = new FilePatternMatchAlways();
+
+ /**
+ * Contains the only instance of {@link FilePatternMatchNever}.
+ */
+ public static final FilePattern MATCH_NEVER = new FilePatternMatchNever();
+
+ /**
+ * @param fileName
+ * the name of the file or directory
+ * @param fileIsDirectory
+ * determines if the file is a directory.
+ * @return true if the pattern matches.
+ */
+ boolean match(String fileName, boolean fileIsDirectory);
+
+ /**
+ *
+ * @param directoryName
+ * the name of a subdirectory.
+ * @return a pattern which can be used to match files in sub directories. A
+ * user may check if the returned value is {@link #MATCH_NEVER} in
+ * order to do some performance optimizations.
+ *
+ */
+ FilePattern getPatternForSubDirectory(String directoryName);
+
+ /**
+ * @return true if {@link #getPatternForSubDirectory(String)} returns true
+ * for every value.
+ */
+ boolean isSameForSubDirectories();
+
+ /**
+ * This implementation does always match.
+ */
+ public static final class FilePatternMatchAlways implements FilePattern {
+
+ private FilePatternMatchAlways() {
+ // declared to make the constructor private
+ }
+
+ public FilePattern getPatternForSubDirectory(String directoryName) {
+ return MATCH_ALWAYS;
+ }
+
+ public boolean match(String fileName, boolean fileIsDirectory) {
+ return true;
+ }
+
+ public boolean isSameForSubDirectories() {
+ return true;
+ }
+ }
+
+ /**
+ * This implementation does never match.
+ */
+ public static final class FilePatternMatchNever implements FilePattern {
+ private FilePatternMatchNever() {
+ // declared to make the constructor private
+ }
+
+ public FilePattern getPatternForSubDirectory(String directoryName) {
+ return MATCH_NEVER;
+ }
+
+ public boolean match(String fileName, boolean fileIsDirectory) {
+ return false;
+ }
+
+ public boolean isSameForSubDirectories() {
+ return true;
+ }
+ }
+
+}
--
1.5.4.3
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [JGIT PATCH v4 03/24] Added the class Rule.
2008-06-13 18:30 [JGIT PATCH v4] Implementation of fnmatch and the ignore rules Florian Köberle
2008-06-13 18:34 ` [JGIT PATCH v4 01/24] Added the package fnmatch and two exceptions Florian Koeberle
2008-06-13 18:34 ` [JGIT PATCH v4 02/24] Added the interface FilePattern Florian Koeberle
@ 2008-06-13 18:35 ` Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 04/24] Added the iterface Rules Florian Koeberle
` (21 subsequent siblings)
24 siblings, 0 replies; 26+ messages in thread
From: Florian Koeberle @ 2008-06-13 18:35 UTC (permalink / raw)
To: git; +Cc: Florian Koeberle
Signed-off-by: Florian Koeberle <florianskarten@web.de>
---
.../src/org/spearce/jgit/treewalk/rules/Rule.java | 82 ++++++++++++++++++++
1 files changed, 82 insertions(+), 0 deletions(-)
create mode 100644 org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/Rule.java
diff --git a/org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/Rule.java b/org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/Rule.java
new file mode 100644
index 0000000..8e5b2f4
--- /dev/null
+++ b/org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/Rule.java
@@ -0,0 +1,82 @@
+/*
+ * Copyright (C) 2008, Florian Köberle <florianskarten@web.de>
+ *
+ * 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 Git Development Community 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.spearce.jgit.treewalk.rules;
+
+/**
+ * A Rule defines what to do with a files which match a specified
+ * {@link FilePattern}.
+ */
+public class Rule {
+ private boolean ignoreAtMatch;
+
+ private FilePattern pattern;
+
+ /**
+ *
+ * @param ignoreAtMatch
+ * defines if the rules ignores or accepts at a match.
+ * @param pattern
+ * the pattern used to test if a file matches.
+ */
+ Rule(boolean ignoreAtMatch, FilePattern pattern) {
+ this.ignoreAtMatch = ignoreAtMatch;
+ this.pattern = pattern;
+ }
+
+ FilePattern getPattern() {
+ return pattern;
+ }
+
+ boolean isIgnoreAtMatch() {
+ return ignoreAtMatch;
+ }
+
+ Rule getRuleForSubDirectory(String directoryName) {
+ final FilePattern subPattern = pattern
+ .getPatternForSubDirectory(directoryName);
+ if (subPattern == pattern) {
+ return this;
+ }
+ return new Rule(ignoreAtMatch, subPattern);
+ }
+
+ boolean isSameForSubDirectories() {
+ return pattern.isSameForSubDirectories();
+ }
+
+}
--
1.5.4.3
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [JGIT PATCH v4 04/24] Added the iterface Rules.
2008-06-13 18:30 [JGIT PATCH v4] Implementation of fnmatch and the ignore rules Florian Köberle
` (2 preceding siblings ...)
2008-06-13 18:35 ` [JGIT PATCH v4 03/24] Added the class Rule Florian Koeberle
@ 2008-06-13 18:35 ` Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 05/24] Added the class FileNamePattern Florian Koeberle
` (20 subsequent siblings)
24 siblings, 0 replies; 26+ messages in thread
From: Florian Koeberle @ 2008-06-13 18:35 UTC (permalink / raw)
To: git; +Cc: Florian Koeberle
Signed-off-by: Florian Koeberle <florianskarten@web.de>
---
.../src/org/spearce/jgit/treewalk/rules/Rules.java | 121 ++++++++++++++++++++
1 files changed, 121 insertions(+), 0 deletions(-)
create mode 100644 org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/Rules.java
diff --git a/org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/Rules.java b/org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/Rules.java
new file mode 100644
index 0000000..0a71150
--- /dev/null
+++ b/org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/Rules.java
@@ -0,0 +1,121 @@
+/*
+ * Copyright (C) 2008, Florian Köberle <florianskarten@web.de>
+ *
+ * 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 Git Development Community 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.spearce.jgit.treewalk.rules;
+
+/**
+ * A {@link Rules} instances defines ignore or do not ignore rules for files in
+ * a directory. It can't directly be used to match files in sub directories, but
+ * provides a method {@link #getRulesForSubDirectory}.
+ *
+ */
+public interface Rules {
+
+ /**
+ * Provides the instance of {@link IgnoreAllRules}.
+ */
+ public static final Rules IGNORE_ALL = new IgnoreAllRules();
+
+ /**
+ * Provides the instance of {@link IgnoreNothingRules}.
+ */
+ public static final Rules IGNORE_NOTHING = new IgnoreNothingRules();
+
+ /**
+ * @param fileName
+ * the name of the file or directory.
+ * @param fileIsDirectory
+ * should be true if the file is a directory.
+ * @return true if the file or directory should be ignored.
+ */
+ public abstract boolean shouldIgnore(String fileName,
+ boolean fileIsDirectory);
+
+ /**
+ * @param directoryName
+ * the sub directory for which you want an {@link Rules}
+ * instance.
+ * @return an {@link Rules} instance, which can be used to check files in
+ * the specified sub directory.
+ */
+ public abstract Rules getRulesForSubDirectory(String directoryName);
+
+ /**
+ * This implementation ignores everything.
+ */
+ public static final class IgnoreAllRules implements Rules {
+ private IgnoreAllRules() {
+ // declared to make the constructor private
+ }
+
+ public Rules getRulesForSubDirectory(String directoryName) {
+ return this;
+ }
+
+ public boolean shouldIgnore(String fileName, boolean fileIsDirectory) {
+ return true;
+ }
+
+ @Override
+ public String toString() {
+ return "Rules[IGNORE_ALL]";
+ }
+ }
+
+ /**
+ * This implementation ignores nothing.
+ */
+ public static final class IgnoreNothingRules implements Rules {
+ private IgnoreNothingRules() {
+ // declared to make the constructor private
+ }
+
+ public Rules getRulesForSubDirectory(String directoryName) {
+ return this;
+ }
+
+ public boolean shouldIgnore(String fileName, boolean fileIsDirectory) {
+ return false;
+ }
+
+ @Override
+ public String toString() {
+ return "Rules[IGNORE_NOTHING]";
+ }
+ }
+
+}
--
1.5.4.3
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [JGIT PATCH v4 05/24] Added the class FileNamePattern.
2008-06-13 18:30 [JGIT PATCH v4] Implementation of fnmatch and the ignore rules Florian Köberle
` (3 preceding siblings ...)
2008-06-13 18:35 ` [JGIT PATCH v4 04/24] Added the iterface Rules Florian Koeberle
@ 2008-06-13 18:35 ` Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 06/24] Added the class FilePathPattern Florian Koeberle
` (19 subsequent siblings)
24 siblings, 0 replies; 26+ messages in thread
From: Florian Koeberle @ 2008-06-13 18:35 UTC (permalink / raw)
To: git; +Cc: Florian Koeberle
Signed-off-by: Florian Koeberle <florianskarten@web.de>
---
.../jgit/treewalk/rules/FileNamePattern.java | 79 ++++++++++++++++++++
1 files changed, 79 insertions(+), 0 deletions(-)
create mode 100644 org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/FileNamePattern.java
diff --git a/org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/FileNamePattern.java b/org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/FileNamePattern.java
new file mode 100644
index 0000000..3760308
--- /dev/null
+++ b/org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/FileNamePattern.java
@@ -0,0 +1,79 @@
+/*
+ * Copyright (C) 2008, Florian Köberle <florianskarten@web.de>
+ *
+ * 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 Git Development Community 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.spearce.jgit.treewalk.rules;
+
+import org.spearce.jgit.errors.InvalidPatternException;
+import org.spearce.jgit.fnmatch.FileNameMatcher;
+
+/**
+ * A {@link FileNamePattern} defines a pattern for all files in a tree. A
+ * typical example would be "*.txt", which would match at any file or subtree
+ * which ends with ".txt" like "a/b/c.txt" and "a.txt/a.c".
+ *
+ */
+class FileNamePattern implements FilePattern {
+ private final FileNameMatcher fileNameMatcher;
+
+ private final boolean matchDirectoriesOnly;
+
+ FileNamePattern(String fnMatchPatternString, boolean matchDirectoriesOnly)
+ throws InvalidPatternException {
+ this.fileNameMatcher = new FileNameMatcher(fnMatchPatternString, null);
+ this.matchDirectoriesOnly = matchDirectoriesOnly;
+ }
+
+ public FilePattern getPatternForSubDirectory(String directoryName) {
+ if (match(directoryName, true)) {
+ return MATCH_ALWAYS;
+ }
+ return this;
+ }
+
+ public boolean match(String fileName, boolean fileIsDirectory) {
+ if (matchDirectoriesOnly && !fileIsDirectory)
+ return false;
+
+ fileNameMatcher.reset();
+ fileNameMatcher.append(fileName);
+ return fileNameMatcher.isMatch();
+ }
+
+ public boolean isSameForSubDirectories() {
+ return false;
+ }
+}
--
1.5.4.3
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [JGIT PATCH v4 06/24] Added the class FilePathPattern.
2008-06-13 18:30 [JGIT PATCH v4] Implementation of fnmatch and the ignore rules Florian Köberle
` (4 preceding siblings ...)
2008-06-13 18:35 ` [JGIT PATCH v4 05/24] Added the class FileNamePattern Florian Koeberle
@ 2008-06-13 18:35 ` Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 07/24] Added the class IgnoreRuleListFactory Florian Koeberle
` (18 subsequent siblings)
24 siblings, 0 replies; 26+ messages in thread
From: Florian Koeberle @ 2008-06-13 18:35 UTC (permalink / raw)
To: git; +Cc: Florian Koeberle
Signed-off-by: Florian Koeberle <florianskarten@web.de>
---
.../jgit/treewalk/rules/FilePathPattern.java | 97 ++++++++++++++++++++
1 files changed, 97 insertions(+), 0 deletions(-)
create mode 100644 org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/FilePathPattern.java
diff --git a/org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/FilePathPattern.java b/org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/FilePathPattern.java
new file mode 100644
index 0000000..cd7c9aa
--- /dev/null
+++ b/org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/FilePathPattern.java
@@ -0,0 +1,97 @@
+/*
+ * Copyright (C) 2008, Florian Köberle <florianskarten@web.de>
+ *
+ * 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 Git Development Community 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.spearce.jgit.treewalk.rules;
+
+import org.spearce.jgit.errors.InvalidPatternException;
+import org.spearce.jgit.fnmatch.FileNameMatcher;
+
+/**
+ * A {@link FilePathPattern} represents a pattern for a file path.
+ *
+ */
+class FilePathPattern implements FilePattern {
+ private final FileNameMatcher matcher;
+
+ private final boolean matchDirectoriesOnly;
+
+ private FilePathPattern(final FileNameMatcher matcher,
+ final boolean matchDirectoriesOnly) {
+ this.matcher = matcher;
+ this.matchDirectoriesOnly = matchDirectoriesOnly;
+ }
+
+ FilePathPattern(final String pattern, final boolean allowSlashInWildCard,
+ final boolean matchDirectoriesOnly) throws InvalidPatternException {
+
+ if (allowSlashInWildCard)
+ this.matcher = new FileNameMatcher(pattern, null);
+ else
+ this.matcher = new FileNameMatcher(pattern, new Character('/'));
+ this.matchDirectoriesOnly = matchDirectoriesOnly;
+ }
+
+ public FilePattern getPatternForSubDirectory(String directoryName) {
+ matcher.reset();
+ matcher.append(directoryName);
+
+ if (matcher.isMatch())
+ return FilePattern.MATCH_ALWAYS;
+
+ matcher.append("/");
+
+ if (!matcher.canAppendMatch())
+ return FilePattern.MATCH_NEVER;
+
+ final FileNameMatcher childMatcher = matcher.createMatcherForSuffix();
+ return new FilePathPattern(childMatcher, matchDirectoriesOnly);
+ }
+
+ public boolean match(String fileName, boolean fileIsDirectory) {
+ if (!fileIsDirectory && matchDirectoriesOnly) {
+ return false;
+ }
+ matcher.reset();
+ matcher.append(fileName);
+ return matcher.isMatch();
+ }
+
+ public boolean isSameForSubDirectories() {
+ return false;
+ }
+
+}
--
1.5.4.3
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [JGIT PATCH v4 07/24] Added the class IgnoreRuleListFactory.
2008-06-13 18:30 [JGIT PATCH v4] Implementation of fnmatch and the ignore rules Florian Köberle
` (5 preceding siblings ...)
2008-06-13 18:35 ` [JGIT PATCH v4 06/24] Added the class FilePathPattern Florian Koeberle
@ 2008-06-13 18:35 ` Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 08/24] Added a Rules interface implementation and a factory for it Florian Koeberle
` (17 subsequent siblings)
24 siblings, 0 replies; 26+ messages in thread
From: Florian Koeberle @ 2008-06-13 18:35 UTC (permalink / raw)
To: git; +Cc: Florian Koeberle
Signed-off-by: Florian Koeberle <florianskarten@web.de>
---
.../jgit/treewalk/rules/IgnoreRuleListFactory.java | 115 ++++++++++++++++++++
1 files changed, 115 insertions(+), 0 deletions(-)
create mode 100644 org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/IgnoreRuleListFactory.java
diff --git a/org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/IgnoreRuleListFactory.java b/org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/IgnoreRuleListFactory.java
new file mode 100644
index 0000000..aa4a384
--- /dev/null
+++ b/org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/IgnoreRuleListFactory.java
@@ -0,0 +1,115 @@
+/*
+ * Copyright (C) 2008, Florian Köberle <florianskarten@web.de>
+ *
+ * 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 Git Development Community 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.spearce.jgit.treewalk.rules;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Scanner;
+
+import org.spearce.jgit.errors.InvalidPatternException;
+
+/**
+ * This class can be used to create lists of {@link Rule} objects from lines of
+ * .gitignore like files.
+ *
+ */
+class IgnoreRuleListFactory {
+
+ List<Rule> createIgnoreRuleList(final Iterable<String> lineIterable)
+ throws InvalidPatternException {
+ LinkedList<Rule> rules = new LinkedList<Rule>();
+ for (String line : lineIterable) {
+ final String trimmedLine = line.trim();
+ if (trimmedLine.startsWith("#")) {
+ continue;
+ }
+ if (trimmedLine.length() == 0) {
+ continue;
+ }
+ rules.add(0, createRule(trimmedLine));
+ }
+ return rules;
+ }
+
+ List<Rule> createIgnoreRuleList(final List<File> files)
+ throws FileNotFoundException, InvalidPatternException {
+ final List<String> lines = new ArrayList<String>();
+ for (File file : files) {
+ Scanner scanner = new Scanner(file);
+ try {
+ while (scanner.hasNextLine()) {
+ lines.add(scanner.nextLine());
+ }
+ } finally {
+ scanner.close();
+ }
+ }
+ return createIgnoreRuleList(lines);
+ }
+
+ private Rule createRule(String patternString)
+ throws InvalidPatternException {
+ final boolean inverse = patternString.startsWith("!");
+ if (inverse)
+ patternString = patternString.substring(1);
+
+ final FilePattern pattern = createFilePattern(patternString);
+ return new Rule(!inverse, pattern);
+ }
+
+ private FilePattern createFilePattern(String patternString)
+ throws InvalidPatternException {
+ final boolean matchDirectoriesOnly = patternString.endsWith("/");
+ if (matchDirectoriesOnly)
+ patternString = patternString.substring(0,
+ patternString.length() - 1);
+ if (patternString.contains("/")) {
+ if (patternString.startsWith("/"))
+ patternString = patternString.substring(1);
+
+ return new FilePathPattern(patternString, false,
+ matchDirectoriesOnly);
+ } else {
+ return new FileNamePattern(patternString, matchDirectoriesOnly);
+ }
+ }
+
+}
--
1.5.4.3
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [JGIT PATCH v4 08/24] Added a Rules interface implementation and a factory for it.
2008-06-13 18:30 [JGIT PATCH v4] Implementation of fnmatch and the ignore rules Florian Köberle
` (6 preceding siblings ...)
2008-06-13 18:35 ` [JGIT PATCH v4 07/24] Added the class IgnoreRuleListFactory Florian Koeberle
@ 2008-06-13 18:35 ` Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 09/24] Added test class OverallIgnoreRulestest Florian Koeberle
` (16 subsequent siblings)
24 siblings, 0 replies; 26+ messages in thread
From: Florian Koeberle @ 2008-06-13 18:35 UTC (permalink / raw)
To: git; +Cc: Florian Koeberle
Signed-off-by: Florian Koeberle <florianskarten@web.de>
---
.../treewalk/rules/RuleListToObjectConverter.java | 151 ++++++++++++++++++++
.../jgit/treewalk/rules/RulesImplementation.java | 88 ++++++++++++
2 files changed, 239 insertions(+), 0 deletions(-)
create mode 100644 org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/RuleListToObjectConverter.java
create mode 100644 org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/RulesImplementation.java
diff --git a/org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/RuleListToObjectConverter.java b/org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/RuleListToObjectConverter.java
new file mode 100644
index 0000000..96f27bb
--- /dev/null
+++ b/org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/RuleListToObjectConverter.java
@@ -0,0 +1,151 @@
+/*
+ * Copyright (C) 2008, Florian Köberle <florianskarten@web.de>
+ *
+ * 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 Git Development Community 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.spearce.jgit.treewalk.rules;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * This class converts a list of {@link Rule} objects into a {@Rules} object.
+ * During the conversion some optimizations are done:
+ * <ul>
+ * <li>Rules which can never match are removed. e.g. There is no need to check
+ * the rule "/a" in the directory b.</li>
+ * <li>The list is cut at the first rule which match always. *.txt\n/a for
+ * example would result in an internal list "ignore all, ignore *.txt" which is
+ * then reduced to "ignore all".</li>
+ * <li>Ignore rules which are direcly before an "ignore all" all rule are
+ * removed. /a\n*.txt for example would result in an intern "ignore *.txt,
+ * ignore all" list which is then reduced to "ignore all",</li>
+ * <li>"do not ignore" rules at the bottom of the intern list are removed. This
+ * optimization would remove !a from "!a\n/b" as it is in the inversed list at
+ * the bottom.</li>
+ * </ul>
+ */
+class RuleListToObjectConverter {
+ protected Rules createIgnoreRules(Iterator<Rule> ruleIterator) {
+ final List<Rule> rules = getNessesaryRulesFromIterator(ruleIterator);
+ removeUnnecessaryDoNotIgnoreRulesAtTheEndOfTheList(rules);
+ removeUnnecessaryIgnoreRulesNearTheEndOfTheList(rules);
+
+ if (rules.size() == 1) {
+ final Rule rule = rules.get(0);
+ if (rule.getPattern() == FilePattern.MATCH_ALWAYS) {
+ if (rule.isIgnoreAtMatch()) {
+ return Rules.IGNORE_ALL;
+ } else {
+ return Rules.IGNORE_NOTHING;
+ }
+ }
+ } else if (rules.isEmpty()) {
+ return Rules.IGNORE_NOTHING;
+ }
+ return new RulesImplementation(rules, this);
+ }
+
+ private List<Rule> getNessesaryRulesFromIterator(Iterator<Rule> ruleIterator) {
+ final List<Rule> rules = new ArrayList<Rule>();
+ while (ruleIterator.hasNext()) {
+ final Rule subRule = ruleIterator.next();
+ if (subRule.getPattern() == FilePattern.MATCH_NEVER) {
+ continue;
+ }
+ rules.add(subRule);
+ // There is no need for rules after a rule witch match always,
+ // as such a rule would never be the first rule which matches.
+ if (subRule.getPattern() == FilePattern.MATCH_ALWAYS) {
+ break;
+ }
+ }
+ return rules;
+ }
+
+ /**
+ * Expects that
+ * {@link #removeUnnecessaryDoNotIgnoreRulesAtTheEndOfTheList(List)} has
+ * been executed first.
+ *
+ * @param rules
+ * rule list to reduce.
+ */
+ private void removeUnnecessaryIgnoreRulesNearTheEndOfTheList(
+ final List<Rule> rules) {
+ // Why the following optimization makes only sense for the end of the
+ // list:
+ // If there is a "ignore all"- rule,
+ // then it is located at the end of the list
+ // See how the list is filled to prove this statement.
+ if (rules.size() >= 2) {
+ final Rule lastRule = rules.get(rules.size() - 1);
+ assert lastRule.isIgnoreAtMatch() : "Expected that no 'not ignore'-rule is at the end of the list any more";
+ final boolean ignoreAllAtEnd = lastRule.getPattern().equals(
+ FilePattern.MATCH_ALWAYS);
+ if (ignoreAllAtEnd) {
+ while (rules.size() >= 2) {
+ final int ruleBeforeLastIndex = rules.size() - 2;
+ final Rule ruleBeforeLast = rules.get(ruleBeforeLastIndex);
+ if (ruleBeforeLast.isIgnoreAtMatch()) {
+ rules.remove(ruleBeforeLastIndex);
+ } else {
+ break;
+ }
+ }
+ }
+ }
+ }
+
+ private void removeUnnecessaryDoNotIgnoreRulesAtTheEndOfTheList(
+ final List<Rule> rules) {
+ // Why it is save to remove "don't ignore rules" at the end of the list
+ // if there is no "ignore rule" below a "don't ignore rule" then
+ // the path which haven't match jet will never be ignored:
+ // -> if another "don't ignore rule" match then the patch will not be
+ // ignored
+ // -> if no "don't ignore rule" match then the path will not be ignored.
+ while (!rules.isEmpty()) {
+ final int indexOfLastRule = rules.size() - 1;
+ final Rule lastRule = rules.get(indexOfLastRule);
+ if (lastRule.isIgnoreAtMatch()) {
+ break;
+ } else {
+ rules.remove(indexOfLastRule);
+ }
+ }
+ }
+}
diff --git a/org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/RulesImplementation.java b/org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/RulesImplementation.java
new file mode 100644
index 0000000..cd12701
--- /dev/null
+++ b/org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/RulesImplementation.java
@@ -0,0 +1,88 @@
+/*
+ * Copyright (C) 2008, Florian Köberle <florianskarten@web.de>
+ *
+ * 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 Git Development Community 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.spearce.jgit.treewalk.rules;
+
+import java.util.Iterator;
+import java.util.List;
+
+class RulesImplementation implements Rules {
+ /**
+ * Complete list of rules. Note that order is: determining rule first.
+ */
+ private final List<Rule> rules;
+
+ /**
+ * Factory used to create {@link Rules} for sub directories.
+ */
+ private RuleListToObjectConverter factory;
+
+ RulesImplementation(List<Rule> rules, RuleListToObjectConverter factory) {
+ this.rules = rules;
+ this.factory = factory;
+ }
+
+ public boolean shouldIgnore(String fileName, boolean fileIsDirectory) {
+ for (Rule rule : rules) {
+ if (rule.getPattern().match(fileName, fileIsDirectory)) {
+ return rule.isIgnoreAtMatch();
+ }
+ }
+ return false;
+ }
+
+ public Rules getRulesForSubDirectory(final String directoryName) {
+ final Iterator<Rule> subRuleIterator = new Iterator<Rule>() {
+ final Iterator<Rule> ruleIterator = rules.iterator();
+
+ public boolean hasNext() {
+ return ruleIterator.hasNext();
+ }
+
+ public Rule next() {
+ return ruleIterator.next()
+ .getRuleForSubDirectory(directoryName);
+ }
+
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+
+ };
+ return factory.createIgnoreRules(subRuleIterator);
+ }
+}
--
1.5.4.3
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [JGIT PATCH v4 09/24] Added test class OverallIgnoreRulestest.
2008-06-13 18:30 [JGIT PATCH v4] Implementation of fnmatch and the ignore rules Florian Köberle
` (7 preceding siblings ...)
2008-06-13 18:35 ` [JGIT PATCH v4 08/24] Added a Rules interface implementation and a factory for it Florian Koeberle
@ 2008-06-13 18:35 ` Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 10/24] Added the class PathNotInProjectDirectoryException Florian Koeberle
` (15 subsequent siblings)
24 siblings, 0 replies; 26+ messages in thread
From: Florian Koeberle @ 2008-06-13 18:35 UTC (permalink / raw)
To: git; +Cc: Florian Koeberle
Signed-off-by: Florian Koeberle <florianskarten@web.de>
---
.../treewalk/rules/OverallIgnoreRulesTest.java | 396 ++++++++++++++++++++
1 files changed, 396 insertions(+), 0 deletions(-)
create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/treewalk/rules/OverallIgnoreRulesTest.java
diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/treewalk/rules/OverallIgnoreRulesTest.java b/org.spearce.jgit.test/tst/org/spearce/jgit/treewalk/rules/OverallIgnoreRulesTest.java
new file mode 100644
index 0000000..55e9de5
--- /dev/null
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/treewalk/rules/OverallIgnoreRulesTest.java
@@ -0,0 +1,396 @@
+/*
+ * Copyright (C) 2008, Florian Köberle <florianskarten@web.de>
+ *
+ * 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 Git Development Community 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.spearce.jgit.treewalk.rules;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.spearce.jgit.errors.InvalidPatternException;
+
+import junit.framework.TestCase;
+
+public class OverallIgnoreRulesTest extends TestCase {
+ private IgnoreRulesFactory factory;
+
+ @Override
+ protected void setUp() throws Exception {
+ factory = new IgnoreRulesFactory();
+ }
+
+ public void testSimpleGlobalPattern() throws Exception {
+ List<String> lines = new ArrayList<String>();
+ lines.add("ab");
+ final Rules ignoreRules = factory.createIgnoreRulesFromLines(lines);
+ assertTrue(ignoreRules.shouldIgnore("ab", false));
+ assertTrue(ignoreRules.shouldIgnore("ab", true));
+ assertFalse(ignoreRules.shouldIgnore("abc", false));
+ assertFalse(ignoreRules.shouldIgnore("abc", true));
+ }
+
+ public void testGlobalPatternWithOneStar() throws Exception {
+ List<String> lines = new ArrayList<String>();
+ lines.add("a*c");
+ final Rules ignoreRules = factory.createIgnoreRulesFromLines(lines);
+
+ assertTrue(ignoreRules.shouldIgnore("ac", false));
+ assertTrue(ignoreRules.shouldIgnore("ac", true));
+
+ assertTrue(ignoreRules.shouldIgnore("abc", false));
+ assertTrue(ignoreRules.shouldIgnore("abc", true));
+
+ assertTrue(ignoreRules.shouldIgnore("abbc", false));
+ assertTrue(ignoreRules.shouldIgnore("abbc", true));
+
+ assertTrue(ignoreRules.shouldIgnore("aabc", false));
+ assertTrue(ignoreRules.shouldIgnore("aabc", true));
+
+ assertFalse(ignoreRules.shouldIgnore("cab", false));
+ assertFalse(ignoreRules.shouldIgnore("cab", true));
+ }
+
+ public void testGlobalPatternWithTwoStars() throws Exception {
+ List<String> lines = new ArrayList<String>();
+ lines.add("a*c*e");
+ final Rules ignoreRules = factory.createIgnoreRulesFromLines(lines);
+
+ assertTrue(ignoreRules.shouldIgnore("ace", false));
+ assertTrue(ignoreRules.shouldIgnore("ace", true));
+
+ assertTrue(ignoreRules.shouldIgnore("abcde", false));
+ assertTrue(ignoreRules.shouldIgnore("abcde", true));
+
+ assertTrue(ignoreRules.shouldIgnore("aHellocWorlde", false));
+ assertTrue(ignoreRules.shouldIgnore("aHellocWorlde", true));
+
+ assertFalse(ignoreRules.shouldIgnore("ae", false));
+ assertFalse(ignoreRules.shouldIgnore("ae", true));
+ }
+
+ public void testGlobalPatternWithDots() throws Exception {
+ List<String> lines = new ArrayList<String>();
+ lines.add("*.tar.gz");
+ final Rules ignoreRules = factory.createIgnoreRulesFromLines(lines);
+
+ assertTrue(ignoreRules.shouldIgnore("test.tar.gz", false));
+ assertTrue(ignoreRules.shouldIgnore("test.tar.gz", true));
+
+ assertTrue(ignoreRules.shouldIgnore(".tar.gz", false));
+ assertTrue(ignoreRules.shouldIgnore(".tar.gz", true));
+
+ assertFalse(ignoreRules.shouldIgnore("test", false));
+ assertFalse(ignoreRules.shouldIgnore("test", true));
+
+ // test that "." isn't handled as "any character"
+ assertFalse(ignoreRules.shouldIgnore(".tarogz", false));
+ assertFalse(ignoreRules.shouldIgnore(".tarogz", true));
+ }
+
+ public void testGlobalPatternDirectoryOnlyRule() throws Exception {
+ List<String> lines = new ArrayList<String>();
+ lines.add("a/");
+ final Rules ignoreRules = factory.createIgnoreRulesFromLines(lines);
+
+ assertTrue(ignoreRules.shouldIgnore("a", true));
+ assertFalse(ignoreRules.shouldIgnore("a", false));
+
+ final Rules ignoreRulesA = ignoreRules.getRulesForSubDirectory("a");
+ assertSame(Rules.IGNORE_ALL, ignoreRulesA);
+
+ final Rules ignoreRulesB = ignoreRules.getRulesForSubDirectory("b");
+ assertTrue(ignoreRulesB.shouldIgnore("a", true));
+ assertFalse(ignoreRulesB.shouldIgnore("a", false));
+
+ final Rules ignoreRulesBA = ignoreRulesB.getRulesForSubDirectory("a");
+ assertSame(Rules.IGNORE_ALL, ignoreRulesBA);
+
+ }
+
+ public void testSimpleFilePathPattern() throws Exception {
+ List<String> lines = new ArrayList<String>();
+ lines.add("a/b/c");
+ final Rules ignoreRules = factory.createIgnoreRulesFromLines(lines);
+ assertFalse(ignoreRules.shouldIgnore("a", true));
+ assertFalse(ignoreRules.shouldIgnore("a", false));
+ assertFalse(ignoreRules.shouldIgnore("b", true));
+ assertFalse(ignoreRules.shouldIgnore("b", false));
+ assertFalse(ignoreRules.shouldIgnore("c", true));
+ assertFalse(ignoreRules.shouldIgnore("c", false));
+
+ final Rules ignoreRulesA = ignoreRules.getRulesForSubDirectory("a");
+ assertFalse(ignoreRulesA.shouldIgnore("a", true));
+ assertFalse(ignoreRulesA.shouldIgnore("a", false));
+ assertFalse(ignoreRulesA.shouldIgnore("b", true));
+ assertFalse(ignoreRulesA.shouldIgnore("b", false));
+ assertFalse(ignoreRulesA.shouldIgnore("c", true));
+ assertFalse(ignoreRulesA.shouldIgnore("c", false));
+
+ final Rules ignoreRulesAB = ignoreRulesA.getRulesForSubDirectory("b");
+ assertFalse(ignoreRulesAB.shouldIgnore("a", true));
+ assertFalse(ignoreRulesAB.shouldIgnore("a", false));
+ assertFalse(ignoreRulesAB.shouldIgnore("b", true));
+ assertFalse(ignoreRulesAB.shouldIgnore("b", false));
+ assertTrue(ignoreRulesAB.shouldIgnore("c", true));
+ assertTrue(ignoreRulesAB.shouldIgnore("c", false));
+
+ final Rules ignoreRulesABA = ignoreRulesAB.getRulesForSubDirectory("a");
+ assertSame(Rules.IGNORE_NOTHING, ignoreRulesABA);
+
+ final Rules ignoreRulesABB = ignoreRulesAB.getRulesForSubDirectory("b");
+ assertSame(Rules.IGNORE_NOTHING, ignoreRulesABB);
+
+ final Rules ignoreRulesABC = ignoreRulesAB.getRulesForSubDirectory("c");
+ assertSame(Rules.IGNORE_ALL, ignoreRulesABC);
+ }
+
+ public void testFilePathPatternDirectoryOnlyRule() throws Exception {
+ List<String> lines = new ArrayList<String>();
+ lines.add("a/b/c/");
+ final Rules ignoreRules = factory.createIgnoreRulesFromLines(lines);
+ assertFalse(ignoreRules.shouldIgnore("a", true));
+ assertFalse(ignoreRules.shouldIgnore("a", false));
+ assertFalse(ignoreRules.shouldIgnore("b", true));
+ assertFalse(ignoreRules.shouldIgnore("b", false));
+ assertFalse(ignoreRules.shouldIgnore("c", true));
+ assertFalse(ignoreRules.shouldIgnore("c", false));
+
+ final Rules ignoreRulesA = ignoreRules.getRulesForSubDirectory("a");
+ assertFalse(ignoreRulesA.shouldIgnore("a", true));
+ assertFalse(ignoreRulesA.shouldIgnore("a", false));
+ assertFalse(ignoreRulesA.shouldIgnore("b", true));
+ assertFalse(ignoreRulesA.shouldIgnore("b", false));
+ assertFalse(ignoreRulesA.shouldIgnore("c", true));
+ assertFalse(ignoreRulesA.shouldIgnore("c", false));
+
+ final Rules ignoreRulesAB = ignoreRulesA.getRulesForSubDirectory("b");
+ assertFalse(ignoreRulesAB.shouldIgnore("a", true));
+ assertFalse(ignoreRulesAB.shouldIgnore("a", false));
+ assertFalse(ignoreRulesAB.shouldIgnore("b", true));
+ assertFalse(ignoreRulesAB.shouldIgnore("b", false));
+ assertTrue(ignoreRulesAB.shouldIgnore("c", true));
+ assertFalse(ignoreRulesAB.shouldIgnore("c", false));
+
+ final Rules ignoreRulesABA = ignoreRulesAB.getRulesForSubDirectory("a");
+ assertSame(Rules.IGNORE_NOTHING, ignoreRulesABA);
+
+ final Rules ignoreRulesABB = ignoreRulesAB.getRulesForSubDirectory("b");
+ assertSame(Rules.IGNORE_NOTHING, ignoreRulesABB);
+
+ final Rules ignoreRulesABC = ignoreRulesAB.getRulesForSubDirectory("c");
+ assertSame(Rules.IGNORE_ALL, ignoreRulesABC);
+ }
+
+ public void testShortPathPattern() throws Exception {
+ List<String> lines = new ArrayList<String>();
+ lines.add("/alpha");
+ final Rules ignoreRules = factory.createIgnoreRulesFromLines(lines);
+ assertTrue(ignoreRules.shouldIgnore("alpha", true));
+ assertTrue(ignoreRules.shouldIgnore("alpha", false));
+
+ final Rules ignoreRulesAlpha = ignoreRules
+ .getRulesForSubDirectory("alpha");
+ assertSame(Rules.IGNORE_ALL, ignoreRulesAlpha);
+
+ final Rules ignoreRulesBeta = ignoreRules
+ .getRulesForSubDirectory("beta");
+ assertSame(Rules.IGNORE_NOTHING, ignoreRulesBeta);
+ }
+
+ public void testShortDirectoryPathPattern() throws Exception {
+ List<String> lines = new ArrayList<String>();
+ lines.add("/alpha/");
+ final Rules ignoreRules = factory.createIgnoreRulesFromLines(lines);
+ assertTrue(ignoreRules.shouldIgnore("alpha", true));
+ assertFalse(ignoreRules.shouldIgnore("alpha", false));
+
+ final Rules ignoreRulesAlpha = ignoreRules
+ .getRulesForSubDirectory("alpha");
+ assertSame(Rules.IGNORE_ALL, ignoreRulesAlpha);
+
+ final Rules ignoreRulesBeta = ignoreRules
+ .getRulesForSubDirectory("beta");
+ assertSame(Rules.IGNORE_NOTHING, ignoreRulesBeta);
+ }
+
+ public void testShortPathPatternWithStar() throws Exception {
+ List<String> lines = new ArrayList<String>();
+ lines.add("/.*");
+ final Rules ignoreRules = factory.createIgnoreRulesFromLines(lines);
+
+ assertTrue(ignoreRules.shouldIgnore(".test", true));
+ assertTrue(ignoreRules.shouldIgnore(".test", false));
+
+ assertFalse(ignoreRules.shouldIgnore("test", true));
+ assertFalse(ignoreRules.shouldIgnore("test", false));
+
+ final Rules ignoreRulesDotTest = ignoreRules
+ .getRulesForSubDirectory(".test");
+ assertSame(Rules.IGNORE_ALL, ignoreRulesDotTest);
+
+ final Rules ignoreRulesTest = ignoreRules
+ .getRulesForSubDirectory("test");
+ assertSame(Rules.IGNORE_NOTHING, ignoreRulesTest);
+ }
+
+ public void testPathPatternWith2Times2Stars() throws Exception {
+ final List<String> lines = new ArrayList<String>();
+ lines.add("he*wor*d/*.*");
+ final Rules ignoreRules = factory.createIgnoreRulesFromLines(lines);
+
+ assertFalse(ignoreRules.shouldIgnore("hello", true));
+ assertFalse(ignoreRules.shouldIgnore("hello", false));
+ final Rules ignoreRulesHello = ignoreRules
+ .getRulesForSubDirectory("hello");
+ assertSame(Rules.IGNORE_NOTHING, ignoreRulesHello);
+
+ assertFalse(ignoreRules.shouldIgnore("helloworld", true));
+ assertFalse(ignoreRules.shouldIgnore("helloworld", false));
+ final Rules ignoreRulesHelloWorld = ignoreRules
+ .getRulesForSubDirectory("helloworld");
+ assertNotSame(Rules.IGNORE_NOTHING, ignoreRulesHelloWorld);
+
+ assertTrue(ignoreRulesHelloWorld.shouldIgnore("test.txt", true));
+ assertTrue(ignoreRulesHelloWorld.shouldIgnore("test.txt", false));
+
+ assertFalse(ignoreRulesHelloWorld.shouldIgnore("test", true));
+ assertFalse(ignoreRulesHelloWorld.shouldIgnore("test", false));
+
+ final Rules ignoreRulesTestTxt = ignoreRulesHelloWorld
+ .getRulesForSubDirectory("test.txt");
+ assertSame(Rules.IGNORE_ALL, ignoreRulesTestTxt);
+
+ final Rules ignoreRulesTest = ignoreRulesHelloWorld
+ .getRulesForSubDirectory("test");
+ assertSame(Rules.IGNORE_NOTHING, ignoreRulesTest);
+ }
+
+ public void testEmptyIgnoreList() throws Exception {
+ final List<String> lines = Collections.emptyList();
+ final Rules ignoreRules = factory.createIgnoreRulesFromLines(lines);
+ assertSame(Rules.IGNORE_NOTHING, ignoreRules);
+ }
+
+ public void testOnlyOneNegatedIgnore() throws Exception {
+ final List<String> lines = new ArrayList<String>();
+ lines.add("!a");
+ final Rules ignoreRules = factory.createIgnoreRulesFromLines(lines);
+ assertSame(Rules.IGNORE_NOTHING, ignoreRules);
+ }
+
+ public void testOnlyThreeNegatedIgnores() throws Exception {
+ final List<String> lines = new ArrayList<String>();
+ lines.add("!a");
+ lines.add("!a/b/c");
+ lines.add("!b*");
+ final Rules ignoreRules = factory.createIgnoreRulesFromLines(lines);
+ assertSame(Rules.IGNORE_NOTHING, ignoreRules);
+ }
+
+ public void testNegatedIgnoreCase1() throws Exception {
+ final List<String> lines = new ArrayList<String>();
+ lines.add("/a");
+ lines.add("!b");
+ final Rules ignoreRules = factory.createIgnoreRulesFromLines(lines);
+ final Rules ignoreRulesA = ignoreRules.getRulesForSubDirectory("a");
+ final Rules ignoreRulesAB = ignoreRulesA.getRulesForSubDirectory("b");
+ final Rules ignoreRulesB = ignoreRules.getRulesForSubDirectory("b");
+ final Rules ignoreRulesC = ignoreRules.getRulesForSubDirectory("c");
+ assertSame(Rules.IGNORE_NOTHING, ignoreRulesB);
+ assertSame(Rules.IGNORE_NOTHING, ignoreRulesAB);
+ assertSame(Rules.IGNORE_NOTHING, ignoreRulesC);
+ assertTrue(ignoreRules.shouldIgnore("a", true));
+ assertTrue(ignoreRules.shouldIgnore("a", false));
+ assertTrue(ignoreRulesA.shouldIgnore("c", true));
+ assertTrue(ignoreRulesA.shouldIgnore("c", false));
+ }
+
+ public void testExceptionOfException() throws Exception {
+ final List<String> lines = new ArrayList<String>();
+ lines.add("*.*");
+ lines.add("!*.c");
+ lines.add("a.c");
+ final Rules ignoreRules = factory.createIgnoreRulesFromLines(lines);
+ assertTrue(ignoreRules.shouldIgnore("b.txt", false));
+ assertTrue(ignoreRules.shouldIgnore("b.txt", true));
+ assertTrue(ignoreRules.shouldIgnore("a.c", false));
+ assertTrue(ignoreRules.shouldIgnore("a.c", true));
+ assertFalse(ignoreRules.shouldIgnore("b.c", false));
+ assertFalse(ignoreRules.shouldIgnore("b.c", true));
+ }
+
+ public void testComplexCase() throws Exception {
+ final List<String> lines = new ArrayList<String>();
+ lines.add("*");
+ lines.add("!/alpha/src");
+ lines.add("*~");
+ final Rules ignoreRules = factory.createIgnoreRulesFromLines(lines);
+ assertTrue(ignoreRules.shouldIgnore("beta", true));
+ assertTrue(ignoreRules.shouldIgnore("alpha", true));
+ final Rules ignoreRulesAlpha = ignoreRules
+ .getRulesForSubDirectory("alpha");
+ final Rules ignoreRulesAlphaBin = ignoreRulesAlpha
+ .getRulesForSubDirectory("bin");
+ final Rules ignoreRulesAlphaSrc = ignoreRulesAlpha
+ .getRulesForSubDirectory("src");
+ assertSame(Rules.IGNORE_ALL, ignoreRulesAlphaBin);
+ assertFalse(ignoreRulesAlphaSrc.shouldIgnore("com", true));
+ assertFalse(ignoreRulesAlphaSrc.shouldIgnore("b.java", false));
+ assertTrue(ignoreRulesAlphaSrc.shouldIgnore("b.java~", true));
+ }
+
+ private class IgnoreRulesFactory {
+ private RuleListToObjectConverter converter = new RuleListToObjectConverter();
+
+ private IgnoreRuleListFactory listFactory = new IgnoreRuleListFactory();
+
+ /**
+ * @param ignoreFileLines
+ * the lines of a ignore file like .gitignore.
+ * @return a immutable IgnoreRules object.
+ * @throws InvalidPatternException
+ */
+ public Rules createIgnoreRulesFromLines(Iterable<String> ignoreFileLines)
+ throws InvalidPatternException {
+ final List<Rule> rules = listFactory
+ .createIgnoreRuleList(ignoreFileLines);
+ return converter.createIgnoreRules(rules.iterator());
+ }
+
+ }
+
+}
--
1.5.4.3
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [JGIT PATCH v4 10/24] Added the class PathNotInProjectDirectoryException.
2008-06-13 18:30 [JGIT PATCH v4] Implementation of fnmatch and the ignore rules Florian Köberle
` (8 preceding siblings ...)
2008-06-13 18:35 ` [JGIT PATCH v4 09/24] Added test class OverallIgnoreRulestest Florian Koeberle
@ 2008-06-13 18:35 ` Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 11/24] Added the class AddRuleListFactory Florian Koeberle
` (14 subsequent siblings)
24 siblings, 0 replies; 26+ messages in thread
From: Florian Koeberle @ 2008-06-13 18:35 UTC (permalink / raw)
To: git; +Cc: Florian Koeberle
Signed-off-by: Florian Koeberle <florianskarten@web.de>
---
.../errors/PathNotInProjectDirectoryException.java | 46 ++++++++++++++++++++
1 files changed, 46 insertions(+), 0 deletions(-)
create mode 100644 org.spearce.jgit/src/org/spearce/jgit/errors/PathNotInProjectDirectoryException.java
diff --git a/org.spearce.jgit/src/org/spearce/jgit/errors/PathNotInProjectDirectoryException.java b/org.spearce.jgit/src/org/spearce/jgit/errors/PathNotInProjectDirectoryException.java
new file mode 100644
index 0000000..5fce2b3
--- /dev/null
+++ b/org.spearce.jgit/src/org/spearce/jgit/errors/PathNotInProjectDirectoryException.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2008, Florian Köberle <florianskarten@web.de>
+ *
+ * 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 Git Development Community 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.spearce.jgit.errors;
+
+/**
+ * Thrown when a path wasn't in the project directory, but expected to be.
+ *
+ */
+public class PathNotInProjectDirectoryException extends
+ IllegalArgumentException {
+}
--
1.5.4.3
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [JGIT PATCH v4 11/24] Added the class AddRuleListFactory.
2008-06-13 18:30 [JGIT PATCH v4] Implementation of fnmatch and the ignore rules Florian Köberle
` (9 preceding siblings ...)
2008-06-13 18:35 ` [JGIT PATCH v4 10/24] Added the class PathNotInProjectDirectoryException Florian Koeberle
@ 2008-06-13 18:35 ` Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 12/24] Formatted Constants class Florian Koeberle
` (13 subsequent siblings)
24 siblings, 0 replies; 26+ messages in thread
From: Florian Koeberle @ 2008-06-13 18:35 UTC (permalink / raw)
To: git; +Cc: Florian Koeberle
Signed-off-by: Florian Koeberle <florianskarten@web.de>
---
.../jgit/treewalk/rules/AddRuleListFactory.java | 96 ++++++++++++++++++++
1 files changed, 96 insertions(+), 0 deletions(-)
create mode 100644 org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/AddRuleListFactory.java
diff --git a/org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/AddRuleListFactory.java b/org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/AddRuleListFactory.java
new file mode 100644
index 0000000..5304a38
--- /dev/null
+++ b/org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/AddRuleListFactory.java
@@ -0,0 +1,96 @@
+/*
+ * Copyright (C) 2008, Florian Köberle <florianskarten@web.de>
+ *
+ * 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 Git Development Community 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.spearce.jgit.treewalk.rules;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.spearce.jgit.errors.InvalidPatternException;
+import org.spearce.jgit.errors.PathNotInProjectDirectoryException;
+
+class AddRuleListFactory {
+
+ private static String getRelativeUnixPath(File projectDirectory, File file)
+ throws PathNotInProjectDirectoryException, IOException {
+ final StringBuilder resultBuilder = new StringBuilder();
+ File currentFile = file.getCanonicalFile();
+ while (!currentFile.equals(projectDirectory)) {
+ if (resultBuilder.length() > 0) {
+ resultBuilder.insert(0, '/');
+ }
+ resultBuilder.insert(0, currentFile.getName());
+ currentFile = currentFile.getParentFile();
+ if (currentFile == null) {
+ throw new PathNotInProjectDirectoryException();
+ }
+ }
+ return resultBuilder.toString();
+ }
+
+ List<Rule> createRuleList(File projectDirectory, File workingDirectory,
+ List<String> filePatternsOfAddCommand)
+ throws PathNotInProjectDirectoryException, IOException,
+ InvalidPatternException {
+
+ final String workingDirectoryPath = getRelativeUnixPath(
+ projectDirectory, workingDirectory);
+
+ final List<Rule> ruleList = new ArrayList<Rule>(
+ filePatternsOfAddCommand.size());
+ for (String pattern : filePatternsOfAddCommand) {
+ boolean matchDirectoriesOnly = false;
+ if (pattern.endsWith(File.separator)) {
+ pattern = pattern.substring(0, pattern.length() - 1);
+ matchDirectoriesOnly = true;
+ }
+ pattern = pattern.replace(File.separatorChar, '/');
+ if (!"".equals(workingDirectoryPath)) {
+ pattern = workingDirectoryPath + "/" + pattern;
+
+ }
+ final FilePattern filePattern = new FilePathPattern(pattern, true,
+ matchDirectoriesOnly);
+ final Rule rule = new Rule(false, filePattern);
+ ruleList.add(rule);
+
+ }
+ return ruleList;
+ }
+}
--
1.5.4.3
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [JGIT PATCH v4 12/24] Formatted Constants class.
2008-06-13 18:30 [JGIT PATCH v4] Implementation of fnmatch and the ignore rules Florian Köberle
` (10 preceding siblings ...)
2008-06-13 18:35 ` [JGIT PATCH v4 11/24] Added the class AddRuleListFactory Florian Koeberle
@ 2008-06-13 18:35 ` Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 13/24] Added constant REPOSITORY_DIRECTORY_NAME to " Florian Koeberle
` (12 subsequent siblings)
24 siblings, 0 replies; 26+ messages in thread
From: Florian Koeberle @ 2008-06-13 18:35 UTC (permalink / raw)
To: git; +Cc: Florian Koeberle, Shawn O. Pearce
Signed-off-by: Florian Koeberle <florianskarten@web.de>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
.../src/org/spearce/jgit/lib/Constants.java | 14 ++++++++------
1 files changed, 8 insertions(+), 6 deletions(-)
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java b/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java
index d1e8a41..26b8c8e 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java
@@ -226,8 +226,9 @@ public final class Constants {
/**
* Convert an OBJ_* type constant to a TYPE_* type constant.
- *
- * @param typeCode the type code, from a pack representation.
+ *
+ * @param typeCode
+ * the type code, from a pack representation.
* @return the canonical string name of this type.
*/
public static String typeString(final int typeCode) {
@@ -248,10 +249,11 @@ public final class Constants {
/**
* Convert an OBJ_* type constant to an ASCII encoded string constant.
* <p>
- * The ASCII encoded string is often the canonical representation of
- * the type within a loose object header, or within a tag header.
- *
- * @param typeCode the type code, from a pack representation.
+ * The ASCII encoded string is often the canonical representation of the
+ * type within a loose object header, or within a tag header.
+ *
+ * @param typeCode
+ * the type code, from a pack representation.
* @return the canonical ASCII encoded name of this type.
*/
public static byte[] encodedTypeString(final int typeCode) {
--
1.5.4.3
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [JGIT PATCH v4 13/24] Added constant REPOSITORY_DIRECTORY_NAME to Constants class.
2008-06-13 18:30 [JGIT PATCH v4] Implementation of fnmatch and the ignore rules Florian Köberle
` (11 preceding siblings ...)
2008-06-13 18:35 ` [JGIT PATCH v4 12/24] Formatted Constants class Florian Koeberle
@ 2008-06-13 18:35 ` Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 14/24] Added class AddRulesFactory Florian Koeberle
` (11 subsequent siblings)
24 siblings, 0 replies; 26+ messages in thread
From: Florian Koeberle @ 2008-06-13 18:35 UTC (permalink / raw)
To: git; +Cc: Florian Koeberle, Shawn O. Pearce
Signed-off-by: Florian Koeberle <florianskarten@web.de>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
.../src/org/spearce/jgit/lib/Constants.java | 6 ++++++
1 files changed, 6 insertions(+), 0 deletions(-)
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java b/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java
index 26b8c8e..744d4d8 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java
@@ -207,6 +207,12 @@ public final class Constants {
public static final String TAGS_PREFIX = "refs/tags";
/**
+ * The name of the repository directory in the project directory. Project
+ * directory means here the tree under version control.
+ */
+ public static final String REPOSITORY_DIRECTORY_NAME = ".git";
+
+ /**
* Create a new digest function for objects.
*
* @return a new digest object.
--
1.5.4.3
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [JGIT PATCH v4 14/24] Added class AddRulesFactory.
2008-06-13 18:30 [JGIT PATCH v4] Implementation of fnmatch and the ignore rules Florian Köberle
` (12 preceding siblings ...)
2008-06-13 18:35 ` [JGIT PATCH v4 13/24] Added constant REPOSITORY_DIRECTORY_NAME to " Florian Koeberle
@ 2008-06-13 18:35 ` Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 15/24] Added the class LightFileTreeIterator and a test for it Florian Koeberle
` (10 subsequent siblings)
24 siblings, 0 replies; 26+ messages in thread
From: Florian Koeberle @ 2008-06-13 18:35 UTC (permalink / raw)
To: git; +Cc: Florian Koeberle
Signed-off-by: Florian Koeberle <florianskarten@web.de>
---
.../jgit/treewalk/rules/AddRulesFactory.java | 127 ++++++++++++++++++++
1 files changed, 127 insertions(+), 0 deletions(-)
create mode 100644 org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/AddRulesFactory.java
diff --git a/org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/AddRulesFactory.java b/org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/AddRulesFactory.java
new file mode 100644
index 0000000..767b5c7
--- /dev/null
+++ b/org.spearce.jgit/src/org/spearce/jgit/treewalk/rules/AddRulesFactory.java
@@ -0,0 +1,127 @@
+/*
+ * Copyright (C) 2008, Florian Köberle <florianskarten@web.de>
+ *
+ * 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 Git Development Community 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.spearce.jgit.treewalk.rules;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.spearce.jgit.errors.InvalidPatternException;
+import org.spearce.jgit.errors.PathNotInProjectDirectoryException;
+import static org.spearce.jgit.lib.Constants.REPOSITORY_DIRECTORY_NAME;
+
+/**
+ * This class is designed to serve the needs of someone who want to implement a
+ * git-add command and needs to determine the files to add.
+ *
+ */
+public class AddRulesFactory {
+ private final RuleListToObjectConverter converter = new RuleListToObjectConverter();
+
+ private final IgnoreRuleListFactory ignoreRuleListFactory = new IgnoreRuleListFactory();
+
+ private final AddRuleListFactory addRuleListFactory = new AddRuleListFactory();
+
+ /**
+ * @param workTreeDirectory
+ * The directory with the files of the project under version
+ * control.
+ * @param workingDirectory
+ * a directory within the workTreeDirectory.
+ * @param filePatternsOfAddCommand
+ * the file patterns passed to the add command.
+ * @return a {@link Rules} containing the specified rules, the .gitignore
+ * and the .git/info/exclude rules.
+ * @throws InvalidPatternException
+ * if a pattern is invalid.
+ * @throws PathNotInProjectDirectoryException
+ * if the directory workingDirectory is not in workTreeDirectory
+ * @throws IOException
+ * for some reasons.
+ */
+ public Rules createRules(File workTreeDirectory, File workingDirectory,
+ List<String> filePatternsOfAddCommand)
+ throws InvalidPatternException, PathNotInProjectDirectoryException,
+ IOException {
+ final Rule gitDirectoryIgnoreRule = createGitDirectoryIgnoreRule();
+ final File gitDirectory = new File(workTreeDirectory,
+ REPOSITORY_DIRECTORY_NAME);
+ final List<Rule> ignoreRuleListFromFiles = createExcludeRules(
+ workTreeDirectory, gitDirectory);
+ final List<Rule> includeRules = addRuleListFactory.createRuleList(
+ workTreeDirectory, workingDirectory, filePatternsOfAddCommand);
+ final List<Rule> ruleList = new ArrayList<Rule>();
+
+ ruleList.add(gitDirectoryIgnoreRule);
+ ruleList.addAll(ignoreRuleListFromFiles);
+ ruleList.addAll(includeRules);
+ ruleList.add(new Rule(true, FilePattern.MATCH_ALWAYS));
+
+ return converter.createIgnoreRules(ruleList.iterator());
+ }
+
+ private List<Rule> createExcludeRules(File projectDirectory,
+ File gitDirectory) throws InvalidPatternException {
+ final List<File> possibleIgnoreFiles = new ArrayList<File>(2);
+ possibleIgnoreFiles.add(new File(projectDirectory, ".gitignore"));
+ possibleIgnoreFiles.add(new File(new File(gitDirectory, "info"),
+ "exclude"));
+
+ final List<File> ignoreFiles = new ArrayList<File>();
+ for (File possibleIgnoreFile : possibleIgnoreFiles) {
+ if (possibleIgnoreFile.isFile()) {
+ ignoreFiles.add(possibleIgnoreFile);
+ }
+ }
+
+ try {
+ return ignoreRuleListFactory.createIgnoreRuleList(ignoreFiles);
+ } catch (FileNotFoundException e) {
+ throw new RuntimeException("unexpected removal of ignore files", e);
+ }
+ }
+
+ private Rule createGitDirectoryIgnoreRule() throws InvalidPatternException {
+ final FilePattern gitDirectoryPattern = new FilePathPattern(
+ REPOSITORY_DIRECTORY_NAME, true, true);
+ final Rule gitDirectoryIgnoreRule = new Rule(true, gitDirectoryPattern);
+ return gitDirectoryIgnoreRule;
+ }
+}
--
1.5.4.3
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [JGIT PATCH v4 15/24] Added the class LightFileTreeIterator and a test for it.
2008-06-13 18:30 [JGIT PATCH v4] Implementation of fnmatch and the ignore rules Florian Köberle
` (13 preceding siblings ...)
2008-06-13 18:35 ` [JGIT PATCH v4 14/24] Added class AddRulesFactory Florian Koeberle
@ 2008-06-13 18:35 ` Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 16/24] Added class LightFileTreeIterable Florian Koeberle
` (9 subsequent siblings)
24 siblings, 0 replies; 26+ messages in thread
From: Florian Koeberle @ 2008-06-13 18:35 UTC (permalink / raw)
To: git; +Cc: Florian Koeberle
Signed-off-by: Florian Koeberle <florianskarten@web.de>
---
.../jgit/treewalk/LightFileTreeIteratorTest.java | 135 ++++++++++++++++++++
.../jgit/treewalk/LightFileTreeIterator.java | 133 +++++++++++++++++++
2 files changed, 268 insertions(+), 0 deletions(-)
create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/treewalk/LightFileTreeIteratorTest.java
create mode 100644 org.spearce.jgit/src/org/spearce/jgit/treewalk/LightFileTreeIterator.java
diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/treewalk/LightFileTreeIteratorTest.java b/org.spearce.jgit.test/tst/org/spearce/jgit/treewalk/LightFileTreeIteratorTest.java
new file mode 100644
index 0000000..d1d8f48
--- /dev/null
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/treewalk/LightFileTreeIteratorTest.java
@@ -0,0 +1,135 @@
+/*
+ * Copyright (C) 2008, Florian Köberle <florianskarten@web.de>
+ *
+ * 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 Git Development Community 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.spearce.jgit.treewalk;
+
+import java.io.File;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+
+import org.spearce.jgit.treewalk.rules.Rules;
+
+import junit.framework.TestCase;
+
+public class LightFileTreeIteratorTest extends TestCase {
+ private String DIRECTORY_A_NAME = "a";
+
+ private String DIRECTORY_AB_NAME = "b";
+
+ private File projectDirectory;
+
+ private File fileB;
+
+ private File directoryA;
+
+ private File directoryAB;
+
+ private File fileABA;
+
+ private File directoryAC;
+
+ private File fileACA;
+
+ private File fileACB;
+
+ @Override
+ protected void setUp() throws Exception {
+ this.projectDirectory = File.createTempFile("FileTreeIteratorTest", "");
+ projectDirectory.delete();
+ projectDirectory.mkdir();
+ projectDirectory.deleteOnExit();
+
+ this.directoryA = new File(projectDirectory, DIRECTORY_A_NAME);
+ directoryA.mkdir();
+
+ this.directoryAB = new File(directoryA, DIRECTORY_AB_NAME);
+ directoryAB.mkdir();
+
+ this.fileABA = new File(directoryAB, "a.y");
+ fileABA.createNewFile();
+
+ this.directoryAC = new File(directoryA, "c");
+ this.directoryAC.mkdir();
+
+ this.fileACA = new File(directoryAC, "a.x");
+ fileACA.createNewFile();
+
+ this.fileACB = new File(directoryAC, "b.y");
+ fileACB.createNewFile();
+
+ this.fileB = new File(projectDirectory, "b.x");
+ fileB.createNewFile();
+ }
+
+ public void testFileTreeIterator() {
+ final Iterator<File> iterator = new LightFileTreeIterator(
+ projectDirectory, Rules.IGNORE_NOTHING, false);
+ final Set<File> actualPaths = new HashSet<File>();
+ while (iterator.hasNext()) {
+ final File next = iterator.next();
+ assertFalse(actualPaths.contains(next));
+ actualPaths.add(next);
+ }
+
+ final Set<File> expectedPaths = new HashSet<File>();
+ expectedPaths.add(directoryA);
+ expectedPaths.add(fileB);
+ expectedPaths.add(directoryAB);
+ expectedPaths.add(fileABA);
+ expectedPaths.add(directoryAC);
+ expectedPaths.add(fileACA);
+ expectedPaths.add(fileACB);
+ assertEquals(expectedPaths, actualPaths);
+
+ }
+
+ public void testFileTreeIteratorWithIgnoreAllRules() {
+ final Iterator<File> iterator = new LightFileTreeIterator(
+ projectDirectory, Rules.IGNORE_ALL, false);
+ final Set<File> actualPaths = new HashSet<File>();
+ while (iterator.hasNext()) {
+ final File next = iterator.next();
+ assertFalse(actualPaths.contains(next));
+ actualPaths.add(next);
+ }
+
+ assertEquals(0, actualPaths.size());
+
+ }
+
+}
diff --git a/org.spearce.jgit/src/org/spearce/jgit/treewalk/LightFileTreeIterator.java b/org.spearce.jgit/src/org/spearce/jgit/treewalk/LightFileTreeIterator.java
new file mode 100644
index 0000000..ae80393
--- /dev/null
+++ b/org.spearce.jgit/src/org/spearce/jgit/treewalk/LightFileTreeIterator.java
@@ -0,0 +1,133 @@
+/*
+ * Copyright (C) 2008, Florian Köberle <florianskarten@web.de>
+ *
+ * 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 Git Development Community 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.spearce.jgit.treewalk;
+
+import java.io.File;
+import java.util.EmptyStackException;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+import java.util.Stack;
+
+import org.spearce.jgit.treewalk.rules.Rules;
+
+class LightFileTreeIterator implements Iterator<File> {
+ private final Stack<File> remainingPaths;
+
+ private final Stack<Directory> remainingDirectories;
+
+ /**
+ * Creates a new Iterator which allows to iterate over the content of the
+ * specified rootDirectory. The rootDirectory itself is never included.
+ *
+ * @param rootDirectory
+ * the directory tree to iterate over.
+ * @param ignoreRules
+ * defines which paths are included and which aren't.
+ * @param includeRootDirectory
+ * the iterator will return the rootDirectory if this is flag is
+ * true.
+ */
+ LightFileTreeIterator(File rootDirectory, Rules ignoreRules,
+ boolean includeRootDirectory) {
+ remainingPaths = new Stack<File>();
+ if (includeRootDirectory) {
+ remainingPaths.add(rootDirectory);
+ }
+ remainingDirectories = new Stack<Directory>();
+ remainingDirectories.add(new Directory(rootDirectory, ignoreRules));
+ }
+
+ public boolean hasNext() {
+ findMorePathsIfNessesary();
+ return !remainingPaths.empty();
+ }
+
+ void findMorePathsIfNessesary() {
+ if (remainingPaths.isEmpty()) {
+ findMorePaths();
+ }
+ }
+
+ void findMorePaths() {
+ while (!remainingDirectories.isEmpty() && remainingPaths.isEmpty()) {
+ final Directory directory = remainingDirectories.pop();
+ final File[] paths = directory.path.listFiles();
+ for (File path : paths) {
+ final boolean fileIsDirectory = path.isDirectory();
+ if (fileIsDirectory) {
+ final Rules subDirectoryIgnoreRules = directory.ignoreRules
+ .getRulesForSubDirectory(path.getName());
+ if (subDirectoryIgnoreRules != Rules.IGNORE_ALL) {
+ final Directory subDirectory = new Directory(path,
+ subDirectoryIgnoreRules);
+ remainingDirectories.add(subDirectory);
+ }
+ }
+ if (!directory.ignoreRules.shouldIgnore(path.getName(),
+ fileIsDirectory)) {
+ remainingPaths.add(path);
+ }
+ }
+ }
+ }
+
+ public File next() {
+ findMorePathsIfNessesary();
+ try {
+ return remainingPaths.pop();
+ } catch (EmptyStackException e) {
+ throw new NoSuchElementException();
+ }
+ }
+
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+
+ private class Directory {
+ final File path;
+
+ final Rules ignoreRules;
+
+ Directory(File path, Rules ignoreRules) {
+ this.path = path;
+ this.ignoreRules = ignoreRules;
+ }
+ }
+
+}
--
1.5.4.3
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [JGIT PATCH v4 16/24] Added class LightFileTreeIterable.
2008-06-13 18:30 [JGIT PATCH v4] Implementation of fnmatch and the ignore rules Florian Köberle
` (14 preceding siblings ...)
2008-06-13 18:35 ` [JGIT PATCH v4 15/24] Added the class LightFileTreeIterator and a test for it Florian Koeberle
@ 2008-06-13 18:35 ` Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 17/24] Added path related constants to the Constants class Florian Koeberle
` (8 subsequent siblings)
24 siblings, 0 replies; 26+ messages in thread
From: Florian Koeberle @ 2008-06-13 18:35 UTC (permalink / raw)
To: git; +Cc: Florian Koeberle
Signed-off-by: Florian Koeberle <florianskarten@web.de>
---
.../jgit/treewalk/LightFileTreeIterable.java | 80 ++++++++++++++++++++
1 files changed, 80 insertions(+), 0 deletions(-)
create mode 100644 org.spearce.jgit/src/org/spearce/jgit/treewalk/LightFileTreeIterable.java
diff --git a/org.spearce.jgit/src/org/spearce/jgit/treewalk/LightFileTreeIterable.java b/org.spearce.jgit/src/org/spearce/jgit/treewalk/LightFileTreeIterable.java
new file mode 100644
index 0000000..ea06e2d
--- /dev/null
+++ b/org.spearce.jgit/src/org/spearce/jgit/treewalk/LightFileTreeIterable.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2008, Florian Köberle <florianskarten@web.de>
+ *
+ * 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 Git Development Community 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.spearce.jgit.treewalk;
+
+import java.io.File;
+import java.util.Iterator;
+
+import org.spearce.jgit.treewalk.rules.Rules;
+
+/**
+ * Use this class to iterate over some by rules determined files in a project
+ * directory.
+ *
+ */
+public class LightFileTreeIterable implements Iterable<File> {
+ private final File projectDirectory;
+
+ private final Rules ignoreRules;
+
+ private final boolean includeRootDirectory;
+
+ /**
+ *
+ * @param workTreeDirectory
+ * directory with the files which are under version control.
+ * @param rules
+ * a {@link Rules} instance which is used to determine which file
+ * are included and which not.
+ * @param includeWorkTreeDirectory
+ * determines if the iterators should return the
+ * workTreeDirectory
+ */
+ public LightFileTreeIterable(File workTreeDirectory, Rules rules,
+ boolean includeWorkTreeDirectory) {
+ this.projectDirectory = workTreeDirectory;
+ this.ignoreRules = rules;
+ this.includeRootDirectory = includeWorkTreeDirectory;
+ }
+
+ public Iterator<File> iterator() {
+ return new LightFileTreeIterator(projectDirectory, ignoreRules,
+ includeRootDirectory);
+ }
+
+}
--
1.5.4.3
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [JGIT PATCH v4 17/24] Added path related constants to the Constants class.
2008-06-13 18:30 [JGIT PATCH v4] Implementation of fnmatch and the ignore rules Florian Köberle
` (15 preceding siblings ...)
2008-06-13 18:35 ` [JGIT PATCH v4 16/24] Added class LightFileTreeIterable Florian Koeberle
@ 2008-06-13 18:35 ` Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 18/24] Added WorkTree class which can be constructed over Repository Florian Koeberle
` (7 subsequent siblings)
24 siblings, 0 replies; 26+ messages in thread
From: Florian Koeberle @ 2008-06-13 18:35 UTC (permalink / raw)
To: git; +Cc: Florian Koeberle, Shawn O. Pearce
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Signed-off-by: Florian Koeberle <florianskarten@web.de>
---
.../src/org/spearce/jgit/lib/Constants.java | 10 ++++++++++
1 files changed, 10 insertions(+), 0 deletions(-)
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java b/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java
index 744d4d8..ee85ded 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java
@@ -213,6 +213,16 @@ public final class Constants {
public static final String REPOSITORY_DIRECTORY_NAME = ".git";
/**
+ * Contains the name of the objects directory in the repository directory.
+ */
+ public static final String OBJECTS_DIRECTORY_NAME = "objects";
+
+ /**
+ * Contains the name of the refs directory in the repository directory.
+ */
+ public static final String REFS_DIRECTORY_NAME = "refs";
+
+ /**
* Create a new digest function for objects.
*
* @return a new digest object.
--
1.5.4.3
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [JGIT PATCH v4 18/24] Added WorkTree class which can be constructed over Repository.
2008-06-13 18:30 [JGIT PATCH v4] Implementation of fnmatch and the ignore rules Florian Köberle
` (16 preceding siblings ...)
2008-06-13 18:35 ` [JGIT PATCH v4 17/24] Added path related constants to the Constants class Florian Koeberle
@ 2008-06-13 18:35 ` Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 19/24] Added the class NoGitRepositoryFoundException Florian Koeberle
` (6 subsequent siblings)
24 siblings, 0 replies; 26+ messages in thread
From: Florian Koeberle @ 2008-06-13 18:35 UTC (permalink / raw)
To: git; +Cc: Florian Koeberle
Signed-off-by: Florian Koeberle <florianskarten@web.de>
---
.../src/org/spearce/jgit/lib/Repository.java | 31 ++++++++
.../src/org/spearce/jgit/lib/WorkTree.java | 81 ++++++++++++++++++++
2 files changed, 112 insertions(+), 0 deletions(-)
create mode 100644 org.spearce.jgit/src/org/spearce/jgit/lib/WorkTree.java
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java b/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java
index 3efe60b..5a21c6e 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java
@@ -986,4 +986,35 @@ public class Repository {
public File getWorkDir() {
return getDirectory().getParentFile();
}
+
+ /**
+ * Creates a new {@link WorkTree} and initialize a repository for it.
+ *
+ * @param workTreeDirectory
+ * the directory with the project files.
+ * @return a new {@link WorkTree} with a new and open {@link Repository}.
+ * @throws IOException
+ * thrown by {@link Repository#create()}
+ */
+ public static WorkTree createWorkTree(File workTreeDirectory)
+ throws IOException {
+ final File gitDirectory = new File(workTreeDirectory,
+ Constants.REPOSITORY_DIRECTORY_NAME);
+ if (gitDirectory.exists()) {
+ throw new IllegalArgumentException(
+ "Repository exists in given project directory.");
+ }
+ final Repository repository = new Repository(gitDirectory);
+ try {
+ repository.create();
+ return new WorkTree(workTreeDirectory, repository);
+ } catch (RuntimeException e) {
+ repository.close();
+ throw e;
+ } catch (IOException e) {
+ repository.close();
+ throw e;
+ }
+ }
+
}
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/WorkTree.java b/org.spearce.jgit/src/org/spearce/jgit/lib/WorkTree.java
new file mode 100644
index 0000000..0040954
--- /dev/null
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/WorkTree.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2008, Florian Köberle <florianskarten@web.de>
+ *
+ * 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 Git Development Community 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.spearce.jgit.lib;
+
+import java.io.File;
+
+/**
+ * Represents a work tree controlled by git. Use {@link Repository} in order to
+ * create an object of this class.
+ *
+ */
+public class WorkTree {
+ private final File directory;
+
+ private final Repository repository;
+
+ /**
+ * This constructor should be only used by the class {@link Repository}.
+ *
+ * @param directory
+ * defines the value of the directory field.
+ * @param repository
+ * defines the value of the repository field.
+ */
+ WorkTree(File directory, Repository repository) {
+ this.directory = directory;
+ this.repository = repository;
+ }
+
+ /**
+ *
+ * @return the directory which contains the files of the project. Usually
+ * this directory contain a .git directory with the repository.
+ */
+ public File getDirectory() {
+ return directory;
+ }
+
+ /**
+ * @return the {@link Repository} object of the project.
+ */
+ public Repository getRepository() {
+ return repository;
+ }
+
+}
--
1.5.4.3
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [JGIT PATCH v4 19/24] Added the class NoGitRepositoryFoundException.
2008-06-13 18:30 [JGIT PATCH v4] Implementation of fnmatch and the ignore rules Florian Köberle
` (17 preceding siblings ...)
2008-06-13 18:35 ` [JGIT PATCH v4 18/24] Added WorkTree class which can be constructed over Repository Florian Koeberle
@ 2008-06-13 18:35 ` Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 20/24] Formatted Repository class Florian Koeberle
` (5 subsequent siblings)
24 siblings, 0 replies; 26+ messages in thread
From: Florian Koeberle @ 2008-06-13 18:35 UTC (permalink / raw)
To: git; +Cc: Florian Koeberle
Signed-off-by: Florian Koeberle <florianskarten@web.de>
---
.../jgit/errors/NoGitRepositoryFoundException.java | 65 ++++++++++++++++++++
1 files changed, 65 insertions(+), 0 deletions(-)
create mode 100644 org.spearce.jgit/src/org/spearce/jgit/errors/NoGitRepositoryFoundException.java
diff --git a/org.spearce.jgit/src/org/spearce/jgit/errors/NoGitRepositoryFoundException.java b/org.spearce.jgit/src/org/spearce/jgit/errors/NoGitRepositoryFoundException.java
new file mode 100644
index 0000000..1d84e60
--- /dev/null
+++ b/org.spearce.jgit/src/org/spearce/jgit/errors/NoGitRepositoryFoundException.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2008, Florian Köberle <florianskarten@web.de>
+ *
+ * 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 Git Development Community 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.spearce.jgit.errors;
+
+import java.io.File;
+
+/**
+ * No git repository could be found for a file or directory.
+ */
+public class NoGitRepositoryFoundException extends Exception {
+ private final File fileWeSearchedRepositoryFor;
+
+ /**
+ * @param fileWeSearchedRepositoryFor
+ * the file for which a repository was searched.
+ */
+ public NoGitRepositoryFoundException(final File fileWeSearchedRepositoryFor) {
+ super(String.format("No repository found for file '%s'",
+ fileWeSearchedRepositoryFor));
+ this.fileWeSearchedRepositoryFor = fileWeSearchedRepositoryFor;
+ }
+
+ /**
+ * @return the file searched for.
+ */
+ public File getFileWeSearchedRepositoryFor() {
+ return fileWeSearchedRepositoryFor;
+ }
+
+}
--
1.5.4.3
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [JGIT PATCH v4 20/24] Formatted Repository class.
2008-06-13 18:30 [JGIT PATCH v4] Implementation of fnmatch and the ignore rules Florian Köberle
` (18 preceding siblings ...)
2008-06-13 18:35 ` [JGIT PATCH v4 19/24] Added the class NoGitRepositoryFoundException Florian Koeberle
@ 2008-06-13 18:35 ` Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 21/24] Added findWorkTree method to " Florian Koeberle
` (4 subsequent siblings)
24 siblings, 0 replies; 26+ messages in thread
From: Florian Koeberle @ 2008-06-13 18:35 UTC (permalink / raw)
To: git; +Cc: Florian Koeberle
Signed-off-by: Florian Koeberle <florianskarten@web.de>
---
.../src/org/spearce/jgit/lib/Repository.java | 285 +++++++++++---------
1 files changed, 160 insertions(+), 125 deletions(-)
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java b/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java
index 5a21c6e..d7c3b13 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java
@@ -66,16 +66,17 @@ import org.spearce.jgit.util.FS;
*
* <ul>
* <li>GIT_DIR
- * <ul>
- * <li>objects/ - objects</li>
- * <li>refs/ - tags and heads</li>
- * <li>config - configuration</li>
- * <li>info/ - more configurations</li>
- * </ul>
+ * <ul>
+ * <li>objects/ - objects</li>
+ * <li>refs/ - tags and heads</li>
+ * <li>config - configuration</li>
+ * <li>info/ - more configurations</li>
+ * </ul>
* </li>
* </ul>
*
- * This implementation only handles a subtly undocumented subset of git features.
+ * This implementation only handles a subtly undocumented subset of git
+ * features.
*
*/
public class Repository {
@@ -93,7 +94,7 @@ public class Repository {
/**
* Construct a representation of a Git repository.
- *
+ *
* @param d
* GIT_DIR (the location of the repository metadata).
* @throws IOException
@@ -106,7 +107,8 @@ public class Repository {
objectsDirs = readObjectsDirs(FS.resolve(gitDir, "objects"),
new ArrayList<File>()).toArray(new File[0]);
} catch (IOException e) {
- IOException ex = new IOException("Cannot find all object dirs for " + gitDir);
+ IOException ex = new IOException("Cannot find all object dirs for "
+ + gitDir);
ex.initCause(e);
throw ex;
}
@@ -130,12 +132,13 @@ public class Repository {
scanForPacks();
}
- private Collection<File> readObjectsDirs(File objectsDir, Collection<File> ret) throws IOException {
+ private Collection<File> readObjectsDirs(File objectsDir,
+ Collection<File> ret) throws IOException {
ret.add(objectsDir);
final File altFile = FS.resolve(objectsDir, "info/alternates");
if (altFile.exists()) {
BufferedReader ar = new BufferedReader(new FileReader(altFile));
- for (String alt=ar.readLine(); alt!=null; alt=ar.readLine()) {
+ for (String alt = ar.readLine(); alt != null; alt = ar.readLine()) {
readObjectsDirs(FS.resolve(objectsDir, alt), ret);
}
ar.close();
@@ -204,9 +207,9 @@ public class Repository {
*/
public File toFile(final AnyObjectId objectId) {
final String n = objectId.toString();
- String d=n.substring(0, 2);
- String f=n.substring(2);
- for (int i=0; i<objectsDirs.length; ++i) {
+ String d = n.substring(0, 2);
+ String f = n.substring(2);
+ for (int i = 0; i < objectsDirs.length; ++i) {
File ret = new File(new File(objectsDirs[i], d), f);
if (ret.exists())
return ret;
@@ -233,14 +236,13 @@ public class Repository {
/**
* @param id
* SHA-1 of an object.
- *
+ *
* @return a {@link ObjectLoader} for accessing the data of the named
* object, or null if the object does not exist.
* @throws IOException
*/
- public ObjectLoader openObject(final AnyObjectId id)
- throws IOException {
- return openObject(new WindowCursor(),id);
+ public ObjectLoader openObject(final AnyObjectId id) throws IOException {
+ return openObject(new WindowCursor(), id);
}
/**
@@ -248,7 +250,7 @@ public class Repository {
* temporary working space associated with the calling thread.
* @param id
* SHA-1 of an object.
- *
+ *
* @return a {@link ObjectLoader} for accessing the data of the named
* object, or null if the object does not exist.
* @throws IOException
@@ -266,7 +268,8 @@ public class Repository {
// This shouldn't happen unless the pack was corrupted
// after we opened it or the VM runs out of memory. This is
// a know problem with memory mapped I/O in java and have
- // been noticed with JDK < 1.6. Tell the gc that now is a good
+ // been noticed with JDK < 1.6. Tell the gc that now is a
+ // good
// time to collect and try once more.
try {
curs.release();
@@ -312,13 +315,15 @@ public class Repository {
}
/**
- * Access a Commit object using a symbolic reference. This reference may
- * be a SHA-1 or ref in combination with a number of symbols translating
- * from one ref or SHA1-1 to another, such as HEAD^ etc.
+ * Access a Commit object using a symbolic reference. This reference may be
+ * a SHA-1 or ref in combination with a number of symbols translating from
+ * one ref or SHA1-1 to another, such as HEAD^ etc.
*
- * @param revstr a reference to a git commit object
+ * @param revstr
+ * a reference to a git commit object
* @return a Commit named by the specified string
- * @throws IOException for I/O error or unexpected object type.
+ * @throws IOException
+ * for I/O error or unexpected object type.
*
* @see #resolve(String)
*/
@@ -332,11 +337,13 @@ public class Repository {
*
* @param id
* SHA-1 of object to read
- * @param refName optional, only relevant for simple tags
+ * @param refName
+ * optional, only relevant for simple tags
* @return The Git object if found or null
* @throws IOException
*/
- public Object mapObject(final ObjectId id, final String refName) throws IOException {
+ public Object mapObject(final ObjectId id, final String refName)
+ throws IOException {
final ObjectLoader or = openObject(id);
final byte[] raw = or.getBytes();
if (or.getType() == Constants.OBJ_TREE)
@@ -352,9 +359,11 @@ public class Repository {
/**
* Access a Commit by SHA'1 id.
+ *
* @param id
* @return Commit or null
- * @throws IOException for I/O error or unexpected object type.
+ * @throws IOException
+ * for I/O error or unexpected object type.
*/
public Commit mapCommit(final ObjectId id) throws IOException {
final ObjectLoader or = openObject(id);
@@ -372,11 +381,12 @@ public class Repository {
}
/**
- * Access a Tree object using a symbolic reference. This reference may
- * be a SHA-1 or ref in combination with a number of symbols translating
- * from one ref or SHA1-1 to another, such as HEAD^{tree} etc.
+ * Access a Tree object using a symbolic reference. This reference may be a
+ * SHA-1 or ref in combination with a number of symbols translating from one
+ * ref or SHA1-1 to another, such as HEAD^{tree} etc.
*
- * @param revstr a reference to a git commit object
+ * @param revstr
+ * a reference to a git commit object
* @return a Tree named by the specified string
* @throws IOException
*
@@ -389,9 +399,11 @@ public class Repository {
/**
* Access a Tree by SHA'1 id.
+ *
* @param id
* @return Tree or null
- * @throws IOException for I/O error or unexpected object type.
+ * @throws IOException
+ * for I/O error or unexpected object type.
*/
public Tree mapTree(final ObjectId id) throws IOException {
final ObjectLoader or = openObject(id);
@@ -406,12 +418,14 @@ public class Repository {
throw new IncorrectObjectTypeException(id, Constants.TYPE_TREE);
}
- private Tree makeTree(final ObjectId id, final byte[] raw) throws IOException {
+ private Tree makeTree(final ObjectId id, final byte[] raw)
+ throws IOException {
Tree ret = new Tree(this, id, raw);
return ret;
}
- private Tag makeTag(final ObjectId id, final String refName, final byte[] raw) {
+ private Tag makeTag(final ObjectId id, final String refName,
+ final byte[] raw) {
Tag ret = new Tag(this, id, refName, raw);
return ret;
}
@@ -421,7 +435,8 @@ public class Repository {
*
* @param revstr
* @return a Tag or null
- * @throws IOException on I/O error or unexpected type
+ * @throws IOException
+ * on I/O error or unexpected type
*/
public Tag mapTag(String revstr) throws IOException {
final ObjectId id = resolve(revstr);
@@ -430,12 +445,15 @@ public class Repository {
/**
* Access a Tag by SHA'1 id
+ *
* @param refName
* @param id
* @return Commit or null
- * @throws IOException for I/O error or unexpected object type.
+ * @throws IOException
+ * for I/O error or unexpected object type.
*/
- public Tag mapTag(final String refName, final ObjectId id) throws IOException {
+ public Tag mapTag(final String refName, final ObjectId id)
+ throws IOException {
final ObjectLoader or = openObject(id);
if (or == null)
return null;
@@ -447,7 +465,7 @@ public class Repository {
/**
* Create a command to update (or create) a ref in this repository.
- *
+ *
* @param ref
* name of the ref the caller wants to modify.
* @return an update command. The caller must finish populating this command
@@ -466,13 +484,13 @@ public class Repository {
*
* Currently supported is combinations of these.
* <ul>
- * <li>SHA-1 - a SHA-1</li>
- * <li>refs/... - a ref name</li>
- * <li>ref^n - nth parent reference</li>
- * <li>ref~n - distance via parent reference</li>
- * <li>ref@{n} - nth version of ref</li>
- * <li>ref^{tree} - tree references by ref</li>
- * <li>ref^{commit} - commit references by ref</li>
+ * <li>SHA-1 - a SHA-1</li>
+ * <li>refs/... - a ref name</li>
+ * <li>ref^n - nth parent reference</li>
+ * <li>ref~n - distance via parent reference</li>
+ * <li>ref@{n} - nth version of ref</li>
+ * <li>ref^{tree} - tree references by ref</li>
+ * <li>ref^{commit} - commit references by ref</li>
* </ul>
*
* Not supported is
@@ -481,9 +499,11 @@ public class Repository {
* <li>abbreviated SHA-1's</li>
* </ul>
*
- * @param revstr A git object references expression
+ * @param revstr
+ * A git object references expression
* @return an ObjectId
- * @throws IOException on serious errors
+ * @throws IOException
+ * on serious errors
*/
public ObjectId resolve(final String revstr) throws IOException {
char[] rev = revstr.toCharArray();
@@ -493,7 +513,7 @@ public class Repository {
switch (rev[i]) {
case '^':
if (refId == null) {
- String refstr = new String(rev,0,i);
+ String refstr = new String(rev, 0, i);
refId = resolveSimple(refstr);
if (refId == null)
return null;
@@ -513,23 +533,24 @@ public class Repository {
int j;
ref = mapObject(refId, null);
if (!(ref instanceof Commit))
- throw new IncorrectObjectTypeException(refId, Constants.TYPE_COMMIT);
- for (j=i+1; j<rev.length; ++j) {
+ throw new IncorrectObjectTypeException(refId,
+ Constants.TYPE_COMMIT);
+ for (j = i + 1; j < rev.length; ++j) {
if (!Character.isDigit(rev[j]))
break;
}
- String parentnum = new String(rev, i+1, j-i-1);
+ String parentnum = new String(rev, i + 1, j - i - 1);
int pnum = Integer.parseInt(parentnum);
if (pnum != 0)
- refId = ((Commit)ref).getParentIds()[pnum - 1];
+ refId = ((Commit) ref).getParentIds()[pnum - 1];
i = j - 1;
break;
case '{':
int k;
String item = null;
- for (k=i+2; k<rev.length; ++k) {
+ for (k = i + 2; k < rev.length; ++k) {
if (rev[k] == '}') {
- item = new String(rev, i+2, k-i-2);
+ item = new String(rev, i + 2, k - i - 2);
break;
}
}
@@ -538,44 +559,43 @@ public class Repository {
if (item.equals("tree")) {
ref = mapObject(refId, null);
while (ref instanceof Tag) {
- Tag t = (Tag)ref;
+ Tag t = (Tag) ref;
refId = t.getObjId();
ref = mapObject(refId, null);
}
if (ref instanceof Treeish)
- refId = ((Treeish)ref).getTreeId();
+ refId = ((Treeish) ref).getTreeId();
else
- throw new IncorrectObjectTypeException(refId, Constants.TYPE_TREE);
- }
- else if (item.equals("commit")) {
+ throw new IncorrectObjectTypeException(
+ refId, Constants.TYPE_TREE);
+ } else if (item.equals("commit")) {
ref = mapObject(refId, null);
while (ref instanceof Tag) {
- Tag t = (Tag)ref;
+ Tag t = (Tag) ref;
refId = t.getObjId();
ref = mapObject(refId, null);
}
if (!(ref instanceof Commit))
- throw new IncorrectObjectTypeException(refId, Constants.TYPE_COMMIT);
- }
- else if (item.equals("blob")) {
+ throw new IncorrectObjectTypeException(
+ refId, Constants.TYPE_COMMIT);
+ } else if (item.equals("blob")) {
ref = mapObject(refId, null);
while (ref instanceof Tag) {
- Tag t = (Tag)ref;
+ Tag t = (Tag) ref;
refId = t.getObjId();
ref = mapObject(refId, null);
}
if (!(ref instanceof byte[]))
- throw new IncorrectObjectTypeException(refId, Constants.TYPE_COMMIT);
- }
- else if (item.equals("")) {
+ throw new IncorrectObjectTypeException(
+ refId, Constants.TYPE_COMMIT);
+ } else if (item.equals("")) {
ref = mapObject(refId, null);
if (ref instanceof Tag)
- refId = ((Tag)ref).getObjId();
+ refId = ((Tag) ref).getObjId();
else {
// self
}
- }
- else
+ } else
throw new RevisionSyntaxException(revstr);
else
throw new RevisionSyntaxException(revstr);
@@ -583,22 +603,24 @@ public class Repository {
default:
ref = mapObject(refId, null);
if (ref instanceof Commit)
- refId = ((Commit)ref).getParentIds()[0];
+ refId = ((Commit) ref).getParentIds()[0];
else
- throw new IncorrectObjectTypeException(refId, Constants.TYPE_COMMIT);
-
+ throw new IncorrectObjectTypeException(refId,
+ Constants.TYPE_COMMIT);
+
}
} else {
ref = mapObject(refId, null);
if (ref instanceof Commit)
- refId = ((Commit)ref).getParentIds()[0];
+ refId = ((Commit) ref).getParentIds()[0];
else
- throw new IncorrectObjectTypeException(refId, Constants.TYPE_COMMIT);
+ throw new IncorrectObjectTypeException(refId,
+ Constants.TYPE_COMMIT);
}
break;
case '~':
if (ref == null) {
- String refstr = new String(rev,0,i);
+ String refstr = new String(rev, 0, i);
refId = resolveSimple(refstr);
ref = mapCommit(refId);
}
@@ -607,10 +629,10 @@ public class Repository {
if (!Character.isDigit(rev[l]))
break;
}
- String distnum = new String(rev, i+1, l-i-1);
+ String distnum = new String(rev, i + 1, l - i - 1);
int dist = Integer.parseInt(distnum);
while (dist >= 0) {
- refId = ((Commit)ref).getParentIds()[0];
+ refId = ((Commit) ref).getParentIds()[0];
ref = mapCommit(refId);
--dist;
}
@@ -619,14 +641,16 @@ public class Repository {
case '@':
int m;
String time = null;
- for (m=i+2; m<rev.length; ++m) {
+ for (m = i + 2; m < rev.length; ++m) {
if (rev[m] == '}') {
- time = new String(rev, i+2, m-i-2);
+ time = new String(rev, i + 2, m - i - 2);
break;
}
}
if (time != null)
- throw new RevisionSyntaxException("reflogs not yet supported by revision parser yet", revstr);
+ throw new RevisionSyntaxException(
+ "reflogs not yet supported by revision parser yet",
+ revstr);
i = m - 1;
break;
default:
@@ -662,7 +686,7 @@ public class Repository {
/**
* Add a single existing pack to the list of available pack files.
- *
+ *
* @param pack
* path of the pack file to open.
* @param idx
@@ -675,10 +699,10 @@ public class Repository {
final String p = pack.getName();
final String i = idx.getName();
if (p.length() != 50 || !p.startsWith("pack-") || !p.endsWith(".pack"))
- throw new IllegalArgumentException("Not a valid pack " + pack);
+ throw new IllegalArgumentException("Not a valid pack " + pack);
if (i.length() != 49 || !i.startsWith("pack-") || !i.endsWith(".idx"))
- throw new IllegalArgumentException("Not a valid pack " + idx);
- if (!p.substring(0,45).equals(i.substring(0,45)))
+ throw new IllegalArgumentException("Not a valid pack " + idx);
+ if (!p.substring(0, 45).equals(i.substring(0, 45)))
throw new IllegalArgumentException("Pack " + pack
+ "does not match index " + idx);
@@ -690,12 +714,11 @@ public class Repository {
}
/**
- * Scan the object dirs, including alternates for packs
- * to use.
+ * Scan the object dirs, including alternates for packs to use.
*/
public void scanForPacks() {
final ArrayList<PackFile> p = new ArrayList<PackFile>();
- for (int i=0; i<objectsDirs.length; ++i)
+ for (int i = 0; i < objectsDirs.length; ++i)
scanForPacks(new File(objectsDirs[i], "pack"), p);
final PackFile[] arr = new PackFile[p.size()];
p.toArray(arr);
@@ -735,14 +758,16 @@ public class Repository {
}
}
- /**
- * Writes a symref (e.g. HEAD) to disk
- *
- * @param name symref name
- * @param target pointed to ref
- * @throws IOException
- */
- public void writeSymref(final String name, final String target)
+ /**
+ * Writes a symref (e.g. HEAD) to disk
+ *
+ * @param name
+ * symref name
+ * @param target
+ * pointed to ref
+ * @throws IOException
+ */
+ public void writeSymref(final String name, final String target)
throws IOException {
refs.link(name, target);
}
@@ -756,12 +781,13 @@ public class Repository {
* @throws IOException
*/
public String getPatch() throws IOException {
- final File ptr = new File(getDirectory(),"patches/"+getBranch()+"/applied");
+ final File ptr = new File(getDirectory(), "patches/" + getBranch()
+ + "/applied");
final BufferedReader br = new BufferedReader(new FileReader(ptr));
- String last=null;
+ String last = null;
try {
String line;
- while ((line=br.readLine())!=null) {
+ while ((line = br.readLine()) != null) {
last = line;
}
} finally {
@@ -775,7 +801,7 @@ public class Repository {
* @throws IOException
*/
public String getFullBranch() throws IOException {
- final File ptr = new File(getDirectory(),"HEAD");
+ final File ptr = new File(getDirectory(), "HEAD");
final BufferedReader br = new BufferedReader(new FileReader(ptr));
String ref;
try {
@@ -787,7 +813,7 @@ public class Repository {
ref = ref.substring(5);
return ref;
}
-
+
/**
* @return name of current branch.
* @throws IOException
@@ -808,7 +834,7 @@ public class Repository {
ref = ref.substring(11);
return ref;
} catch (FileNotFoundException e) {
- final File ptr = new File(getDirectory(),"head-name");
+ final File ptr = new File(getDirectory(), "head-name");
final BufferedReader br = new BufferedReader(new FileReader(ptr));
String ref;
try {
@@ -819,7 +845,7 @@ public class Repository {
return ref;
}
}
-
+
/**
* @return all known refs (heads, tags, remotes).
*/
@@ -864,13 +890,17 @@ public class Repository {
* @return applied patches in a map indexed on current commit id
* @throws IOException
*/
- public Map<ObjectId,StGitPatch> getAppliedPatches() throws IOException {
- Map<ObjectId,StGitPatch> ret = new HashMap<ObjectId,StGitPatch>();
+ public Map<ObjectId, StGitPatch> getAppliedPatches() throws IOException {
+ Map<ObjectId, StGitPatch> ret = new HashMap<ObjectId, StGitPatch>();
if (isStGitMode()) {
- File patchDir = new File(new File(getDirectory(),"patches"),getBranch());
- BufferedReader apr = new BufferedReader(new FileReader(new File(patchDir,"applied")));
- for (String patchName=apr.readLine(); patchName!=null; patchName=apr.readLine()) {
- File topFile = new File(new File(new File(patchDir,"patches"), patchName), "top");
+ File patchDir = new File(new File(getDirectory(), "patches"),
+ getBranch());
+ BufferedReader apr = new BufferedReader(new FileReader(new File(
+ patchDir, "applied")));
+ for (String patchName = apr.readLine(); patchName != null; patchName = apr
+ .readLine()) {
+ File topFile = new File(new File(new File(patchDir, "patches"),
+ patchName), "top");
BufferedReader tfr = new BufferedReader(new FileReader(topFile));
String objectId = tfr.readLine();
ObjectId id = ObjectId.fromString(objectId);
@@ -881,7 +911,7 @@ public class Repository {
}
return ret;
}
-
+
/** Clean up stale caches */
public void refreshFromDisk() {
refs.clearCache();
@@ -904,7 +934,7 @@ public class Repository {
static byte[] gitInternalSlash(byte[] bytes) {
if (File.separatorChar == '/')
return bytes;
- for (int i=0; i<bytes.length; ++i)
+ for (int i = 0; i < bytes.length; ++i)
if (bytes[i] == File.separatorChar)
bytes[i] = '/';
return bytes;
@@ -916,18 +946,18 @@ public class Repository {
public RepositoryState getRepositoryState() {
if (new File(getWorkDir(), ".dotest").exists())
return RepositoryState.REBASING;
- if (new File(gitDir,".dotest-merge").exists())
+ if (new File(gitDir, ".dotest-merge").exists())
return RepositoryState.REBASING_INTERACTIVE;
- if (new File(gitDir,"MERGE_HEAD").exists())
+ if (new File(gitDir, "MERGE_HEAD").exists())
return RepositoryState.MERGING;
- if (new File(gitDir,"BISECT_LOG").exists())
+ if (new File(gitDir, "BISECT_LOG").exists())
return RepositoryState.BISECTING;
return RepositoryState.SAFE;
}
/**
- * Check validty of a ref name. It must not contain character that has
- * a special meaning in a Git object reference expression. Some other
+ * Check validty of a ref name. It must not contain character that has a
+ * special meaning in a Git object reference expression. Some other
* dangerous characters are also excluded.
*
* @param refName
@@ -937,11 +967,11 @@ public class Repository {
public static boolean isValidRefName(final String refName) {
final int len = refName.length();
char p = '\0';
- for (int i=0; i<len; ++i) {
+ for (int i = 0; i < len; ++i) {
char c = refName.charAt(i);
if (c <= ' ')
return false;
- switch(c) {
+ switch (c) {
case '.':
if (i == 0)
return false;
@@ -953,11 +983,14 @@ public class Repository {
case '/':
if (i == 0)
return false;
- if (i == len -1)
+ if (i == len - 1)
return false;
break;
- case '~': case '^': case ':':
- case '?': case '[':
+ case '~':
+ case '^':
+ case ':':
+ case '?':
+ case '[':
return false;
case '*':
return false;
@@ -970,8 +1003,10 @@ public class Repository {
/**
* String work dir and return normalized repository path
*
- * @param wd Work dir
- * @param f File whose path shall be stripp off it's workdir
+ * @param wd
+ * Work dir
+ * @param f
+ * File whose path shall be stripp off it's workdir
* @return normalized repository relative path
*/
public static String stripWorkDir(File wd, File f) {
@@ -989,7 +1024,7 @@ public class Repository {
/**
* Creates a new {@link WorkTree} and initialize a repository for it.
- *
+ *
* @param workTreeDirectory
* the directory with the project files.
* @return a new {@link WorkTree} with a new and open {@link Repository}.
--
1.5.4.3
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [JGIT PATCH v4 21/24] Added findWorkTree method to Repository class.
2008-06-13 18:30 [JGIT PATCH v4] Implementation of fnmatch and the ignore rules Florian Köberle
` (19 preceding siblings ...)
2008-06-13 18:35 ` [JGIT PATCH v4 20/24] Formatted Repository class Florian Koeberle
@ 2008-06-13 18:35 ` Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 22/24] Formatted RepositoryTestCase Florian Koeberle
` (3 subsequent siblings)
24 siblings, 0 replies; 26+ messages in thread
From: Florian Koeberle @ 2008-06-13 18:35 UTC (permalink / raw)
To: git; +Cc: Florian Koeberle
Signed-off-by: Florian Koeberle <florianskarten@web.de>
---
.../src/org/spearce/jgit/lib/Repository.java | 80 ++++++++++++++++++++
1 files changed, 80 insertions(+), 0 deletions(-)
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java b/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java
index d7c3b13..ee7bbe4 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java
@@ -39,6 +39,11 @@
package org.spearce.jgit.lib;
+import static org.spearce.jgit.lib.Constants.HEAD;
+import static org.spearce.jgit.lib.Constants.OBJECTS_DIRECTORY_NAME;
+import static org.spearce.jgit.lib.Constants.REFS_DIRECTORY_NAME;
+import static org.spearce.jgit.lib.Constants.REPOSITORY_DIRECTORY_NAME;
+
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
@@ -51,6 +56,7 @@ import java.util.HashMap;
import java.util.Map;
import org.spearce.jgit.errors.IncorrectObjectTypeException;
+import org.spearce.jgit.errors.NoGitRepositoryFoundException;
import org.spearce.jgit.errors.RevisionSyntaxException;
import org.spearce.jgit.stgit.StGitPatch;
import org.spearce.jgit.util.FS;
@@ -1052,4 +1058,78 @@ public class Repository {
}
}
+ /**
+ * Find the git repository for the current working directory.
+ *
+ * @return a {@link Repository}.
+ * @throws NoGitRepositoryFoundException
+ * if no git repository could be found for the current
+ * directory.
+ * @throws IOException
+ * if not able to determine the absolute path of the current
+ * working directory.
+ */
+ public static WorkTree findWorkTree() throws NoGitRepositoryFoundException,
+ IOException {
+ return findWorkTree(new File("."));
+ }
+
+ /**
+ * Checks if a path is a valid git repository. Works similar like the method
+ * is_git_directory from the original setup.c file.
+ *
+ * @param directory
+ * the path which should be checked.
+ * @return true if the path is a valid git repository.
+ */
+ private static boolean isRepository(File directory) {
+ if (!directory.isDirectory()) {
+ return false;
+ }
+
+ final File objectDirectory = new File(directory, OBJECTS_DIRECTORY_NAME);
+ if (!objectDirectory.isDirectory()) {
+ return false;
+ }
+
+ final File refsDirectory = new File(directory, REFS_DIRECTORY_NAME);
+ if (!refsDirectory.isDirectory()) {
+ return false;
+ }
+
+ final File head = new File(directory, HEAD);
+ if (!hasValidateHeadRef(head)) {
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * Checks for example if a path is a valid HEAD file.
+ *
+ * @param path
+ * is the path of the HEAD file.
+ * @return true if it has a valid head reference.
+ */
+ private static final boolean hasValidateHeadRef(File path) {
+ return true; // TODO implement this method
+ }
+
+ private static WorkTree findWorkTree(final File directory)
+ throws IOException, NoGitRepositoryFoundException {
+ File currentDirectory = directory.getAbsoluteFile();
+ while (currentDirectory != null) {
+ final File commonGitDirectory = new File(directory,
+ REPOSITORY_DIRECTORY_NAME);
+ if (isRepository(commonGitDirectory))
+ return new WorkTree(currentDirectory, new Repository(
+ commonGitDirectory));
+ if (isRepository(currentDirectory))
+ return new WorkTree(null, new Repository(currentDirectory));
+ currentDirectory = currentDirectory.getParentFile();
+ }
+ throw new NoGitRepositoryFoundException(directory);
+ }
+
}
--
1.5.4.3
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [JGIT PATCH v4 22/24] Formatted RepositoryTestCase.
2008-06-13 18:30 [JGIT PATCH v4] Implementation of fnmatch and the ignore rules Florian Köberle
` (20 preceding siblings ...)
2008-06-13 18:35 ` [JGIT PATCH v4 21/24] Added findWorkTree method to " Florian Koeberle
@ 2008-06-13 18:35 ` Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 23/24] Added a super class for RepositoryTestCase with a createTemporaryDirectory() method Florian Koeberle
` (2 subsequent siblings)
24 siblings, 0 replies; 26+ messages in thread
From: Florian Koeberle @ 2008-06-13 18:35 UTC (permalink / raw)
To: git; +Cc: Florian Koeberle
Signed-off-by: Florian Koeberle <florianskarten@web.de>
---
.../org/spearce/jgit/lib/RepositoryTestCase.java | 22 ++++++++++---------
1 files changed, 12 insertions(+), 10 deletions(-)
diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java
index 14e7179..aae9ed6 100644
--- a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java
@@ -113,8 +113,8 @@ public abstract class RepositoryTestCase extends TestCase {
throws IOException {
Reader r = new InputStreamReader(new FileInputStream(f), "ISO-8859-1");
char[] data = new char[(int) f.length()];
- if (f.length() != r.read(data))
- throw new IOException("Internal error reading file data from "+f);
+ if (f.length() != r.read(data))
+ throw new IOException("Internal error reading file data from " + f);
assertEquals(checkData, new String(data));
}
@@ -123,7 +123,7 @@ public abstract class RepositoryTestCase extends TestCase {
public void setUp() throws Exception {
super.setUp();
recursiveDelete(trashParent);
- trash = new File(trashParent,"trash"+System.currentTimeMillis());
+ trash = new File(trashParent, "trash" + System.currentTimeMillis());
trash_git = new File(trash, ".git");
Runtime.getRuntime().addShutdownHook(new Thread() {
@@ -140,8 +140,7 @@ public abstract class RepositoryTestCase extends TestCase {
"pack-34be9032ac282b11fa9babdc2b2a93ca996c9c2f",
"pack-df2982f284bbabb6bdb59ee3fcc6eb0983e20371",
"pack-9fb5b411fe6dfa89cc2e6b89d2bd8e5de02b5745",
- "pack-e6d07037cbcf13376308a0a995d1fa48f8f76aaa"
- };
+ "pack-e6d07037cbcf13376308a0a995d1fa48f8f76aaa" };
final File tst = new File("tst");
final File packDir = new File(db.getObjectsDirectory(), "pack");
for (int k = 0; k < packs.length; k++) {
@@ -151,7 +150,8 @@ public abstract class RepositoryTestCase extends TestCase {
packs[k] + ".idx"));
}
- copyFile(new File(tst, "packed-refs"), new File(trash_git,"packed-refs"));
+ copyFile(new File(tst, "packed-refs"), new File(trash_git,
+ "packed-refs"));
db.scanForPacks();
}
@@ -163,15 +163,17 @@ public abstract class RepositoryTestCase extends TestCase {
/**
* Helper for creating extra empty repos
- *
+ *
* @return a new empty git repository for testing purposes
- *
+ *
* @throws IOException
*/
protected Repository createNewEmptyRepo() throws IOException {
- File newTestRepo = new File(trashParent, "new"+System.currentTimeMillis()+"/.git");
+ File newTestRepo = new File(trashParent, "new"
+ + System.currentTimeMillis() + "/.git");
assertFalse(newTestRepo.exists());
- File unusedDir = new File(trashParent, "tmp"+System.currentTimeMillis());
+ File unusedDir = new File(trashParent, "tmp"
+ + System.currentTimeMillis());
assertTrue(unusedDir.mkdirs());
final Repository newRepo = new Repository(newTestRepo);
newRepo.create();
--
1.5.4.3
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [JGIT PATCH v4 23/24] Added a super class for RepositoryTestCase with a createTemporaryDirectory() method.
2008-06-13 18:30 [JGIT PATCH v4] Implementation of fnmatch and the ignore rules Florian Köberle
` (21 preceding siblings ...)
2008-06-13 18:35 ` [JGIT PATCH v4 22/24] Formatted RepositoryTestCase Florian Koeberle
@ 2008-06-13 18:35 ` Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 24/24] Added the test class AddCommandIterationTest Florian Koeberle
2008-06-18 6:43 ` [JGIT PATCH v4] Implementation of fnmatch and the ignore rules Robin Rosenberg
24 siblings, 0 replies; 26+ messages in thread
From: Florian Koeberle @ 2008-06-13 18:35 UTC (permalink / raw)
To: git; +Cc: Florian Koeberle
Signed-off-by: Florian Koeberle <florianskarten@web.de>
---
.../tst/org/spearce/jgit/lib/AbstractTestCase.java | 108 ++++++++++++++++++++
.../org/spearce/jgit/lib/RepositoryTestCase.java | 22 +----
2 files changed, 109 insertions(+), 21 deletions(-)
create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/lib/AbstractTestCase.java
diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/AbstractTestCase.java b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/AbstractTestCase.java
new file mode 100644
index 0000000..c017fc0
--- /dev/null
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/AbstractTestCase.java
@@ -0,0 +1,108 @@
+/*
+ * Copyright (C) 2006, Shawn Pearce <spearce@spearce.org>
+ * Copyright (C) 2008, Florian Köberle <florianskarten@web.de>
+ *
+ * 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 Git Development Community 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.spearce.jgit.lib;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Stack;
+
+import junit.framework.TestCase;
+
+/**
+ * This class can be used as base testcase
+ *
+ */
+public abstract class AbstractTestCase extends TestCase {
+ private final Stack<File> directoriesToDelete = new Stack<File>();
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ // Do not delete directories from other test runs
+ // in order to avoid side effects between tests.
+ directoriesToDelete.clear();
+ }
+
+ /**
+ * Creates a new directory for test purposes. The directory and it's content
+ * will be deleted when the test is over.
+ *
+ * @return a temporary directory for the use in one test.
+ * @throws IOException
+ * if the creation of the temporary directory fails.
+ */
+ protected File createTemporaryDirectory() throws IOException {
+ File directory = null;
+ do {
+ directory = File.createTempFile("test", "");
+ if (!directory.delete())
+ throw new IOException("can't delete temporary file");
+
+ if (!directory.mkdir())
+ directory = null;
+
+ } while (directory == null);
+ return directory;
+ }
+
+ protected static void recursiveDelete(final File dir) {
+ final File[] ls = dir.listFiles();
+ if (ls != null) {
+ for (int k = 0; k < ls.length; k++) {
+ final File e = ls[k];
+ if (e.isDirectory()) {
+ recursiveDelete(e);
+ } else {
+ e.delete();
+ }
+ }
+ }
+ dir.delete();
+ if (dir.exists()) {
+ System.out.println("Warning: Failed to delete " + dir);
+ }
+ }
+
+ protected void tearDown() throws Exception {
+ while (!directoriesToDelete.isEmpty()) {
+ final File directory = directoriesToDelete.pop();
+ recursiveDelete(directory);
+ }
+ super.tearDown();
+ }
+}
diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java
index aae9ed6..9719066 100644
--- a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java
@@ -46,9 +46,7 @@ import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
-import junit.framework.TestCase;
-
-public abstract class RepositoryTestCase extends TestCase {
+public abstract class RepositoryTestCase extends AbstractTestCase {
protected final File trashParent = new File("trash");
@@ -65,24 +63,6 @@ public abstract class RepositoryTestCase extends TestCase {
jcommitter = new PersonIdent("J. Committer", "jcommitter@example.com");
}
- protected static void recursiveDelete(final File dir) {
- final File[] ls = dir.listFiles();
- if (ls != null) {
- for (int k = 0; k < ls.length; k++) {
- final File e = ls[k];
- if (e.isDirectory()) {
- recursiveDelete(e);
- } else {
- e.delete();
- }
- }
- }
- dir.delete();
- if (dir.exists()) {
- System.out.println("Warning: Failed to delete " + dir);
- }
- }
-
protected static void copyFile(final File src, final File dst)
throws IOException {
final FileInputStream fis = new FileInputStream(src);
--
1.5.4.3
^ permalink raw reply related [flat|nested] 26+ messages in thread
* [JGIT PATCH v4 24/24] Added the test class AddCommandIterationTest.
2008-06-13 18:30 [JGIT PATCH v4] Implementation of fnmatch and the ignore rules Florian Köberle
` (22 preceding siblings ...)
2008-06-13 18:35 ` [JGIT PATCH v4 23/24] Added a super class for RepositoryTestCase with a createTemporaryDirectory() method Florian Koeberle
@ 2008-06-13 18:35 ` Florian Koeberle
2008-06-18 6:43 ` [JGIT PATCH v4] Implementation of fnmatch and the ignore rules Robin Rosenberg
24 siblings, 0 replies; 26+ messages in thread
From: Florian Koeberle @ 2008-06-13 18:35 UTC (permalink / raw)
To: git; +Cc: Florian Koeberle
Signed-off-by: Florian Koeberle <florianskarten@web.de>
---
.../treewalk/rules/AddCommandIterationTest.java | 339 ++++++++++++++++++++
1 files changed, 339 insertions(+), 0 deletions(-)
create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/treewalk/rules/AddCommandIterationTest.java
diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/treewalk/rules/AddCommandIterationTest.java b/org.spearce.jgit.test/tst/org/spearce/jgit/treewalk/rules/AddCommandIterationTest.java
new file mode 100644
index 0000000..a17fc18
--- /dev/null
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/treewalk/rules/AddCommandIterationTest.java
@@ -0,0 +1,339 @@
+/*
+ * Copyright (C) 2008, Florian Köberle <florianskarten@web.de>
+ *
+ * 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 Git Development Community 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.spearce.jgit.treewalk.rules;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Set;
+
+import org.spearce.jgit.errors.InvalidPatternException;
+import org.spearce.jgit.errors.PathNotInProjectDirectoryException;
+import org.spearce.jgit.lib.AbstractTestCase;
+import org.spearce.jgit.lib.Repository;
+import org.spearce.jgit.lib.WorkTree;
+import org.spearce.jgit.treewalk.LightFileTreeIterable;
+
+public class AddCommandIterationTest extends AbstractTestCase {
+
+ private WorkTree workTree;
+
+ private AddRulesFactory factory;
+
+ private Iterable<File> createIterable(File workingDirectory,
+ List<String> filePatternsOfAddCommand)
+ throws PathNotInProjectDirectoryException, InvalidPatternException,
+ IOException {
+ final Rules rules = factory.createRules(workTree.getDirectory(),
+ workingDirectory, filePatternsOfAddCommand);
+ return new LightFileTreeIterable(workTree.getDirectory(), rules, false);
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ final File projectDirectory = createTemporaryDirectory();
+ this.workTree = Repository.createWorkTree(projectDirectory);
+ this.factory = new AddRulesFactory();
+ }
+
+ public void testNoPattern() throws Exception {
+ createFile("a.txt");
+ final Iterable<File> iterable = createIterable(workTree.getDirectory(),
+ Collections.<String> emptyList());
+ final Set<File> expectedPaths = Collections.emptySet();
+ assertIsValidIterable(iterable);
+ assertIterableReturnsSet(expectedPaths, iterable);
+ }
+
+ public void testTreePattern1() throws Exception {
+ final Set<File> expectedFiles = new HashSet<File>();
+ createFile("a.txt");
+ expectedFiles.add(createFile("a", "a.txt"));
+ expectedFiles.add(createFile("a", "a", "a.txt"));
+ expectedFiles.add(createFile("a", "b", "a.txt"));
+ createFile("a", "b", "a.c");
+
+ final File directoryA = new File(workTree.getDirectory(), "a");
+
+ final Iterable<File> iterable = createIterable(directoryA, Arrays
+ .asList("*.txt"));
+ assertIsValidIterable(iterable);
+ assertIterableReturnsSet(expectedFiles, iterable);
+ }
+
+ public void testTreePattern2() throws Exception {
+ final Set<File> expectedFiles = new HashSet<File>();
+ createFile("a.txt");
+ expectedFiles.add(createFile("a", "a.txt"));
+ expectedFiles.add(createFile("a", "a", "a.txt"));
+ expectedFiles.add(createFile("a", "b", "a.txt"));
+ createFile("a", "b", "a.c");
+
+ final Iterable<File> iterable = createIterable(workTree.getDirectory(),
+ Arrays.asList("a" + File.separator + "*.txt"));
+ assertIsValidIterable(iterable);
+ assertIterableReturnsSet(expectedFiles, iterable);
+ }
+
+ public void testSelectCompleteSubdirectory() throws Exception {
+ final Set<File> expectedPaths = new HashSet<File>();
+ final File directoryA = new File(workTree.getDirectory(), "a");
+ final File directoryAA = new File(directoryA, "a");
+ final File directoryAB = new File(directoryA, "b");
+ createFile("a.txt");
+ expectedPaths.add(directoryA);
+ expectedPaths.add(directoryAA);
+ expectedPaths.add(createFile("a", "a.txt"));
+ expectedPaths.add(createFile("a", "a", "a.txt"));
+ expectedPaths.add(directoryAB);
+ expectedPaths.add(createFile("a", "b", "a.txt"));
+ expectedPaths.add(createFile("a", "b", "a.c"));
+
+ final Iterable<File> iterable = createIterable(workTree.getDirectory(),
+ Arrays.asList("a"));
+
+ assertIsValidIterable(iterable);
+ assertIterableReturnsSet(expectedPaths, iterable);
+ }
+
+ public void testSelectTwoSubdirectories() throws Exception {
+ final Set<File> expectedPaths = new HashSet<File>();
+ final File directoryA = new File(workTree.getDirectory(), "a");
+ final File directoryAA = new File(directoryA, "a");
+ final File directoryAB = new File(directoryA, "b");
+ createFile("a.txt");
+ createFile("a", "a.txt");
+ expectedPaths.add(directoryAA);
+ expectedPaths.add(createFile("a", "a", "a.txt"));
+ expectedPaths.add(directoryAB);
+ expectedPaths.add(createFile("a", "b", "a.txt"));
+ expectedPaths.add(createFile("a", "b", "a.c"));
+
+ final List<String> patternList = new ArrayList<String>(2);
+ patternList.add("a" + File.separator + "a");
+ patternList.add("a" + File.separator + "b");
+ final Iterable<File> iterable = createIterable(workTree.getDirectory(),
+ patternList);
+
+ assertIsValidIterable(iterable);
+ assertIterableReturnsSet(expectedPaths, iterable);
+ }
+
+ public void testTwoDifferentSelects() throws Exception {
+ final Set<File> expectedPaths = new HashSet<File>();
+ final File directoryA = new File(workTree.getDirectory(), "a");
+ final File directoryAA = new File(directoryA, "a");
+ createFile("a.txt");
+ createFile("a", "a.txt");
+ expectedPaths.add(directoryAA);
+ expectedPaths.add(createFile("a", "a", "a.txt"));
+ expectedPaths.add(createFile("a", "b", "a.txt"));
+ expectedPaths.add(createFile("a", "b", "c", "a.txt"));
+ createFile("a", "b", "a.c");
+
+ final List<String> patternList = new ArrayList<String>(2);
+ patternList.add("a" + File.separator + "a");
+ patternList.add("a" + File.separator + "b" + File.separator + "*.txt");
+
+ final Iterable<File> iterable = createIterable(workTree.getDirectory(),
+ patternList);
+
+ assertIsValidIterable(iterable);
+ assertIterableReturnsSet(expectedPaths, iterable);
+ }
+
+ public void testRealisticExample() throws Exception {
+ final Set<File> expectedPaths = new HashSet<File>();
+
+ // write the .gitignore file
+ final File dotGitIgnoreFile = createFile(".gitignore");
+ final PrintWriter dotGitIgnoreFilePrinter = new PrintWriter(
+ dotGitIgnoreFile);
+ try {
+ dotGitIgnoreFilePrinter.println("/alpha/config.xml");
+ dotGitIgnoreFilePrinter.println("*.class");
+ dotGitIgnoreFilePrinter.println("!/alpha/test/ressources/");
+ dotGitIgnoreFilePrinter.println("*~");
+ } finally {
+ dotGitIgnoreFilePrinter.close();
+ }
+
+ // write the .git/info/exclude file
+ final File repositoryDirectory = workTree.getRepository()
+ .getDirectory();
+ final File infoDirectory = new File(repositoryDirectory, "info");
+ infoDirectory.mkdir();
+ final File infoExcludeFile = new File(infoDirectory, "exclude");
+ final PrintWriter infoExcludeFilePrinter = new PrintWriter(
+ infoExcludeFile);
+ try {
+ infoExcludeFilePrinter.println("/alpha/test/ressources/mytest.txt");
+ } finally {
+ infoExcludeFilePrinter.close();
+ }
+
+ createFile("alpha", "config.xml");
+ expectedPaths.add(createFile("alpha", "src", "Main.java"));
+ createFile("alpha", "src", "Main.class");
+ expectedPaths.add(createFile("alpha", "test", "ressources",
+ "Example.class"));
+ expectedPaths
+ .add(createFile("alpha", "test", "ressources", "input.txt"));
+ createFile("alpha", "test", "ressources", "input.txt~");
+ createFile("alpha", "test", "ressources", "mytest.txt");
+
+ final File alphaDirectory = new File(workTree.getDirectory(), "alpha");
+ final File srcDirectory = new File(alphaDirectory, "src");
+ final File testDirectory = new File(alphaDirectory, "test");
+ final File ressources = new File(testDirectory, "ressources");
+
+ expectedPaths.add(alphaDirectory);
+ expectedPaths.add(srcDirectory);
+ expectedPaths.add(testDirectory);
+ expectedPaths.add(ressources);
+
+ final List<String> patternList = new ArrayList<String>(2);
+ patternList.add("alpha");
+
+ final Iterable<File> iterable = createIterable(workTree.getDirectory(),
+ patternList);
+
+ assertIsValidIterable(iterable);
+ assertIterableReturnsSet(expectedPaths, iterable);
+ }
+
+ public void testSingleFile() throws Exception {
+ createFile("a.txt");
+ createFile("a", "a.txt");
+ createFile("a", "a", "a.txt");
+ final File expectedFile = createFile("a", "b", "a.txt");
+ createFile("a", "b", "a.c");
+
+ final String pattern = "a" + File.separator + "b" + File.separator
+ + "a.txt";
+ final Iterable<File> iterable = createIterable(workTree.getDirectory(),
+ Arrays.asList(pattern));
+ final Set<File> expectedPaths = Collections.singleton(expectedFile);
+ assertIsValidIterable(iterable);
+ assertIterableReturnsSet(expectedPaths, iterable);
+ }
+
+ /**
+ * Tests if the specified {@link Iterable} returns the specified set of
+ * {@link File}s. The assertion will fail if the {@link Iterable} returns
+ * to much, to less files. It will also fail if the {@link Iterable} returns
+ * a file twice.
+ *
+ * @param expectedContent
+ * the expected set of files.
+ * @param iterable
+ * the {@link Iterable} to test.
+ */
+ private void assertIterableReturnsSet(Set<File> expectedContent,
+ Iterable<File> iterable) {
+ final Set<File> returnedFiles = new HashSet<File>();
+ final List<File> doubleReturnedFiles = new ArrayList<File>();
+
+ final Iterator<File> iterator = iterable.iterator();
+ while (iterator.hasNext()) {
+ final File file = iterator.next();
+ if (!returnedFiles.add(file)) {
+ doubleReturnedFiles.add(file);
+ }
+ }
+ final Set<File> missingFiles = new HashSet<File>();
+ for (File file : expectedContent) {
+ if (!returnedFiles.contains(file)) {
+ missingFiles.add(file);
+ }
+ }
+ if (!missingFiles.isEmpty()) {
+ fail(String.format("missing paths: %s", missingFiles));
+ }
+
+ final Set<File> unexpectedFiles = new HashSet<File>();
+ for (File file : returnedFiles) {
+ if (!expectedContent.contains(file)) {
+ unexpectedFiles.add(file);
+ }
+ }
+ if (!unexpectedFiles.isEmpty()) {
+ fail(String.format("unexpected paths: %s", unexpectedFiles));
+ }
+
+ if (!doubleReturnedFiles.isEmpty()) {
+ fail(String.format("multiple times returned paths: %s",
+ doubleReturnedFiles));
+ }
+
+ }
+
+ private static void assertIsValidIterable(Iterable<File> iterable) {
+ final Iterator<File> iterator = iterable.iterator();
+ while (iterator.hasNext()) {
+ iterator.next();
+ }
+ try {
+ iterator.next();
+ fail();
+ } catch (NoSuchElementException e) {
+ // expected
+ }
+ }
+
+ private File createFile(String... path) throws IOException {
+ File file = workTree.getDirectory();
+ for (int i = 0; i < path.length; i++) {
+ file = new File(file, path[i]);
+ if (i == path.length - 1) {
+ file.getParentFile().mkdirs();
+ file.createNewFile();
+ break;
+ }
+ }
+ return file;
+ }
+}
--
1.5.4.3
^ permalink raw reply related [flat|nested] 26+ messages in thread
* Re: [JGIT PATCH v4] Implementation of fnmatch and the ignore rules
2008-06-13 18:30 [JGIT PATCH v4] Implementation of fnmatch and the ignore rules Florian Köberle
` (23 preceding siblings ...)
2008-06-13 18:35 ` [JGIT PATCH v4 24/24] Added the test class AddCommandIterationTest Florian Koeberle
@ 2008-06-18 6:43 ` Robin Rosenberg
24 siblings, 0 replies; 26+ messages in thread
From: Robin Rosenberg @ 2008-06-18 6:43 UTC (permalink / raw)
To: Florian Köberle; +Cc: git, Shawn O. Pearce
fredagen den 13 juni 2008 20.30.34 skrev Florian Köberle:
> Hi
>
> Thanks for the suggestions and comments, the new patches are available here:
> http://repo.or.cz/w/egit/florian.git?a=shortlog;h=refs/heads/mailinglist-patches-4
>
> I will send them as a reply to this email soon.
>
>
> The most noticeable change is that I improved the fnmatch functionality.
>
> All character classes of the type [:class:] work now.
> Other character classes, including the character classes [=class=] and
> [.class.] will cause a InvalidPatternException. The last two aren't
> supported at my bash neighter, but are defined in the POSIX standard.
>
> Two other implementation differences are:
>
> The bash shell doesn't support the full range of digits, but my
> implementatiton does. e.g. ۹ (U+06F9 EXTENDED ARABIC-INDIC DIGIT NINE)
Interesting. I'm not sure what to say here. Full compatibility with Git or "better"?
> A pattern like [[:] results in a InvalidPatternException in my
> implementation. The shell has there a strange behavior: if the files "["
> and ":" exists then "ls [[:]" shows only ":".
Having : in a filename is asking for trouble. I think the parser thinks [[: as
in [[:alpha:]] and break the parsing somhow. This is possibly even undefined,
so I think you should disallow this. [[\:] works in bash btw.
> The other main change is that I introduced a AbstractTestCase class
> which which offers functionality to create temporary folders.
> RepositoryTestCase extends now from this new class.
>
> An other patch reducing the duplication between AbstractTestCase and
> RepositoryTestCase class will follow soon, if you like the change.
Not trying to clean up got me into trouble with tons and tons of garbage, that
is why I placed new repos in a directory and try to delete it if possible. That
ofcourse excludes parallell runs. Anyone with a good idea on how to
really handle this?
> I also updated all license statements to the 3-clause BSD.
>
> Even if you don't like the last change (the AbstractTestCase change) it
> would be cool if you would accept at least some patches.
The patch constituents are somwehat loose. Would this rebase do?
pick e331b13 Added the package fnmatch and two exceptions.
squash 64d8aa2 Added the interface FilePattern.
squash ae7915f Added the class Rule.
squash 48496f5 Added the interface Rules.
squash d7f58cb Added the class FileNamePattern.
squash f757bf1 Added the class FilePathPattern.
pick 39ea57e Added the class IgnoreRuleListFactory.
squash b61bf41 Added a Rules interface implementation and a factory for it.
pick f1c6c47 Added test class OverallIgnoreRulestest.
pick 5321c90 Formatted Constants class.
pick b59c160 Added constant REPOSITORY_DIRECTORY_NAME to Constants class.
squash 435b176 Added path related constants to the Constants class.
pick 76ad8d2 Added the class NoGitRepositoryFoundException.
pick 0164c9e Formatted Repository class.
pick fab1279 Added WorkTree class which can be constructed over Repository.
squash afda1f4 Added findWorkTree method to Repository class.
pick eb61f60 Formatted RepositoryTestCase.
pick 5aca2f0 Added a super class for RepositoryTestCase with a createTemporaryDirectory() method.
pick 9b6a188 Added the class LightFileTreeIterator and a test for it.
squash c9a4795 Added class LightFileTreeIterable.
pick 3b068aa Added the class PathNotInProjectDirectoryException.
pick ef7be7f Added the class AddRuleListFactory.
squash 0b81a9c Added class AddRulesFactory.
pick 9bbb0c3 Added the test class AddCommandIterationTest.
This groups patches into related chunks.
I had to run native2ascii -encoding UTF-8 on FileNameMatcherTest.java to make it pass. The reason
ofcourse is that java doesn't specify a source file encoding. You can tell javac which encoding to use,
but I think we better avoid it. For author names in comments I think we have no choice but to state
that UTF-8 is the one and only encoding for comments and documentation where ascii is unavoidable.
native2ascii does these kinds of things
public void testWordroupCase1() throws Exception {
- assertMatch("[[:word:]]", "ö", true, false);
+ assertMatch("[[:word:]]", "\u00f6", true, false);
}
-- robin
^ permalink raw reply [flat|nested] 26+ messages in thread
end of thread, other threads:[~2008-06-18 6:48 UTC | newest]
Thread overview: 26+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-06-13 18:30 [JGIT PATCH v4] Implementation of fnmatch and the ignore rules Florian Köberle
2008-06-13 18:34 ` [JGIT PATCH v4 01/24] Added the package fnmatch and two exceptions Florian Koeberle
2008-06-13 18:34 ` [JGIT PATCH v4 02/24] Added the interface FilePattern Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 03/24] Added the class Rule Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 04/24] Added the iterface Rules Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 05/24] Added the class FileNamePattern Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 06/24] Added the class FilePathPattern Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 07/24] Added the class IgnoreRuleListFactory Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 08/24] Added a Rules interface implementation and a factory for it Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 09/24] Added test class OverallIgnoreRulestest Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 10/24] Added the class PathNotInProjectDirectoryException Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 11/24] Added the class AddRuleListFactory Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 12/24] Formatted Constants class Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 13/24] Added constant REPOSITORY_DIRECTORY_NAME to " Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 14/24] Added class AddRulesFactory Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 15/24] Added the class LightFileTreeIterator and a test for it Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 16/24] Added class LightFileTreeIterable Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 17/24] Added path related constants to the Constants class Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 18/24] Added WorkTree class which can be constructed over Repository Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 19/24] Added the class NoGitRepositoryFoundException Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 20/24] Formatted Repository class Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 21/24] Added findWorkTree method to " Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 22/24] Formatted RepositoryTestCase Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 23/24] Added a super class for RepositoryTestCase with a createTemporaryDirectory() method Florian Koeberle
2008-06-13 18:35 ` [JGIT PATCH v4 24/24] Added the test class AddCommandIterationTest Florian Koeberle
2008-06-18 6:43 ` [JGIT PATCH v4] Implementation of fnmatch and the ignore rules Robin Rosenberg
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).