comparison hg4j/src/main/java/org/tmatesoft/hg/core/HgLogCommand.java @ 213:6ec4af642ba8 gradle

Project uses Gradle for build - actual changes
author Alexander Kitaev <kitaev@gmail.com>
date Tue, 10 May 2011 10:52:53 +0200
parents
children
comparison
equal deleted inserted replaced
212:edb2e2829352 213:6ec4af642ba8
1 /*
2 * Copyright (c) 2011 TMate Software Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com
16 */
17 package org.tmatesoft.hg.core;
18
19 import static org.tmatesoft.hg.repo.HgRepository.TIP;
20
21 import java.io.IOException;
22 import java.util.Calendar;
23 import java.util.Collections;
24 import java.util.ConcurrentModificationException;
25 import java.util.LinkedList;
26 import java.util.List;
27 import java.util.Set;
28 import java.util.TreeSet;
29
30 import org.tmatesoft.hg.repo.HgChangelog.RawChangeset;
31 import org.tmatesoft.hg.repo.HgChangelog;
32 import org.tmatesoft.hg.repo.HgDataFile;
33 import org.tmatesoft.hg.repo.HgRepository;
34 import org.tmatesoft.hg.util.ByteChannel;
35 import org.tmatesoft.hg.util.CancelledException;
36 import org.tmatesoft.hg.util.Path;
37
38
39 /**
40 * Access to changelog, 'hg log' command counterpart.
41 *
42 * <pre>
43 * Usage:
44 * new LogCommand().limit(20).branch("maintenance-2.1").user("me").execute(new MyHandler());
45 * </pre>
46 * Not thread-safe (each thread has to use own {@link HgLogCommand} instance).
47 *
48 * @author Artem Tikhomirov
49 * @author TMate Software Ltd.
50 */
51 public class HgLogCommand implements HgChangelog.Inspector {
52
53 private final HgRepository repo;
54 private Set<String> users;
55 private Set<String> branches;
56 private int limit = 0, count = 0;
57 private int startRev = 0, endRev = TIP;
58 private Calendar date;
59 private Path file;
60 private boolean followHistory; // makes sense only when file != null
61 private ChangesetTransformer csetTransform;
62 private HgChangelog.ParentWalker parentHelper;
63
64 public HgLogCommand(HgRepository hgRepo) {
65 repo = hgRepo;
66 }
67
68 /**
69 * Limit search to specified user. Multiple user names may be specified. Once set, user names can't be
70 * cleared, use new command instance in such cases.
71 * @param user - full or partial name of the user, case-insensitive, non-null.
72 * @return <code>this</code> instance for convenience
73 * @throws IllegalArgumentException when argument is null
74 */
75 public HgLogCommand user(String user) {
76 if (user == null) {
77 throw new IllegalArgumentException();
78 }
79 if (users == null) {
80 users = new TreeSet<String>();
81 }
82 users.add(user.toLowerCase());
83 return this;
84 }
85
86 /**
87 * Limit search to specified branch. Multiple branch specification possible (changeset from any of these
88 * would be included in result). If unspecified, all branches are considered. There's no way to clean branch selection
89 * once set, create fresh new command instead.
90 * @param branch - branch name, case-sensitive, non-null.
91 * @return <code>this</code> instance for convenience
92 * @throws IllegalArgumentException when branch argument is null
93 */
94 public HgLogCommand branch(String branch) {
95 if (branch == null) {
96 throw new IllegalArgumentException();
97 }
98 if (branches == null) {
99 branches = new TreeSet<String>();
100 }
101 branches.add(branch);
102 return this;
103 }
104
105 // limit search to specific date
106 // multiple?
107 public HgLogCommand date(Calendar date) {
108 this.date = date;
109 // FIXME implement
110 // isSet(field) - false => don't use in detection of 'same date'
111 throw HgRepository.notImplemented();
112 }
113
114 /**
115 *
116 * @param num - number of changeset to produce. Pass 0 to clear the limit.
117 * @return <code>this</code> instance for convenience
118 */
119 public HgLogCommand limit(int num) {
120 limit = num;
121 return this;
122 }
123
124 /**
125 * Limit to specified subset of Changelog, [min(rev1,rev2), max(rev1,rev2)], inclusive.
126 * Revision may be specified with {@link HgRepository#TIP}
127 * @param rev1 - local revision number
128 * @param rev2 - local revision number
129 * @return <code>this</code> instance for convenience
130 */
131 public HgLogCommand range(int rev1, int rev2) {
132 if (rev1 != TIP && rev2 != TIP) {
133 startRev = rev2 < rev1 ? rev2 : rev1;
134 endRev = startRev == rev2 ? rev1 : rev2;
135 } else if (rev1 == TIP && rev2 != TIP) {
136 startRev = rev2;
137 endRev = rev1;
138 } else {
139 startRev = rev1;
140 endRev = rev2;
141 }
142 return this;
143 }
144
145 /**
146 * Visit history of a given file only.
147 * @param file path relative to repository root. Pass <code>null</code> to reset.
148 * @param followCopyRename true to report changesets of the original file(-s), if copy/rename ever occured to the file.
149 */
150 public HgLogCommand file(Path file, boolean followCopyRename) {
151 // multiple? Bad idea, would need to include extra method into Handler to tell start of next file
152 this.file = file;
153 followHistory = followCopyRename;
154 return this;
155 }
156
157 /**
158 * Handy analog of {@link #file(Path, boolean)} when clients' paths come from filesystem and need conversion to repository's
159 */
160 public HgLogCommand file(String file, boolean followCopyRename) {
161 return file(Path.create(repo.getToRepoPathHelper().rewrite(file)), followCopyRename);
162 }
163
164 /**
165 * Similar to {@link #execute(org.tmatesoft.hg.repo.RawChangeset.Inspector)}, collects and return result as a list.
166 */
167 public List<HgChangeset> execute() throws HgException {
168 CollectHandler collector = new CollectHandler();
169 execute(collector);
170 return collector.getChanges();
171 }
172
173 /**
174 *
175 * @param handler callback to process changesets.
176 * @throws IllegalArgumentException when inspector argument is null
177 * @throws ConcurrentModificationException if this log command instance is already running
178 */
179 public void execute(HgChangesetHandler handler) throws HgException {
180 if (handler == null) {
181 throw new IllegalArgumentException();
182 }
183 if (csetTransform != null) {
184 throw new ConcurrentModificationException();
185 }
186 try {
187 count = 0;
188 HgChangelog.ParentWalker pw = parentHelper; // leave it uninitialized unless we iterate whole repo
189 if (file == null) {
190 pw = getParentHelper();
191 }
192 // ChangesetTransfrom creates a blank PathPool, and #file(String, boolean) above
193 // may utilize it as well. CommandContext? How about StatusCollector there as well?
194 csetTransform = new ChangesetTransformer(repo, handler, pw);
195 if (file == null) {
196 repo.getChangelog().range(startRev, endRev, this);
197 } else {
198 HgDataFile fileNode = repo.getFileNode(file);
199 fileNode.history(startRev, endRev, this);
200 if (fileNode.isCopy()) {
201 // even if we do not follow history, report file rename
202 do {
203 if (handler instanceof FileHistoryHandler) {
204 FileRevision src = new FileRevision(repo, fileNode.getCopySourceRevision(), fileNode.getCopySourceName());
205 FileRevision dst = new FileRevision(repo, fileNode.getRevision(0), fileNode.getPath());
206 ((FileHistoryHandler) handler).copy(src, dst);
207 }
208 if (limit > 0 && count >= limit) {
209 // if limit reach, follow is useless.
210 break;
211 }
212 if (followHistory) {
213 fileNode = repo.getFileNode(fileNode.getCopySourceName());
214 fileNode.history(this);
215 }
216 } while (followHistory && fileNode.isCopy());
217 }
218 }
219 } finally {
220 csetTransform = null;
221 }
222 }
223
224 //
225
226 public void next(int revisionNumber, Nodeid nodeid, RawChangeset cset) {
227 if (limit > 0 && count >= limit) {
228 return;
229 }
230 if (branches != null && !branches.contains(cset.branch())) {
231 return;
232 }
233 if (users != null) {
234 String csetUser = cset.user().toLowerCase();
235 boolean found = false;
236 for (String u : users) {
237 if (csetUser.indexOf(u) != -1) {
238 found = true;
239 break;
240 }
241 }
242 if (!found) {
243 return;
244 }
245 }
246 if (date != null) {
247 // FIXME
248 }
249 count++;
250 csetTransform.next(revisionNumber, nodeid, cset);
251 }
252
253 private HgChangelog.ParentWalker getParentHelper() {
254 if (parentHelper == null) {
255 parentHelper = repo.getChangelog().new ParentWalker();
256 parentHelper.init();
257 }
258 return parentHelper;
259 }
260
261
262 /**
263 * @deprecated Use {@link HgChangesetHandler} instead. This interface is left temporarily for compatibility.
264 */
265 @Deprecated()
266 public interface Handler extends HgChangesetHandler {
267 }
268
269 /**
270 * When {@link HgLogCommand} is executed against file, handler passed to {@link HgLogCommand#execute(HgChangesetHandler)} may optionally
271 * implement this interface to get information about file renames. Method {@link #copy(FileRevision, FileRevision)} would
272 * get invoked prior any changeset of the original file (if file history being followed) is reported via {@link #next(HgChangeset)}.
273 *
274 * For {@link HgLogCommand#file(Path, boolean)} with renamed file path and follow argument set to false,
275 * {@link #copy(FileRevision, FileRevision)} would be invoked for the first copy/rename in the history of the file, but not
276 * followed by any changesets.
277 *
278 * @author Artem Tikhomirov
279 * @author TMate Software Ltd.
280 */
281 public interface FileHistoryHandler extends HgChangesetHandler {
282 // XXX perhaps, should distinguish copy from rename? And what about merged revisions and following them?
283 void copy(FileRevision from, FileRevision to);
284 }
285
286 public static class CollectHandler implements HgChangesetHandler {
287 private final List<HgChangeset> result = new LinkedList<HgChangeset>();
288
289 public List<HgChangeset> getChanges() {
290 return Collections.unmodifiableList(result);
291 }
292
293 public void next(HgChangeset changeset) {
294 result.add(changeset.clone());
295 }
296 }
297
298 public static final class FileRevision {
299 private final HgRepository repo;
300 private final Nodeid revision;
301 private final Path path;
302
303 /*package-local*/FileRevision(HgRepository hgRepo, Nodeid rev, Path p) {
304 if (hgRepo == null || rev == null || p == null) {
305 // since it's package local, it is our code to blame for non validated arguments
306 throw new HgBadStateException();
307 }
308 repo = hgRepo;
309 revision = rev;
310 path = p;
311 }
312
313 public Path getPath() {
314 return path;
315 }
316 public Nodeid getRevision() {
317 return revision;
318 }
319 public void putContentTo(ByteChannel sink) throws HgDataStreamException, IOException, CancelledException {
320 HgDataFile fn = repo.getFileNode(path);
321 int localRevision = fn.getLocalRevision(revision);
322 fn.contentWithFilters(localRevision, sink);
323 }
324 }
325 }