git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] Create a fnmatch-style pattern TreeFilter
@ 2008-06-22 23:25 Robin Rosenberg
  2008-06-22 23:25 ` [PATCH 2/2] LsTree: Enable pattern matching in LsTree Robin Rosenberg
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Robin Rosenberg @ 2008-06-22 23:25 UTC (permalink / raw)
  To: git; +Cc: Shawn O. Pearce, Marek Zawirski, Florian Koeberle,
	Robin Rosenberg

This uses Florian's pattern matcher to perform the matching.

Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
---
 .../jgit/revwalk/filter/WildCardTreeFilter.java    |  101 ++++++++++++++++++++
 1 files changed, 101 insertions(+), 0 deletions(-)
 create mode 100644 org.spearce.jgit/src/org/spearce/jgit/revwalk/filter/WildCardTreeFilter.java

diff --git a/org.spearce.jgit/src/org/spearce/jgit/revwalk/filter/WildCardTreeFilter.java b/org.spearce.jgit/src/org/spearce/jgit/revwalk/filter/WildCardTreeFilter.java
new file mode 100644
index 0000000..fd75458
--- /dev/null
+++ b/org.spearce.jgit/src/org/spearce/jgit/revwalk/filter/WildCardTreeFilter.java
@@ -0,0 +1,101 @@
+/*
+ * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
+ *
+ * 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.revwalk.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);
+	}
+
+}
-- 
1.5.5.1.178.g1f811

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/2] LsTree: Enable pattern matching in LsTree
  2008-06-22 23:25 [PATCH 1/2] Create a fnmatch-style pattern TreeFilter Robin Rosenberg
@ 2008-06-22 23:25 ` Robin Rosenberg
  2008-06-23  0:27 ` [PATCH 1/2] Create a fnmatch-style pattern TreeFilter Shawn O. Pearce
  2008-06-23 17:32 ` Florian Köberle
  2 siblings, 0 replies; 5+ messages in thread
From: Robin Rosenberg @ 2008-06-22 23:25 UTC (permalink / raw)
  To: git; +Cc: Shawn O. Pearce, Marek Zawirski, Florian Koeberle,
	Robin Rosenberg

Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
---
 .../src/org/spearce/jgit/pgm/LsTree.java           |   19 +++++++++++++------
 1 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/org.spearce.jgit/src/org/spearce/jgit/pgm/LsTree.java b/org.spearce.jgit/src/org/spearce/jgit/pgm/LsTree.java
index c242bd7..2cda485 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/pgm/LsTree.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/pgm/LsTree.java
@@ -41,10 +41,13 @@ import java.io.File;
 
 import org.spearce.jgit.lib.Constants;
 import org.spearce.jgit.lib.FileMode;
+import org.spearce.jgit.revwalk.filter.WildCardTreeFilter;
 import org.spearce.jgit.treewalk.FileTreeIterator;
 import org.spearce.jgit.treewalk.TreeWalk;
