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.

RevCommitList.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*
  2. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.revwalk;
  44. import java.io.IOException;
  45. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  46. import org.eclipse.jgit.errors.MissingObjectException;
  47. import org.eclipse.jgit.revwalk.filter.RevFilter;
  48. /**
  49. * An ordered list of {@link RevCommit} subclasses.
  50. *
  51. * @param <E>
  52. * type of subclass of RevCommit the list is storing.
  53. */
  54. public class RevCommitList<E extends RevCommit> extends RevObjectList<E> {
  55. private RevWalk walker;
  56. @Override
  57. public void clear() {
  58. super.clear();
  59. walker = null;
  60. }
  61. /**
  62. * Apply a flag to all commits matching the specified filter.
  63. * <p>
  64. * Same as <code>applyFlag(matching, flag, 0, size())</code>, but without
  65. * the incremental behavior.
  66. *
  67. * @param matching
  68. * the filter to test commits with. If the filter includes a
  69. * commit it will have the flag set; if the filter does not
  70. * include the commit the flag will be unset.
  71. * @param flag
  72. * the flag to apply (or remove). Applications are responsible
  73. * for allocating this flag from the source RevWalk.
  74. * @throws IOException
  75. * revision filter needed to read additional objects, but an
  76. * error occurred while reading the pack files or loose objects
  77. * of the repository.
  78. * @throws IncorrectObjectTypeException
  79. * revision filter needed to read additional objects, but an
  80. * object was not of the correct type. Repository corruption may
  81. * have occurred.
  82. * @throws MissingObjectException
  83. * revision filter needed to read additional objects, but an
  84. * object that should be present was not found. Repository
  85. * corruption may have occurred.
  86. */
  87. public void applyFlag(final RevFilter matching, final RevFlag flag)
  88. throws MissingObjectException, IncorrectObjectTypeException,
  89. IOException {
  90. applyFlag(matching, flag, 0, size());
  91. }
  92. /**
  93. * Apply a flag to all commits matching the specified filter.
  94. * <p>
  95. * This version allows incremental testing and application, such as from a
  96. * background thread that needs to periodically halt processing and send
  97. * updates to the UI.
  98. *
  99. * @param matching
  100. * the filter to test commits with. If the filter includes a
  101. * commit it will have the flag set; if the filter does not
  102. * include the commit the flag will be unset.
  103. * @param flag
  104. * the flag to apply (or remove). Applications are responsible
  105. * for allocating this flag from the source RevWalk.
  106. * @param rangeBegin
  107. * first commit within the list to begin testing at, inclusive.
  108. * Must not be negative, but may be beyond the end of the list.
  109. * @param rangeEnd
  110. * last commit within the list to end testing at, exclusive. If
  111. * smaller than or equal to <code>rangeBegin</code> then no
  112. * commits will be tested.
  113. * @throws IOException
  114. * revision filter needed to read additional objects, but an
  115. * error occurred while reading the pack files or loose objects
  116. * of the repository.
  117. * @throws IncorrectObjectTypeException
  118. * revision filter needed to read additional objects, but an
  119. * object was not of the correct type. Repository corruption may
  120. * have occurred.
  121. * @throws MissingObjectException
  122. * revision filter needed to read additional objects, but an
  123. * object that should be present was not found. Repository
  124. * corruption may have occurred.
  125. */
  126. public void applyFlag(final RevFilter matching, final RevFlag flag,
  127. int rangeBegin, int rangeEnd) throws MissingObjectException,
  128. IncorrectObjectTypeException, IOException {
  129. final RevWalk w = flag.getRevWalk();
  130. rangeEnd = Math.min(rangeEnd, size());
  131. while (rangeBegin < rangeEnd) {
  132. int index = rangeBegin;
  133. Block s = contents;
  134. while (s.shift > 0) {
  135. final int i = index >> s.shift;
  136. index -= i << s.shift;
  137. s = (Block) s.contents[i];
  138. }
  139. while (rangeBegin++ < rangeEnd && index < BLOCK_SIZE) {
  140. final RevCommit c = (RevCommit) s.contents[index++];
  141. if (matching.include(w, c))
  142. c.add(flag);
  143. else
  144. c.remove(flag);
  145. }
  146. }
  147. }
  148. /**
  149. * Remove the given flag from all commits.
  150. * <p>
  151. * Same as <code>clearFlag(flag, 0, size())</code>, but without the
  152. * incremental behavior.
  153. *
  154. * @param flag
  155. * the flag to remove. Applications are responsible for
  156. * allocating this flag from the source RevWalk.
  157. */
  158. public void clearFlag(final RevFlag flag) {
  159. clearFlag(flag, 0, size());
  160. }
  161. /**
  162. * Remove the given flag from all commits.
  163. * <p>
  164. * This method is actually implemented in terms of:
  165. * <code>applyFlag(RevFilter.NONE, flag, rangeBegin, rangeEnd)</code>.
  166. *
  167. * @param flag
  168. * the flag to remove. Applications are responsible for
  169. * allocating this flag from the source RevWalk.
  170. * @param rangeBegin
  171. * first commit within the list to begin testing at, inclusive.
  172. * Must not be negative, but may be beyond the end of the list.
  173. * @param rangeEnd
  174. * last commit within the list to end testing at, exclusive. If
  175. * smaller than or equal to <code>rangeBegin</code> then no
  176. * commits will be tested.
  177. */
  178. public void clearFlag(final RevFlag flag, final int rangeBegin,
  179. final int rangeEnd) {
  180. try {
  181. applyFlag(RevFilter.NONE, flag, rangeBegin, rangeEnd);
  182. } catch (IOException e) {
  183. // Never happen. The filter we use does not throw any
  184. // exceptions, for any reason.
  185. }
  186. }
  187. /**
  188. * Find the next commit that has the given flag set.
  189. *
  190. * @param flag
  191. * the flag to test commits against.
  192. * @param begin
  193. * first commit index to test at. Applications may wish to begin
  194. * at 0, to test the first commit in the list.
  195. * @return index of the first commit at or after index <code>begin</code>
  196. * that has the specified flag set on it; -1 if no match is found.
  197. */
  198. public int indexOf(final RevFlag flag, int begin) {
  199. while (begin < size()) {
  200. int index = begin;
  201. Block s = contents;
  202. while (s.shift > 0) {
  203. final int i = index >> s.shift;
  204. index -= i << s.shift;
  205. s = (Block) s.contents[i];
  206. }
  207. while (begin++ < size() && index < BLOCK_SIZE) {
  208. final RevCommit c = (RevCommit) s.contents[index++];
  209. if (c.has(flag))
  210. return begin;
  211. }
  212. }
  213. return -1;
  214. }
  215. /**
  216. * Find the next commit that has the given flag set.
  217. *
  218. * @param flag
  219. * the flag to test commits against.
  220. * @param begin
  221. * first commit index to test at. Applications may wish to begin
  222. * at <code>size()-1</code>, to test the last commit in the
  223. * list.
  224. * @return index of the first commit at or before index <code>begin</code>
  225. * that has the specified flag set on it; -1 if no match is found.
  226. */
  227. public int lastIndexOf(final RevFlag flag, int begin) {
  228. begin = Math.min(begin, size() - 1);
  229. while (begin >= 0) {
  230. int index = begin;
  231. Block s = contents;
  232. while (s.shift > 0) {
  233. final int i = index >> s.shift;
  234. index -= i << s.shift;
  235. s = (Block) s.contents[i];
  236. }
  237. while (begin-- >= 0 && index >= 0) {
  238. final RevCommit c = (RevCommit) s.contents[index--];
  239. if (c.has(flag))
  240. return begin;
  241. }
  242. }
  243. return -1;
  244. }
  245. /**
  246. * Set the revision walker this list populates itself from.
  247. *
  248. * @param w
  249. * the walker to populate from.
  250. * @see #fillTo(int)
  251. */
  252. public void source(final RevWalk w) {
  253. walker = w;
  254. }
  255. /**
  256. * Is this list still pending more items?
  257. *
  258. * @return true if {@link #fillTo(int)} might be able to extend the list
  259. * size when called.
  260. */
  261. public boolean isPending() {
  262. return walker != null;
  263. }
  264. /**
  265. * Ensure this list contains at least a specified number of commits.
  266. * <p>
  267. * The revision walker specified by {@link #source(RevWalk)} is pumped until
  268. * the given number of commits are contained in this list. If there are
  269. * fewer total commits available from the walk then the method will return
  270. * early. Callers can test the final size of the list by {@link #size()} to
  271. * determine if the high water mark specified was met.
  272. *
  273. * @param highMark
  274. * number of commits the caller wants this list to contain when
  275. * the fill operation is complete.
  276. * @throws IOException
  277. * see {@link RevWalk#next()}
  278. * @throws IncorrectObjectTypeException
  279. * see {@link RevWalk#next()}
  280. * @throws MissingObjectException
  281. * see {@link RevWalk#next()}
  282. */
  283. public void fillTo(final int highMark) throws MissingObjectException,
  284. IncorrectObjectTypeException, IOException {
  285. if (walker == null || size > highMark)
  286. return;
  287. RevCommit c = walker.next();
  288. if (c == null) {
  289. walker = null;
  290. return;
  291. }
  292. enter(size, (E) c);
  293. add((E) c);
  294. while (size <= highMark) {
  295. int index = size;
  296. Block s = contents;
  297. while (index >> s.shift >= BLOCK_SIZE) {
  298. s = new Block(s.shift + BLOCK_SHIFT);
  299. s.contents[0] = contents;
  300. contents = s;
  301. }
  302. while (s.shift > 0) {
  303. final int i = index >> s.shift;
  304. index -= i << s.shift;
  305. if (s.contents[i] == null)
  306. s.contents[i] = new Block(s.shift - BLOCK_SHIFT);
  307. s = (Block) s.contents[i];
  308. }
  309. final Object[] dst = s.contents;
  310. while (size <= highMark && index < BLOCK_SIZE) {
  311. c = walker.next();
  312. if (c == null) {
  313. walker = null;
  314. return;
  315. }
  316. enter(size++, (E) c);
  317. dst[index++] = c;
  318. }
  319. }
  320. }
  321. /**
  322. * Ensures all commits until the given commit are loaded. The revision
  323. * walker specified by {@link #source(RevWalk)} is pumped until the
  324. * specified commit is loaded. Callers can test the final size of the list
  325. * by {@link #size()} to determine if the high water mark specified was met.
  326. * <p>
  327. *
  328. * @param commitToLoad
  329. * commit the caller wants this list to contain when the fill
  330. * operation is complete.
  331. * @param highMark
  332. * maximum number of commits the caller wants this list to
  333. * contain when the fill operation is complete. If highMark is 0
  334. * the walk is pumped until the specified commit or the end of
  335. * the walk is reached.
  336. * @throws IOException
  337. * see {@link RevWalk#next()}
  338. * @throws IncorrectObjectTypeException
  339. * see {@link RevWalk#next()}
  340. * @throws MissingObjectException
  341. * see {@link RevWalk#next()}
  342. */
  343. public void fillTo(final RevCommit commitToLoad, int highMark)
  344. throws MissingObjectException, IncorrectObjectTypeException,
  345. IOException {
  346. if (walker == null || commitToLoad == null
  347. || (highMark > 0 && size > highMark))
  348. return;
  349. RevCommit c = walker.next();
  350. if (c == null) {
  351. walker = null;
  352. return;
  353. }
  354. enter(size, (E) c);
  355. add((E) c);
  356. while ((highMark == 0 || size <= highMark) && !c.equals(commitToLoad)) {
  357. int index = size;
  358. Block s = contents;
  359. while (index >> s.shift >= BLOCK_SIZE) {
  360. s = new Block(s.shift + BLOCK_SHIFT);
  361. s.contents[0] = contents;
  362. contents = s;
  363. }
  364. while (s.shift > 0) {
  365. final int i = index >> s.shift;
  366. index -= i << s.shift;
  367. if (s.contents[i] == null)
  368. s.contents[i] = new Block(s.shift - BLOCK_SHIFT);
  369. s = (Block) s.contents[i];
  370. }
  371. final Object[] dst = s.contents;
  372. while ((highMark == 0 || size <= highMark) && index < BLOCK_SIZE
  373. && !c.equals(commitToLoad)) {
  374. c = walker.next();
  375. if (c == null) {
  376. walker = null;
  377. return;
  378. }
  379. enter(size++, (E) c);
  380. dst[index++] = c;
  381. }
  382. }
  383. }
  384. /**
  385. * Optional callback invoked when commits enter the list by fillTo.
  386. * <p>
  387. * This method is only called during {@link #fillTo(int)}.
  388. *
  389. * @param index
  390. * the list position this object will appear at.
  391. * @param e
  392. * the object being added (or set) into the list.
  393. */
  394. protected void enter(final int index, final E e) {
  395. // Do nothing by default.
  396. }
  397. }