comparison src/org/tmatesoft/hg/repo/HgChangelog.java @ 673:545b1d4cc11d

Refactor HgBundle.GroupElement (clear experimental mark), resolve few technical debt issues
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 12 Jul 2013 20:14:24 +0200
parents fba85bc1dfb8
children
comparison
equal deleted inserted replaced
672:d2552e6a5af6 673:545b1d4cc11d
18 18
19 import java.io.IOException; 19 import java.io.IOException;
20 import java.util.ArrayList; 20 import java.util.ArrayList;
21 import java.util.Arrays; 21 import java.util.Arrays;
22 import java.util.Calendar; 22 import java.util.Calendar;
23 import java.util.Collections;
24 import java.util.Date; 23 import java.util.Date;
25 import java.util.Formatter; 24 import java.util.Formatter;
26 import java.util.HashMap;
27 import java.util.List; 25 import java.util.List;
28 import java.util.Locale; 26 import java.util.Locale;
29 import java.util.Map; 27 import java.util.Map;
30 import java.util.TimeZone; 28 import java.util.TimeZone;
31 29
32 import org.tmatesoft.hg.core.Nodeid; 30 import org.tmatesoft.hg.core.Nodeid;
33 import org.tmatesoft.hg.core.SessionContext; 31 import org.tmatesoft.hg.core.SessionContext;
34 import org.tmatesoft.hg.internal.Callback; 32 import org.tmatesoft.hg.internal.Callback;
33 import org.tmatesoft.hg.internal.ChangesetParser;
35 import org.tmatesoft.hg.internal.DataAccess; 34 import org.tmatesoft.hg.internal.DataAccess;
36 import org.tmatesoft.hg.internal.EncodingHelper;
37 import org.tmatesoft.hg.internal.Internals;
38 import org.tmatesoft.hg.internal.Lifecycle; 35 import org.tmatesoft.hg.internal.Lifecycle;
39 import org.tmatesoft.hg.internal.LifecycleBridge; 36 import org.tmatesoft.hg.internal.LifecycleBridge;
40 import org.tmatesoft.hg.internal.Pool;
41 import org.tmatesoft.hg.internal.RevlogStream; 37 import org.tmatesoft.hg.internal.RevlogStream;
42 import org.tmatesoft.hg.util.Adaptable; 38 import org.tmatesoft.hg.util.Adaptable;
43 import org.tmatesoft.hg.util.CancelSupport; 39 import org.tmatesoft.hg.util.CancelSupport;
44 import org.tmatesoft.hg.util.ProgressSupport; 40 import org.tmatesoft.hg.util.ProgressSupport;
45 41
148 /** 144 /**
149 * Entry in the Changelog 145 * Entry in the Changelog
150 */ 146 */
151 public static final class RawChangeset implements Cloneable /* for those that would like to keep a copy */{ 147 public static final class RawChangeset implements Cloneable /* for those that would like to keep a copy */{
152 // would be nice to get it immutable, but then we can't reuse instances 148 // would be nice to get it immutable, but then we can't reuse instances
153 private/* final */Nodeid manifest; 149 /* final */Nodeid manifest;
154 private String user; 150 String user;
155 private String comment; 151 String comment;
156 private String[] files; // shall not be modified (#clone() does shallow copy) 152 String[] files; // shall not be modified (#clone() does shallow copy)
157 private Date time; 153 Date time;
158 private int timezone; 154 int timezone;
159 // http://mercurial.selenic.com/wiki/PruningDeadBranches - Closing changesets can be identified by close=1 in the changeset's extra field. 155 // http://mercurial.selenic.com/wiki/PruningDeadBranches - Closing changesets can be identified by close=1 in the changeset's extra field.
160 private Map<String, String> extras; 156 Map<String, String> extras;
161 157
162 private RawChangeset() { 158 private RawChangeset() {
163 } 159 }
164 160
165 public Nodeid manifest() { 161 public Nodeid manifest() {
239 throw new InternalError(ex.toString()); 235 throw new InternalError(ex.toString());
240 } 236 }
241 } 237 }
242 } 238 }
243 239
244 /** 240 /*package-local*/static final class RawCsetFactory implements ChangesetParser.CsetFactory {
245 * @see mercurial/changelog.py:read() 241 private RawChangeset cset;
246 * 242
247 * <pre> 243 public RawCsetFactory(boolean shallReuseCsetInstance) {
248 * format used:
249 * nodeid\n : manifest node in ascii
250 * user\n : user, no \n or \r allowed
251 * time tz extra\n : date (time is int or float, timezone is int)
252 * : extra is metadatas, encoded and separated by '\0'
253 * : older versions ignore it
254 * files\n\n : files modified by the cset, no \n or \r allowed
255 * (.*) : comment (free text, ideally utf-8)
256 *
257 * changelog v0 doesn't use extra
258 * </pre>
259 */
260 /*package-local*/static final class ChangesetParser {
261 private final EncodingHelper encHelper;
262 // it's likely user names get repeated again and again throughout repository.
263 private final Pool<String> usersPool;
264 private final Pool<String> filesPool;
265 private final boolean reuseChangesetInstance;
266 private RawChangeset target;
267
268 public ChangesetParser(SessionContext.Source sessionContex, boolean shallReuseCsetInstance) {
269 encHelper = Internals.buildFileNameEncodingHelper(sessionContex);
270 usersPool = new Pool<String>();
271 filesPool = new Pool<String>();
272 reuseChangesetInstance = shallReuseCsetInstance;
273 if (shallReuseCsetInstance) { 244 if (shallReuseCsetInstance) {
245 cset = new RawChangeset();
246 }
247 }
248
249 public RawChangeset create(Nodeid nodeidManifest, String user, Date time, int timezone, List<String> files, String comment, Map<String, String> extrasMap) {
250 RawChangeset target;
251 if (cset != null) {
252 target = cset;
253 } else {
274 target = new RawChangeset(); 254 target = new RawChangeset();
275 } 255 }
276 } 256 target.manifest = nodeidManifest;
277 257 target.user = user;
278 public void dispose() { 258 target.time = time;
279 usersPool.clear(); 259 target.timezone = timezone;
280 filesPool.clear(); 260 target.files = files == null ? new String[0] : files.toArray(new String[files.size()]);
281 } 261 target.comment = comment;
282 262 target.extras = extrasMap;
283 public RawChangeset parse(DataAccess da) throws IOException, HgInvalidDataFormatException {
284 byte[] data = da.byteArray();
285 if (!reuseChangesetInstance) {
286 target = new RawChangeset();
287 }
288 init(data, 0, data.length);
289 return target; 263 return target;
290 } 264 }
291 265 }
292 private void init(byte[] data, int offset, int length) throws HgInvalidDataFormatException { 266
293 final int bufferEndIndex = offset + length;
294 final byte lineBreak = (byte) '\n';
295 int breakIndex1 = indexOf(data, lineBreak, offset, bufferEndIndex);
296 if (breakIndex1 == -1) {
297 throw new HgInvalidDataFormatException("Bad Changeset data");
298 }
299 Nodeid _nodeid = Nodeid.fromAscii(data, 0, breakIndex1);
300 int breakIndex2 = indexOf(data, lineBreak, breakIndex1 + 1, bufferEndIndex);
301 if (breakIndex2 == -1) {
302 throw new HgInvalidDataFormatException("Bad Changeset data");
303 }
304 String _user;
305 _user = encHelper.userFromChangeset(data, breakIndex1 + 1, breakIndex2 - breakIndex1 - 1);
306 _user = usersPool.unify(_user);
307
308 int breakIndex3 = indexOf(data, lineBreak, breakIndex2 + 1, bufferEndIndex);
309 if (breakIndex3 == -1) {
310 throw new HgInvalidDataFormatException("Bad Changeset data");
311 }
312 String _timeString = new String(data, breakIndex2 + 1, breakIndex3 - breakIndex2 - 1);
313 int space1 = _timeString.indexOf(' ');
314 if (space1 == -1) {
315 throw new HgInvalidDataFormatException(String.format("Bad Changeset data: %s in [%d..%d]", "time string", breakIndex2+1, breakIndex3));
316 }
317 int space2 = _timeString.indexOf(' ', space1 + 1);
318 if (space2 == -1) {
319 space2 = _timeString.length();
320 }
321 long unixTime = Long.parseLong(_timeString.substring(0, space1));
322 int _timezone = Integer.parseInt(_timeString.substring(space1 + 1, space2));
323 // unixTime is local time, and timezone records difference of the local time to UTC.
324 Date _time = new Date(unixTime * 1000);
325 String _extras = space2 < _timeString.length() ? _timeString.substring(space2 + 1) : null;
326 Map<String, String> _extrasMap = parseExtras(_extras);
327 //
328 int lastStart = breakIndex3 + 1;
329 int breakIndex4 = indexOf(data, lineBreak, lastStart, bufferEndIndex);
330 ArrayList<String> _files = null;
331 if (breakIndex4 > lastStart) {
332 // if breakIndex4 == lastStart, we already found \n\n and hence there are no files (e.g. merge revision)
333 _files = new ArrayList<String>(5);
334 while (breakIndex4 != -1 && breakIndex4 + 1 < bufferEndIndex) {
335 String fname = encHelper.fileFromChangeset(data, lastStart, breakIndex4 - lastStart);
336 _files.add(filesPool.unify(fname));
337 lastStart = breakIndex4 + 1;
338 if (data[breakIndex4 + 1] == lineBreak) {
339 // found \n\n
340 break;
341 } else {
342 breakIndex4 = indexOf(data, lineBreak, lastStart, bufferEndIndex);
343 }
344 }
345 if (breakIndex4 == -1 || breakIndex4 >= bufferEndIndex) {
346 throw new HgInvalidDataFormatException("Bad Changeset data");
347 }
348 } else {
349 breakIndex4--;
350 }
351 String _comment = encHelper.commentFromChangeset(data, breakIndex4 + 2, bufferEndIndex - breakIndex4 - 2);
352 // change this instance at once, don't leave it partially changes in case of error
353 target.manifest = _nodeid;
354 target.user = _user;
355 target.time = _time;
356 target.timezone = _timezone;
357 target.files = _files == null ? new String[0] : _files.toArray(new String[_files.size()]);
358 target.comment = _comment;
359 target.extras = _extrasMap;
360 }
361
362 private Map<String, String> parseExtras(String _extras) {
363 final String extras_branch_key = "branch";
364 _extras = _extras == null ? null : _extras.trim();
365 if (_extras == null || _extras.length() == 0) {
366 return Collections.singletonMap(extras_branch_key, HgRepository.DEFAULT_BRANCH_NAME);
367 }
368 Map<String, String> _extrasMap = new HashMap<String, String>();
369 int lastIndex = 0;
370 do {
371 String pair;
372 int sp = _extras.indexOf('\0', lastIndex);
373 if (sp == -1) {
374 sp = _extras.length();
375 }
376 if (sp > lastIndex) {
377 pair = _extras.substring(lastIndex, sp);
378 pair = decode(pair);
379 int eq = pair.indexOf(':');
380 _extrasMap.put(pair.substring(0, eq), pair.substring(eq + 1));
381 lastIndex = sp + 1;
382 }
383 } while (lastIndex < _extras.length());
384 if (!_extrasMap.containsKey(extras_branch_key)) {
385 _extrasMap.put(extras_branch_key, HgRepository.DEFAULT_BRANCH_NAME);
386 }
387 return Collections.unmodifiableMap(_extrasMap);
388 }
389
390 private static int indexOf(byte[] src, byte what, int startOffset, int endIndex) {
391 for (int i = startOffset; i < endIndex; i++) {
392 if (src[i] == what) {
393 return i;
394 }
395 }
396 return -1;
397 }
398
399 private static String decode(String s) {
400 if (s != null && s.indexOf('\\') != -1) {
401 // TestAuxUtilities#testChangelogExtrasDecode
402 return s.replace("\\\\", "\\").replace("\\n", "\n").replace("\\r", "\r").replace("\\0", "\00");
403 }
404 return s;
405 }
406 }
407
408 private static class RawCsetCollector implements Inspector { 267 private static class RawCsetCollector implements Inspector {
409 final ArrayList<RawChangeset> result; 268 final ArrayList<RawChangeset> result;
410 269
411 public RawCsetCollector(int count) { 270 public RawCsetCollector(int count) {
412 result = new ArrayList<RawChangeset>(count > 0 ? count : 5); 271 result = new ArrayList<RawChangeset>(count > 0 ? count : 5);
428 private final Lifecycle inspectorLifecycle; 287 private final Lifecycle inspectorLifecycle;
429 288
430 public RawCsetParser(SessionContext.Source sessionContext, HgChangelog.Inspector delegate) { 289 public RawCsetParser(SessionContext.Source sessionContext, HgChangelog.Inspector delegate) {
431 assert delegate != null; 290 assert delegate != null;
432 inspector = delegate; 291 inspector = delegate;
433 csetBuilder = new ChangesetParser(sessionContext, true); 292 csetBuilder = new ChangesetParser(sessionContext, new RawCsetFactory(true));
434 inspectorLifecycle = Adaptable.Factory.getAdapter(delegate, Lifecycle.class, null); 293 inspectorLifecycle = Adaptable.Factory.getAdapter(delegate, Lifecycle.class, null);
435 if (inspectorLifecycle == null) { 294 if (inspectorLifecycle == null) {
436 ProgressSupport ph = Adaptable.Factory.getAdapter(delegate, ProgressSupport.class, null); 295 ProgressSupport ph = Adaptable.Factory.getAdapter(delegate, ProgressSupport.class, null);
437 CancelSupport cs = Adaptable.Factory.getAdapter(delegate, CancelSupport.class, null); 296 CancelSupport cs = Adaptable.Factory.getAdapter(delegate, CancelSupport.class, null);
438 if (cs != null || ph != null) { 297 if (cs != null || ph != null) {