Move org.spearce.jgit.pgm to its own Java project
[egit/graphgui.git] / org.spearce.jgit.pgm / src / org / spearce / jgit / pgm / RevWalkTextBuiltin.java
blobb99276752955f78b66a24fcdfa0c79dc38a6601b
1 /*
2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
21 * written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org.spearce.jgit.pgm;
40 import java.util.ArrayList;
41 import java.util.EnumSet;
42 import java.util.List;
44 import org.spearce.jgit.lib.Constants;
45 import org.spearce.jgit.revwalk.ObjectWalk;
46 import org.spearce.jgit.revwalk.RevCommit;
47 import org.spearce.jgit.revwalk.RevObject;
48 import org.spearce.jgit.revwalk.RevSort;
49 import org.spearce.jgit.revwalk.RevWalk;
50 import org.spearce.jgit.revwalk.filter.AndRevFilter;
51 import org.spearce.jgit.revwalk.filter.AuthorRevFilter;
52 import org.spearce.jgit.revwalk.filter.CommitterRevFilter;
53 import org.spearce.jgit.revwalk.filter.MessageRevFilter;
54 import org.spearce.jgit.revwalk.filter.RevFilter;
55 import org.spearce.jgit.treewalk.filter.AndTreeFilter;
56 import org.spearce.jgit.treewalk.filter.PathFilter;
57 import org.spearce.jgit.treewalk.filter.PathFilterGroup;
58 import org.spearce.jgit.treewalk.filter.TreeFilter;
60 abstract class RevWalkTextBuiltin extends TextBuiltin {
61 RevWalk walk;
63 boolean objects = false;
65 boolean parents = false;
67 boolean count = false;
69 char[] outbuffer = new char[Constants.OBJECT_ID_LENGTH * 2];
71 @Override
72 final void execute(String[] args) throws Exception {
73 final EnumSet<RevSort> sorting = EnumSet.noneOf(RevSort.class);
74 final List<String> argList = new ArrayList<String>();
75 final List<RevFilter> revLimiter = new ArrayList<RevFilter>();
76 List<PathFilter> pathLimiter = null;
77 for (final String a : args) {
78 if (pathLimiter != null)
79 pathLimiter.add(PathFilter.create(a));
80 else if ("--".equals(a))
81 pathLimiter = new ArrayList<PathFilter>();
82 else if (a.startsWith("--author="))
83 revLimiter.add(AuthorRevFilter.create(a.substring("--author="
84 .length())));
85 else if (a.startsWith("--committer="))
86 revLimiter.add(CommitterRevFilter.create(a
87 .substring("--committer=".length())));
88 else if (a.startsWith("--grep="))
89 revLimiter.add(MessageRevFilter.create(a.substring("--grep="
90 .length())));
91 else if ("--objects".equals(a))
92 objects = true;
93 else if ("--date-order".equals(a))
94 sorting.add(RevSort.COMMIT_TIME_DESC);
95 else if ("--topo-order".equals(a))
96 sorting.add(RevSort.TOPO);
97 else if ("--reverse".equals(a))
98 sorting.add(RevSort.REVERSE);
99 else if ("--boundary".equals(a))
100 sorting.add(RevSort.BOUNDARY);
101 else if ("--parents".equals(a))
102 parents = true;
103 else if ("--total-count".equals(a))
104 count = true;
105 else
106 argList.add(a);
109 walk = createWalk();
110 for (final RevSort s : sorting)
111 walk.sort(s, true);
113 if (pathLimiter != null && !pathLimiter.isEmpty())
114 walk.setTreeFilter(AndTreeFilter.create(PathFilterGroup
115 .create(pathLimiter), TreeFilter.ANY_DIFF));
117 if (revLimiter.size() == 1)
118 walk.setRevFilter(revLimiter.get(0));
119 else if (revLimiter.size() > 1)
120 walk.setRevFilter(AndRevFilter.create(revLimiter));
122 final long start = System.currentTimeMillis();
123 boolean have_revision = false;
124 boolean not = false;
125 for (String a : argList) {
126 if ("--not".equals(a)) {
127 not = true;
128 continue;
130 boolean menot = false;
131 if (a.startsWith("^")) {
132 a = a.substring(1);
133 menot = true;
135 final RevCommit c = walk.parseCommit(resolve(a));
136 if (not ^ menot)
137 walk.markUninteresting(c);
138 else {
139 walk.markStart(c);
140 have_revision = true;
143 if (!have_revision)
144 walk.markStart(walk.parseCommit(resolve("HEAD")));
146 int n = walkLoop();
148 if (count) {
149 final long end = System.currentTimeMillis();
150 System.err.print(n);
151 System.err.print(' ');
152 System.err.print(end - start);
153 System.err.print(" ms");
154 System.err.println();
158 protected RevWalk createWalk() {
159 if (objects)
160 return new ObjectWalk(db);
161 return new RevWalk(db);
164 protected int walkLoop() throws Exception {
165 int n = 0;
166 for (final RevCommit c : walk) {
167 n++;
168 show(c);
170 if (walk instanceof ObjectWalk) {
171 final ObjectWalk ow = (ObjectWalk) walk;
172 for (;;) {
173 final RevObject obj = ow.nextObject();
174 if (obj == null)
175 break;
176 show(ow, obj);
179 return n;
182 protected abstract void show(final RevCommit c) throws Exception;
184 protected void show(final ObjectWalk objectWalk,
185 final RevObject currentObject) throws Exception {
186 // Do nothing by default. Most applications cannot show an object.