Mercurial > hg4j
comparison src/org/tmatesoft/hg/core/LogCommand.java @ 64:19e9e220bf68
Convenient commands constitute hi-level API. org.tmatesoft namespace, GPL2 statement
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Fri, 21 Jan 2011 05:56:43 +0100 |
parents | |
children | 993f6f8e1314 |
comparison
equal
deleted
inserted
replaced
63:a47530a2ea12 | 64:19e9e220bf68 |
---|---|
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@svnkit.com | |
16 */ | |
17 package org.tmatesoft.hg.core; | |
18 | |
19 import static com.tmate.hgkit.ll.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.util.PathPool; | |
30 | |
31 import com.tmate.hgkit.ll.Changeset; | |
32 import com.tmate.hgkit.ll.HgRepository; | |
33 import com.tmate.hgkit.ll.Nodeid; | |
34 import com.tmate.hgkit.ll.StatusCollector; | |
35 | |
36 /** | |
37 * <pre> | |
38 * new LogCommand().limit(20).branch("maintenance-2.1").user("me").execute(); | |
39 * </pre> | |
40 * Not thread-safe (each thread has to use own {@link LogCommand} instance). | |
41 * | |
42 * @author Artem Tikhomirov | |
43 * @author TMate Software Ltd. | |
44 */ | |
45 public class LogCommand implements Changeset.Inspector { | |
46 | |
47 private final HgRepository repo; | |
48 private Set<String> users; | |
49 private Set<String> branches; | |
50 private int limit = 0, count = 0; | |
51 private int startRev = 0, endRev = TIP; | |
52 private Handler delegate; | |
53 private Calendar date; | |
54 private Cset changeset; | |
55 | |
56 public LogCommand(HgRepository hgRepo) { | |
57 this.repo = hgRepo; | |
58 } | |
59 | |
60 /** | |
61 * Limit search to specified user. Multiple user names may be specified. | |
62 * @param user - full or partial name of the user, case-insensitive, non-null. | |
63 * @return <code>this</code> instance for convenience | |
64 */ | |
65 public LogCommand user(String user) { | |
66 if (user == null) { | |
67 throw new IllegalArgumentException(); | |
68 } | |
69 if (users == null) { | |
70 users = new TreeSet<String>(); | |
71 } | |
72 users.add(user.toLowerCase()); | |
73 return this; | |
74 } | |
75 | |
76 /** | |
77 * Limit search to specified branch. Multiple branch specification possible (changeset from any of these | |
78 * would be included in result). If unspecified, all branches are considered. | |
79 * @param branch - branch name, case-sensitive, non-null. | |
80 * @return <code>this</code> instance for convenience | |
81 */ | |
82 public LogCommand branch(String branch) { | |
83 if (branch == null) { | |
84 throw new IllegalArgumentException(); | |
85 } | |
86 if (branches == null) { | |
87 branches = new TreeSet<String>(); | |
88 } | |
89 branches.add(branch); | |
90 return this; | |
91 } | |
92 | |
93 // limit search to specific date | |
94 // multiple? | |
95 public LogCommand date(Calendar date) { | |
96 this.date = date; | |
97 // FIXME implement | |
98 // isSet(field) - false => don't use in detection of 'same date' | |
99 throw HgRepository.notImplemented(); | |
100 } | |
101 | |
102 /** | |
103 * | |
104 * @param num - number of changeset to produce. Pass 0 to clear the limit. | |
105 * @return <code>this</code> instance for convenience | |
106 */ | |
107 public LogCommand limit(int num) { | |
108 limit = num; | |
109 return this; | |
110 } | |
111 | |
112 /** | |
113 * Limit to specified subset of Changelog, [min(rev1,rev2), max(rev1,rev2)], inclusive. | |
114 * Revision may be specified with {@link HgRepository#TIP} | |
115 * @param rev1 | |
116 * @param rev2 | |
117 * @return <code>this</code> instance for convenience | |
118 */ | |
119 public LogCommand range(int rev1, int rev2) { | |
120 if (rev1 != TIP && rev2 != TIP) { | |
121 startRev = rev2 < rev1 ? rev2 : rev1; | |
122 endRev = startRev == rev2 ? rev1 : rev2; | |
123 } else if (rev1 == TIP && rev2 != TIP) { | |
124 startRev = rev2; | |
125 endRev = rev1; | |
126 } else { | |
127 startRev = rev1; | |
128 endRev = rev2; | |
129 } | |
130 return this; | |
131 } | |
132 | |
133 // multiple? Bad idea, would need to include extra method into Handler to tell start of next file | |
134 public LogCommand file(Path file) { | |
135 // implicit --follow in this case | |
136 throw HgRepository.notImplemented(); | |
137 } | |
138 | |
139 /** | |
140 * Similar to {@link #execute(com.tmate.hgkit.ll.Changeset.Inspector)}, collects and return result as a list. | |
141 */ | |
142 public List<Cset> execute() { | |
143 CollectHandler collector = new CollectHandler(); | |
144 execute(collector); | |
145 return collector.getChanges(); | |
146 } | |
147 | |
148 /** | |
149 * | |
150 * @param inspector | |
151 * @throws IllegalArgumentException when inspector argument is null | |
152 * @throws ConcurrentModificationException if this log command instance is already running | |
153 */ | |
154 public void execute(Handler handler) { | |
155 if (handler == null) { | |
156 throw new IllegalArgumentException(); | |
157 } | |
158 if (delegate != null) { | |
159 throw new ConcurrentModificationException(); | |
160 } | |
161 try { | |
162 delegate = handler; | |
163 count = 0; | |
164 changeset = new Cset(new StatusCollector(repo), new PathPool(repo.getPathHelper())); | |
165 repo.getChangelog().range(startRev, endRev, this); | |
166 } finally { | |
167 delegate = null; | |
168 changeset = null; | |
169 } | |
170 } | |
171 | |
172 // | |
173 | |
174 public void next(int revisionNumber, Nodeid nodeid, Changeset cset) { | |
175 if (limit > 0 && count >= limit) { | |
176 return; | |
177 } | |
178 if (branches != null && !branches.contains(cset.branch())) { | |
179 return; | |
180 } | |
181 if (users != null) { | |
182 String csetUser = cset.user().toLowerCase(); | |
183 boolean found = false; | |
184 for (String u : users) { | |
185 if (csetUser.indexOf(u) != -1) { | |
186 found = true; | |
187 break; | |
188 } | |
189 } | |
190 if (!found) { | |
191 return; | |
192 } | |
193 } | |
194 if (date != null) { | |
195 // FIXME | |
196 } | |
197 count++; | |
198 changeset.init(revisionNumber, nodeid, cset); | |
199 delegate.next(changeset); | |
200 } | |
201 | |
202 public interface Handler { | |
203 /** | |
204 * @param changeset not necessarily a distinct instance each time, {@link Cset#clone() clone()} if need a copy. | |
205 */ | |
206 void next(Cset changeset); | |
207 } | |
208 | |
209 public static class CollectHandler implements Handler { | |
210 private final List<Cset> result = new LinkedList<Cset>(); | |
211 | |
212 public List<Cset> getChanges() { | |
213 return Collections.unmodifiableList(result); | |
214 } | |
215 | |
216 public void next(Cset changeset) { | |
217 result.add(changeset.clone()); | |
218 } | |
219 } | |
220 | |
221 public static final class FileRevision { | |
222 private final HgRepository repo; | |
223 private final Nodeid revision; | |
224 private final Path path; | |
225 | |
226 public FileRevision(HgRepository hgRepo, Nodeid rev, Path p) { | |
227 if (hgRepo == null || rev == null || p == null) { | |
228 throw new IllegalArgumentException(); | |
229 } | |
230 repo = hgRepo; | |
231 revision = rev; | |
232 path = p; | |
233 } | |
234 | |
235 public Path getPath() { | |
236 return path; | |
237 } | |
238 public Nodeid getRevision() { | |
239 return revision; | |
240 } | |
241 public byte[] getContent() { | |
242 // XXX Content wrapper, to allow formats other than byte[], e.g. Stream, DataAccess, etc? | |
243 return repo.getFileNode(path).content(); | |
244 } | |
245 } | |
246 } |