1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.pgm;
45
46 import java.text.MessageFormat;
47 import java.util.ArrayList;
48 import java.util.EnumSet;
49 import java.util.List;
50 import java.util.Map;
51
52 import org.eclipse.jgit.diff.DiffConfig;
53 import org.eclipse.jgit.errors.IncorrectObjectTypeException;
54 import org.eclipse.jgit.lib.Constants;
55 import org.eclipse.jgit.lib.ObjectId;
56 import org.eclipse.jgit.lib.Ref;
57 import org.eclipse.jgit.lib.RefDatabase;
58 import org.eclipse.jgit.pgm.internal.CLIText;
59 import org.eclipse.jgit.pgm.opt.PathTreeFilterHandler;
60 import org.eclipse.jgit.revwalk.FollowFilter;
61 import org.eclipse.jgit.revwalk.ObjectWalk;
62 import org.eclipse.jgit.revwalk.RevCommit;
63 import org.eclipse.jgit.revwalk.RevFlag;
64 import org.eclipse.jgit.revwalk.RevObject;
65 import org.eclipse.jgit.revwalk.RevSort;
66 import org.eclipse.jgit.revwalk.RevWalk;
67 import org.eclipse.jgit.revwalk.filter.AndRevFilter;
68 import org.eclipse.jgit.revwalk.filter.AuthorRevFilter;
69 import org.eclipse.jgit.revwalk.filter.CommitterRevFilter;
70 import org.eclipse.jgit.revwalk.filter.MessageRevFilter;
71 import org.eclipse.jgit.revwalk.filter.RevFilter;
72 import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
73 import org.eclipse.jgit.treewalk.filter.TreeFilter;
74 import org.kohsuke.args4j.Argument;
75 import org.kohsuke.args4j.Option;
76
77 abstract class RevWalkTextBuiltin extends TextBuiltin {
78 RevWalk walk;
79
80 @Option(name = "--objects")
81 boolean objects = false;
82
83 @Option(name = "--parents")
84 boolean parents = false;
85
86 @Option(name = "--total-count")
87 boolean count = false;
88
89 @Option(name = "--all")
90 boolean all = false;
91
92 char[] outbuffer = new char[Constants.OBJECT_ID_LENGTH * 2];
93
94 private final EnumSet<RevSort> sorting = EnumSet.noneOf(RevSort.class);
95
96 private void enableRevSort(final RevSort type, final boolean on) {
97 if (on)
98 sorting.add(type);
99 else
100 sorting.remove(type);
101 }
102
103 @Option(name = "--date-order")
104 void enableDateOrder(final boolean on) {
105 enableRevSort(RevSort.COMMIT_TIME_DESC, on);
106 }
107
108 @Option(name = "--topo-order")
109 void enableTopoOrder(final boolean on) {
110 enableRevSort(RevSort.TOPO, on);
111 }
112
113 @Option(name = "--reverse")
114 void enableReverse(final boolean on) {
115 enableRevSort(RevSort.REVERSE, on);
116 }
117
118 @Option(name = "--boundary")
119 void enableBoundary(final boolean on) {
120 enableRevSort(RevSort.BOUNDARY, on);
121 }
122
123 @Option(name = "--follow", metaVar = "metaVar_path")
124 private String followPath;
125
126 @Argument(index = 0, metaVar = "metaVar_commitish")
127 private final List<RevCommit> commits = new ArrayList<RevCommit>();
128
129 @Option(name = "--", metaVar = "metaVar_path", multiValued = true, handler = PathTreeFilterHandler.class)
130 protected TreeFilter pathFilter = TreeFilter.ALL;
131
132 private final List<RevFilter> revLimiter = new ArrayList<RevFilter>();
133
134 @Option(name = "--author")
135 void addAuthorRevFilter(final String who) {
136 revLimiter.add(AuthorRevFilter.create(who));
137 }
138
139 @Option(name = "--committer")
140 void addCommitterRevFilter(final String who) {
141 revLimiter.add(CommitterRevFilter.create(who));
142 }
143
144 @Option(name = "--grep")
145 void addCMessageRevFilter(final String msg) {
146 revLimiter.add(MessageRevFilter.create(msg));
147 }
148
149 @Option(name = "--max-count", aliases = "-n", metaVar = "metaVar_n")
150 private int maxCount = -1;
151
152 @Override
153 protected void run() throws Exception {
154 walk = createWalk();
155 for (final RevSort s : sorting)
156 walk.sort(s, true);
157
158 if (pathFilter == TreeFilter.ALL) {
159 if (followPath != null)
160 walk.setTreeFilter(FollowFilter.create(followPath,
161 db.getConfig().get(DiffConfig.KEY)));
162 } else if (pathFilter != TreeFilter.ALL) {
163 walk.setTreeFilter(AndTreeFilter.create(pathFilter,
164 TreeFilter.ANY_DIFF));
165 }
166
167 if (revLimiter.size() == 1)
168 walk.setRevFilter(revLimiter.get(0));
169 else if (revLimiter.size() > 1)
170 walk.setRevFilter(AndRevFilter.create(revLimiter));
171
172 if (all) {
173 Map<String, Ref> refs =
174 db.getRefDatabase().getRefs(RefDatabase.ALL);
175 for (Ref a : refs.values()) {
176 ObjectId oid = a.getPeeledObjectId();
177 if (oid == null)
178 oid = a.getObjectId();
179 try {
180 commits.add(walk.parseCommit(oid));
181 } catch (IncorrectObjectTypeException e) {
182
183 }
184 }
185 }
186
187 if (commits.isEmpty()) {
188 final ObjectId head = db.resolve(Constants.HEAD);
189 if (head == null)
190 throw die(MessageFormat.format(CLIText.get().cannotResolve, Constants.HEAD));
191 commits.add(walk.parseCommit(head));
192 }
193 for (final RevCommit c : commits) {
194 final RevCommit real = argWalk == walk ? c : walk.parseCommit(c);
195 if (c.has(RevFlag.UNINTERESTING))
196 walk.markUninteresting(real);
197 else
198 walk.markStart(real);
199 }
200
201 final long start = System.currentTimeMillis();
202 final int n = walkLoop();
203 if (count) {
204 final long end = System.currentTimeMillis();
205 errw.print(n);
206 errw.print(' ');
207 errw.println(MessageFormat.format(
208 CLIText.get().timeInMilliSeconds,
209 Long.valueOf(end - start)));
210 }
211 }
212
213 protected RevWalk createWalk() {
214 RevWalk result;
215 if (objects)
216 result = new ObjectWalk(db);
217 else if (argWalk != null)
218 result = argWalk;
219 else
220 result = argWalk = new RevWalk(db);
221 result.setRewriteParents(false);
222 return result;
223 }
224
225 protected int walkLoop() throws Exception {
226 int n = 0;
227 for (final RevCommit c : walk) {
228 if (++n > maxCount && maxCount >= 0)
229 break;
230 show(c);
231 }
232 if (walk instanceof ObjectWalk) {
233 final ObjectWalk ow = (ObjectWalk) walk;
234 for (;;) {
235 final RevObject obj = ow.nextObject();
236 if (obj == null)
237 break;
238 show(ow, obj);
239 }
240 }
241 return n;
242 }
243
244
245
246
247
248
249
250
251
252
253
254 protected abstract void show(final RevCommit c) throws Exception;
255
256
257
258
259
260
261
262
263
264
265
266
267
268 protected void show(final ObjectWalk objectWalk,
269 final RevObject currentObject) throws Exception {
270
271 }
272 }