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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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.util.Arrays;
  47. import org.eclipse.jgit.lib.AnyObjectId;
  48. import org.eclipse.jgit.lib.Repository;
  49. import org.eclipse.jgit.lib.WindowCursor;
  50. import org.eclipse.jgit.treewalk.AbstractTreeIterator;
  51. import org.eclipse.jgit.treewalk.CanonicalTreeParser;
  52. import org.eclipse.jgit.treewalk.TreeWalk;
  53. /**
  54. * Updates a {@link DirCache} by adding individual {@link DirCacheEntry}s.
  55. * <p>
  56. * A builder always starts from a clean slate and appends in every single
  57. * <code>DirCacheEntry</code> which the final updated index must have to reflect
  58. * its new content.
  59. * <p>
  60. * For maximum performance applications should add entries in path name order.
  61. * Adding entries out of order is permitted, however a final sorting pass will
  62. * be implicitly performed during {@link #finish()} to correct any out-of-order
  63. * entries. Duplicate detection is also delayed until the sorting is complete.
  64. *
  65. * @see DirCacheEditor
  66. */
  67. public class DirCacheBuilder extends BaseDirCacheEditor {
  68. private boolean sorted;
  69. /**
  70. * Construct a new builder.
  71. *
  72. * @param dc
  73. * the cache this builder will eventually update.
  74. * @param ecnt
  75. * estimated number of entries the builder will have upon
  76. * completion. This sizes the initial entry table.
  77. */
  78. protected DirCacheBuilder(final DirCache dc, final int ecnt) {
  79. super(dc, ecnt);
  80. }
  81. /**
  82. * Append one entry into the resulting entry list.
  83. * <p>
  84. * The entry is placed at the end of the entry list. If the entry causes the
  85. * list to now be incorrectly sorted a final sorting phase will be
  86. * automatically enabled within {@link #finish()}.
  87. * <p>
  88. * The internal entry table is automatically expanded if there is
  89. * insufficient space for the new addition.
  90. *
  91. * @param newEntry
  92. * the new entry to add.
  93. * @throws IllegalArgumentException
  94. * If the FileMode of the entry was not set by the caller.
  95. */
  96. public void add(final DirCacheEntry newEntry) {
  97. if (newEntry.getRawMode() == 0)
  98. throw new IllegalArgumentException("FileMode not set for path "
  99. + newEntry.getPathString());
  100. beforeAdd(newEntry);
  101. fastAdd(newEntry);
  102. }
  103. /**
  104. * Add a range of existing entries from the destination cache.
  105. * <p>
  106. * The entries are placed at the end of the entry list. If any of the
  107. * entries causes the list to now be incorrectly sorted a final sorting
  108. * phase will be automatically enabled within {@link #finish()}.
  109. * <p>
  110. * This method copies from the destination cache, which has not yet been
  111. * updated with this editor's new table. So all offsets into the destination
  112. * cache are not affected by any updates that may be currently taking place
  113. * in this editor.
  114. * <p>
  115. * The internal entry table is automatically expanded if there is
  116. * insufficient space for the new additions.
  117. *
  118. * @param pos
  119. * first entry to copy from the destination cache.
  120. * @param cnt
  121. * number of entries to copy.
  122. */
  123. public void keep(final int pos, int cnt) {
  124. beforeAdd(cache.getEntry(pos));
  125. fastKeep(pos, cnt);
  126. }
  127. /**
  128. * Recursively add an entire tree into this builder.
  129. * <p>
  130. * If pathPrefix is "a/b" and the tree contains file "c" then the resulting
  131. * DirCacheEntry will have the path "a/b/c".
  132. * <p>
  133. * All entries are inserted at stage 0, therefore assuming that the
  134. * application will not insert any other paths with the same pathPrefix.
  135. *
  136. * @param pathPrefix
  137. * UTF-8 encoded prefix to mount the tree's entries at. If the
  138. * path does not end with '/' one will be automatically inserted
  139. * as necessary.
  140. * @param stage
  141. * stage of the entries when adding them.
  142. * @param db
  143. * repository the tree(s) will be read from during recursive
  144. * traversal. This must be the same repository that the resulting
  145. * DirCache would be written out to (or used in) otherwise the
  146. * caller is simply asking for deferred MissingObjectExceptions.
  147. * @param tree
  148. * the tree to recursively add. This tree's contents will appear
  149. * under <code>pathPrefix</code>. The ObjectId must be that of a
  150. * tree; the caller is responsible for dereferencing a tag or
  151. * commit (if necessary).
  152. * @throws IOException
  153. * a tree cannot be read to iterate through its entries.
  154. */
  155. public void addTree(final byte[] pathPrefix, final int stage,
  156. final Repository db, final AnyObjectId tree) throws IOException {
  157. final TreeWalk tw = new TreeWalk(db);
  158. tw.reset();
  159. final WindowCursor curs = new WindowCursor();
  160. try {
  161. tw.addTree(new CanonicalTreeParser(pathPrefix, db, tree
  162. .toObjectId(), curs));
  163. } finally {
  164. curs.release();
  165. }
  166. tw.setRecursive(true);
  167. if (tw.next()) {
  168. final DirCacheEntry newEntry = toEntry(stage, tw);
  169. beforeAdd(newEntry);
  170. fastAdd(newEntry);
  171. while (tw.next())
  172. fastAdd(toEntry(stage, tw));
  173. }
  174. }
  175. private DirCacheEntry toEntry(final int stage, final TreeWalk tw) {
  176. final DirCacheEntry e = new DirCacheEntry(tw.getRawPath(), stage);
  177. final AbstractTreeIterator i;
  178. i = tw.getTree(0, AbstractTreeIterator.class);
  179. e.setFileMode(tw.getFileMode(0));
  180. e.setObjectIdFromRaw(i.idBuffer(), i.idOffset());
  181. return e;
  182. }
  183. public void finish() {
  184. if (!sorted)
  185. resort();
  186. replace();
  187. }
  188. private void beforeAdd(final DirCacheEntry newEntry) {
  189. if (sorted && entryCnt > 0) {
  190. final DirCacheEntry lastEntry = entries[entryCnt - 1];
  191. final int cr = DirCache.cmp(lastEntry, newEntry);
  192. if (cr > 0) {
  193. // The new entry sorts before the old entry; we are
  194. // no longer sorted correctly. We'll need to redo
  195. // the sorting before we can close out the build.
  196. //
  197. sorted = false;
  198. } else if (cr == 0) {
  199. // Same file path; we can only insert this if the
  200. // stages won't be violated.
  201. //
  202. final int peStage = lastEntry.getStage();
  203. final int dceStage = newEntry.getStage();
  204. if (peStage == dceStage)
  205. throw bad(newEntry, "Duplicate stages not allowed");
  206. if (peStage == 0 || dceStage == 0)
  207. throw bad(newEntry, "Mixed stages not allowed");
  208. if (peStage > dceStage)
  209. sorted = false;
  210. }
  211. }
  212. }
  213. private void resort() {
  214. Arrays.sort(entries, 0, entryCnt, DirCache.ENT_CMP);
  215. for (int entryIdx = 1; entryIdx < entryCnt; entryIdx++) {
  216. final DirCacheEntry pe = entries[entryIdx - 1];
  217. final DirCacheEntry ce = entries[entryIdx];
  218. final int cr = DirCache.cmp(pe, ce);
  219. if (cr == 0) {
  220. // Same file path; we can only allow this if the stages
  221. // are 1-3 and no 0 exists.
  222. //
  223. final int peStage = pe.getStage();
  224. final int ceStage = ce.getStage();
  225. if (peStage == ceStage)
  226. throw bad(ce, "Duplicate stages not allowed");
  227. if (peStage == 0 || ceStage == 0)
  228. throw bad(ce, "Mixed stages not allowed");
  229. }
  230. }
  231. sorted = true;
  232. }
  233. private static IllegalStateException bad(final DirCacheEntry a,
  234. final String msg) {
  235. return new IllegalStateException(msg + ": " + a.getStage() + " "
  236. + a.getPathString());
  237. }
  238. }