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 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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.kohsuke.args4j.Argument;
  49. import org.kohsuke.args4j.Option;
  50. import org.eclipse.jgit.lib.Constants;
  51. import org.eclipse.jgit.lib.ObjectId;
  52. import org.eclipse.jgit.pgm.opt.PathTreeFilterHandler;
  53. import org.eclipse.jgit.revwalk.ObjectWalk;
  54. import org.eclipse.jgit.revwalk.RevCommit;
  55. import org.eclipse.jgit.revwalk.RevFlag;
  56. import org.eclipse.jgit.revwalk.RevObject;
  57. import org.eclipse.jgit.revwalk.RevSort;
  58. import org.eclipse.jgit.revwalk.RevWalk;
  59. import org.eclipse.jgit.revwalk.filter.AndRevFilter;
  60. import org.eclipse.jgit.revwalk.filter.AuthorRevFilter;
  61. import org.eclipse.jgit.revwalk.filter.CommitterRevFilter;
  62. import org.eclipse.jgit.revwalk.filter.MessageRevFilter;
  63. import org.eclipse.jgit.revwalk.filter.RevFilter;
  64. import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
  65. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  66. abstract class RevWalkTextBuiltin extends TextBuiltin {
  67. RevWalk walk;
  68. @Option(name = "--objects")
  69. boolean objects = false;
  70. @Option(name = "--parents")
  71. boolean parents = false;
  72. @Option(name = "--total-count")
  73. boolean count = false;
  74. char[] outbuffer = new char[Constants.OBJECT_ID_LENGTH * 2];
  75. private final EnumSet<RevSort> sorting = EnumSet.noneOf(RevSort.class);
  76. private void enableRevSort(final RevSort type, final boolean on) {
  77. if (on)
  78. sorting.add(type);
  79. else
  80. sorting.remove(type);
  81. }
  82. @Option(name = "--date-order")
  83. void enableDateOrder(final boolean on) {
  84. enableRevSort(RevSort.COMMIT_TIME_DESC, on);
  85. }
  86. @Option(name = "--topo-order")
  87. void enableTopoOrder(final boolean on) {
  88. enableRevSort(RevSort.TOPO, on);
  89. }
  90. @Option(name = "--reverse")
  91. void enableReverse(final boolean on) {
  92. enableRevSort(RevSort.REVERSE, on);
  93. }
  94. @Option(name = "--boundary")
  95. void enableBoundary(final boolean on) {
  96. enableRevSort(RevSort.BOUNDARY, on);
  97. }
  98. @Argument(index = 0, metaVar = "metaVar_commitish")
  99. private final List<RevCommit> commits = new ArrayList<RevCommit>();
  100. @Option(name = "--", metaVar = "metaVar_path", multiValued = true, handler = PathTreeFilterHandler.class)
  101. private TreeFilter pathFilter = TreeFilter.ALL;
  102. private final List<RevFilter> revLimiter = new ArrayList<RevFilter>();
  103. @Option(name = "--author")
  104. void addAuthorRevFilter(final String who) {
  105. revLimiter.add(AuthorRevFilter.create(who));
  106. }
  107. @Option(name = "--committer")
  108. void addCommitterRevFilter(final String who) {
  109. revLimiter.add(CommitterRevFilter.create(who));
  110. }
  111. @Option(name = "--grep")
  112. void addCMessageRevFilter(final String msg) {
  113. revLimiter.add(MessageRevFilter.create(msg));
  114. }
  115. @Override
  116. protected void run() throws Exception {
  117. walk = createWalk();
  118. for (final RevSort s : sorting)
  119. walk.sort(s, true);
  120. if (pathFilter != TreeFilter.ALL)
  121. walk.setTreeFilter(AndTreeFilter.create(pathFilter,
  122. TreeFilter.ANY_DIFF));
  123. if (revLimiter.size() == 1)
  124. walk.setRevFilter(revLimiter.get(0));
  125. else if (revLimiter.size() > 1)
  126. walk.setRevFilter(AndRevFilter.create(revLimiter));
  127. if (commits.isEmpty()) {
  128. final ObjectId head = db.resolve(Constants.HEAD);
  129. if (head == null)
  130. throw die(MessageFormat.format(CLIText.get().cannotResolve, Constants.HEAD));
  131. commits.add(walk.parseCommit(head));
  132. }
  133. for (final RevCommit c : commits) {
  134. final RevCommit real = argWalk == walk ? c : walk.parseCommit(c);
  135. if (c.has(RevFlag.UNINTERESTING))
  136. walk.markUninteresting(real);
  137. else
  138. walk.markStart(real);
  139. }
  140. final long start = System.currentTimeMillis();
  141. final int n = walkLoop();
  142. if (count) {
  143. final long end = System.currentTimeMillis();
  144. System.err.print(n);
  145. System.err.print(' ');
  146. System.err.println(MessageFormat.format(
  147. CLIText.get().timeInMilliSeconds, end - start));
  148. }
  149. }
  150. protected RevWalk createWalk() {
  151. if (objects)
  152. return new ObjectWalk(db);
  153. if (argWalk == null)
  154. argWalk = new RevWalk(db);
  155. return argWalk;
  156. }
  157. protected int walkLoop() throws Exception {
  158. int n = 0;
  159. for (final RevCommit c : walk) {
  160. n++;
  161. show(c);
  162. }
  163. if (walk instanceof ObjectWalk) {
  164. final ObjectWalk ow = (ObjectWalk) walk;
  165. for (;;) {
  166. final RevObject obj = ow.nextObject();
  167. if (obj == null)
  168. break;
  169. show(ow, obj);
  170. }
  171. }
  172. return n;
  173. }
  174. protected abstract void show(final RevCommit c) throws Exception;
  175. protected void show(final ObjectWalk objectWalk,
  176. final RevObject currentObject) throws Exception {
  177. // Do nothing by default. Most applications cannot show an object.
  178. }
  179. }