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.

FollowFilter.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (C) 2010, 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.revwalk;
  11. import java.io.IOException;
  12. import org.eclipse.jgit.diff.DiffConfig;
  13. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  14. import org.eclipse.jgit.errors.MissingObjectException;
  15. import org.eclipse.jgit.treewalk.TreeWalk;
  16. import org.eclipse.jgit.treewalk.filter.PathFilter;
  17. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  18. /**
  19. * Updates the internal path filter to follow copy/renames.
  20. * <p>
  21. * This is a special filter that performs {@code AND(path, ANY_DIFF)}, but also
  22. * triggers rename detection so that the path node is updated to include a prior
  23. * file name as the RevWalk traverses history.
  24. *
  25. * The renames found will be reported to a
  26. * {@link org.eclipse.jgit.revwalk.RenameCallback} if one is set.
  27. * <p>
  28. * Results with this filter are unpredictable if the path being followed is a
  29. * subdirectory.
  30. */
  31. public class FollowFilter extends TreeFilter {
  32. /**
  33. * Create a new tree filter for a user supplied path.
  34. * <p>
  35. * Path strings are relative to the root of the repository. If the user's
  36. * input should be assumed relative to a subdirectory of the repository the
  37. * caller must prepend the subdirectory's path prior to creating the filter.
  38. * <p>
  39. * Path strings use '/' to delimit directories on all platforms.
  40. *
  41. * @param path
  42. * the path to filter on. Must not be the empty string. All
  43. * trailing '/' characters will be trimmed before string's length
  44. * is checked or is used as part of the constructed filter.
  45. * @param cfg
  46. * diff config specifying rename detection options.
  47. * @return a new filter for the requested path.
  48. * @throws java.lang.IllegalArgumentException
  49. * the path supplied was the empty string.
  50. * @since 3.0
  51. */
  52. public static FollowFilter create(String path, DiffConfig cfg) {
  53. return new FollowFilter(PathFilter.create(path), cfg);
  54. }
  55. private final PathFilter path;
  56. final DiffConfig cfg;
  57. private RenameCallback renameCallback;
  58. FollowFilter(PathFilter path, DiffConfig cfg) {
  59. this.path = path;
  60. this.cfg = cfg;
  61. }
  62. /** @return the path this filter matches. */
  63. /**
  64. * Get the path this filter matches.
  65. *
  66. * @return the path this filter matches.
  67. */
  68. public String getPath() {
  69. return path.getPath();
  70. }
  71. /** {@inheritDoc} */
  72. @Override
  73. public boolean include(TreeWalk walker)
  74. throws MissingObjectException, IncorrectObjectTypeException,
  75. IOException {
  76. return path.include(walker) && ANY_DIFF.include(walker);
  77. }
  78. /** {@inheritDoc} */
  79. @Override
  80. public boolean shouldBeRecursive() {
  81. return path.shouldBeRecursive() || ANY_DIFF.shouldBeRecursive();
  82. }
  83. /** {@inheritDoc} */
  84. @Override
  85. public TreeFilter clone() {
  86. return new FollowFilter(path.clone(), cfg);
  87. }
  88. /** {@inheritDoc} */
  89. @SuppressWarnings("nls")
  90. @Override
  91. public String toString() {
  92. return "(FOLLOW(" + path.toString() + ")" //
  93. + " AND " //
  94. + ANY_DIFF.toString() + ")";
  95. }
  96. /**
  97. * Get the callback to which renames are reported.
  98. *
  99. * @return the callback to which renames are reported, or <code>null</code>
  100. * if none
  101. */
  102. public RenameCallback getRenameCallback() {
  103. return renameCallback;
  104. }
  105. /**
  106. * Sets the callback to which renames shall be reported.
  107. *
  108. * @param callback
  109. * the callback to use
  110. */
  111. public void setRenameCallback(RenameCallback callback) {
  112. renameCallback = callback;
  113. }
  114. }