View Javadoc
1   /*
2    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.revwalk.filter;
12  
13  import java.util.regex.Pattern;
14  
15  import org.eclipse.jgit.internal.JGitText;
16  import org.eclipse.jgit.revwalk.RevCommit;
17  import org.eclipse.jgit.util.RawCharSequence;
18  import org.eclipse.jgit.util.RawParseUtils;
19  
20  /**
21   * Matches only commits whose committer name matches the pattern.
22   */
23  public class CommitterRevFilter {
24  	/**
25  	 * Create a new committer filter.
26  	 * <p>
27  	 * An optimized substring search may be automatically selected if the
28  	 * pattern does not contain any regular expression meta-characters.
29  	 * <p>
30  	 * The search is performed using a case-insensitive comparison. The
31  	 * character encoding of the commit message itself is not respected. The
32  	 * filter matches on raw UTF-8 byte sequences.
33  	 *
34  	 * @param pattern
35  	 *            regular expression pattern to match.
36  	 * @return a new filter that matches the given expression against the author
37  	 *         name and address of a commit.
38  	 */
39  	public static RevFilter create(String pattern) {
40  		if (pattern.length() == 0)
41  			throw new IllegalArgumentException(JGitText.get().cannotMatchOnEmptyString);
42  		if (SubStringRevFilter.safe(pattern))
43  			return new SubStringSearch(pattern);
44  		return new PatternSearch(pattern);
45  	}
46  
47  	private CommitterRevFilter() {
48  		// Don't permit us to be created.
49  	}
50  
51  	static RawCharSequence textFor(RevCommit cmit) {
52  		final byte[] raw = cmit.getRawBuffer();
53  		final int b = RawParseUtils.committer(raw, 0);
54  		if (b < 0)
55  			return RawCharSequence.EMPTY;
56  		final int e = RawParseUtils.nextLF(raw, b, '>');
57  		return new RawCharSequence(raw, b, e);
58  	}
59  
60  	private static class PatternSearch extends PatternMatchRevFilter {
61  		PatternSearch(String patternText) {
62  			super(patternText, true, true, Pattern.CASE_INSENSITIVE);
63  		}
64  
65  		@Override
66  		protected CharSequence text(RevCommit cmit) {
67  			return textFor(cmit);
68  		}
69  
70  		@Override
71  		public RevFilter clone() {
72  			return new PatternSearch(pattern());
73  		}
74  	}
75  
76  	private static class SubStringSearch extends SubStringRevFilter {
77  		SubStringSearch(String patternText) {
78  			super(patternText);
79  		}
80  
81  		@Override
82  		protected RawCharSequence text(RevCommit cmit) {
83  			return textFor(cmit);
84  		}
85  	}
86  }