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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 java.io.IOException;
  46. import java.text.MessageFormat;
  47. import java.util.Arrays;
  48. import org.eclipse.jgit.internal.JGitText;
  49. import org.eclipse.jgit.lib.AnyObjectId;
  50. import org.eclipse.jgit.lib.ObjectReader;
  51. import org.eclipse.jgit.treewalk.AbstractTreeIterator;
  52. import org.eclipse.jgit.treewalk.CanonicalTreeParser;
  53. import org.eclipse.jgit.treewalk.TreeWalk;
  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(JGitText.get().fileModeNotSetForPath
  100. , newEntry.getPathString()));
  101. beforeAdd(newEntry);
  102. fastAdd(newEntry);
  103. }
  104. /**
  105. * Add a range of existing entries from the destination cache.
  106. * <p>
  107. * The entries are placed at the end of the entry list. If any of the
  108. * entries causes the list to now be incorrectly sorted a final sorting
  109. * phase will be automatically enabled within {@link #finish()}.
  110. * <p>
  111. * This method copies from the destination cache, which has not yet been
  112. * updated with this editor's new table. So all offsets into the destination
  113. * cache are not affected by any updates that may be currently taking place
  114. * in this editor.
  115. * <p>
  116. * The internal entry table is automatically expanded if there is
  117. * insufficient space for the new additions.
  118. *
  119. * @param pos
  120. * first entry to copy from the destination cache.
  121. * @param cnt
  122. * number of entries to copy.
  123. */
  124. public void keep(final int pos, int cnt) {
  125. beforeAdd(cache.getEntry(pos));
  126. fastKeep(pos, cnt);
  127. }
  128. /**
  129. * Recursively add an entire tree into this builder.
  130. * <p>
  131. * If pathPrefix is "a/b" and the tree contains file "c" then the resulting
  132. * DirCacheEntry will have the path "a/b/c".
  133. * <p>
  134. * All entries are inserted at stage 0, therefore assuming that the
  135. * application will not insert any other paths with the same pathPrefix.
  136. *
  137. * @param pathPrefix
  138. * UTF-8 encoded prefix to mount the tree's entries at. If the
  139. * path does not end with '/' one will be automatically inserted
  140. * as necessary.
  141. * @param stage
  142. * stage of the entries when adding them.
  143. * @param reader
  144. * reader the tree(s) will be read from during recursive
  145. * traversal. This must be the same repository that the resulting
  146. * DirCache would be written out to (or used in) otherwise the
  147. * caller is simply asking for deferred MissingObjectExceptions.
  148. * Caller is responsible for releasing this reader when done.
  149. * @param tree
  150. * the tree to recursively add. This tree's contents will appear
  151. * under <code>pathPrefix</code>. The ObjectId must be that of a
  152. * tree; the caller is responsible for dereferencing a tag or
  153. * commit (if necessary).
  154. * @throws IOException
  155. * a tree cannot be read to iterate through its entries.
  156. */
  157. public void addTree(final byte[] pathPrefix, final int stage,
  158. final ObjectReader reader, final AnyObjectId tree) throws IOException {
  159. final TreeWalk tw = new TreeWalk(reader);
  160. tw.addTree(new CanonicalTreeParser(pathPrefix, reader, tree
  161. .toObjectId()));
  162. tw.setRecursive(true);
  163. if (tw.next()) {
  164. final DirCacheEntry newEntry = toEntry(stage, tw);
  165. beforeAdd(newEntry);
  166. fastAdd(newEntry);
  167. while (tw.next())
  168. fastAdd(toEntry(stage, tw));
  169. }
  170. }
  171. private DirCacheEntry toEntry(final int stage, final TreeWalk tw) {
  172. final DirCacheEntry e = new DirCacheEntry(tw.getRawPath(), stage);
  173. final AbstractTreeIterator i;
  174. i = tw.getTree(0, AbstractTreeIterator.class);
  175. e.setFileMode(tw.getFileMode(0));
  176. e.setObjectIdFromRaw(i.idBuffer(), i.idOffset());
  177. return e;
  178. }
  179. public void finish() {
  180. if (!sorted)
  181. resort();
  182. replace();
  183. }
  184. private void beforeAdd(final DirCacheEntry newEntry) {
  185. if (sorted && entryCnt > 0) {
  186. final DirCacheEntry lastEntry = entries[entryCnt - 1];
  187. final int cr = DirCache.cmp(lastEntry, newEntry);
  188. if (cr > 0) {
  189. // The new entry sorts before the old entry; we are
  190. // no longer sorted correctly. We'll need to redo
  191. // the sorting before we can close out the build.
  192. //
  193. sorted = false;
  194. } else if (cr == 0) {
  195. // Same file path; we can only insert this if the
  196. // stages won't be violated.
  197. //
  198. final int peStage = lastEntry.getStage();
  199. final int dceStage = newEntry.getStage();
  200. if (peStage == dceStage)
  201. throw bad(newEntry, JGitText.get().duplicateStagesNotAllowed);
  202. if (peStage == 0 || dceStage == 0)
  203. throw bad(newEntry, JGitText.get().mixedStagesNotAllowed);
  204. if (peStage > dceStage)
  205. sorted = false;
  206. }
  207. }
  208. }
  209. private void resort() {
  210. Arrays.sort(entries, 0, entryCnt, DirCache.ENT_CMP);
  211. for (int entryIdx = 1; entryIdx < entryCnt; entryIdx++) {
  212. final DirCacheEntry pe = entries[entryIdx - 1];
  213. final DirCacheEntry ce = entries[entryIdx];
  214. final int cr = DirCache.cmp(pe, ce);
  215. if (cr == 0) {
  216. // Same file path; we can only allow this if the stages
  217. // are 1-3 and no 0 exists.
  218. //
  219. final int peStage = pe.getStage();
  220. final int ceStage = ce.getStage();
  221. if (peStage == ceStage)
  222. throw bad(ce, JGitText.get().duplicateStagesNotAllowed);
  223. if (peStage == 0 || ceStage == 0)
  224. throw bad(ce, JGitText.get().mixedStagesNotAllowed);
  225. }
  226. }
  227. sorted = true;
  228. }
  229. private static IllegalStateException bad(final DirCacheEntry a,
  230. final String msg) {
  231. return new IllegalStateException(msg + ": " + a.getStage() + " " //$NON-NLS-1$ //$NON-NLS-2$
  232. + a.getPathString());
  233. }
  234. }