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.

RevWalkTextBuiltin.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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.pgm;
  44. import java.text.MessageFormat;
  45. import java.util.ArrayList;
  46. import java.util.EnumSet;
  47. import java.util.List;
  48. import org.eclipse.jgit.diff.DiffConfig;
  49. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  50. import org.eclipse.jgit.lib.Constants;
  51. import org.eclipse.jgit.lib.ObjectId;
  52. import org.eclipse.jgit.lib.Ref;
  53. import org.eclipse.jgit.pgm.internal.CLIText;
  54. import org.eclipse.jgit.pgm.opt.PathTreeFilterHandler;
  55. import org.eclipse.jgit.revwalk.FollowFilter;
  56. import org.eclipse.jgit.revwalk.ObjectWalk;
  57. import org.eclipse.jgit.revwalk.RevCommit;
  58. import org.eclipse.jgit.revwalk.RevFlag;
  59. import org.eclipse.jgit.revwalk.RevObject;
  60. import org.eclipse.jgit.revwalk.RevSort;
  61. import org.eclipse.jgit.revwalk.RevWalk;
  62. import org.eclipse.jgit.revwalk.filter.AndRevFilter;
  63. import org.eclipse.jgit.revwalk.filter.AuthorRevFilter;
  64. import org.eclipse.jgit.revwalk.filter.CommitterRevFilter;
  65. import org.eclipse.jgit.revwalk.filter.MessageRevFilter;
  66. import org.eclipse.jgit.revwalk.filter.RevFilter;
  67. import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
  68. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  69. import org.kohsuke.args4j.Argument;
  70. import org.kohsuke.args4j.Option;
  71. abstract class RevWalkTextBuiltin extends TextBuiltin {
  72. RevWalk walk;
  73. @Option(name = "--objects")
  74. boolean objects = false;
  75. @Option(name = "--parents")
  76. boolean parents = false;
  77. @Option(name = "--total-count")
  78. boolean count = false;
  79. @Option(name = "--all")
  80. boolean all = false;
  81. char[] outbuffer = new char[Constants.OBJECT_ID_LENGTH * 2];
  82. private final EnumSet<RevSort> sorting = EnumSet.noneOf(RevSort.class);
  83. private void enableRevSort(RevSort type, boolean on) {
  84. if (on)
  85. sorting.add(type);
  86. else
  87. sorting.remove(type);
  88. }
  89. @Option(name = "--date-order")
  90. void enableDateOrder(boolean on) {
  91. enableRevSort(RevSort.COMMIT_TIME_DESC, on);
  92. }
  93. @Option(name = "--topo-order")
  94. void enableTopoOrder(boolean on) {
  95. enableRevSort(RevSort.TOPO, on);
  96. }
  97. @Option(name = "--reverse")
  98. void enableReverse(boolean on) {
  99. enableRevSort(RevSort.REVERSE, on);
  100. }
  101. @Option(name = "--boundary")
  102. void enableBoundary(boolean on) {
  103. enableRevSort(RevSort.BOUNDARY, on);
  104. }
  105. @Option(name = "--follow", metaVar = "metaVar_path")
  106. private String followPath;
  107. @Argument(index = 0, metaVar = "metaVar_commitish")
  108. private List<RevCommit> commits = new ArrayList<>();
  109. @Option(name = "--", metaVar = "metaVar_path", handler = PathTreeFilterHandler.class)
  110. protected TreeFilter pathFilter = TreeFilter.ALL;
  111. private final List<RevFilter> revLimiter = new ArrayList<>();
  112. @Option(name = "--author")
  113. void addAuthorRevFilter(String who) {
  114. revLimiter.add(AuthorRevFilter.create(who));
  115. }
  116. @Option(name = "--committer")
  117. void addCommitterRevFilter(String who) {
  118. revLimiter.add(CommitterRevFilter.create(who));
  119. }
  120. @Option(name = "--grep")
  121. void addCMessageRevFilter(String msg) {
  122. revLimiter.add(MessageRevFilter.create(msg));
  123. }
  124. @Option(name = "--max-count", aliases = "-n", metaVar = "metaVar_n")
  125. private int maxCount = -1;
  126. /** {@inheritDoc} */
  127. @Override
  128. protected void run() throws Exception {
  129. walk = createWalk();
  130. for (RevSort s : sorting)
  131. walk.sort(s, true);
  132. if (pathFilter == TreeFilter.ALL) {
  133. if (followPath != null)
  134. walk.setTreeFilter(FollowFilter.create(followPath,
  135. db.getConfig().get(DiffConfig.KEY)));
  136. } else if (pathFilter != TreeFilter.ALL) {
  137. walk.setTreeFilter(AndTreeFilter.create(pathFilter,
  138. TreeFilter.ANY_DIFF));
  139. }
  140. if (revLimiter.size() == 1)
  141. walk.setRevFilter(revLimiter.get(0));
  142. else if (revLimiter.size() > 1)
  143. walk.setRevFilter(AndRevFilter.create(revLimiter));
  144. if (all) {
  145. for (Ref a : db.getRefDatabase().getRefs()) {
  146. ObjectId oid = a.getPeeledObjectId();
  147. if (oid == null)
  148. oid = a.getObjectId();
  149. try {
  150. commits.add(walk.parseCommit(oid));
  151. } catch (IncorrectObjectTypeException e) {
  152. // Ignore all refs which are not commits
  153. }
  154. }
  155. }
  156. if (commits.isEmpty()) {
  157. final ObjectId head = db.resolve(Constants.HEAD);
  158. if (head == null)
  159. throw die(MessageFormat.format(CLIText.get().cannotResolve, Constants.HEAD));
  160. commits.add(walk.parseCommit(head));
  161. }
  162. for (RevCommit c : commits) {
  163. final RevCommit real = argWalk == walk ? c : walk.parseCommit(c);
  164. if (c.has(RevFlag.UNINTERESTING))
  165. walk.markUninteresting(real);
  166. else
  167. walk.markStart(real);
  168. }
  169. final long start = System.currentTimeMillis();
  170. final int n = walkLoop();
  171. if (count) {
  172. final long end = System.currentTimeMillis();
  173. errw.print(n);
  174. errw.print(' ');
  175. errw.println(MessageFormat.format(
  176. CLIText.get().timeInMilliSeconds,
  177. Long.valueOf(end - start)));
  178. }
  179. }
  180. /**
  181. * Create RevWalk
  182. *
  183. * @return a {@link org.eclipse.jgit.revwalk.RevWalk} object.
  184. */
  185. protected RevWalk createWalk() {
  186. RevWalk result;
  187. if (objects)
  188. result = new ObjectWalk(db);
  189. else if (argWalk != null)
  190. result = argWalk;
  191. else
  192. result = argWalk = new RevWalk(db);
  193. result.setRewriteParents(false);
  194. return result;
  195. }
  196. /**
  197. * Loop the walk
  198. *
  199. * @return number of RevCommits walked
  200. * @throws java.lang.Exception
  201. * if any.
  202. */
  203. protected int walkLoop() throws Exception {
  204. int n = 0;
  205. for (RevCommit c : walk) {
  206. if (++n > maxCount && maxCount >= 0)
  207. break;
  208. show(c);
  209. }
  210. if (walk instanceof ObjectWalk) {
  211. final ObjectWalk ow = (ObjectWalk) walk;
  212. for (;;) {
  213. final RevObject obj = ow.nextObject();
  214. if (obj == null)
  215. break;
  216. show(ow, obj);
  217. }
  218. }
  219. return n;
  220. }
  221. /**
  222. * "Show" the current RevCommit when called from the main processing loop.
  223. * <p>
  224. * Implement this methods to define the behavior for subclasses of
  225. * RevWalkTextBuiltin.
  226. *
  227. * @param c
  228. * The current {@link org.eclipse.jgit.revwalk.RevCommit}
  229. * @throws java.lang.Exception
  230. */
  231. protected abstract void show(RevCommit c) throws Exception;
  232. /**
  233. * "Show" the current RevCommit when called from the main processing loop.
  234. * <p>
  235. * The default implementation does nothing because most subclasses only
  236. * process RevCommits.
  237. *
  238. * @param objectWalk
  239. * the {@link org.eclipse.jgit.revwalk.ObjectWalk} used by
  240. * {@link #walkLoop()}
  241. * @param currentObject
  242. * The current {@link org.eclipse.jgit.revwalk.RevObject}
  243. * @throws java.lang.Exception
  244. */
  245. protected void show(final ObjectWalk objectWalk,
  246. final RevObject currentObject) throws Exception {
  247. // Do nothing by default. Most applications cannot show an object.
  248. }
  249. }