You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PatternMatchRevFilter.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (C) 2009, Google Inc.
  3. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
  4. *
  5. * This program and the accompanying materials are made available under the
  6. * terms of the Eclipse Distribution License v. 1.0 which is available at
  7. * https://www.eclipse.org/org/documents/edl-v10.php.
  8. *
  9. * SPDX-License-Identifier: BSD-3-Clause
  10. */
  11. package org.eclipse.jgit.revwalk.filter;
  12. import java.io.IOException;
  13. import java.util.regex.Matcher;
  14. import java.util.regex.Pattern;
  15. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  16. import org.eclipse.jgit.errors.MissingObjectException;
  17. import org.eclipse.jgit.internal.JGitText;
  18. import org.eclipse.jgit.lib.Constants;
  19. import org.eclipse.jgit.revwalk.RevCommit;
  20. import org.eclipse.jgit.revwalk.RevWalk;
  21. /**
  22. * Abstract filter that searches text using extended regular expressions.
  23. */
  24. public abstract class PatternMatchRevFilter extends RevFilter {
  25. /**
  26. * Encode a string pattern for faster matching on byte arrays.
  27. * <p>
  28. * Force the characters to our funny UTF-8 only convention that we use on
  29. * raw buffers. This avoids needing to perform character set decodes on the
  30. * individual commit buffers.
  31. *
  32. * @param patternText
  33. * original pattern string supplied by the user or the
  34. * application.
  35. * @return same pattern, but re-encoded to match our funny raw UTF-8
  36. * character sequence {@link org.eclipse.jgit.util.RawCharSequence}.
  37. */
  38. protected static final String forceToRaw(String patternText) {
  39. final byte[] b = Constants.encode(patternText);
  40. final StringBuilder needle = new StringBuilder(b.length);
  41. for (byte element : b)
  42. needle.append((char) (element & 0xff));
  43. return needle.toString();
  44. }
  45. private final String patternText;
  46. private final Matcher compiledPattern;
  47. /**
  48. * Construct a new pattern matching filter.
  49. *
  50. * @param pattern
  51. * text of the pattern. Callers may want to surround their
  52. * pattern with ".*" on either end to allow matching in the
  53. * middle of the string.
  54. * @param innerString
  55. * should .* be wrapped around the pattern of ^ and $ are
  56. * missing? Most users will want this set.
  57. * @param rawEncoding
  58. * should {@link #forceToRaw(String)} be applied to the pattern
  59. * before compiling it?
  60. * @param flags
  61. * flags from {@link java.util.regex.Pattern} to control how
  62. * matching performs.
  63. */
  64. protected PatternMatchRevFilter(String pattern, final boolean innerString,
  65. final boolean rawEncoding, final int flags) {
  66. if (pattern.length() == 0)
  67. throw new IllegalArgumentException(JGitText.get().cannotMatchOnEmptyString);
  68. patternText = pattern;
  69. if (innerString) {
  70. if (!pattern.startsWith("^") && !pattern.startsWith(".*")) //$NON-NLS-1$ //$NON-NLS-2$
  71. pattern = ".*" + pattern; //$NON-NLS-1$
  72. if (!pattern.endsWith("$") && !pattern.endsWith(".*")) //$NON-NLS-1$ //$NON-NLS-2$
  73. pattern = pattern + ".*"; //$NON-NLS-1$
  74. }
  75. final String p = rawEncoding ? forceToRaw(pattern) : pattern;
  76. compiledPattern = Pattern.compile(p, flags).matcher(""); //$NON-NLS-1$
  77. }
  78. /**
  79. * Get the pattern this filter uses.
  80. *
  81. * @return the pattern this filter is applying to candidate strings.
  82. */
  83. public String pattern() {
  84. return patternText;
  85. }
  86. /** {@inheritDoc} */
  87. @Override
  88. public boolean include(RevWalk walker, RevCommit cmit)
  89. throws MissingObjectException, IncorrectObjectTypeException,
  90. IOException {
  91. return compiledPattern.reset(text(cmit)).matches();
  92. }
  93. /** {@inheritDoc} */
  94. @Override
  95. public boolean requiresCommitBody() {
  96. return true;
  97. }
  98. /**
  99. * Obtain the raw text to match against.
  100. *
  101. * @param cmit
  102. * current commit being evaluated.
  103. * @return sequence for the commit's content that we need to match on.
  104. */
  105. protected abstract CharSequence text(RevCommit cmit);
  106. /** {@inheritDoc} */
  107. @SuppressWarnings("nls")
  108. @Override
  109. public String toString() {
  110. return super.toString() + "(\"" + patternText + "\")";
  111. }
  112. }