extras() {
tikhomirov@153: return extras;
tikhomirov@153: }
tikhomirov@153:
tikhomirov@153: public String branch() {
tikhomirov@153: return extras.get("branch");
tikhomirov@153: }
tikhomirov@153:
tikhomirov@153: @Override
tikhomirov@153: public String toString() {
tikhomirov@153: StringBuilder sb = new StringBuilder();
tikhomirov@153: sb.append("Changeset {");
tikhomirov@153: sb.append("User: ").append(user).append(", ");
tikhomirov@153: sb.append("Comment: ").append(comment).append(", ");
tikhomirov@153: sb.append("Manifest: ").append(manifest).append(", ");
tikhomirov@153: sb.append("Date: ").append(time).append(", ");
tikhomirov@667: sb.append("Files: ").append(files.length);
tikhomirov@153: for (String s : files) {
tikhomirov@153: sb.append(", ").append(s);
tikhomirov@153: }
tikhomirov@153: if (extras != null) {
tikhomirov@153: sb.append(", Extra: ").append(extras);
tikhomirov@153: }
tikhomirov@153: sb.append("}");
tikhomirov@153: return sb.toString();
tikhomirov@153: }
tikhomirov@153:
tikhomirov@153: @Override
tikhomirov@154: public RawChangeset clone() {
tikhomirov@153: try {
tikhomirov@154: return (RawChangeset) super.clone();
tikhomirov@153: } catch (CloneNotSupportedException ex) {
tikhomirov@153: throw new InternalError(ex.toString());
tikhomirov@153: }
tikhomirov@153: }
tikhomirov@667: }
tikhomirov@667:
tikhomirov@667: /**
tikhomirov@667: * @see mercurial/changelog.py:read()
tikhomirov@667: *
tikhomirov@667: *
tikhomirov@667: * format used:
tikhomirov@667: * nodeid\n : manifest node in ascii
tikhomirov@667: * user\n : user, no \n or \r allowed
tikhomirov@667: * time tz extra\n : date (time is int or float, timezone is int)
tikhomirov@667: * : extra is metadatas, encoded and separated by '\0'
tikhomirov@667: * : older versions ignore it
tikhomirov@667: * files\n\n : files modified by the cset, no \n or \r allowed
tikhomirov@667: * (.*) : comment (free text, ideally utf-8)
tikhomirov@667: *
tikhomirov@667: * changelog v0 doesn't use extra
tikhomirov@667: *
tikhomirov@667: */
tikhomirov@667: /*package-local*/static final class ChangesetParser {
tikhomirov@667: private final EncodingHelper encHelper;
tikhomirov@667: // it's likely user names get repeated again and again throughout repository.
tikhomirov@667: private final Pool usersPool;
tikhomirov@667: private final Pool filesPool;
tikhomirov@667: private final boolean reuseChangesetInstance;
tikhomirov@667: private RawChangeset target;
tikhomirov@667:
tikhomirov@667: public ChangesetParser(SessionContext.Source sessionContex, boolean shallReuseCsetInstance) {
tikhomirov@667: encHelper = Internals.buildFileNameEncodingHelper(sessionContex);
tikhomirov@667: usersPool = new Pool();
tikhomirov@667: filesPool = new Pool();
tikhomirov@667: reuseChangesetInstance = shallReuseCsetInstance;
tikhomirov@667: if (shallReuseCsetInstance) {
tikhomirov@667: target = new RawChangeset();
tikhomirov@667: }
tikhomirov@667: }
tikhomirov@667:
tikhomirov@667: public void dispose() {
tikhomirov@667: usersPool.clear();
tikhomirov@667: filesPool.clear();
tikhomirov@153: }
tikhomirov@153:
tikhomirov@667: public RawChangeset parse(DataAccess da) throws IOException, HgInvalidDataFormatException {
tikhomirov@667: byte[] data = da.byteArray();
tikhomirov@667: if (!reuseChangesetInstance) {
tikhomirov@667: target = new RawChangeset();
tikhomirov@667: }
tikhomirov@667: init(data, 0, data.length);
tikhomirov@667: return target;
tikhomirov@667: }
tikhomirov@667:
tikhomirov@667: private void init(byte[] data, int offset, int length) throws HgInvalidDataFormatException {
tikhomirov@153: final int bufferEndIndex = offset + length;
tikhomirov@153: final byte lineBreak = (byte) '\n';
tikhomirov@153: int breakIndex1 = indexOf(data, lineBreak, offset, bufferEndIndex);
tikhomirov@153: if (breakIndex1 == -1) {
tikhomirov@427: throw new HgInvalidDataFormatException("Bad Changeset data");
tikhomirov@153: }
tikhomirov@153: Nodeid _nodeid = Nodeid.fromAscii(data, 0, breakIndex1);
tikhomirov@153: int breakIndex2 = indexOf(data, lineBreak, breakIndex1 + 1, bufferEndIndex);
tikhomirov@153: if (breakIndex2 == -1) {
tikhomirov@427: throw new HgInvalidDataFormatException("Bad Changeset data");
tikhomirov@153: }
tikhomirov@589: String _user;
tikhomirov@667: _user = encHelper.userFromChangeset(data, breakIndex1 + 1, breakIndex2 - breakIndex1 - 1);
tikhomirov@667: _user = usersPool.unify(_user);
tikhomirov@589:
tikhomirov@153: int breakIndex3 = indexOf(data, lineBreak, breakIndex2 + 1, bufferEndIndex);
tikhomirov@153: if (breakIndex3 == -1) {
tikhomirov@427: throw new HgInvalidDataFormatException("Bad Changeset data");
tikhomirov@153: }
tikhomirov@153: String _timeString = new String(data, breakIndex2 + 1, breakIndex3 - breakIndex2 - 1);
tikhomirov@153: int space1 = _timeString.indexOf(' ');
tikhomirov@153: if (space1 == -1) {
tikhomirov@427: throw new HgInvalidDataFormatException(String.format("Bad Changeset data: %s in [%d..%d]", "time string", breakIndex2+1, breakIndex3));
tikhomirov@153: }
tikhomirov@153: int space2 = _timeString.indexOf(' ', space1 + 1);
tikhomirov@153: if (space2 == -1) {
tikhomirov@153: space2 = _timeString.length();
tikhomirov@153: }
tikhomirov@363: long unixTime = Long.parseLong(_timeString.substring(0, space1));
tikhomirov@153: int _timezone = Integer.parseInt(_timeString.substring(space1 + 1, space2));
tikhomirov@363: // unixTime is local time, and timezone records difference of the local time to UTC.
tikhomirov@153: Date _time = new Date(unixTime * 1000);
tikhomirov@153: String _extras = space2 < _timeString.length() ? _timeString.substring(space2 + 1) : null;
tikhomirov@656: Map _extrasMap = parseExtras(_extras);
tikhomirov@153: //
tikhomirov@153: int lastStart = breakIndex3 + 1;
tikhomirov@153: int breakIndex4 = indexOf(data, lineBreak, lastStart, bufferEndIndex);
tikhomirov@153: ArrayList _files = null;
tikhomirov@153: if (breakIndex4 > lastStart) {
tikhomirov@153: // if breakIndex4 == lastStart, we already found \n\n and hence there are no files (e.g. merge revision)
tikhomirov@153: _files = new ArrayList(5);
tikhomirov@153: while (breakIndex4 != -1 && breakIndex4 + 1 < bufferEndIndex) {
tikhomirov@667: String fname = encHelper.fileFromChangeset(data, lastStart, breakIndex4 - lastStart);
tikhomirov@667: _files.add(filesPool.unify(fname));
tikhomirov@153: lastStart = breakIndex4 + 1;
tikhomirov@153: if (data[breakIndex4 + 1] == lineBreak) {
tikhomirov@153: // found \n\n
tikhomirov@153: break;
tikhomirov@153: } else {
tikhomirov@153: breakIndex4 = indexOf(data, lineBreak, lastStart, bufferEndIndex);
tikhomirov@129: }
tikhomirov@153: }
tikhomirov@153: if (breakIndex4 == -1 || breakIndex4 >= bufferEndIndex) {
tikhomirov@427: throw new HgInvalidDataFormatException("Bad Changeset data");
tikhomirov@153: }
tikhomirov@153: } else {
tikhomirov@153: breakIndex4--;
tikhomirov@153: }
tikhomirov@667: String _comment = encHelper.commentFromChangeset(data, breakIndex4 + 2, bufferEndIndex - breakIndex4 - 2);
tikhomirov@153: // change this instance at once, don't leave it partially changes in case of error
tikhomirov@667: target.manifest = _nodeid;
tikhomirov@667: target.user = _user;
tikhomirov@667: target.time = _time;
tikhomirov@667: target.timezone = _timezone;
tikhomirov@667: target.files = _files == null ? new String[0] : _files.toArray(new String[_files.size()]);
tikhomirov@667: target.comment = _comment;
tikhomirov@667: target.extras = _extrasMap;
tikhomirov@153: }
tikhomirov@129:
tikhomirov@656: private Map parseExtras(String _extras) {
tikhomirov@656: final String extras_branch_key = "branch";
tikhomirov@656: _extras = _extras == null ? null : _extras.trim();
tikhomirov@656: if (_extras == null || _extras.length() == 0) {
tikhomirov@656: return Collections.singletonMap(extras_branch_key, HgRepository.DEFAULT_BRANCH_NAME);
tikhomirov@656: }
tikhomirov@656: Map _extrasMap = new HashMap();
tikhomirov@656: int lastIndex = 0;
tikhomirov@656: do {
tikhomirov@656: String pair;
tikhomirov@656: int sp = _extras.indexOf('\0', lastIndex);
tikhomirov@656: if (sp == -1) {
tikhomirov@656: sp = _extras.length();
tikhomirov@656: }
tikhomirov@656: if (sp > lastIndex) {
tikhomirov@656: pair = _extras.substring(lastIndex, sp);
tikhomirov@656: pair = decode(pair);
tikhomirov@656: int eq = pair.indexOf(':');
tikhomirov@656: _extrasMap.put(pair.substring(0, eq), pair.substring(eq + 1));
tikhomirov@656: lastIndex = sp + 1;
tikhomirov@656: }
tikhomirov@656: } while (lastIndex < _extras.length());
tikhomirov@656: if (!_extrasMap.containsKey(extras_branch_key)) {
tikhomirov@656: _extrasMap.put(extras_branch_key, HgRepository.DEFAULT_BRANCH_NAME);
tikhomirov@656: }
tikhomirov@656: return Collections.unmodifiableMap(_extrasMap);
tikhomirov@656: }
tikhomirov@656:
tikhomirov@153: private static int indexOf(byte[] src, byte what, int startOffset, int endIndex) {
tikhomirov@153: for (int i = startOffset; i < endIndex; i++) {
tikhomirov@153: if (src[i] == what) {
tikhomirov@153: return i;
tikhomirov@153: }
tikhomirov@153: }
tikhomirov@153: return -1;
tikhomirov@153: }
tikhomirov@363:
tikhomirov@363: private static String decode(String s) {
tikhomirov@363: if (s != null && s.indexOf('\\') != -1) {
tikhomirov@363: // TestAuxUtilities#testChangelogExtrasDecode
tikhomirov@363: return s.replace("\\\\", "\\").replace("\\n", "\n").replace("\\r", "\r").replace("\\0", "\00");
tikhomirov@363: }
tikhomirov@363: return s;
tikhomirov@363: }
tikhomirov@129: }
tikhomirov@129:
tikhomirov@196: private static class RawCsetCollector implements Inspector {
tikhomirov@196: final ArrayList result;
tikhomirov@196:
tikhomirov@196: public RawCsetCollector(int count) {
tikhomirov@196: result = new ArrayList(count > 0 ? count : 5);
tikhomirov@196: }
tikhomirov@196:
tikhomirov@196: public void next(int revisionNumber, Nodeid nodeid, RawChangeset cset) {
tikhomirov@196: result.add(cset.clone());
tikhomirov@196: }
tikhomirov@196: }
tikhomirov@196:
tikhomirov@667: private static final class RawCsetParser implements RevlogStream.Inspector, Adaptable, Lifecycle {
tikhomirov@196:
tikhomirov@196: private final Inspector inspector;
tikhomirov@667: private final ChangesetParser csetBuilder;
tikhomirov@520: // non-null when inspector uses high-level lifecycle entities (progress and/or cancel supports)
tikhomirov@520: private final LifecycleBridge lifecycleStub;
tikhomirov@520: // non-null when inspector relies on low-level lifecycle and is responsible
tikhomirov@520: // to proceed any possible high-level entities himself.
tikhomirov@520: private final Lifecycle inspectorLifecycle;
tikhomirov@196:
tikhomirov@667: public RawCsetParser(SessionContext.Source sessionContext, HgChangelog.Inspector delegate) {
tikhomirov@196: assert delegate != null;
tikhomirov@196: inspector = delegate;
tikhomirov@667: csetBuilder = new ChangesetParser(sessionContext, true);
tikhomirov@520: inspectorLifecycle = Adaptable.Factory.getAdapter(delegate, Lifecycle.class, null);
tikhomirov@520: if (inspectorLifecycle == null) {
tikhomirov@520: ProgressSupport ph = Adaptable.Factory.getAdapter(delegate, ProgressSupport.class, null);
tikhomirov@520: CancelSupport cs = Adaptable.Factory.getAdapter(delegate, CancelSupport.class, null);
tikhomirov@520: if (cs != null || ph != null) {
tikhomirov@520: lifecycleStub = new LifecycleBridge(ph, cs);
tikhomirov@520: } else {
tikhomirov@520: lifecycleStub = null;
tikhomirov@520: }
tikhomirov@520: } else {
tikhomirov@520: lifecycleStub = null;
tikhomirov@520: }
tikhomirov@196: }
tikhomirov@196:
tikhomirov@628: public void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, DataAccess da) throws HgRuntimeException {
tikhomirov@196: try {
tikhomirov@667: RawChangeset cset = csetBuilder.parse(da);
tikhomirov@196: // XXX there's no guarantee for Changeset.Callback that distinct instance comes each time, consider instance reuse
tikhomirov@196: inspector.next(revisionNumber, Nodeid.fromBinary(nodeid, 0), cset);
tikhomirov@520: if (lifecycleStub != null) {
tikhomirov@520: lifecycleStub.nextStep();
tikhomirov@520: }
tikhomirov@427: } catch (HgInvalidDataFormatException ex) {
tikhomirov@427: throw ex.setRevisionIndex(revisionNumber);
tikhomirov@366: } catch (IOException ex) {
tikhomirov@423: // XXX need better exception, perhaps smth like HgChangelogException (extends HgInvalidControlFileException)
tikhomirov@423: throw new HgInvalidControlFileException("Failed reading changelog", ex, null).setRevisionIndex(revisionNumber);
tikhomirov@196: }
tikhomirov@520: }
tikhomirov@520:
tikhomirov@520: public T getAdapter(Class adapterClass) {
tikhomirov@520: if (adapterClass == Lifecycle.class) {
tikhomirov@667: return adapterClass.cast(this);
tikhomirov@312: }
tikhomirov@667: // XXX what if caller takes Progress/Cancel (which we update through lifecycleStub, too)
tikhomirov@520: return Adaptable.Factory.getAdapter(inspector, adapterClass, null);
tikhomirov@312: }
tikhomirov@312:
tikhomirov@667: public void start(int count, Callback callback, Object token) {
tikhomirov@667: if (inspectorLifecycle != null) {
tikhomirov@667: inspectorLifecycle.start(count, callback, token);
tikhomirov@667: } else if (lifecycleStub != null) {
tikhomirov@667: lifecycleStub.start(count, callback, token);
tikhomirov@667: }
tikhomirov@667: }
tikhomirov@667:
tikhomirov@667: public void finish(Object token) {
tikhomirov@667: if (inspectorLifecycle != null) {
tikhomirov@667: inspectorLifecycle.finish(token);
tikhomirov@667: } else if (lifecycleStub != null) {
tikhomirov@667: lifecycleStub.finish(token);
tikhomirov@667: }
tikhomirov@667: csetBuilder.dispose();
tikhomirov@667: }
tikhomirov@667:
tikhomirov@196: }
tikhomirov@0: }