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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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.internal.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. if (missing) {
  137. final DirCacheEntry ent = new DirCacheEntry(e.path);
  138. e.apply(ent);
  139. if (ent.getRawMode() == 0)
  140. throw new IllegalArgumentException(MessageFormat.format(JGitText.get().fileModeNotSetForPath
  141. , ent.getPathString()));
  142. fastAdd(ent);
  143. } else {
  144. // Apply to all entries of the current path (different stages)
  145. for (int i = eIdx; i < lastIdx; i++) {
  146. final DirCacheEntry ent = cache.getEntry(i);
  147. e.apply(ent);
  148. fastAdd(ent);
  149. }
  150. }
  151. }
  152. final int cnt = maxIdx - lastIdx;
  153. if (cnt > 0)
  154. fastKeep(lastIdx, cnt);
  155. }
  156. /**
  157. * Any index record update.
  158. * <p>
  159. * Applications should subclass and provide their own implementation for the
  160. * {@link #apply(DirCacheEntry)} method. The editor will invoke apply once
  161. * for each record in the index which matches the path name. If there are
  162. * multiple records (for example in stages 1, 2 and 3), the edit instance
  163. * will be called multiple times, once for each stage.
  164. */
  165. public abstract static class PathEdit {
  166. final byte[] path;
  167. /**
  168. * Create a new update command by path name.
  169. *
  170. * @param entryPath
  171. * path of the file within the repository.
  172. */
  173. public PathEdit(final String entryPath) {
  174. path = Constants.encode(entryPath);
  175. }
  176. /**
  177. * Create a new update command for an existing entry instance.
  178. *
  179. * @param ent
  180. * entry instance to match path of. Only the path of this
  181. * entry is actually considered during command evaluation.
  182. */
  183. public PathEdit(final DirCacheEntry ent) {
  184. path = ent.path;
  185. }
  186. /**
  187. * Apply the update to a single cache entry matching the path.
  188. * <p>
  189. * After apply is invoked the entry is added to the output table, and
  190. * will be included in the new index.
  191. *
  192. * @param ent
  193. * the entry being processed. All fields are zeroed out if
  194. * the path is a new path in the index.
  195. */
  196. public abstract void apply(DirCacheEntry ent);
  197. }
  198. /**
  199. * Deletes a single file entry from the index.
  200. * <p>
  201. * This deletion command removes only a single file at the given location,
  202. * but removes multiple stages (if present) for that path. To remove a
  203. * complete subtree use {@link DeleteTree} instead.
  204. *
  205. * @see DeleteTree
  206. */
  207. public static final class DeletePath extends PathEdit {
  208. /**
  209. * Create a new deletion command by path name.
  210. *
  211. * @param entryPath
  212. * path of the file within the repository.
  213. */
  214. public DeletePath(final String entryPath) {
  215. super(entryPath);
  216. }
  217. /**
  218. * Create a new deletion command for an existing entry instance.
  219. *
  220. * @param ent
  221. * entry instance to remove. Only the path of this entry is
  222. * actually considered during command evaluation.
  223. */
  224. public DeletePath(final DirCacheEntry ent) {
  225. super(ent);
  226. }
  227. public void apply(final DirCacheEntry ent) {
  228. throw new UnsupportedOperationException(JGitText.get().noApplyInDelete);
  229. }
  230. }
  231. /**
  232. * Recursively deletes all paths under a subtree.
  233. * <p>
  234. * This deletion command is more generic than {@link DeletePath} as it can
  235. * remove all records which appear recursively under the same subtree.
  236. * Multiple stages are removed (if present) for any deleted entry.
  237. * <p>
  238. * This command will not remove a single file entry. To remove a single file
  239. * use {@link DeletePath}.
  240. *
  241. * @see DeletePath
  242. */
  243. public static final class DeleteTree extends PathEdit {
  244. /**
  245. * Create a new tree deletion command by path name.
  246. *
  247. * @param entryPath
  248. * path of the subtree within the repository. If the path
  249. * does not end with "/" a "/" is implicitly added to ensure
  250. * only the subtree's contents are matched by the command.
  251. * The special case "" (not "/"!) deletes all entries.
  252. */
  253. public DeleteTree(final String entryPath) {
  254. super(
  255. (entryPath.endsWith("/") || entryPath.length() == 0) ? entryPath //$NON-NLS-1$
  256. : entryPath + "/"); //$NON-NLS-1$
  257. }
  258. public void apply(final DirCacheEntry ent) {
  259. throw new UnsupportedOperationException(JGitText.get().noApplyInDelete);
  260. }
  261. }
  262. }