Mercurial > jhg
comparison src/org/tmatesoft/hg/core/HgLogCommand.java @ 131:aa1629f36482
Renamed .core classes to start with Hg prefix
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Wed, 16 Feb 2011 20:47:56 +0100 |
parents | src/org/tmatesoft/hg/core/LogCommand.java@645829962785 |
children | 4a948ec83980 |
comparison
equal
deleted
inserted
replaced
130:7567f4a42fe5 | 131:aa1629f36482 |
---|---|
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.util.Calendar; | |
22 import java.util.Collections; | |
23 import java.util.ConcurrentModificationException; | |
24 import java.util.LinkedList; | |
25 import java.util.List; | |
26 import java.util.Set; | |
27 import java.util.TreeSet; | |
28 | |
29 import org.tmatesoft.hg.repo.HgChangelog.Changeset; | |
30 import org.tmatesoft.hg.repo.HgChangelog; | |
31 import org.tmatesoft.hg.repo.HgDataFile; | |
32 import org.tmatesoft.hg.repo.HgRepository; | |
33 import org.tmatesoft.hg.repo.HgStatusCollector; | |
34 import org.tmatesoft.hg.util.PathPool; | |
35 | |
36 | |
37 /** | |
38 * Access to changelog, 'hg log' command counterpart. | |
39 * | |
40 * <pre> | |
41 * Usage: | |
42 * new LogCommand().limit(20).branch("maintenance-2.1").user("me").execute(new MyHandler()); | |
43 * </pre> | |
44 * Not thread-safe (each thread has to use own {@link HgLogCommand} instance). | |
45 * | |
46 * @author Artem Tikhomirov | |
47 * @author TMate Software Ltd. | |
48 */ | |
49 public class HgLogCommand implements HgChangelog.Inspector { | |
50 | |
51 private final HgRepository repo; | |
52 private Set<String> users; | |
53 private Set<String> branches; | |
54 private int limit = 0, count = 0; | |
55 private int startRev = 0, endRev = TIP; | |
56 private Handler delegate; | |
57 private Calendar date; | |
58 private Path file; | |
59 private boolean followHistory; // makes sense only when file != null | |
60 private HgChangeset changeset; | |
61 | |
62 public HgLogCommand(HgRepository hgRepo) { | |
63 repo = hgRepo; | |
64 } | |
65 | |
66 /** | |
67 * Limit search to specified user. Multiple user names may be specified. | |
68 * @param user - full or partial name of the user, case-insensitive, non-null. | |
69 * @return <code>this</code> instance for convenience | |
70 */ | |
71 public HgLogCommand user(String user) { | |
72 if (user == null) { | |
73 throw new IllegalArgumentException(); | |
74 } | |
75 if (users == null) { | |
76 users = new TreeSet<String>(); | |
77 } | |
78 users.add(user.toLowerCase()); | |
79 return this; | |
80 } | |
81 | |
82 /** | |
83 * Limit search to specified branch. Multiple branch specification possible (changeset from any of these | |
84 * would be included in result). If unspecified, all branches are considered. | |
85 * @param branch - branch name, case-sensitive, non-null. | |
86 * @return <code>this</code> instance for convenience | |
87 */ | |
88 public HgLogCommand branch(String branch) { | |
89 if (branch == null) { | |
90 throw new IllegalArgumentException(); | |
91 } | |
92 if (branches == null) { | |
93 branches = new TreeSet<String>(); | |
94 } | |
95 branches.add(branch); | |
96 return this; | |
97 } | |
98 | |
99 // limit search to specific date | |
100 // multiple? | |
101 public HgLogCommand date(Calendar date) { | |
102 this.date = date; | |
103 // FIXME implement | |
104 // isSet(field) - false => don't use in detection of 'same date' | |
105 throw HgRepository.notImplemented(); | |
106 } | |
107 | |
108 /** | |
109 * | |
110 * @param num - number of changeset to produce. Pass 0 to clear the limit. | |
111 * @return <code>this</code> instance for convenience | |
112 */ | |
113 public HgLogCommand limit(int num) { | |
114 limit = num; | |
115 return this; | |
116 } | |
117 | |
118 /** | |
119 * Limit to specified subset of Changelog, [min(rev1,rev2), max(rev1,rev2)], inclusive. | |
120 * Revision may be specified with {@link HgRepository#TIP} | |
121 * @param rev1 | |
122 * @param rev2 | |
123 * @return <code>this</code> instance for convenience | |
124 */ | |
125 public HgLogCommand range(int rev1, int rev2) { | |
126 if (rev1 != TIP && rev2 != TIP) { | |
127 startRev = rev2 < rev1 ? rev2 : rev1; | |
128 endRev = startRev == rev2 ? rev1 : rev2; | |
129 } else if (rev1 == TIP && rev2 != TIP) { | |
130 startRev = rev2; | |
131 endRev = rev1; | |
132 } else { | |
133 startRev = rev1; | |
134 endRev = rev2; | |
135 } | |
136 return this; | |
137 } | |
138 | |
139 /** | |
140 * Visit history of a given file only. | |
141 * @param file path relative to repository root. Pass <code>null</code> to reset. | |
142 * @param followCopyRename true to report changesets of the original file(-s), if copy/rename ever occured to the file. | |
143 */ | |
144 public HgLogCommand file(Path file, boolean followCopyRename) { | |
145 // multiple? Bad idea, would need to include extra method into Handler to tell start of next file | |
146 this.file = file; | |
147 followHistory = followCopyRename; | |
148 return this; | |
149 } | |
150 | |
151 /** | |
152 * Similar to {@link #execute(org.tmatesoft.hg.repo.Changeset.Inspector)}, collects and return result as a list. | |
153 */ | |
154 public List<HgChangeset> execute() { | |
155 CollectHandler collector = new CollectHandler(); | |
156 execute(collector); | |
157 return collector.getChanges(); | |
158 } | |
159 | |
160 /** | |
161 * | |
162 * @param inspector | |
163 * @throws IllegalArgumentException when inspector argument is null | |
164 * @throws ConcurrentModificationException if this log command instance is already running | |
165 */ | |
166 public void execute(Handler handler) { | |
167 if (handler == null) { | |
168 throw new IllegalArgumentException(); | |
169 } | |
170 if (delegate != null) { | |
171 throw new ConcurrentModificationException(); | |
172 } | |
173 try { | |
174 delegate = handler; | |
175 count = 0; | |
176 changeset = new HgChangeset(new HgStatusCollector(repo), new PathPool(repo.getPathHelper())); | |
177 if (file == null) { | |
178 repo.getChangelog().range(startRev, endRev, this); | |
179 } else { | |
180 HgDataFile fileNode = repo.getFileNode(file); | |
181 fileNode.history(startRev, endRev, this); | |
182 if (fileNode.isCopy()) { | |
183 // even if we do not follow history, report file rename | |
184 do { | |
185 if (handler instanceof FileHistoryHandler) { | |
186 FileRevision src = new FileRevision(repo, fileNode.getCopySourceRevision(), fileNode.getCopySourceName()); | |
187 FileRevision dst = new FileRevision(repo, fileNode.getRevision(0), fileNode.getPath()); | |
188 ((FileHistoryHandler) handler).copy(src, dst); | |
189 } | |
190 if (limit > 0 && count >= limit) { | |
191 // if limit reach, follow is useless. | |
192 break; | |
193 } | |
194 if (followHistory) { | |
195 fileNode = repo.getFileNode(fileNode.getCopySourceName()); | |
196 fileNode.history(this); | |
197 } | |
198 } while (followHistory && fileNode.isCopy()); | |
199 } | |
200 } | |
201 } finally { | |
202 delegate = null; | |
203 changeset = null; | |
204 } | |
205 } | |
206 | |
207 // | |
208 | |
209 public void next(int revisionNumber, Nodeid nodeid, Changeset cset) { | |
210 if (limit > 0 && count >= limit) { | |
211 return; | |
212 } | |
213 if (branches != null && !branches.contains(cset.branch())) { | |
214 return; | |
215 } | |
216 if (users != null) { | |
217 String csetUser = cset.user().toLowerCase(); | |
218 boolean found = false; | |
219 for (String u : users) { | |
220 if (csetUser.indexOf(u) != -1) { | |
221 found = true; | |
222 break; | |
223 } | |
224 } | |
225 if (!found) { | |
226 return; | |
227 } | |
228 } | |
229 if (date != null) { | |
230 // FIXME | |
231 } | |
232 count++; | |
233 changeset.init(revisionNumber, nodeid, cset); | |
234 delegate.next(changeset); | |
235 } | |
236 | |
237 public interface Handler { | |
238 /** | |
239 * @param changeset not necessarily a distinct instance each time, {@link HgChangeset#clone() clone()} if need a copy. | |
240 */ | |
241 void next(HgChangeset changeset); | |
242 } | |
243 | |
244 /** | |
245 * When {@link HgLogCommand} is executed against file, handler passed to {@link HgLogCommand#execute(Handler)} may optionally | |
246 * implement this interface to get information about file renames. Method {@link #copy(FileRevision, FileRevision)} would | |
247 * get invoked prior any changeset of the original file (if file history being followed) is reported via {@link #next(HgChangeset)}. | |
248 * | |
249 * For {@link HgLogCommand#file(Path, boolean)} with renamed file path and follow argument set to false, | |
250 * {@link #copy(FileRevision, FileRevision)} would be invoked for the first copy/rename in the history of the file, but not | |
251 * followed by any changesets. | |
252 * | |
253 * @author Artem Tikhomirov | |
254 * @author TMate Software Ltd. | |
255 */ | |
256 public interface FileHistoryHandler extends Handler { | |
257 // XXX perhaps, should distinguish copy from rename? And what about merged revisions and following them? | |
258 void copy(FileRevision from, FileRevision to); | |
259 } | |
260 | |
261 public static class CollectHandler implements Handler { | |
262 private final List<HgChangeset> result = new LinkedList<HgChangeset>(); | |
263 | |
264 public List<HgChangeset> getChanges() { | |
265 return Collections.unmodifiableList(result); | |
266 } | |
267 | |
268 public void next(HgChangeset changeset) { | |
269 result.add(changeset.clone()); | |
270 } | |
271 } | |
272 | |
273 public static final class FileRevision { | |
274 private final HgRepository repo; | |
275 private final Nodeid revision; | |
276 private final Path path; | |
277 | |
278 /*package-local*/FileRevision(HgRepository hgRepo, Nodeid rev, Path p) { | |
279 if (hgRepo == null || rev == null || p == null) { | |
280 throw new IllegalArgumentException(); | |
281 } | |
282 repo = hgRepo; | |
283 revision = rev; | |
284 path = p; | |
285 } | |
286 | |
287 public Path getPath() { | |
288 return path; | |
289 } | |
290 public Nodeid getRevision() { | |
291 return revision; | |
292 } | |
293 public byte[] getContent() { | |
294 // XXX Content wrapper, to allow formats other than byte[], e.g. Stream, DataAccess, etc? | |
295 return repo.getFileNode(path).content(revision); | |
296 } | |
297 } | |
298 } |