+import org.spearce.jgit.treewalk.filter.TreeFilter;
 
 class LsTree extends TextBuiltin {
+
 	@Override
 	void execute(final String[] args) throws Exception {
 		final TreeWalk walk = new TreeWalk(db);
@@ -60,17 +63,21 @@ 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");
-
-		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(1);
 			if (mode == FileMode.TREE)
-- 
1.5.5.1.178.g1f811

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/2] Create a fnmatch-style pattern TreeFilter
  2008-06-22 23:25 [PATCH 1/2] Create a fnmatch-style pattern TreeFilter Robin Rosenberg
  2008-06-22 23:25 ` [PATCH 2/2] LsTree: Enable pattern matching in LsTree Robin Rosenberg
@ 2008-06-23  0:27 ` Shawn O. Pearce
  2008-06-23 17:32 ` Florian Köberle
  2 siblings, 0 replies; 5+ messages in thread
From: Shawn O. Pearce @ 2008-06-23  0:27 UTC (permalink / raw)
  To: Robin Rosenberg; +Cc: git, Marek Zawirski, Florian Koeberle

Robin Rosenberg <robin.rosenberg@dewire.com> wrote:
> +
> +package org.spearce.jgit.revwalk.filter;

This should be treewalk.filter, its a filter for tree entries.

> + * This class implements a TreeeFilter that uses the wildcard style pattern

fyi, minooooor typo on TreeFilter.

> +public class WildCardTreeFilter extends TreeFilter {
...
> +	@Override
> +	public boolean include(TreeWalk walker) throws MissingObjectException,
> +			IncorrectObjectTypeException, IOException {
> +		matcher.reset();
> +		matcher.append(walker.getPathString());
> +		if (matcher.isMatch())
> +			return true;
> +		return false;
> +	}

Hmm. 

It isn't as efficient as it could be.  Obtaining the string of
the path is somewhat costly as we have to convert from the byte[]
to a char[] and then wrap that into a String, just to check the
pattern again.  Its more accurate to convert to the string, but
it is a lot slower.

But I'm wondering about what happens when the TreeWalk is considering
a subtree path name.  Does the matcher still match the pattern?  By
that I mean lets say the pattern was:

	src/xdiff/*.c

In this case the include method is first called with the path "src";
if it returns false the TreeWalk won't recurse into the subtree and
thus you'll never get to consider "src/xdiff" or "src/xdiff/foo.c".

-- 
Shawn.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/2] Create a fnmatch-style pattern TreeFilter
  2008-06-22 23:25 [PATCH 1/2] Create a fnmatch-style pattern TreeFilter Robin Rosenberg
  2008-06-22 23:25 ` [PATCH 2/2] LsTree: Enable pattern matching in LsTree Robin Rosenberg
  2008-06-23  0:27 ` [PATCH 1/2] Create a fnmatch-style pattern TreeFilter Shawn O. Pearce
@ 2008-06-23 17:32 ` Florian Köberle
  2008-06-23 17:43   ` [[JGIT PATCH]] Implementation of a copy constructor for FileNameMatcher Florian Köberle
  2 siblings, 1 reply; 5+ messages in thread
From: Florian Köberle @ 2008-06-23 17:32 UTC (permalink / raw)
  To: Robin Rosenberg; +Cc: git, Shawn O. Pearce, Marek Zawirski

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi Robin,

thank you for accepting my first patch :D.

| +/**
| + * This class implements a TreeeFilter that uses the wildcard style
pattern
| + * matching like of Posix fnmatch function.
| + */
Typo: One 'e' to much in TreeeFilter.

It would be more efficient to
| +	@Override
| +	public TreeFilter clone() {
| +		return new WildCardTreeFilter(pattern);
| +	}

One way to create a clone of the FileNameMatcher is to call:
originalMatcher.reset()
FileNameMatcher clone = originalMatcher.createMatcherForSuffix()

I will send a patch which implements a copy constructor for FileNameMatcher.

First I wanted to implement a clone() method, but found this page and
decided then to implement a copy constructor:
http://www.javapractices.com/topic/TopicAction.do?Id=71

A Implementor of a super class could imply that clone() of object gets
called, as stated in the javadoc of clone():

quote (javadoc ob Object#clone()):
- -----------
By convention, the returned object should be obtained by calling super.clone
- -----------

I think this is a bad convention, as one should not rely on
Object#clone() to do the copy job for one. If you really need a clone
method then I would do it the same way you did, by calling a constructor
which does the job.

Best regards,
Florian

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFIX94k59ca4mzhfxMRAgDjAJ9S76L8I5Lqed4lKfgTf+2cp2IQ9gCfQNVh
z72+NGvmIy3H0gwveKRfn+w=
=wnpy
-----END PGP SIGNATURE-----

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [[JGIT PATCH]] Implementation of a copy constructor for FileNameMatcher.
  2008-06-23 17:32 ` Florian Köberle
@ 2008-06-23 17:43   ` Florian Köberle
  0 siblings, 0 replies; 5+ messages in thread
From: Florian Köberle @ 2008-06-23 17:43 UTC (permalink / raw)
  To: git; +Cc: robin.rosenberg, spearce, Florian Köberle

Signed-off-by: Florian Köberle <florianskarten@web.de>
---
 .../spearce/jgit/fnmatch/FileNameMatcherTest.java  |   31 ++++++++++++++++++++
 .../org/spearce/jgit/fnmatch/FileNameMatcher.java  |   30 +++++++++++++++++--
 2 files changed, 58 insertions(+), 3 deletions(-)

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
index ad72ac8..0c9501b 100644
--- a/org.spearce.jgit.test/tst/org/spearce/jgit/fnmatch/FileNameMatcherTest.java
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/fnmatch/FileNameMatcherTest.java
@@ -723,4 +723,35 @@ public class FileNameMatcherTest extends TestCase {
 		assertEquals(true, childMatcher.isMatch());
 		assertEquals(false, childMatcher.canAppendMatch());
 	}
+
+	public void testCopyConstructor() throws Exception {
+		final String pattern = "helloworld";
+		final FileNameMatcher matcher = new FileNameMatcher(pattern, null);
+		matcher.append("hello");
+		final FileNameMatcher copy = new FileNameMatcher(matcher);
+		assertEquals(false, matcher.isMatch());
+		assertEquals(true, matcher.canAppendMatch());
+		assertEquals(false, copy.isMatch());
+		assertEquals(true, copy.canAppendMatch());
+		matcher.append("world");
+		assertEquals(true, matcher.isMatch());
+		assertEquals(false, matcher.canAppendMatch());
+		assertEquals(false, copy.isMatch());
+		assertEquals(true, copy.canAppendMatch());
+		copy.append("world");
+		assertEquals(true, matcher.isMatch());
+		assertEquals(false, matcher.canAppendMatch());
+		assertEquals(true, copy.isMatch());
+		assertEquals(false, copy.canAppendMatch());
+		copy.reset();
+		assertEquals(true, matcher.isMatch());
+		assertEquals(false, matcher.canAppendMatch());
+		assertEquals(false, copy.isMatch());
+		assertEquals(true, copy.canAppendMatch());
+		copy.append("helloworld");
+		assertEquals(true, matcher.isMatch());
+		assertEquals(false, matcher.canAppendMatch());
+		assertEquals(true, copy.isMatch());
+		assertEquals(false, copy.canAppendMatch());
+	}
 }
diff --git a/org.spearce.jgit/src/org/spearce/jgit/fnmatch/FileNameMatcher.java b/org.spearce.jgit/src/org/spearce/jgit/fnmatch/FileNameMatcher.java
index 9ac1875..702f7b3 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/fnmatch/FileNameMatcher.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/fnmatch/FileNameMatcher.java
@@ -99,10 +99,23 @@ public class FileNameMatcher {
 	 *            must be a list which will never be modified.
 	 */
 	private FileNameMatcher(final List<Head> headsStartValue) {
+		this(headsStartValue, headsStartValue);
+	}
+
+	/**
+	 *
+	 * @param headsStartValue
+	 *            must be a list which will never be modified.
+	 * @param heads
+	 *            a list which will be cloned and then used as current head
+	 *            list.
+	 */
+	private FileNameMatcher(final List<Head> headsStartValue,
+			final List<Head> heads) {
 		this.headsStartValue = headsStartValue;
-		this.heads = new ArrayList<Head>(headsStartValue.size());
-		this.heads.addAll(this.headsStartValue);
-		this.listForLocalUseage = new ArrayList<Head>(headsStartValue.size());
+		this.heads = new ArrayList<Head>(heads.size());
+		this.heads.addAll(heads);
+		this.listForLocalUseage = new ArrayList<Head>(heads.size());
 	}
 
 	/**
@@ -120,6 +133,17 @@ public class FileNameMatcher {
 		this(createHeadsStartValues(patternString, invalidWildgetCharacter));
 	}
 
+	/**
+	 * A Copy Constructor which creates a new {@link FileNameMatcher} with the
+	 * same state and reset point like <code>other</code>.
+	 *
+	 * @param other
+	 *            another {@link FileNameMatcher} instance.
+	 */
+	public FileNameMatcher(FileNameMatcher other) {
+		this(other.headsStartValue, other.heads);
+	}
+
 	private static List<Head> createHeadsStartValues(
 			final String patternString, final Character invalidWildgetCharacter)
 			throws InvalidPatternException {
-- 
1.5.4.3

^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2008-06-23 17:45 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-06-22 23:25 [PATCH 1/2] Create a fnmatch-style pattern TreeFilter Robin Rosenberg
2008-06-22 23:25 ` [PATCH 2/2] LsTree: Enable pattern matching in LsTree Robin Rosenberg
2008-06-23  0:27 ` [PATCH 1/2] Create a fnmatch-style pattern TreeFilter Shawn O. Pearce
2008-06-23 17:32 ` Florian Köberle
2008-06-23 17:43   ` [[JGIT PATCH]] Implementation of a copy constructor for FileNameMatcher Florian Köberle

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).