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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 org.eclipse.jgit.revwalk.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. /** {@inheritDoc} */
  57. @Override
  58. public void clear() {
  59. super.clear();
  60. walker = null;
  61. }
  62. /**
  63. * Apply a flag to all commits matching the specified filter.
  64. * <p>
  65. * Same as <code>applyFlag(matching, flag, 0, size())</code>, but without
  66. * the incremental behavior.
  67. *
  68. * @param matching
  69. * the filter to test commits with. If the filter includes a
  70. * commit it will have the flag set; if the filter does not
  71. * include the commit the flag will be unset.
  72. * @param flag
  73. * the flag to apply (or remove). Applications are responsible
  74. * for allocating this flag from the source RevWalk.
  75. * @throws java.io.IOException
  76. * revision filter needed to read additional objects, but an
  77. * error occurred while reading the pack files or loose objects
  78. * of the repository.
  79. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  80. * revision filter needed to read additional objects, but an
  81. * object was not of the correct type. Repository corruption may
  82. * have occurred.
  83. * @throws org.eclipse.jgit.errors.MissingObjectException
  84. * revision filter needed to read additional objects, but an
  85. * object that should be present was not found. Repository
  86. * corruption may have occurred.
  87. */
  88. public void applyFlag(RevFilter matching, RevFlag flag)
  89. throws MissingObjectException, IncorrectObjectTypeException,
  90. IOException {
  91. applyFlag(matching, flag, 0, size());
  92. }
  93. /**
  94. * Apply a flag to all commits matching the specified filter.
  95. * <p>
  96. * This version allows incremental testing and application, such as from a
  97. * background thread that needs to periodically halt processing and send
  98. * updates to the UI.
  99. *
  100. * @param matching
  101. * the filter to test commits with. If the filter includes a
  102. * commit it will have the flag set; if the filter does not
  103. * include the commit the flag will be unset.
  104. * @param flag
  105. * the flag to apply (or remove). Applications are responsible
  106. * for allocating this flag from the source RevWalk.
  107. * @param rangeBegin
  108. * first commit within the list to begin testing at, inclusive.
  109. * Must not be negative, but may be beyond the end of the list.
  110. * @param rangeEnd
  111. * last commit within the list to end testing at, exclusive. If
  112. * smaller than or equal to <code>rangeBegin</code> then no
  113. * commits will be tested.
  114. * @throws java.io.IOException
  115. * revision filter needed to read additional objects, but an
  116. * error occurred while reading the pack files or loose objects
  117. * of the repository.
  118. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  119. * revision filter needed to read additional objects, but an
  120. * object was not of the correct type. Repository corruption may
  121. * have occurred.
  122. * @throws org.eclipse.jgit.errors.MissingObjectException
  123. * revision filter needed to read additional objects, but an
  124. * object that should be present was not found. Repository
  125. * corruption may have occurred.
  126. */
  127. public void applyFlag(final RevFilter matching, final RevFlag flag,
  128. int rangeBegin, int rangeEnd) throws MissingObjectException,
  129. IncorrectObjectTypeException, IOException {
  130. final RevWalk w = flag.getRevWalk();
  131. rangeEnd = Math.min(rangeEnd, size());
  132. while (rangeBegin < rangeEnd) {
  133. int index = rangeBegin;
  134. Block s = contents;
  135. while (s.shift > 0) {
  136. final int i = index >> s.shift;
  137. index -= i << s.shift;
  138. s = (Block) s.contents[i];
  139. }
  140. while (rangeBegin++ < rangeEnd && index < BLOCK_SIZE) {
  141. final RevCommit c = (RevCommit) s.contents[index++];
  142. if (matching.include(w, c))
  143. c.add(flag);
  144. else
  145. c.remove(flag);
  146. }
  147. }
  148. }
  149. /**
  150. * Remove the given flag from all commits.
  151. * <p>
  152. * Same as <code>clearFlag(flag, 0, size())</code>, but without the
  153. * incremental behavior.
  154. *
  155. * @param flag
  156. * the flag to remove. Applications are responsible for
  157. * allocating this flag from the source RevWalk.
  158. */
  159. public void clearFlag(RevFlag flag) {
  160. clearFlag(flag, 0, size());
  161. }
  162. /**
  163. * Remove the given flag from all commits.
  164. * <p>
  165. * This method is actually implemented in terms of:
  166. * <code>applyFlag(RevFilter.NONE, flag, rangeBegin, rangeEnd)</code>.
  167. *
  168. * @param flag
  169. * the flag to remove. Applications are responsible for
  170. * allocating this flag from the source RevWalk.
  171. * @param rangeBegin
  172. * first commit within the list to begin testing at, inclusive.
  173. * Must not be negative, but may be beyond the end of the list.
  174. * @param rangeEnd
  175. * last commit within the list to end testing at, exclusive. If
  176. * smaller than or equal to <code>rangeBegin</code> then no
  177. * commits will be tested.
  178. */
  179. public void clearFlag(final RevFlag flag, final int rangeBegin,
  180. final int rangeEnd) {
  181. try {
  182. applyFlag(RevFilter.NONE, flag, rangeBegin, rangeEnd);
  183. } catch (IOException e) {
  184. // Never happen. The filter we use does not throw any
  185. // exceptions, for any reason.
  186. }
  187. }
  188. /**
  189. * Find the next commit that has the given flag set.
  190. *
  191. * @param flag
  192. * the flag to test commits against.
  193. * @param begin
  194. * first commit index to test at. Applications may wish to begin
  195. * at 0, to test the first commit in the list.
  196. * @return index of the first commit at or after index <code>begin</code>
  197. * that has the specified flag set on it; -1 if no match is found.
  198. */
  199. public int indexOf(RevFlag flag, int begin) {
  200. while (begin < size()) {
  201. int index = begin;
  202. Block s = contents;
  203. while (s.shift > 0) {
  204. final int i = index >> s.shift;
  205. index -= i << s.shift;
  206. s = (Block) s.contents[i];
  207. }
  208. while (begin++ < size() && index < BLOCK_SIZE) {
  209. final RevCommit c = (RevCommit) s.contents[index++];
  210. if (c.has(flag))
  211. return begin;
  212. }
  213. }
  214. return -1;
  215. }
  216. /**
  217. * Find the next commit that has the given flag set.
  218. *
  219. * @param flag
  220. * the flag to test commits against.
  221. * @param begin
  222. * first commit index to test at. Applications may wish to begin
  223. * at <code>size()-1</code>, to test the last commit in the
  224. * list.
  225. * @return index of the first commit at or before index <code>begin</code>
  226. * that has the specified flag set on it; -1 if no match is found.
  227. */
  228. public int lastIndexOf(RevFlag flag, int begin) {
  229. begin = Math.min(begin, size() - 1);
  230. while (begin >= 0) {
  231. int index = begin;
  232. Block s = contents;
  233. while (s.shift > 0) {
  234. final int i = index >> s.shift;
  235. index -= i << s.shift;
  236. s = (Block) s.contents[i];
  237. }
  238. while (begin-- >= 0 && index >= 0) {
  239. final RevCommit c = (RevCommit) s.contents[index--];
  240. if (c.has(flag))
  241. return begin;
  242. }
  243. }
  244. return -1;
  245. }
  246. /**
  247. * Set the revision walker this list populates itself from.
  248. *
  249. * @param w
  250. * the walker to populate from.
  251. * @see #fillTo(int)
  252. */
  253. public void source(RevWalk w) {
  254. walker = w;
  255. }
  256. /**
  257. * Is this list still pending more items?
  258. *
  259. * @return true if {@link #fillTo(int)} might be able to extend the list
  260. * size when called.
  261. */
  262. public boolean isPending() {
  263. return walker != null;
  264. }
  265. /**
  266. * Ensure this list contains at least a specified number of commits.
  267. * <p>
  268. * The revision walker specified by {@link #source(RevWalk)} is pumped until
  269. * the given number of commits are contained in this list. If there are
  270. * fewer total commits available from the walk then the method will return
  271. * early. Callers can test the final size of the list by {@link #size()} to
  272. * determine if the high water mark specified was met.
  273. *
  274. * @param highMark
  275. * number of commits the caller wants this list to contain when
  276. * the fill operation is complete.
  277. * @throws java.io.IOException
  278. * see {@link org.eclipse.jgit.revwalk.RevWalk#next()}
  279. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  280. * see {@link org.eclipse.jgit.revwalk.RevWalk#next()}
  281. * @throws org.eclipse.jgit.errors.MissingObjectException
  282. * see {@link org.eclipse.jgit.revwalk.RevWalk#next()}
  283. */
  284. @SuppressWarnings("unchecked")
  285. public void fillTo(int highMark) throws MissingObjectException,
  286. IncorrectObjectTypeException, IOException {
  287. if (walker == null || size > highMark)
  288. return;
  289. RevCommit c = walker.next();
  290. if (c == null) {
  291. walker = null;
  292. return;
  293. }
  294. enter(size, (E) c);
  295. add((E) c);
  296. while (size <= highMark) {
  297. int index = size;
  298. Block s = contents;
  299. while (index >> s.shift >= BLOCK_SIZE) {
  300. s = new Block(s.shift + BLOCK_SHIFT);
  301. s.contents[0] = contents;
  302. contents = s;
  303. }
  304. while (s.shift > 0) {
  305. final int i = index >> s.shift;
  306. index -= i << s.shift;
  307. if (s.contents[i] == null)
  308. s.contents[i] = new Block(s.shift - BLOCK_SHIFT);
  309. s = (Block) s.contents[i];
  310. }
  311. final Object[] dst = s.contents;
  312. while (size <= highMark && index < BLOCK_SIZE) {
  313. c = walker.next();
  314. if (c == null) {
  315. walker = null;
  316. return;
  317. }
  318. enter(size++, (E) c);
  319. dst[index++] = c;
  320. }
  321. }
  322. }
  323. /**
  324. * Ensures all commits until the given commit are loaded. The revision
  325. * walker specified by {@link #source(RevWalk)} is pumped until the
  326. * specified commit is loaded. Callers can test the final size of the list
  327. * by {@link #size()} to determine if the high water mark specified was met.
  328. * <p>
  329. *
  330. * @param commitToLoad
  331. * commit the caller wants this list to contain when the fill
  332. * operation is complete.
  333. * @param highMark
  334. * maximum number of commits the caller wants this list to
  335. * contain when the fill operation is complete. If highMark is 0
  336. * the walk is pumped until the specified commit or the end of
  337. * the walk is reached.
  338. * @throws java.io.IOException
  339. * see {@link org.eclipse.jgit.revwalk.RevWalk#next()}
  340. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  341. * see {@link org.eclipse.jgit.revwalk.RevWalk#next()}
  342. * @throws org.eclipse.jgit.errors.MissingObjectException
  343. * see {@link org.eclipse.jgit.revwalk.RevWalk#next()}
  344. */
  345. @SuppressWarnings("unchecked")
  346. public void fillTo(RevCommit commitToLoad, int highMark)
  347. throws MissingObjectException, IncorrectObjectTypeException,
  348. IOException {
  349. if (walker == null || commitToLoad == null
  350. || (highMark > 0 && size > highMark))
  351. return;
  352. RevCommit c = walker.next();
  353. if (c == null) {
  354. walker = null;
  355. return;
  356. }
  357. enter(size, (E) c);
  358. add((E) c);
  359. while ((highMark == 0 || size <= highMark) && !c.equals(commitToLoad)) {
  360. int index = size;
  361. Block s = contents;
  362. while (index >> s.shift >= BLOCK_SIZE) {
  363. s = new Block(s.shift + BLOCK_SHIFT);
  364. s.contents[0] = contents;
  365. contents = s;
  366. }
  367. while (s.shift > 0) {
  368. final int i = index >> s.shift;
  369. index -= i << s.shift;
  370. if (s.contents[i] == null)
  371. s.contents[i] = new Block(s.shift - BLOCK_SHIFT);
  372. s = (Block) s.contents[i];
  373. }
  374. final Object[] dst = s.contents;
  375. while ((highMark == 0 || size <= highMark) && index < BLOCK_SIZE
  376. && !c.equals(commitToLoad)) {
  377. c = walker.next();
  378. if (c == null) {
  379. walker = null;
  380. return;
  381. }
  382. enter(size++, (E) c);
  383. dst[index++] = c;
  384. }
  385. }
  386. }
  387. /**
  388. * Optional callback invoked when commits enter the list by fillTo.
  389. * <p>
  390. * This method is only called during {@link #fillTo(int)}.
  391. *
  392. * @param index
  393. * the list position this object will appear at.
  394. * @param e
  395. * the object being added (or set) into the list.
  396. */
  397. protected void enter(int index, E e) {
  398. // Do nothing by default.
  399. }
  400. }