extras;
tikhomirov@129:
tikhomirov@153: /**
tikhomirov@153: * @see mercurial/changelog.py:read()
tikhomirov@153: *
tikhomirov@153: *
tikhomirov@153: * format used:
tikhomirov@153: * nodeid\n : manifest node in ascii
tikhomirov@153: * user\n : user, no \n or \r allowed
tikhomirov@153: * time tz extra\n : date (time is int or float, timezone is int)
tikhomirov@153: * : extra is metadatas, encoded and separated by '\0'
tikhomirov@153: * : older versions ignore it
tikhomirov@153: * files\n\n : files modified by the cset, no \n or \r allowed
tikhomirov@153: * (.*) : comment (free text, ideally utf-8)
tikhomirov@153: *
tikhomirov@153: * changelog v0 doesn't use extra
tikhomirov@153: *
tikhomirov@153: */
tikhomirov@154: private RawChangeset() {
tikhomirov@153: }
tikhomirov@129:
tikhomirov@153: public Nodeid manifest() {
tikhomirov@153: return manifest;
tikhomirov@153: }
tikhomirov@129:
tikhomirov@153: public String user() {
tikhomirov@153: return user;
tikhomirov@153: }
tikhomirov@153:
tikhomirov@153: public String comment() {
tikhomirov@153: return comment;
tikhomirov@153: }
tikhomirov@153:
tikhomirov@153: public List files() {
tikhomirov@153: return files;
tikhomirov@153: }
tikhomirov@153:
tikhomirov@153: public Date date() {
tikhomirov@153: return time;
tikhomirov@153: }
tikhomirov@211:
tikhomirov@211: /**
tikhomirov@211: * @return time zone value, as is, positive for Western Hemisphere.
tikhomirov@211: */
tikhomirov@211: public int timezone() {
tikhomirov@211: return timezone;
tikhomirov@211: }
tikhomirov@153:
tikhomirov@153: public String dateString() {
tikhomirov@153: // XXX keep once formatted? Perhaps, there's faster way to set up calendar/time zone?
tikhomirov@153: StringBuilder sb = new StringBuilder(30);
tikhomirov@153: Formatter f = new Formatter(sb, Locale.US);
tikhomirov@211: TimeZone tz = TimeZone.getTimeZone(TimeZone.getAvailableIDs(timezone * 1000)[0]);
tikhomirov@153: // apparently timezone field records number of seconds time differs from UTC,
tikhomirov@153: // i.e. value to substract from time to get UTC time. Calendar seems to add
tikhomirov@153: // timezone offset to UTC, instead, hence sign change.
tikhomirov@211: // tz.setRawOffset(timezone * -1000);
tikhomirov@153: Calendar c = Calendar.getInstance(tz, Locale.US);
tikhomirov@153: c.setTime(time);
tikhomirov@153: f.format("%ta % 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@153: sb.append("Files: ").append(files.size());
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@153:
tikhomirov@427: /*package*/ static RawChangeset parse(DataAccess da) throws IOException, HgInvalidDataFormatException {
tikhomirov@365: byte[] data = da.byteArray();
tikhomirov@365: RawChangeset rv = new RawChangeset();
tikhomirov@365: rv.init(data, 0, data.length, null);
tikhomirov@365: return rv;
tikhomirov@153: }
tikhomirov@153:
tikhomirov@196: // @param usersPool - it's likely user names get repeated again and again throughout repository. can be null
tikhomirov@427: /* package-local */void init(byte[] data, int offset, int length, Pool usersPool) 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@589: try {
tikhomirov@589: // TODO use encoding helper? Although where encoding is fixed (like here), seems to be just too much
tikhomirov@589: _user = new String(data, breakIndex1 + 1, breakIndex2 - breakIndex1 - 1, "UTF-8");
tikhomirov@589: if (usersPool != null) {
tikhomirov@589: _user = usersPool.unify(_user);
tikhomirov@589: }
tikhomirov@589: } catch (UnsupportedEncodingException ex) {
tikhomirov@589: _user = "";
tikhomirov@589: // Could hardly happen
tikhomirov@589: throw new HgInvalidDataFormatException("Bad Changeset data", ex);
tikhomirov@196: }
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@153: Map _extrasMap;
tikhomirov@252: final String extras_branch_key = "branch";
tikhomirov@386: if (_extras == null || _extras.trim().length() == 0) {
tikhomirov@252: _extrasMap = Collections.singletonMap(extras_branch_key, HgRepository.DEFAULT_BRANCH_NAME);
tikhomirov@153: } else {
tikhomirov@153: _extrasMap = new HashMap();
tikhomirov@153: for (String pair : _extras.split("\00")) {
tikhomirov@363: pair = decode(pair);
tikhomirov@153: int eq = pair.indexOf(':');
tikhomirov@153: _extrasMap.put(pair.substring(0, eq), pair.substring(eq + 1));
tikhomirov@153: }
tikhomirov@252: if (!_extrasMap.containsKey(extras_branch_key)) {
tikhomirov@252: _extrasMap.put(extras_branch_key, HgRepository.DEFAULT_BRANCH_NAME);
tikhomirov@153: }
tikhomirov@153: _extrasMap = Collections.unmodifiableMap(_extrasMap);
tikhomirov@153: }
tikhomirov@153:
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@153: _files.add(new String(data, lastStart, breakIndex4 - lastStart));
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@153: String _comment;
tikhomirov@153: try {
tikhomirov@153: _comment = new String(data, breakIndex4 + 2, bufferEndIndex - breakIndex4 - 2, "UTF-8");
tikhomirov@418: // TODO post-1.0 respect ui.fallbackencoding and try to decode if set; use EncodingHelper
tikhomirov@153: } catch (UnsupportedEncodingException ex) {
tikhomirov@153: _comment = "";
tikhomirov@366: // Could hardly happen
tikhomirov@427: throw new HgInvalidDataFormatException("Bad Changeset data", ex);
tikhomirov@153: }
tikhomirov@153: // change this instance at once, don't leave it partially changes in case of error
tikhomirov@153: this.manifest = _nodeid;
tikhomirov@153: this.user = _user;
tikhomirov@153: this.time = _time;
tikhomirov@153: this.timezone = _timezone;
tikhomirov@153: this.files = _files == null ? Collections. emptyList() : Collections.unmodifiableList(_files);
tikhomirov@153: this.comment = _comment;
tikhomirov@153: this.extras = _extrasMap;
tikhomirov@153: }
tikhomirov@129:
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@520: private static class RawCsetParser implements RevlogStream.Inspector, Adaptable {
tikhomirov@196:
tikhomirov@196: private final Inspector inspector;
tikhomirov@196: private final Pool usersPool;
tikhomirov@196: private final RawChangeset cset = new RawChangeset();
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@196: public RawCsetParser(HgChangelog.Inspector delegate) {
tikhomirov@196: assert delegate != null;
tikhomirov@196: inspector = delegate;
tikhomirov@196: usersPool = new Pool();
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@196: byte[] data = da.byteArray();
tikhomirov@196: cset.init(data, 0, data.length, usersPool);
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@520: if (inspectorLifecycle != null) {
tikhomirov@520: return adapterClass.cast(inspectorLifecycle);
tikhomirov@520: }
tikhomirov@520: // reveal interest in lifecycle only when either progress or cancel support is there
tikhomirov@520: // and inspector itself doesn't respond to lifecycle request
tikhomirov@520: // lifecycleStub may still be null here (no progress and cancel), it's ok to cast(null)
tikhomirov@520: return adapterClass.cast(lifecycleStub);
tikhomirov@520:
tikhomirov@312: }
tikhomirov@520: return Adaptable.Factory.getAdapter(inspector, adapterClass, null);
tikhomirov@312: }
tikhomirov@312:
tikhomirov@196: }
tikhomirov@0: }