comparison hg4j/src/main/java/org/tmatesoft/hg/repo/HgChangelog.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) 2010-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.repo;
18
19 import java.io.IOException;
20 import java.io.UnsupportedEncodingException;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.Calendar;
24 import java.util.Collections;
25 import java.util.Date;
26 import java.util.Formatter;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Locale;
30 import java.util.Map;
31 import java.util.TimeZone;
32
33 import org.tmatesoft.hg.core.HgBadStateException;
34 import org.tmatesoft.hg.core.Nodeid;
35 import org.tmatesoft.hg.internal.DataAccess;
36 import org.tmatesoft.hg.internal.Pool;
37 import org.tmatesoft.hg.internal.RevlogStream;
38
39 /**
40 * Representation of the Mercurial changelog file (list of ChangeSets)
41 *
42 * @author Artem Tikhomirov
43 * @author TMate Software Ltd.
44 */
45 public class HgChangelog extends Revlog {
46
47 /* package-local */HgChangelog(HgRepository hgRepo, RevlogStream content) {
48 super(hgRepo, content);
49 }
50
51 public void all(final HgChangelog.Inspector inspector) {
52 range(0, getLastRevision(), inspector);
53 }
54
55 public void range(int start, int end, final HgChangelog.Inspector inspector) {
56 if (inspector == null) {
57 throw new IllegalArgumentException();
58 }
59 content.iterate(start, end, true, new RawCsetParser(inspector));
60 }
61
62 public List<RawChangeset> range(int start, int end) {
63 final RawCsetCollector c = new RawCsetCollector(end - start + 1);
64 range(start, end, c);
65 return c.result;
66 }
67
68 public void range(final HgChangelog.Inspector inspector, final int... revisions) {
69 if (revisions == null || revisions.length == 0) {
70 return;
71 }
72 RevlogStream.Inspector i = new RevlogStream.Inspector() {
73 private final RawCsetParser delegate = new RawCsetParser(inspector);
74
75 public void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, DataAccess da) {
76 if (Arrays.binarySearch(revisions, revisionNumber) >= 0) {
77 delegate.next(revisionNumber, actualLen, baseRevision, linkRevision, parent1Revision, parent2Revision, nodeid, da);
78 }
79 }
80 };
81 Arrays.sort(revisions);
82 content.iterate(revisions[0], revisions[revisions.length - 1], true, i);
83 }
84
85 public interface Inspector {
86 // TODO describe whether cset is new instance each time
87 // describe what revisionNumber is when Inspector is used with HgBundle (BAD_REVISION or bundle's local order?)
88 void next(int revisionNumber, Nodeid nodeid, RawChangeset cset);
89 }
90
91 /**
92 * Entry in the Changelog
93 */
94 public static class RawChangeset implements Cloneable /* for those that would like to keep a copy */{
95 // TODO immutable
96 private/* final */Nodeid manifest;
97 private String user;
98 private String comment;
99 private List<String> files; // unmodifiable collection (otherwise #files() and implicit #clone() shall be revised)
100 private Date time;
101 private int timezone;
102 // http://mercurial.selenic.com/wiki/PruningDeadBranches - Closing changesets can be identified by close=1 in the changeset's extra field.
103 private Map<String, String> extras;
104
105 /**
106 * @see mercurial/changelog.py:read()
107 *
108 * <pre>
109 * format used:
110 * nodeid\n : manifest node in ascii
111 * user\n : user, no \n or \r allowed
112 * time tz extra\n : date (time is int or float, timezone is int)
113 * : extra is metadatas, encoded and separated by '\0'
114 * : older versions ignore it
115 * files\n\n : files modified by the cset, no \n or \r allowed
116 * (.*) : comment (free text, ideally utf-8)
117 *
118 * changelog v0 doesn't use extra
119 * </pre>
120 */
121 private RawChangeset() {
122 }
123
124 public Nodeid manifest() {
125 return manifest;
126 }
127
128 public String user() {
129 return user;
130 }
131
132 public String comment() {
133 return comment;
134 }
135
136 public List<String> files() {
137 return files;
138 }
139
140 public Date date() {
141 return time;
142 }
143
144 /**
145 * @return time zone value, as is, positive for Western Hemisphere.
146 */
147 public int timezone() {
148 return timezone;
149 }
150
151 public String dateString() {
152 // XXX keep once formatted? Perhaps, there's faster way to set up calendar/time zone?
153 StringBuilder sb = new StringBuilder(30);
154 Formatter f = new Formatter(sb, Locale.US);
155 TimeZone tz = TimeZone.getTimeZone(TimeZone.getAvailableIDs(timezone * 1000)[0]);
156 // apparently timezone field records number of seconds time differs from UTC,
157 // i.e. value to substract from time to get UTC time. Calendar seems to add
158 // timezone offset to UTC, instead, hence sign change.
159 // tz.setRawOffset(timezone * -1000);
160 Calendar c = Calendar.getInstance(tz, Locale.US);
161 c.setTime(time);
162 f.format("%ta %<tb %<td %<tH:%<tM:%<tS %<tY %<tz", c);
163 return sb.toString();
164 }
165
166 public Map<String, String> extras() {
167 return extras;
168 }
169
170 public String branch() {
171 return extras.get("branch");
172 }
173
174 @Override
175 public String toString() {
176 StringBuilder sb = new StringBuilder();
177 sb.append("Changeset {");
178 sb.append("User: ").append(user).append(", ");
179 sb.append("Comment: ").append(comment).append(", ");
180 sb.append("Manifest: ").append(manifest).append(", ");
181 sb.append("Date: ").append(time).append(", ");
182 sb.append("Files: ").append(files.size());
183 for (String s : files) {
184 sb.append(", ").append(s);
185 }
186 if (extras != null) {
187 sb.append(", Extra: ").append(extras);
188 }
189 sb.append("}");
190 return sb.toString();
191 }
192
193 @Override
194 public RawChangeset clone() {
195 try {
196 return (RawChangeset) super.clone();
197 } catch (CloneNotSupportedException ex) {
198 throw new InternalError(ex.toString());
199 }
200 }
201
202 public static RawChangeset parse(DataAccess da) {
203 try {
204 byte[] data = da.byteArray();
205 RawChangeset rv = new RawChangeset();
206 rv.init(data, 0, data.length, null);
207 return rv;
208 } catch (IOException ex) {
209 throw new HgBadStateException(ex); // FIXME "Error reading changeset data"
210 }
211 }
212
213 // @param usersPool - it's likely user names get repeated again and again throughout repository. can be null
214 /* package-local */void init(byte[] data, int offset, int length, Pool<String> usersPool) {
215 final int bufferEndIndex = offset + length;
216 final byte lineBreak = (byte) '\n';
217 int breakIndex1 = indexOf(data, lineBreak, offset, bufferEndIndex);
218 if (breakIndex1 == -1) {
219 throw new IllegalArgumentException("Bad Changeset data");
220 }
221 Nodeid _nodeid = Nodeid.fromAscii(data, 0, breakIndex1);
222 int breakIndex2 = indexOf(data, lineBreak, breakIndex1 + 1, bufferEndIndex);
223 if (breakIndex2 == -1) {
224 throw new IllegalArgumentException("Bad Changeset data");
225 }
226 String _user = new String(data, breakIndex1 + 1, breakIndex2 - breakIndex1 - 1);
227 if (usersPool != null) {
228 _user = usersPool.unify(_user);
229 }
230 int breakIndex3 = indexOf(data, lineBreak, breakIndex2 + 1, bufferEndIndex);
231 if (breakIndex3 == -1) {
232 throw new IllegalArgumentException("Bad Changeset data");
233 }
234 String _timeString = new String(data, breakIndex2 + 1, breakIndex3 - breakIndex2 - 1);
235 int space1 = _timeString.indexOf(' ');
236 if (space1 == -1) {
237 throw new IllegalArgumentException("Bad Changeset data");
238 }
239 int space2 = _timeString.indexOf(' ', space1 + 1);
240 if (space2 == -1) {
241 space2 = _timeString.length();
242 }
243 long unixTime = Long.parseLong(_timeString.substring(0, space1)); // XXX Float, perhaps
244 int _timezone = Integer.parseInt(_timeString.substring(space1 + 1, space2));
245 // XXX not sure need to add timezone here - I can't figure out whether Hg keeps GMT time, and records timezone just for info, or unixTime is taken local
246 // on commit and timezone is recorded to adjust it to UTC.
247 Date _time = new Date(unixTime * 1000);
248 String _extras = space2 < _timeString.length() ? _timeString.substring(space2 + 1) : null;
249 Map<String, String> _extrasMap;
250 if (_extras == null) {
251 _extrasMap = Collections.singletonMap("branch", "default");
252 } else {
253 _extrasMap = new HashMap<String, String>();
254 for (String pair : _extras.split("\00")) {
255 int eq = pair.indexOf(':');
256 // FIXME need to decode key/value, @see changelog.py:decodeextra
257 _extrasMap.put(pair.substring(0, eq), pair.substring(eq + 1));
258 }
259 if (!_extrasMap.containsKey("branch")) {
260 _extrasMap.put("branch", "default");
261 }
262 _extrasMap = Collections.unmodifiableMap(_extrasMap);
263 }
264
265 //
266 int lastStart = breakIndex3 + 1;
267 int breakIndex4 = indexOf(data, lineBreak, lastStart, bufferEndIndex);
268 ArrayList<String> _files = null;
269 if (breakIndex4 > lastStart) {
270 // if breakIndex4 == lastStart, we already found \n\n and hence there are no files (e.g. merge revision)
271 _files = new ArrayList<String>(5);
272 while (breakIndex4 != -1 && breakIndex4 + 1 < bufferEndIndex) {
273 _files.add(new String(data, lastStart, breakIndex4 - lastStart));
274 lastStart = breakIndex4 + 1;
275 if (data[breakIndex4 + 1] == lineBreak) {
276 // found \n\n
277 break;
278 } else {
279 breakIndex4 = indexOf(data, lineBreak, lastStart, bufferEndIndex);
280 }
281 }
282 if (breakIndex4 == -1 || breakIndex4 >= bufferEndIndex) {
283 throw new IllegalArgumentException("Bad Changeset data");
284 }
285 } else {
286 breakIndex4--;
287 }
288 String _comment;
289 try {
290 _comment = new String(data, breakIndex4 + 2, bufferEndIndex - breakIndex4 - 2, "UTF-8");
291 // FIXME respect ui.fallbackencoding and try to decode if set
292 } catch (UnsupportedEncodingException ex) {
293 _comment = "";
294 throw new IllegalStateException("Could hardly happen");
295 }
296 // change this instance at once, don't leave it partially changes in case of error
297 this.manifest = _nodeid;
298 this.user = _user;
299 this.time = _time;
300 this.timezone = _timezone;
301 this.files = _files == null ? Collections.<String> emptyList() : Collections.unmodifiableList(_files);
302 this.comment = _comment;
303 this.extras = _extrasMap;
304 }
305
306 private static int indexOf(byte[] src, byte what, int startOffset, int endIndex) {
307 for (int i = startOffset; i < endIndex; i++) {
308 if (src[i] == what) {
309 return i;
310 }
311 }
312 return -1;
313 }
314 }
315
316 private static class RawCsetCollector implements Inspector {
317 final ArrayList<RawChangeset> result;
318
319 public RawCsetCollector(int count) {
320 result = new ArrayList<RawChangeset>(count > 0 ? count : 5);
321 }
322
323 public void next(int revisionNumber, Nodeid nodeid, RawChangeset cset) {
324 result.add(cset.clone());
325 }
326 }
327
328 private static class RawCsetParser implements RevlogStream.Inspector {
329
330 private final Inspector inspector;
331 private final Pool<String> usersPool;
332 private final RawChangeset cset = new RawChangeset();
333
334 public RawCsetParser(HgChangelog.Inspector delegate) {
335 assert delegate != null;
336 inspector = delegate;
337 usersPool = new Pool<String>();
338 }
339
340 public void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, DataAccess da) {
341 try {
342 byte[] data = da.byteArray();
343 cset.init(data, 0, data.length, usersPool);
344 // XXX there's no guarantee for Changeset.Callback that distinct instance comes each time, consider instance reuse
345 inspector.next(revisionNumber, Nodeid.fromBinary(nodeid, 0), cset);
346 } catch (Exception ex) {
347 throw new HgBadStateException(ex); // FIXME exception handling
348 }
349 }
350 }
351 }