comparison src/org/tmatesoft/hg/repo/Changeset.java @ 74:6f1b88693d48

Complete refactoring to org.tmatesoft
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Mon, 24 Jan 2011 03:14:45 +0100
parents src/com/tmate/hgkit/ll/Changeset.java@19e9e220bf68
children 25f2e5d1cd8b
comparison
equal deleted inserted replaced
73:0d279bcc4442 74:6f1b88693d48
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@svnkit.com
16 */
17 package org.tmatesoft.hg.repo;
18
19 import java.io.UnsupportedEncodingException;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.Date;
23 import java.util.Formatter;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Locale;
27 import java.util.Map;
28
29 import org.tmatesoft.hg.core.Nodeid;
30
31 /**
32 * @see mercurial/changelog.py:read()
33 * <pre>
34 format used:
35 nodeid\n : manifest node in ascii
36 user\n : user, no \n or \r allowed
37 time tz extra\n : date (time is int or float, timezone is int)
38 : extra is metadatas, encoded and separated by '\0'
39 : older versions ignore it
40 files\n\n : files modified by the cset, no \n or \r allowed
41 (.*) : comment (free text, ideally utf-8)
42
43 changelog v0 doesn't use extra
44 * </pre>
45 *
46 * @author Artem Tikhomirov
47 * @author TMate Software Ltd.
48 */
49 public class Changeset implements Cloneable /*for those that would like to keep a copy*/ {
50 // TODO immutable
51 private /*final*/ Nodeid manifest;
52 private String user;
53 private String comment;
54 private List<String> files; // unmodifiable collection (otherwise #files() and implicit #clone() shall be revised)
55 private Date time;
56 private int timezone; // not sure it's of any use
57 private Map<String,String> extras;
58
59 private Changeset() {
60 }
61
62 public Nodeid manifest() {
63 return manifest;
64 }
65
66 public String user() {
67 return user;
68 }
69
70 public String comment() {
71 return comment;
72 }
73
74 public List<String> files() {
75 return files;
76 }
77
78 public Date date() {
79 return time;
80 }
81
82 public String dateString() {
83 StringBuilder sb = new StringBuilder(30);
84 Formatter f = new Formatter(sb, Locale.US);
85 f.format("%ta %<tb %<td %<tH:%<tM:%<tS %<tY %<tz", time);
86 return sb.toString();
87 }
88
89 public Map<String, String> extras() {
90 return extras;
91 }
92
93 public String branch() {
94 return extras.get("branch");
95 }
96
97 @Override
98 public String toString() {
99 StringBuilder sb = new StringBuilder();
100 sb.append("Changeset {");
101 sb.append("User: ").append(user).append(", ");
102 sb.append("Comment: ").append(comment).append(", ");
103 sb.append("Manifest: ").append(manifest).append(", ");
104 sb.append("Date: ").append(time).append(", ");
105 sb.append("Files: ").append(files.size());
106 for (String s : files) {
107 sb.append(", ").append(s);
108 }
109 if (extras != null) {
110 sb.append(", Extra: ").append(extras);
111 }
112 sb.append("}");
113 return sb.toString();
114 }
115
116 @Override
117 public Changeset clone() {
118 try {
119 return (Changeset) super.clone();
120 } catch (CloneNotSupportedException ex) {
121 throw new InternalError(ex.toString());
122 }
123 }
124
125 public static Changeset parse(byte[] data, int offset, int length) {
126 Changeset rv = new Changeset();
127 rv.init(data, offset, length);
128 return rv;
129 }
130
131 /*package-local*/ void init(byte[] data, int offset, int length) {
132 final int bufferEndIndex = offset + length;
133 final byte lineBreak = (byte) '\n';
134 int breakIndex1 = indexOf(data, lineBreak, offset, bufferEndIndex);
135 if (breakIndex1 == -1) {
136 throw new IllegalArgumentException("Bad Changeset data");
137 }
138 Nodeid _nodeid = Nodeid.fromAscii(data, 0, breakIndex1);
139 int breakIndex2 = indexOf(data, lineBreak, breakIndex1+1, bufferEndIndex);
140 if (breakIndex2 == -1) {
141 throw new IllegalArgumentException("Bad Changeset data");
142 }
143 String _user = new String(data, breakIndex1+1, breakIndex2 - breakIndex1 - 1);
144 int breakIndex3 = indexOf(data, lineBreak, breakIndex2+1, bufferEndIndex);
145 if (breakIndex3 == -1) {
146 throw new IllegalArgumentException("Bad Changeset data");
147 }
148 String _timeString = new String(data, breakIndex2+1, breakIndex3 - breakIndex2 - 1);
149 int space1 = _timeString.indexOf(' ');
150 if (space1 == -1) {
151 throw new IllegalArgumentException("Bad Changeset data");
152 }
153 int space2 = _timeString.indexOf(' ', space1+1);
154 if (space2 == -1) {
155 space2 = _timeString.length();
156 }
157 long unixTime = Long.parseLong(_timeString.substring(0, space1)); // XXX Float, perhaps
158 int _timezone = Integer.parseInt(_timeString.substring(space1+1, space2));
159 // 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
160 // on commit and timezone is recorded to adjust it to UTC.
161 Date _time = new Date(unixTime * 1000);
162 String _extras = space2 < _timeString.length() ? _timeString.substring(space2+1) : null;
163 Map<String, String> _extrasMap;
164 if (_extras == null) {
165 _extrasMap = Collections.singletonMap("branch", "default");
166 } else {
167 _extrasMap = new HashMap<String, String>();
168 for (String pair : _extras.split("\00")) {
169 int eq = pair.indexOf(':');
170 // FIXME need to decode key/value, @see changelog.py:decodeextra
171 _extrasMap.put(pair.substring(0, eq), pair.substring(eq+1));
172 }
173 if (!_extrasMap.containsKey("branch")) {
174 _extrasMap.put("branch", "default");
175 }
176 _extrasMap = Collections.unmodifiableMap(_extrasMap);
177 }
178
179 //
180 int lastStart = breakIndex3 + 1;
181 int breakIndex4 = indexOf(data, lineBreak, lastStart, bufferEndIndex);
182 ArrayList<String> _files = new ArrayList<String>(5);
183 while (breakIndex4 != -1 && breakIndex4 + 1 < bufferEndIndex) {
184 _files.add(new String(data, lastStart, breakIndex4 - lastStart));
185 lastStart = breakIndex4 + 1;
186 if (data[breakIndex4 + 1] == lineBreak) {
187 // found \n\n
188 break;
189 } else {
190 breakIndex4 = indexOf(data, lineBreak, lastStart, bufferEndIndex);
191 }
192 }
193 if (breakIndex4 == -1 || breakIndex4 >= bufferEndIndex) {
194 throw new IllegalArgumentException("Bad Changeset data");
195 }
196 String _comment;
197 try {
198 _comment = new String(data, breakIndex4+2, bufferEndIndex - breakIndex4 - 2, "UTF-8");
199 } catch (UnsupportedEncodingException ex) {
200 _comment = "";
201 throw new IllegalStateException("Could hardly happen");
202 }
203 // change this instance at once, don't leave it partially changes in case of error
204 this.manifest = _nodeid;
205 this.user = _user;
206 this.time = _time;
207 this.timezone = _timezone;
208 this.files = Collections.unmodifiableList(_files);
209 this.comment = _comment;
210 this.extras = _extrasMap;
211 }
212
213 private static int indexOf(byte[] src, byte what, int startOffset, int endIndex) {
214 for (int i = startOffset; i < endIndex; i++) {
215 if (src[i] == what) {
216 return i;
217 }
218 }
219 return -1;
220 }
221
222 public interface Inspector {
223 // first(), last(), single().
224 // <T>
225 // TODO describe whether cset is new instance each time
226 void next(int revisionNumber, Nodeid nodeid, Changeset cset);
227 }
228 }