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.

NameConflictTreeWalk.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Copyright (C) 2008, Google Inc.
  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;
  44. import java.io.IOException;
  45. import org.eclipse.jgit.dircache.DirCacheBuilder;
  46. import org.eclipse.jgit.errors.CorruptObjectException;
  47. import org.eclipse.jgit.lib.FileMode;
  48. import org.eclipse.jgit.lib.ObjectReader;
  49. import org.eclipse.jgit.lib.Repository;
  50. /**
  51. * Specialized TreeWalk to detect directory-file (D/F) name conflicts.
  52. * <p>
  53. * Due to the way a Git tree is organized the standard {@link TreeWalk} won't
  54. * easily find a D/F conflict when merging two or more trees together. In the
  55. * standard TreeWalk the file will be returned first, and then much later the
  56. * directory will be returned. This makes it impossible for the application to
  57. * efficiently detect and handle the conflict.
  58. * <p>
  59. * Using this walk implementation causes the directory to report earlier than
  60. * usual, at the same time as the non-directory entry. This permits the
  61. * application to handle the D/F conflict in a single step. The directory is
  62. * returned only once, so it does not get returned later in the iteration.
  63. * <p>
  64. * When a D/F conflict is detected {@link TreeWalk#isSubtree()} will return true
  65. * and {@link TreeWalk#enterSubtree()} will recurse into the subtree, no matter
  66. * which iterator originally supplied the subtree.
  67. * <p>
  68. * Because conflicted directories report early, using this walk implementation
  69. * to populate a {@link DirCacheBuilder} may cause the automatic resorting to
  70. * run and fix the entry ordering.
  71. * <p>
  72. * This walk implementation requires more CPU to implement a look-ahead and a
  73. * look-behind to merge a D/F pair together, or to skip a previously reported
  74. * directory. In typical Git repositories the look-ahead cost is 0 and the
  75. * look-behind doesn't trigger, as users tend not to create trees which contain
  76. * both "foo" as a directory and "foo.c" as a file.
  77. * <p>
  78. * In the worst-case however several thousand look-ahead steps per walk step may
  79. * be necessary, making the overhead quite significant. Since this worst-case
  80. * should never happen this walk implementation has made the time/space tradeoff
  81. * in favor of more-time/less-space, as that better suits the typical case.
  82. */
  83. public class NameConflictTreeWalk extends TreeWalk {
  84. private static final int TREE_MODE = FileMode.TREE.getBits();
  85. private boolean fastMinHasMatch;
  86. private AbstractTreeIterator dfConflict;
  87. /**
  88. * Create a new tree walker for a given repository.
  89. *
  90. * @param repo
  91. * the repository the walker will obtain data from.
  92. */
  93. public NameConflictTreeWalk(final Repository repo) {
  94. super(repo);
  95. }
  96. /**
  97. * Create a new tree walker for a given repository.
  98. *
  99. * @param repo
  100. * the repository the walker will obtain data from.
  101. * @param or
  102. * the reader the walker will obtain tree data from.
  103. * @since 4.3
  104. */
  105. public NameConflictTreeWalk(Repository repo, final ObjectReader or) {
  106. super(repo, or);
  107. }
  108. /**
  109. * Create a new tree walker for a given repository.
  110. *
  111. * @param or
  112. * the reader the walker will obtain tree data from.
  113. */
  114. public NameConflictTreeWalk(final ObjectReader or) {
  115. super(or);
  116. }
  117. @Override
  118. AbstractTreeIterator min() throws CorruptObjectException {
  119. for (;;) {
  120. final AbstractTreeIterator minRef = fastMin();
  121. if (fastMinHasMatch)
  122. return minRef;
  123. if (isTree(minRef)) {
  124. if (skipEntry(minRef)) {
  125. for (final AbstractTreeIterator t : trees) {
  126. if (t.matches == minRef) {
  127. t.next(1);
  128. t.matches = null;
  129. }
  130. }
  131. continue;
  132. }
  133. return minRef;
  134. }
  135. return combineDF(minRef);
  136. }
  137. }
  138. private AbstractTreeIterator fastMin() {
  139. fastMinHasMatch = true;
  140. int i = 0;
  141. AbstractTreeIterator minRef = trees[i];
  142. while (minRef.eof() && ++i < trees.length)
  143. minRef = trees[i];
  144. if (minRef.eof())
  145. return minRef;
  146. boolean hasConflict = false;
  147. minRef.matches = minRef;
  148. while (++i < trees.length) {
  149. final AbstractTreeIterator t = trees[i];
  150. if (t.eof())
  151. continue;
  152. final int cmp = t.pathCompare(minRef);
  153. if (cmp < 0) {
  154. if (fastMinHasMatch && isTree(minRef) && !isTree(t)
  155. && nameEqual(minRef, t)) {
  156. // We used to be at a tree, but now we are at a file
  157. // with the same name. Allow the file to match the
  158. // tree anyway.
  159. //
  160. t.matches = minRef;
  161. hasConflict = true;
  162. } else {
  163. fastMinHasMatch = false;
  164. t.matches = t;
  165. minRef = t;
  166. }
  167. } else if (cmp == 0) {
  168. // Exact name/mode match is best.
  169. //
  170. t.matches = minRef;
  171. } else if (fastMinHasMatch && isTree(t) && !isTree(minRef)
  172. && nameEqual(t, minRef)) {
  173. // The minimum is a file (non-tree) but the next entry
  174. // of this iterator is a tree whose name matches our file.
  175. // This is a classic D/F conflict and commonly occurs like
  176. // this, with no gaps in between the file and directory.
  177. //
  178. // Use the tree as the minimum instead (see combineDF).
  179. //
  180. for (int k = 0; k < i; k++) {
  181. final AbstractTreeIterator p = trees[k];
  182. if (p.matches == minRef)
  183. p.matches = t;
  184. }
  185. t.matches = t;
  186. minRef = t;
  187. hasConflict = true;
  188. } else
  189. fastMinHasMatch = false;
  190. }
  191. if (hasConflict && fastMinHasMatch && dfConflict == null)
  192. dfConflict = minRef;
  193. return minRef;
  194. }
  195. private static boolean nameEqual(final AbstractTreeIterator a,
  196. final AbstractTreeIterator b) {
  197. return a.pathCompare(b, TREE_MODE) == 0;
  198. }
  199. private static boolean isTree(final AbstractTreeIterator p) {
  200. return FileMode.TREE.equals(p.mode);
  201. }
  202. private boolean skipEntry(final AbstractTreeIterator minRef)
  203. throws CorruptObjectException {
  204. // A tree D/F may have been handled earlier. We need to
  205. // not report this path if it has already been reported.
  206. //
  207. for (final AbstractTreeIterator t : trees) {
  208. if (t.matches == minRef || t.first())
  209. continue;
  210. int stepsBack = 0;
  211. for (;;) {
  212. stepsBack++;
  213. t.back(1);
  214. final int cmp = t.pathCompare(minRef, 0);
  215. if (cmp == 0) {
  216. // We have already seen this "$path" before. Skip it.
  217. //
  218. t.next(stepsBack);
  219. return true;
  220. } else if (cmp < 0 || t.first()) {
  221. // We cannot find "$path" in t; it will never appear.
  222. //
  223. t.next(stepsBack);
  224. break;
  225. }
  226. }
  227. }
  228. // We have never seen the current path before.
  229. //
  230. return false;
  231. }
  232. private AbstractTreeIterator combineDF(final AbstractTreeIterator minRef)
  233. throws CorruptObjectException {
  234. // Look for a possible D/F conflict forward in the tree(s)
  235. // as there may be a "$path/" which matches "$path". Make
  236. // such entries match this entry.
  237. //
  238. AbstractTreeIterator treeMatch = null;
  239. for (final AbstractTreeIterator t : trees) {
  240. if (t.matches == minRef || t.eof())
  241. continue;
  242. for (;;) {
  243. final int cmp = t.pathCompare(minRef, TREE_MODE);
  244. if (cmp < 0) {
  245. // The "$path/" may still appear later.
  246. //
  247. t.matchShift++;
  248. t.next(1);
  249. if (t.eof()) {
  250. t.back(t.matchShift);
  251. t.matchShift = 0;
  252. break;
  253. }
  254. } else if (cmp == 0) {
  255. // We have a conflict match here.
  256. //
  257. t.matches = minRef;
  258. treeMatch = t;
  259. break;
  260. } else {
  261. // A conflict match is not possible.
  262. //
  263. if (t.matchShift != 0) {
  264. t.back(t.matchShift);
  265. t.matchShift = 0;
  266. }
  267. break;
  268. }
  269. }
  270. }
  271. if (treeMatch != null) {
  272. // If we do have a conflict use one of the directory
  273. // matching iterators instead of the file iterator.
  274. // This way isSubtree is true and isRecursive works.
  275. //
  276. for (final AbstractTreeIterator t : trees)
  277. if (t.matches == minRef)
  278. t.matches = treeMatch;
  279. if (dfConflict == null)
  280. dfConflict = treeMatch;
  281. return treeMatch;
  282. }
  283. return minRef;
  284. }
  285. @Override
  286. void popEntriesEqual() throws CorruptObjectException {
  287. final AbstractTreeIterator ch = currentHead;
  288. for (int i = 0; i < trees.length; i++) {
  289. final AbstractTreeIterator t = trees[i];
  290. if (t.matches == ch) {
  291. if (t.matchShift == 0)
  292. t.next(1);
  293. else {
  294. t.back(t.matchShift);
  295. t.matchShift = 0;
  296. }
  297. t.matches = null;
  298. }
  299. }
  300. if (ch == dfConflict)
  301. dfConflict = null;
  302. }
  303. @Override
  304. void skipEntriesEqual() throws CorruptObjectException {
  305. final AbstractTreeIterator ch = currentHead;
  306. for (int i = 0; i < trees.length; i++) {
  307. final AbstractTreeIterator t = trees[i];
  308. if (t.matches == ch) {
  309. if (t.matchShift == 0)
  310. t.skip();
  311. else {
  312. t.back(t.matchShift);
  313. t.matchShift = 0;
  314. }
  315. t.matches = null;
  316. }
  317. }
  318. if (ch == dfConflict)
  319. dfConflict = null;
  320. }
  321. void stopWalk() throws IOException {
  322. if (!needsStopWalk()) {
  323. return;
  324. }
  325. // Name conflicts make aborting early difficult. Multiple paths may
  326. // exist between the file and directory versions of a name. To ensure
  327. // the directory version is skipped over (as it was previously visited
  328. // during the file version step) requires popping up the stack and
  329. // finishing out each subtree that the walker dove into. Siblings in
  330. // parents do not need to be recursed into, bounding the cost.
  331. for (;;) {
  332. AbstractTreeIterator t = min();
  333. if (t.eof()) {
  334. if (depth > 0) {
  335. exitSubtree();
  336. popEntriesEqual();
  337. continue;
  338. }
  339. return;
  340. }
  341. currentHead = t;
  342. skipEntriesEqual();
  343. }
  344. }
  345. private boolean needsStopWalk() {
  346. for (AbstractTreeIterator t : trees) {
  347. if (t.needsStopWalk()) {
  348. return true;
  349. }
  350. }
  351. return false;
  352. }
  353. /**
  354. * True if the current entry is covered by a directory/file conflict.
  355. *
  356. * This means that for some prefix of the current entry's path, this walk
  357. * has detected a directory/file conflict. Also true if the current entry
  358. * itself is a directory/file conflict.
  359. *
  360. * Example: If this TreeWalk points to foo/bar/a.txt and this method returns
  361. * true then you know that either for path foo or for path foo/bar files and
  362. * folders were detected.
  363. *
  364. * @return <code>true</code> if the current entry is covered by a
  365. * directory/file conflict, <code>false</code> otherwise
  366. */
  367. public boolean isDirectoryFileConflict() {
  368. return dfConflict != null;
  369. }
  370. }