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

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