View Javadoc
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  
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 					// Ignore all refs which are not commits
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 	 * "Show" the current RevCommit when called from the main processing loop.
246 	 * <p>
247 	 * Implement this methods to define the behavior for subclasses of
248 	 * RevWalkTextBuiltin.
249 	 *
250 	 * @param c
251 	 *            The current {@link RevCommit}
252 	 * @throws Exception
253 	 */
254 	protected abstract void show(final RevCommit c) throws Exception;
255 
256 	/**
257 	 * "Show" the current RevCommit when called from the main processing loop.
258 	 * <p>
259 	 * The default implementation does nothing because most subclasses only
260 	 * process RevCommits.
261 	 *
262 	 * @param objectWalk
263 	 *            the {@link ObjectWalk} used by {@link #walkLoop()}
264 	 * @param currentObject
265 	 *            The current {@link RevObject}
266 	 * @throws Exception
267 	 */
268 	protected void show(final ObjectWalk objectWalk,
269 			final RevObject currentObject) throws Exception {
270 		// Do nothing by default. Most applications cannot show an object.
271 	}
272 }