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.

DirCacheEditor.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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.ArrayList;
  48. import java.util.Collections;
  49. import java.util.Comparator;
  50. import java.util.List;
  51. import org.eclipse.jgit.JGitText;
  52. import org.eclipse.jgit.lib.Constants;
  53. /**
  54. * Updates a {@link DirCache} by supplying discrete edit commands.
  55. * <p>
  56. * An editor updates a DirCache by taking a list of {@link PathEdit} commands
  57. * and executing them against the entries of the destination cache to produce a
  58. * new cache. This edit style allows applications to insert a few commands and
  59. * then have the editor compute the proper entry indexes necessary to perform an
  60. * efficient in-order update of the index records. This can be easier to use
  61. * than {@link DirCacheBuilder}.
  62. * <p>
  63. *
  64. * @see DirCacheBuilder
  65. */
  66. public class DirCacheEditor extends BaseDirCacheEditor {
  67. private static final Comparator<PathEdit> EDIT_CMP = new Comparator<PathEdit>() {
  68. public int compare(final PathEdit o1, final PathEdit o2) {
  69. final byte[] a = o1.path;
  70. final byte[] b = o2.path;
  71. return DirCache.cmp(a, a.length, b, b.length);
  72. }
  73. };
  74. private final List<PathEdit> edits;
  75. /**
  76. * Construct a new editor.
  77. *
  78. * @param dc
  79. * the cache this editor will eventually update.
  80. * @param ecnt
  81. * estimated number of entries the editor will have upon
  82. * completion. This sizes the initial entry table.
  83. */
  84. protected DirCacheEditor(final DirCache dc, final int ecnt) {
  85. super(dc, ecnt);
  86. edits = new ArrayList<PathEdit>();
  87. }
  88. /**
  89. * Append one edit command to the list of commands to be applied.
  90. * <p>
  91. * Edit commands may be added in any order chosen by the application. They
  92. * are automatically rearranged by the builder to provide the most efficient
  93. * update possible.
  94. *
  95. * @param edit
  96. * another edit command.
  97. */
  98. public void add(final PathEdit edit) {
  99. edits.add(edit);
  100. }
  101. @Override
  102. public boolean commit() throws IOException {
  103. if (edits.isEmpty()) {
  104. // No changes? Don't rewrite the index.
  105. //
  106. cache.unlock();
  107. return true;
  108. }
  109. return super.commit();
  110. }
  111. public void finish() {
  112. if (!edits.isEmpty()) {
  113. applyEdits();
  114. replace();
  115. }
  116. }
  117. private void applyEdits() {
  118. Collections.sort(edits, EDIT_CMP);
  119. final int maxIdx = cache.getEntryCount();
  120. int lastIdx = 0;
  121. for (final PathEdit e : edits) {
  122. int eIdx = cache.findEntry(e.path, e.path.length);
  123. final boolean missing = eIdx < 0;
  124. if (eIdx < 0)
  125. eIdx = -(eIdx + 1);
  126. final int cnt = Math.min(eIdx, maxIdx) - lastIdx;
  127. if (cnt > 0)
  128. fastKeep(lastIdx, cnt);
  129. lastIdx = missing ? eIdx : cache.nextEntry(eIdx);
  130. if (e instanceof DeletePath)
  131. continue;
  132. if (e instanceof DeleteTree) {
  133. lastIdx = cache.nextEntry(e.path, e.path.length, eIdx);
  134. continue;
  135. }
  136. final DirCacheEntry ent;
  137. if (missing) {
  138. ent = new DirCacheEntry(e.path);
  139. e.apply(ent);
  140. if (ent.getRawMode() == 0)
  141. throw new IllegalArgumentException(MessageFormat.format(JGitText.get().fileModeNotSetForPath
  142. , ent.getPathString()));
  143. } else {
  144. ent = cache.getEntry(eIdx);
  145. e.apply(ent);
  146. }
  147. fastAdd(ent);
  148. }
  149. final int cnt = maxIdx - lastIdx;
  150. if (cnt > 0)
  151. fastKeep(lastIdx, cnt);
  152. }
  153. /**
  154. * Any index record update.
  155. * <p>
  156. * Applications should subclass and provide their own implementation for the
  157. * {@link #apply(DirCacheEntry)} method. The editor will invoke apply once
  158. * for each record in the index which matches the path name. If there are
  159. * multiple records (for example in stages 1, 2 and 3), the edit instance
  160. * will be called multiple times, once for each stage.
  161. */
  162. public abstract static class PathEdit {
  163. final byte[] path;
  164. /**
  165. * Create a new update command by path name.
  166. *
  167. * @param entryPath
  168. * path of the file within the repository.
  169. */
  170. public PathEdit(final String entryPath) {
  171. path = Constants.encode(entryPath);
  172. }
  173. /**
  174. * Create a new update command for an existing entry instance.
  175. *
  176. * @param ent
  177. * entry instance to match path of. Only the path of this
  178. * entry is actually considered during command evaluation.
  179. */
  180. public PathEdit(final DirCacheEntry ent) {
  181. path = ent.path;
  182. }
  183. /**
  184. * Apply the update to a single cache entry matching the path.
  185. * <p>
  186. * After apply is invoked the entry is added to the output table, and
  187. * will be included in the new index.
  188. *
  189. * @param ent
  190. * the entry being processed. All fields are zeroed out if
  191. * the path is a new path in the index.
  192. */
  193. public abstract void apply(DirCacheEntry ent);
  194. }
  195. /**
  196. * Deletes a single file entry from the index.
  197. * <p>
  198. * This deletion command removes only a single file at the given location,
  199. * but removes multiple stages (if present) for that path. To remove a
  200. * complete subtree use {@link DeleteTree} instead.
  201. *
  202. * @see DeleteTree
  203. */
  204. public static final class DeletePath extends PathEdit {
  205. /**
  206. * Create a new deletion command by path name.
  207. *
  208. * @param entryPath
  209. * path of the file within the repository.
  210. */
  211. public DeletePath(final String entryPath) {
  212. super(entryPath);
  213. }
  214. /**
  215. * Create a new deletion command for an existing entry instance.
  216. *
  217. * @param ent
  218. * entry instance to remove. Only the path of this entry is
  219. * actually considered during command evaluation.
  220. */
  221. public DeletePath(final DirCacheEntry ent) {
  222. super(ent);
  223. }
  224. public void apply(final DirCacheEntry ent) {
  225. throw new UnsupportedOperationException(JGitText.get().noApplyInDelete);
  226. }
  227. }
  228. /**
  229. * Recursively deletes all paths under a subtree.
  230. * <p>
  231. * This deletion command is more generic than {@link DeletePath} as it can
  232. * remove all records which appear recursively under the same subtree.
  233. * Multiple stages are removed (if present) for any deleted entry.
  234. * <p>
  235. * This command will not remove a single file entry. To remove a single file
  236. * use {@link DeletePath}.
  237. *
  238. * @see DeletePath
  239. */
  240. public static final class DeleteTree extends PathEdit {
  241. /**
  242. * Create a new tree deletion command by path name.
  243. *
  244. * @param entryPath
  245. * path of the subtree within the repository. If the path
  246. * does not end with "/" a "/" is implicitly added to ensure
  247. * only the subtree's contents are matched by the command.
  248. */
  249. public DeleteTree(final String entryPath) {
  250. super(entryPath.endsWith("/") ? entryPath : entryPath + "/");
  251. }
  252. public void apply(final DirCacheEntry ent) {
  253. throw new UnsupportedOperationException(JGitText.get().noApplyInDelete);
  254. }
  255. }
  256. }