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.

DirCacheBuilder.java 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  3. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.dircache;
  45. import static org.eclipse.jgit.lib.FileMode.TYPE_MASK;
  46. import static org.eclipse.jgit.lib.FileMode.TYPE_TREE;
  47. import java.io.IOException;
  48. import java.text.MessageFormat;
  49. import java.util.Arrays;
  50. import org.eclipse.jgit.internal.JGitText;
  51. import org.eclipse.jgit.lib.AnyObjectId;
  52. import org.eclipse.jgit.lib.ObjectReader;
  53. import org.eclipse.jgit.treewalk.CanonicalTreeParser;
  54. /**
  55. * Updates a {@link DirCache} by adding individual {@link DirCacheEntry}s.
  56. * <p>
  57. * A builder always starts from a clean slate and appends in every single
  58. * <code>DirCacheEntry</code> which the final updated index must have to reflect
  59. * its new content.
  60. * <p>
  61. * For maximum performance applications should add entries in path name order.
  62. * Adding entries out of order is permitted, however a final sorting pass will
  63. * be implicitly performed during {@link #finish()} to correct any out-of-order
  64. * entries. Duplicate detection is also delayed until the sorting is complete.
  65. *
  66. * @see DirCacheEditor
  67. */
  68. public class DirCacheBuilder extends BaseDirCacheEditor {
  69. private boolean sorted;
  70. /**
  71. * Construct a new builder.
  72. *
  73. * @param dc
  74. * the cache this builder will eventually update.
  75. * @param ecnt
  76. * estimated number of entries the builder will have upon
  77. * completion. This sizes the initial entry table.
  78. */
  79. protected DirCacheBuilder(final DirCache dc, final int ecnt) {
  80. super(dc, ecnt);
  81. }
  82. /**
  83. * Append one entry into the resulting entry list.
  84. * <p>
  85. * The entry is placed at the end of the entry list. If the entry causes the
  86. * list to now be incorrectly sorted a final sorting phase will be
  87. * automatically enabled within {@link #finish()}.
  88. * <p>
  89. * The internal entry table is automatically expanded if there is
  90. * insufficient space for the new addition.
  91. *
  92. * @param newEntry
  93. * the new entry to add.
  94. * @throws IllegalArgumentException
  95. * If the FileMode of the entry was not set by the caller.
  96. */
  97. public void add(final DirCacheEntry newEntry) {
  98. if (newEntry.getRawMode() == 0)
  99. throw new IllegalArgumentException(MessageFormat.format(
  100. JGitText.get().fileModeNotSetForPath,
  101. newEntry.getPathString()));
  102. beforeAdd(newEntry);
  103. fastAdd(newEntry);
  104. }
  105. /**
  106. * Add a range of existing entries from the destination cache.
  107. * <p>
  108. * The entries are placed at the end of the entry list. If any of the
  109. * entries causes the list to now be incorrectly sorted a final sorting
  110. * phase will be automatically enabled within {@link #finish()}.
  111. * <p>
  112. * This method copies from the destination cache, which has not yet been
  113. * updated with this editor's new table. So all offsets into the destination
  114. * cache are not affected by any updates that may be currently taking place
  115. * in this editor.
  116. * <p>
  117. * The internal entry table is automatically expanded if there is
  118. * insufficient space for the new additions.
  119. *
  120. * @param pos
  121. * first entry to copy from the destination cache.
  122. * @param cnt
  123. * number of entries to copy.
  124. */
  125. public void keep(final int pos, int cnt) {
  126. beforeAdd(cache.getEntry(pos));
  127. fastKeep(pos, cnt);
  128. }
  129. /**
  130. * Recursively add an entire tree into this builder.
  131. * <p>
  132. * If pathPrefix is "a/b" and the tree contains file "c" then the resulting
  133. * DirCacheEntry will have the path "a/b/c".
  134. * <p>
  135. * All entries are inserted at stage 0, therefore assuming that the
  136. * application will not insert any other paths with the same pathPrefix.
  137. *
  138. * @param pathPrefix
  139. * UTF-8 encoded prefix to mount the tree's entries at. If the
  140. * path does not end with '/' one will be automatically inserted
  141. * as necessary.
  142. * @param stage
  143. * stage of the entries when adding them.
  144. * @param reader
  145. * reader the tree(s) will be read from during recursive
  146. * traversal. This must be the same repository that the resulting
  147. * DirCache would be written out to (or used in) otherwise the
  148. * caller is simply asking for deferred MissingObjectExceptions.
  149. * Caller is responsible for releasing this reader when done.
  150. * @param tree
  151. * the tree to recursively add. This tree's contents will appear
  152. * under <code>pathPrefix</code>. The ObjectId must be that of a
  153. * tree; the caller is responsible for dereferencing a tag or
  154. * commit (if necessary).
  155. * @throws IOException
  156. * a tree cannot be read to iterate through its entries.
  157. */
  158. public void addTree(byte[] pathPrefix, int stage, ObjectReader reader,
  159. AnyObjectId tree) throws IOException {
  160. CanonicalTreeParser p = createTreeParser(pathPrefix, reader, tree);
  161. while (!p.eof()) {
  162. if (isTree(p)) {
  163. p = enterTree(p, reader);
  164. continue;
  165. }
  166. DirCacheEntry first = toEntry(stage, p);
  167. beforeAdd(first);
  168. fastAdd(first);
  169. p = p.next();
  170. break;
  171. }
  172. // Rest of tree entries are correctly sorted; use fastAdd().
  173. while (!p.eof()) {
  174. if (isTree(p)) {
  175. p = enterTree(p, reader);
  176. } else {
  177. fastAdd(toEntry(stage, p));
  178. p = p.next();
  179. }
  180. }
  181. }
  182. private static CanonicalTreeParser createTreeParser(byte[] pathPrefix,
  183. ObjectReader reader, AnyObjectId tree) throws IOException {
  184. return new CanonicalTreeParser(pathPrefix, reader, tree);
  185. }
  186. private static boolean isTree(CanonicalTreeParser p) {
  187. return (p.getEntryRawMode() & TYPE_MASK) == TYPE_TREE;
  188. }
  189. private static CanonicalTreeParser enterTree(CanonicalTreeParser p,
  190. ObjectReader reader) throws IOException {
  191. p = p.createSubtreeIterator(reader);
  192. return p.eof() ? p.next() : p;
  193. }
  194. private static DirCacheEntry toEntry(int stage, CanonicalTreeParser i) {
  195. byte[] buf = i.getEntryPathBuffer();
  196. int len = i.getEntryPathLength();
  197. byte[] path = new byte[len];
  198. System.arraycopy(buf, 0, path, 0, len);
  199. DirCacheEntry e = new DirCacheEntry(path, stage);
  200. e.setFileMode(i.getEntryRawMode());
  201. e.setObjectIdFromRaw(i.idBuffer(), i.idOffset());
  202. return e;
  203. }
  204. public void finish() {
  205. if (!sorted)
  206. resort();
  207. replace();
  208. }
  209. private void beforeAdd(final DirCacheEntry newEntry) {
  210. if (sorted && entryCnt > 0) {
  211. final DirCacheEntry lastEntry = entries[entryCnt - 1];
  212. final int cr = DirCache.cmp(lastEntry, newEntry);
  213. if (cr > 0) {
  214. // The new entry sorts before the old entry; we are
  215. // no longer sorted correctly. We'll need to redo
  216. // the sorting before we can close out the build.
  217. //
  218. sorted = false;
  219. } else if (cr == 0) {
  220. // Same file path; we can only insert this if the
  221. // stages won't be violated.
  222. //
  223. final int peStage = lastEntry.getStage();
  224. final int dceStage = newEntry.getStage();
  225. if (peStage == dceStage)
  226. throw bad(newEntry, JGitText.get().duplicateStagesNotAllowed);
  227. if (peStage == 0 || dceStage == 0)
  228. throw bad(newEntry, JGitText.get().mixedStagesNotAllowed);
  229. if (peStage > dceStage)
  230. sorted = false;
  231. }
  232. }
  233. }
  234. private void resort() {
  235. Arrays.sort(entries, 0, entryCnt, DirCache.ENT_CMP);
  236. for (int entryIdx = 1; entryIdx < entryCnt; entryIdx++) {
  237. final DirCacheEntry pe = entries[entryIdx - 1];
  238. final DirCacheEntry ce = entries[entryIdx];
  239. final int cr = DirCache.cmp(pe, ce);
  240. if (cr == 0) {
  241. // Same file path; we can only allow this if the stages
  242. // are 1-3 and no 0 exists.
  243. //
  244. final int peStage = pe.getStage();
  245. final int ceStage = ce.getStage();
  246. if (peStage == ceStage)
  247. throw bad(ce, JGitText.get().duplicateStagesNotAllowed);
  248. if (peStage == 0 || ceStage == 0)
  249. throw bad(ce, JGitText.get().mixedStagesNotAllowed);
  250. }
  251. }
  252. sorted = true;
  253. }
  254. private static IllegalStateException bad(DirCacheEntry a, String msg) {
  255. return new IllegalStateException(String.format(
  256. "%s: %d %s", //$NON-NLS-1$
  257. msg, Integer.valueOf(a.getStage()), a.getPathString()));
  258. }
  259. }