2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org
.spearce
.jgit
.revwalk
.filter
;
40 import java
.util
.regex
.Pattern
;
42 import org
.spearce
.jgit
.revwalk
.RevCommit
;
43 import org
.spearce
.jgit
.util
.RawCharSequence
;
44 import org
.spearce
.jgit
.util
.RawParseUtils
;
46 /** Matches only commits whose author name matches the pattern. */
47 public class AuthorRevFilter
{
49 * Create a new author filter.
51 * An optimized substring search may be automatically selected if the
52 * pattern does not contain any regular expression meta-characters.
54 * The search is performed using a case-insensitive comparison. The
55 * character encoding of the commit message itself is not respected. The
56 * filter matches on raw UTF-8 byte sequences.
59 * regular expression pattern to match.
60 * @return a new filter that matches the given expression against the author
61 * name and address of a commit.
63 public static RevFilter
create(String pattern
) {
64 if (pattern
.length() == 0)
65 throw new IllegalArgumentException("Cannot match on empty string.");
66 if (SubStringRevFilter
.safe(pattern
))
67 return new SubStringSearch(pattern
);
68 return new PatternSearch(pattern
);
71 private AuthorRevFilter() {
72 // Don't permit us to be created.
75 static RawCharSequence
textFor(final RevCommit cmit
) {
76 final byte[] raw
= cmit
.getRawBuffer();
77 final int b
= RawParseUtils
.author(raw
, 0);
79 return RawCharSequence
.EMPTY
;
80 final int e
= RawParseUtils
.nextLF(raw
, b
, '>');
81 return new RawCharSequence(raw
, b
, e
);
84 private static class PatternSearch
extends PatternMatchRevFilter
{
85 PatternSearch(final String patternText
) {
86 super(patternText
, true, true, Pattern
.CASE_INSENSITIVE
);
90 protected CharSequence
text(final RevCommit cmit
) {
95 public RevFilter
clone() {
96 return new PatternSearch(pattern());
100 private static class SubStringSearch
extends SubStringRevFilter
{
101 SubStringSearch(final String patternText
) {
106 protected RawCharSequence
text(final RevCommit cmit
) {
107 return textFor(cmit
);