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 11KB

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