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.

PathSuffixFilter.java 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (C) 2009, 2021 Google Inc. 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. package org.eclipse.jgit.treewalk.filter;
  11. import java.io.IOException;
  12. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  13. import org.eclipse.jgit.errors.MissingObjectException;
  14. import org.eclipse.jgit.internal.JGitText;
  15. import org.eclipse.jgit.lib.Constants;
  16. import org.eclipse.jgit.treewalk.TreeWalk;
  17. /**
  18. * Includes tree entries only if they end with the configured path (suffix
  19. * match).
  20. * <p>
  21. * For example, <code>PathSuffixFilter.create(".txt")</code> will match all
  22. * paths ending in <code>.txt</code>.
  23. * <p>
  24. * Using this filter is recommended instead of filtering the entries using
  25. * {@link org.eclipse.jgit.treewalk.TreeWalk#getPathString()} and
  26. * <code>endsWith</code> or some other type of string match function.
  27. */
  28. public class PathSuffixFilter extends TreeFilter {
  29. /**
  30. * Create a new tree filter for a user supplied path suffix.
  31. * <p>
  32. * Path strings use '/' to delimit directories on all platforms.
  33. *
  34. * @param path
  35. * the path suffix to filter on. Must not be the empty string.
  36. * @return a new filter for the requested path.
  37. * @throws java.lang.IllegalArgumentException
  38. * the path supplied was the empty string.
  39. */
  40. public static PathSuffixFilter create(String path) {
  41. if (path.length() == 0)
  42. throw new IllegalArgumentException(JGitText.get().emptyPathNotPermitted);
  43. return new PathSuffixFilter(path);
  44. }
  45. final String pathStr;
  46. final byte[] pathRaw;
  47. private PathSuffixFilter(String s) {
  48. pathStr = s;
  49. pathRaw = Constants.encode(pathStr);
  50. }
  51. /** {@inheritDoc} */
  52. @Override
  53. public TreeFilter clone() {
  54. return this;
  55. }
  56. /** {@inheritDoc} */
  57. @Override
  58. public boolean include(TreeWalk walker) throws MissingObjectException,
  59. IncorrectObjectTypeException, IOException {
  60. if (walker.isSubtree()) {
  61. return true;
  62. }
  63. return walker.isPathSuffix(pathRaw, pathRaw.length);
  64. }
  65. @Override
  66. public int matchFilter(TreeWalk walker) throws MissingObjectException,
  67. IncorrectObjectTypeException, IOException {
  68. if (walker.isSubtree()) {
  69. return -1;
  70. }
  71. return super.matchFilter(walker);
  72. }
  73. /** {@inheritDoc} */
  74. @Override
  75. public boolean shouldBeRecursive() {
  76. return true;
  77. }
  78. }