LsTree: Do not use the default first empty tree in the walker
PATCH 1/4 LsTree: Do not use the default first empty tree in the walker
In reply to a message not in the archiveIn f0ef5e1ef09d346432fead17bc82d78b7cfbd621 an empty tree was added to all TreeWalkers.
--- .../src/org/spearce/jgit/pgm/LsTree.java | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
org.spearce.jgit/src/org/spearce/jgit/pgm/LsTree.java +1 -0
@@ -65,6 +65,7 @@ class LsTree extends TextBuiltin { else if (argi + 1 < args.length) throw die("too many arguments"); + walk.reset(); // drop the first empty tree, which we do not need here final String n = args[argi]; if (is_WorkDir(n)) walk.addTree(new FileTreeIterator(new File(n)));
Signature · 1 line
1.5.5.1.178.g1f811
PATCH 2/4 Create a fnmatch-style pattern TreeFilter
This uses Florian's pattern matcher to perform the matching.
--- .../jgit/treewalk/filter/WildCardTreeFilter.java | 101 ++++++++++++++++++++ 1 files changed, 101 insertions(+), 0 deletions(-) create mode 100644 org.spearce.jgit/src/org/spearce/jgit/treewalk/filter/WildCardTreeFilter.java
org.spearce.jgit/src/org/spearce/jgit/treewalk/filter/WildCardTreeFilter.java new file +101 -0
@@ -0,0 +1,101 @@ +/* + * Copyright (C) 2008, Robin Rosenberg <[email protected]> + * + * 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.filter; + +import java.io.IOException; + +import org.spearce.jgit.errors.IncorrectObjectTypeException; +import org.spearce.jgit.errors.InvalidPatternException; +import org.spearce.jgit.errors.MissingObjectException; +import org.spearce.jgit.fnmatch.FileNameMatcher; +import org.spearce.jgit.treewalk.TreeWalk; +import org.spearce.jgit.treewalk.filter.TreeFilter; + +/** + * This class implements a TreeeFilter that uses the wildcard style pattern + * matching like of Posix fnmatch function. + */ +public class WildCardTreeFilter extends TreeFilter { + + private final FileNameMatcher matcher; + + private final String pattern; + + protected WildCardTreeFilter(final String pattern) { + try { + this.pattern = pattern; + matcher = new FileNameMatcher(pattern, null); + } catch (InvalidPatternException e) { + throw new IllegalArgumentException(e); + } + } + + @Override + public TreeFilter clone() { + return new WildCardTreeFilter(pattern); + } + + @Override + public boolean include(TreeWalk walker) throws MissingObjectException, + IncorrectObjectTypeException, IOException { + matcher.reset(); + matcher.append(walker.getPathString()); + if (matcher.isMatch()) + return true; + return false; + } + + @Override + public boolean shouldBeRecursive() { + return true; + } + + /** + * Construct a WildCardmatcher like POSIX fnmatch. + * + * @param pattern + * A POSIX wildcard pattern + * @return a {@link TreeFilter} that matches pattern + * @throws IllegalArgumentException + * if the pattern is malformed + */ + public static TreeFilter create(final String pattern) { + return new WildCardTreeFilter(pattern); + } + +}
Signature · 1 line
1.5.5.1.178.g1f811
PATCH 3/4 Added a method to get the non-relative name from the tree walker
--- .../src/org/spearce/jgit/treewalk/TreeWalk.java | 21 ++++++++++++++++++++ 1 files changed, 21 insertions(+), 0 deletions(-)
org.spearce.jgit/src/org/spearce/jgit/treewalk/TreeWalk.java +21 -0
@@ -499,6 +499,17 @@ public class TreeWalk { } /** + * Get the current entry's name. + * <p> + * + * @return name of the current entry only. + * @see #getPathString() + */ + public String getName() { + return nameOf(currentHead); + } + + /** * Test if the supplied path matches the current entry's path. * <p> * This method tests that the supplied path is exactly equal to the current
@@ -659,4 +670,14 @@ public class TreeWalk { + Constants.CHARACTER_ENCODING, uee); } } + + private static String nameOf(final AbstractTreeIterator t) { + try { + return new String(t.path, t.pathOffset, t.pathLen - t.pathOffset, + Constants.CHARACTER_ENCODING); + } catch (UnsupportedEncodingException uee) { + throw new RuntimeException("JVM doesn't support " + + Constants.CHARACTER_ENCODING, uee); + } + } }
Signature · 1 line
1.5.5.1.178.g1f811
PATCH 4/4 LsTree: Enable pattern matching in LsTree
--- .../src/org/spearce/jgit/pgm/LsTree.java | 14 +++++++++++++- .../jgit/treewalk/filter/WildCardTreeFilter.java | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-)
org.spearce.jgit/src/org/spearce/jgit/pgm/LsTree.java +13 -1
@@ -43,8 +43,11 @@ import org.spearce.jgit.lib.Constants; import org.spearce.jgit.lib.FileMode; import org.spearce.jgit.treewalk.FileTreeIterator; import org.spearce.jgit.treewalk.TreeWalk; +import org.spearce.jgit.treewalk.filter.TreeFilter; +import org.spearce.jgit.treewalk.filter.WildCardTreeFilter; class LsTree extends TextBuiltin { + @Override void execute(final String[] args) throws Exception { final TreeWalk walk = new TreeWalk(db);
@@ -66,12 +69,21 @@ class LsTree extends TextBuiltin { throw die("too many arguments"); walk.reset(); // drop the first empty tree, which we do not need here - final String n = args[argi]; + final String n = args[argi++]; if (is_WorkDir(n)) walk.addTree(new FileTreeIterator(new File(n))); else walk.addTree(resolve(n)); + if (argi == args.length - 1) { + TreeFilter filter = WildCardTreeFilter.create(args[argi++]); + walk.setFilter(filter); + } + if (argi + 1 == args.length) + throw die("usage: [-r] treename [pattern]"); + else if (argi + 1 < args.length) + throw die("too many arguments"); + while (walk.next()) { final FileMode mode = walk.getFileMode(0); if (mode == FileMode.TREE)
org.spearce.jgit/src/org/spearce/jgit/treewalk/filter/WildCardTreeFilter.java +1 -1
@@ -74,7 +74,7 @@ public class WildCardTreeFilter extends TreeFilter { public boolean include(TreeWalk walker) throws MissingObjectException, IncorrectObjectTypeException, IOException { matcher.reset(); - matcher.append(walker.getPathString()); + matcher.append(walker.getName()); if (matcher.isMatch()) return true; return false;
Signature · 1 line
1.5.5.1.178.g1f811
oops, resend
In reply to a message not in the archiveI f*d the first series. 1 2 3 testing.. :(
-- robin
PATCH 1/4 LsTree: Do not use the default first empty tree in the walker
In f0ef5e1ef09d346432fead17bc82d78b7cfbd621 an empty tree was added to all TreeWalkers.
--- .../src/org/spearce/jgit/pgm/LsTree.java | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
org.spearce.jgit/src/org/spearce/jgit/pgm/LsTree.java +1 -0
@@ -65,6 +65,7 @@ class LsTree extends TextBuiltin { else if (argi + 1 < args.length) throw die("too many arguments"); + walk.reset(); // drop the first empty tree, which we do not need here final String n = args[argi]; if (is_WorkDir(n)) walk.addTree(new FileTreeIterator(new File(n)));
Signature · 1 line
1.5.5.1.178.g1f811
PATCH 2/4 Create a fnmatch-style pattern TreeFilter
This uses Florian's pattern matcher to perform the matching.
--- .../jgit/treewalk/filter/WildCardTreeFilter.java | 103 ++++++++++++++++++++ 1 files changed, 103 insertions(+), 0 deletions(-) create mode 100644 org.spearce.jgit/src/org/spearce/jgit/treewalk/filter/WildCardTreeFilter.java
org.spearce.jgit/src/org/spearce/jgit/treewalk/filter/WildCardTreeFilter.java new file +103 -0
@@ -0,0 +1,103 @@ +/* + * Copyright (C) 2008, Robin Rosenberg <[email protected]> + * + * 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.filter; + +import java.io.IOException; + +import org.spearce.jgit.errors.IncorrectObjectTypeException; +import org.spearce.jgit.errors.InvalidPatternException; +import org.spearce.jgit.errors.MissingObjectException; +import org.spearce.jgit.fnmatch.FileNameMatcher; +import org.spearce.jgit.treewalk.TreeWalk; +import org.spearce.jgit.treewalk.filter.TreeFilter; + +/** + * This class implements a TreeeFilter that uses the wildcard style pattern + * matching like of Posix fnmatch function. + */ +public class WildCardTreeFilter extends TreeFilter { + + private final FileNameMatcher matcher; + + private final String pattern; + + protected WildCardTreeFilter(final String pattern) { + try { + this.pattern = pattern; + matcher = new FileNameMatcher(pattern, null); + } catch (InvalidPatternException e) { + throw new IllegalArgumentException(e); + } + } + + @Override + public TreeFilter clone() { + return new WildCardTreeFilter(pattern); + } + + @Override + public boolean include(TreeWalk walker) throws MissingObjectException, + IncorrectObjectTypeException, IOException { + if (walker.isRecursive() && walker.isSubtree()) + return true; + matcher.reset(); + matcher.append(walker.getPathString()); + if (matcher.isMatch()) + return true; + return false; + } + + @Override + public boolean shouldBeRecursive() { + return true; + } + + /** + * Construct a WildCardmatcher like POSIX fnmatch. + * + * @param pattern + * A POSIX wildcard pattern + * @return a {@link TreeFilter} that matches pattern + * @throws IllegalArgumentException + * if the pattern is malformed + */ + public static TreeFilter create(final String pattern) { + return new WildCardTreeFilter(pattern); + } + +}
Signature · 1 line
1.5.5.1.178.g1f811
PATCH 3/4 Added a method to get the non-relative name from the tree walker
--- .../src/org/spearce/jgit/treewalk/TreeWalk.java | 21 ++++++++++++++++++++ 1 files changed, 21 insertions(+), 0 deletions(-)
org.spearce.jgit/src/org/spearce/jgit/treewalk/TreeWalk.java +21 -0
@@ -499,6 +499,17 @@ public class TreeWalk { } /** + * Get the current entry's name. + * <p> + * + * @return name of the current entry only. + * @see #getPathString() + */ + public String getName() { + return nameOf(currentHead); + } + + /** * Test if the supplied path matches the current entry's path. * <p> * This method tests that the supplied path is exactly equal to the current
@@ -659,4 +670,14 @@ public class TreeWalk { + Constants.CHARACTER_ENCODING, uee); } } + + private static String nameOf(final AbstractTreeIterator t) { + try { + return new String(t.path, t.pathOffset, t.pathLen - t.pathOffset, + Constants.CHARACTER_ENCODING); + } catch (UnsupportedEncodingException uee) { + throw new RuntimeException("JVM doesn't support " + + Constants.CHARACTER_ENCODING, uee); + } + } }
Signature · 1 line
1.5.5.1.178.g1f811
PATCH 4/4 LsTree: Enable pattern matching in LsTree
--- .../src/org/spearce/jgit/pgm/LsTree.java | 19 +++++++++++++------ .../jgit/treewalk/filter/WildCardTreeFilter.java | 2 +- 2 files changed, 14 insertions(+), 7 deletions(-)
org.spearce.jgit/src/org/spearce/jgit/pgm/LsTree.java +13 -6
@@ -43,8 +43,11 @@ import org.spearce.jgit.lib.Constants; import org.spearce.jgit.lib.FileMode; import org.spearce.jgit.treewalk.FileTreeIterator; import org.spearce.jgit.treewalk.TreeWalk; +import org.spearce.jgit.treewalk.filter.TreeFilter; +import org.spearce.jgit.treewalk.filter.WildCardTreeFilter; class LsTree extends TextBuiltin { + @Override void execute(final String[] args) throws Exception { final TreeWalk walk = new TreeWalk(db);
@@ -60,18 +63,22 @@ class LsTree extends TextBuiltin { break; } - if (argi == args.length) - throw die("usage: [-r] treename"); - else if (argi + 1 < args.length) - throw die("too many arguments"); - walk.reset(); // drop the first empty tree, which we do not need here - final String n = args[argi]; + final String n = args[argi++]; if (is_WorkDir(n)) walk.addTree(new FileTreeIterator(new File(n))); else walk.addTree(resolve(n)); + if (argi == args.length - 1) { + TreeFilter filter = WildCardTreeFilter.create(args[argi++]); + walk.setFilter(filter); + } + if (argi + 1 == args.length) + throw die("usage: [-r] treename [pattern]"); + else if (argi + 1 < args.length) + throw die("too many arguments"); + while (walk.next()) { final FileMode mode = walk.getFileMode(0); if (mode == FileMode.TREE)
org.spearce.jgit/src/org/spearce/jgit/treewalk/filter/WildCardTreeFilter.java +1 -1
@@ -76,7 +76,7 @@ public class WildCardTreeFilter extends TreeFilter { if (walker.isRecursive() && walker.isSubtree()) return true; matcher.reset(); - matcher.append(walker.getPathString()); + matcher.append(walker.getName()); if (matcher.isMatch()) return true; return false;
Signature · 1 line
1.5.5.1.178.g1f811
Re: [JGIT PATCH 4/4] LsTree: Enable pattern matching in LsTree
org.spearce.jgit/src/org/spearce/jgit/treewalk/filter/WildCardTreeFilter.java public class WildCardTreeFilter extends TreeFilter { · line 82
Quoted 13 lines · Robin Rosenberg
org.spearce.jgit/src/org/spearce/jgit/treewalk/filter/WildCardTreeFilter.java +1
-1@@ -76,7 +76,7 @@ public class WildCardTreeFilter extends TreeFilter { if (walker.isRecursive() && walker.isSubtree()) return true; matcher.reset(); - matcher.append(walker.getPathString()); + matcher.append(walker.getName()); if (matcher.isMatch()) return true; return false;
Are we only supporting jgit ls-tree . '*.c' ? Or do we want to allow jgit ls-tree . 'src/*.c'?
ls-tree is only a little sample program that is not likely to have a lot of real-world users calling it; but is a good demonstration of how to use TreeWalk. So I really don't care either way.
The WildCardTreeFilter on the other hand could be applied to a RevWalk, such as to grab history for not just 'foo.c' but anything that matches 'f*.c'. But then you have to ask, why is the filter limited to testing only the last component of the path? Why can't it test 'src/f*.c'?
Otherwise everything in your series up until here makes sense and looks good to me. Be nice if the tree filter wasn't so abusive in needing to look at every path in the project, as that would really hurt if it was applied to a RevWalk. But its not required to be fast, if you ask for fnmatch paths, you get what you asked for...
Signature · 1 line
Shawn.