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.

PathFilterLogicTest.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * Copyright (C) 2017 Magnus Vigerlöf (magnus.vigerlof@gmail.com)
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.treewalk.filter;
  44. import org.eclipse.jgit.dircache.DirCache;
  45. import org.eclipse.jgit.dircache.DirCacheBuilder;
  46. import org.eclipse.jgit.dircache.DirCacheEntry;
  47. import org.eclipse.jgit.junit.RepositoryTestCase;
  48. import org.eclipse.jgit.lib.FileMode;
  49. import org.eclipse.jgit.lib.ObjectId;
  50. import org.eclipse.jgit.lib.ObjectInserter;
  51. import org.eclipse.jgit.treewalk.TreeWalk;
  52. import org.junit.Before;
  53. import org.junit.Test;
  54. import java.io.IOException;
  55. import java.util.ArrayList;
  56. import java.util.Arrays;
  57. import java.util.Collections;
  58. import java.util.List;
  59. import static org.junit.Assert.assertEquals;
  60. public class PathFilterLogicTest extends RepositoryTestCase {
  61. private ObjectId treeId;
  62. @Before
  63. public void setup() throws IOException {
  64. String[] paths = new String[] {
  65. "a.txt",
  66. "sub1.txt",
  67. "sub1/suba/a.txt",
  68. "sub1/subb/b.txt",
  69. "sub2/suba/a.txt"
  70. };
  71. treeId = createTree(paths);
  72. }
  73. @Test
  74. public void testSinglePath() throws IOException {
  75. List<String> expected = Arrays.asList("sub1/suba/a.txt",
  76. "sub1/subb/b.txt");
  77. TreeFilter tf = PathFilter.create("sub1");
  78. List<String> paths = getMatchingPaths(treeId, tf);
  79. assertEquals(expected, paths);
  80. }
  81. @Test
  82. public void testSingleSubPath() throws IOException {
  83. List<String> expected = Collections.singletonList("sub1/suba/a.txt");
  84. TreeFilter tf = PathFilter.create("sub1/suba");
  85. List<String> paths = getMatchingPaths(treeId, tf);
  86. assertEquals(expected, paths);
  87. }
  88. @Test
  89. public void testSinglePathNegate() throws IOException {
  90. List<String> expected = Arrays.asList("a.txt", "sub1.txt",
  91. "sub2/suba/a.txt");
  92. TreeFilter tf = PathFilter.create("sub1").negate();
  93. List<String> paths = getMatchingPaths(treeId, tf);
  94. assertEquals(expected, paths);
  95. }
  96. @Test
  97. public void testSingleSubPathNegate() throws IOException {
  98. List<String> expected = Arrays.asList("a.txt", "sub1.txt",
  99. "sub1/subb/b.txt", "sub2/suba/a.txt");
  100. TreeFilter tf = PathFilter.create("sub1/suba").negate();
  101. List<String> paths = getMatchingPaths(treeId, tf);
  102. assertEquals(expected, paths);
  103. }
  104. @Test
  105. public void testOrMultiTwoPath() throws IOException {
  106. List<String> expected = Arrays.asList("sub1/suba/a.txt",
  107. "sub1/subb/b.txt", "sub2/suba/a.txt");
  108. TreeFilter[] tf = new TreeFilter[] {PathFilter.create("sub1"),
  109. PathFilter.create("sub2")};
  110. List<String> paths = getMatchingPaths(treeId, OrTreeFilter.create(tf));
  111. assertEquals(expected, paths);
  112. }
  113. @Test
  114. public void testOrMultiThreePath() throws IOException {
  115. List<String> expected = Arrays.asList("sub1.txt", "sub1/suba/a.txt",
  116. "sub1/subb/b.txt", "sub2/suba/a.txt");
  117. TreeFilter[] tf = new TreeFilter[] {PathFilter.create("sub1"),
  118. PathFilter.create("sub2"), PathFilter.create("sub1.txt")};
  119. List<String> paths = getMatchingPaths(treeId, OrTreeFilter.create(tf));
  120. assertEquals(expected, paths);
  121. }
  122. @Test
  123. public void testOrMultiTwoSubPath() throws IOException {
  124. List<String> expected = Arrays.asList("sub1/subb/b.txt",
  125. "sub2/suba/a.txt");
  126. TreeFilter[] tf = new TreeFilter[] {PathFilter.create("sub1/subb"),
  127. PathFilter.create("sub2/suba")};
  128. List<String> paths = getMatchingPaths(treeId, OrTreeFilter.create(tf));
  129. assertEquals(expected, paths);
  130. }
  131. @Test
  132. public void testOrMultiTwoMixSubPath() throws IOException {
  133. List<String> expected = Arrays.asList("sub1/subb/b.txt",
  134. "sub2/suba/a.txt");
  135. TreeFilter[] tf = new TreeFilter[] {PathFilter.create("sub1/subb"),
  136. PathFilter.create("sub2")};
  137. List<String> paths = getMatchingPaths(treeId, OrTreeFilter.create(tf));
  138. assertEquals(expected, paths);
  139. }
  140. @Test
  141. public void testOrMultiTwoMixSubPathNegate() throws IOException {
  142. List<String> expected = Arrays.asList("a.txt", "sub1.txt",
  143. "sub1/suba/a.txt", "sub2/suba/a.txt");
  144. TreeFilter[] tf = new TreeFilter[] {PathFilter.create("sub1").negate(),
  145. PathFilter.create("sub1/suba")};
  146. List<String> paths = getMatchingPaths(treeId, OrTreeFilter.create(tf));
  147. assertEquals(expected, paths);
  148. }
  149. @Test
  150. public void testOrMultiThreeMixSubPathNegate() throws IOException {
  151. List<String> expected = Arrays.asList("a.txt", "sub1.txt",
  152. "sub1/suba/a.txt", "sub2/suba/a.txt");
  153. TreeFilter[] tf = new TreeFilter[] {PathFilter.create("sub1").negate(),
  154. PathFilter.create("sub1/suba"), PathFilter.create("no/path")};
  155. List<String> paths = getMatchingPaths(treeId, OrTreeFilter.create(tf));
  156. assertEquals(expected, paths);
  157. }
  158. @Test
  159. public void testPatternParentFileMatch() throws IOException {
  160. List<String> expected = Collections.emptyList();
  161. TreeFilter tf = PathFilter.create("a.txt/test/path");
  162. List<String> paths = getMatchingPaths(treeId, tf);
  163. assertEquals(expected, paths);
  164. }
  165. @Test
  166. public void testAndMultiPath() throws IOException {
  167. List<String> expected = Collections.emptyList();
  168. TreeFilter[] tf = new TreeFilter[] {PathFilter.create("sub1"),
  169. PathFilter.create("sub2")};
  170. List<String> paths = getMatchingPaths(treeId, AndTreeFilter.create(tf));
  171. assertEquals(expected, paths);
  172. }
  173. @Test
  174. public void testAndMultiPathNegate() throws IOException {
  175. List<String> expected = Arrays.asList("sub1/suba/a.txt",
  176. "sub1/subb/b.txt");
  177. TreeFilter[] tf = new TreeFilter[] {PathFilter.create("sub1"),
  178. PathFilter.create("sub2").negate()};
  179. List<String> paths = getMatchingPaths(treeId, AndTreeFilter.create(tf));
  180. assertEquals(expected, paths);
  181. }
  182. @Test
  183. public void testAndMultiSubPathDualNegate() throws IOException {
  184. List<String> expected = Arrays.asList("a.txt", "sub1.txt",
  185. "sub1/subb/b.txt");
  186. TreeFilter[] tf = new TreeFilter[] {PathFilter.create("sub1/suba").negate(),
  187. PathFilter.create("sub2").negate()};
  188. List<String> paths = getMatchingPaths(treeId, AndTreeFilter.create(tf));
  189. assertEquals(expected, paths);
  190. }
  191. @Test
  192. public void testAndMultiSubPath() throws IOException {
  193. List<String> expected = Collections.emptyList();
  194. TreeFilter[] tf = new TreeFilter[] {PathFilter.create("sub1"),
  195. PathFilter.create("sub2/suba")};
  196. List<String> paths = getMatchingPaths(treeId, AndTreeFilter.create(tf));
  197. assertEquals(expected, paths);
  198. }
  199. @Test
  200. public void testAndMultiSubPathNegate() throws IOException {
  201. List<String> expected = Collections.singletonList("sub1/subb/b.txt");
  202. TreeFilter[] tf = new TreeFilter[] {PathFilter.create("sub1"),
  203. PathFilter.create("sub1/suba").negate()};
  204. List<String> paths = getMatchingPaths(treeId, AndTreeFilter.create(tf));
  205. assertEquals(expected, paths);
  206. }
  207. @Test
  208. public void testAndMultiThreeSubPathNegate() throws IOException {
  209. List<String> expected = Collections.singletonList("sub1/subb/b.txt");
  210. TreeFilter[] tf = new TreeFilter[]{PathFilter.create("sub1"),
  211. PathFilter.create("sub1/suba").negate(),
  212. PathFilter.create("no/path").negate()};
  213. List<String> paths = getMatchingPaths(treeId, AndTreeFilter.create(tf));
  214. assertEquals(expected, paths);
  215. }
  216. @Test
  217. public void testTopAndMultiPathDualNegate() throws IOException {
  218. List<String> expected = Arrays.asList("a.txt", "sub1.txt");
  219. TreeFilter[] tf = new TreeFilter[] {PathFilter.create("sub1").negate(),
  220. PathFilter.create("sub2").negate()};
  221. List<String> paths = getMatchingPathsFlat(treeId, AndTreeFilter.create(tf));
  222. assertEquals(expected, paths);
  223. }
  224. @Test
  225. public void testTopAndMultiSubPathDualNegate() throws IOException {
  226. List<String> expected = Arrays.asList("a.txt", "sub1.txt", "sub1");
  227. // Filter on 'sub1/suba' is kind of silly for a non-recursive walk.
  228. // The result is interesting though as the 'sub1' path should be
  229. // returned, due to the fact that there may be hits once the pattern
  230. // is tested with one of the leaf paths.
  231. TreeFilter[] tf = new TreeFilter[] {PathFilter.create("sub1/suba").negate(),
  232. PathFilter.create("sub2").negate()};
  233. List<String> paths = getMatchingPathsFlat(treeId, AndTreeFilter.create(tf));
  234. assertEquals(expected, paths);
  235. }
  236. @Test
  237. public void testTopOrMultiPathDual() throws IOException {
  238. List<String> expected = Arrays.asList("sub1.txt", "sub2");
  239. TreeFilter[] tf = new TreeFilter[] {PathFilter.create("sub1.txt"),
  240. PathFilter.create("sub2")};
  241. List<String> paths = getMatchingPathsFlat(treeId, OrTreeFilter.create(tf));
  242. assertEquals(expected, paths);
  243. }
  244. @Test
  245. public void testTopNotPath() throws IOException {
  246. List<String> expected = Arrays.asList("a.txt", "sub1.txt", "sub2");
  247. TreeFilter tf = PathFilter.create("sub1");
  248. List<String> paths = getMatchingPathsFlat(treeId, NotTreeFilter.create(tf));
  249. assertEquals(expected, paths);
  250. }
  251. private List<String> getMatchingPaths(final ObjectId objId,
  252. TreeFilter tf) throws IOException {
  253. return getMatchingPaths(objId, tf, true);
  254. }
  255. private List<String> getMatchingPathsFlat(final ObjectId objId,
  256. TreeFilter tf) throws IOException {
  257. return getMatchingPaths(objId, tf, false);
  258. }
  259. private List<String> getMatchingPaths(final ObjectId objId,
  260. TreeFilter tf, boolean recursive) throws IOException {
  261. try (TreeWalk tw = new TreeWalk(db)) {
  262. tw.setFilter(tf);
  263. tw.setRecursive(recursive);
  264. tw.addTree(objId);
  265. List<String> paths = new ArrayList<>();
  266. while (tw.next()) {
  267. paths.add(tw.getPathString());
  268. }
  269. return paths;
  270. }
  271. }
  272. private ObjectId createTree(String... paths) throws IOException {
  273. final ObjectInserter odi = db.newObjectInserter();
  274. final DirCache dc = db.readDirCache();
  275. final DirCacheBuilder builder = dc.builder();
  276. for (String path : paths) {
  277. DirCacheEntry entry = createEntry(path, FileMode.REGULAR_FILE);
  278. builder.add(entry);
  279. }
  280. builder.finish();
  281. final ObjectId objId = dc.writeTree(odi);
  282. odi.flush();
  283. return objId;
  284. }
  285. }