comparison src/org/tmatesoft/hg/core/HgChangeset.java @ 423:9c9c442b5f2e

Major refactoring of exception handling. Low-level API uses RuntimeExceptions, while checked are left for higher level
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 23 Mar 2012 22:51:18 +0100
parents ee8264d80747
children 6437d261048a
comparison
equal deleted inserted replaced
422:5d1cc7366d04 423:9c9c442b5f2e
21 import java.util.List; 21 import java.util.List;
22 import java.util.Map; 22 import java.util.Map;
23 23
24 import org.tmatesoft.hg.repo.HgChangelog.RawChangeset; 24 import org.tmatesoft.hg.repo.HgChangelog.RawChangeset;
25 import org.tmatesoft.hg.repo.HgChangelog; 25 import org.tmatesoft.hg.repo.HgChangelog;
26 import org.tmatesoft.hg.repo.HgInvalidStateException;
27 import org.tmatesoft.hg.repo.HgInvalidControlFileException;
26 import org.tmatesoft.hg.repo.HgRepository; 28 import org.tmatesoft.hg.repo.HgRepository;
29 import org.tmatesoft.hg.repo.HgRuntimeException;
27 import org.tmatesoft.hg.repo.HgStatusCollector; 30 import org.tmatesoft.hg.repo.HgStatusCollector;
31 import org.tmatesoft.hg.util.CancelledException;
28 import org.tmatesoft.hg.util.Path; 32 import org.tmatesoft.hg.util.Path;
29 33
30 34
31 /** 35 /**
32 * Record in the Mercurial changelog, describing single commit. 36 * Record in the Mercurial changelog, describing single commit.
168 172
169 /** 173 /**
170 * Figures out files and specific revisions thereof that were modified in this commit 174 * Figures out files and specific revisions thereof that were modified in this commit
171 * 175 *
172 * @return revisions of files modified in this commit 176 * @return revisions of files modified in this commit
173 * @throws HgInvalidControlFileException if access to revlog index/data entry failed 177 * @throws HgRuntimeException subclass thereof to indicate issues with the library. <em>Runtime exception</em>
174 * @throws HgException in case of some other library issue 178 */
175 */ 179 public List<HgFileRevision> getModifiedFiles() throws HgRuntimeException {
176 public List<HgFileRevision> getModifiedFiles() throws HgException {
177 if (modifiedFiles == null) { 180 if (modifiedFiles == null) {
178 initFileChanges(); 181 initFileChanges();
179 } 182 }
180 return modifiedFiles; 183 return modifiedFiles;
181 } 184 }
182 185
183 /** 186 /**
184 * Figures out files added in this commit 187 * Figures out files added in this commit
185 * 188 *
186 * @return revisions of files added in this commit 189 * @return revisions of files added in this commit
187 * @throws HgInvalidControlFileException if access to revlog index/data entry failed 190 * @throws HgRuntimeException subclass thereof to indicate issues with the library. <em>Runtime exception</em>
188 * @throws HgException in case of some other library issue 191 */
189 */ 192 public List<HgFileRevision> getAddedFiles() throws HgRuntimeException {
190 public List<HgFileRevision> getAddedFiles() throws HgException {
191 if (addedFiles == null) { 193 if (addedFiles == null) {
192 initFileChanges(); 194 initFileChanges();
193 } 195 }
194 return addedFiles; 196 return addedFiles;
195 } 197 }
196 198
197 /** 199 /**
198 * Figures out files that were deleted as part of this commit 200 * Figures out files that were deleted as part of this commit
199 * 201 *
200 * @return revisions of files deleted in this commit 202 * @return revisions of files deleted in this commit
201 * @throws HgInvalidControlFileException if access to revlog index/data entry failed 203 * @throws HgRuntimeException subclass thereof to indicate issues with the library. <em>Runtime exception</em>
202 * @throws HgException in case of some other library issue 204 */
203 */ 205 public List<Path> getRemovedFiles() throws HgRuntimeException {
204 public List<Path> getRemovedFiles() throws HgException {
205 if (deletedFiles == null) { 206 if (deletedFiles == null) {
206 initFileChanges(); 207 initFileChanges();
207 } 208 }
208 return deletedFiles; 209 return deletedFiles;
209 } 210 }
213 return !(getFirstParentRevision().isNull() || getSecondParentRevision().isNull()); 214 return !(getFirstParentRevision().isNull() || getSecondParentRevision().isNull());
214 } 215 }
215 216
216 /** 217 /**
217 * @return never <code>null</code> 218 * @return never <code>null</code>
218 * @throws HgInvalidControlFileException if access to revlog index/data entry failed 219 * @throws HgRuntimeException subclass thereof to indicate issues with the library. <em>Runtime exception</em>
219 */ 220 */
220 public Nodeid getFirstParentRevision() throws HgInvalidControlFileException { 221 public Nodeid getFirstParentRevision() throws HgRuntimeException {
221 if (parentHelper != null) { 222 if (parentHelper != null) {
222 return parentHelper.safeFirstParent(nodeid); 223 return parentHelper.safeFirstParent(nodeid);
223 } 224 }
224 // read once for both p1 and p2 225 // read once for both p1 and p2
225 if (parent1 == null) { 226 if (parent1 == null) {
230 return Nodeid.fromBinary(parent1, 0); 231 return Nodeid.fromBinary(parent1, 0);
231 } 232 }
232 233
233 /** 234 /**
234 * @return never <code>null</code> 235 * @return never <code>null</code>
235 * @throws HgInvalidControlFileException if access to revlog index/data entry failed 236 * @throws HgRuntimeException subclass thereof to indicate issues with the library. <em>Runtime exception</em>
236 */ 237 */
237 public Nodeid getSecondParentRevision() throws HgInvalidControlFileException { 238 public Nodeid getSecondParentRevision() throws HgRuntimeException {
238 if (parentHelper != null) { 239 if (parentHelper != null) {
239 return parentHelper.safeSecondParent(nodeid); 240 return parentHelper.safeSecondParent(nodeid);
240 } 241 }
241 if (parent2 == null) { 242 if (parent2 == null) {
242 parent1 = new byte[20]; 243 parent1 = new byte[20];
258 } catch (CloneNotSupportedException ex) { 259 } catch (CloneNotSupportedException ex) {
259 throw new InternalError(ex.toString()); 260 throw new InternalError(ex.toString());
260 } 261 }
261 } 262 }
262 263
263 private /*synchronized*/ void initFileChanges() throws HgException { 264 private /*synchronized*/ void initFileChanges() throws HgRuntimeException {
264 ArrayList<Path> deleted = new ArrayList<Path>(); 265 ArrayList<Path> deleted = new ArrayList<Path>();
265 ArrayList<HgFileRevision> modified = new ArrayList<HgFileRevision>(); 266 ArrayList<HgFileRevision> modified = new ArrayList<HgFileRevision>();
266 ArrayList<HgFileRevision> added = new ArrayList<HgFileRevision>(); 267 ArrayList<HgFileRevision> added = new ArrayList<HgFileRevision>();
267 HgStatusCollector.Record r = new HgStatusCollector.Record(); 268 HgStatusCollector.Record r = new HgStatusCollector.Record();
268 statusHelper.change(revNumber, r); 269 try {
270 statusHelper.change(revNumber, r);
271 } catch (CancelledException ex) {
272 // Record can't cancel
273 throw new HgInvalidStateException("Internal error");
274 }
269 final HgRepository repo = statusHelper.getRepo(); 275 final HgRepository repo = statusHelper.getRepo();
270 for (Path s : r.getModified()) { 276 for (Path s : r.getModified()) {
271 Nodeid nid = r.nodeidAfterChange(s); 277 Nodeid nid = r.nodeidAfterChange(s);
272 if (nid == null) { 278 if (nid == null) {
273 throw new HgException(String.format("For the file %s recorded as modified couldn't find revision after change", s)); 279 throw new HgInvalidStateException(String.format("For the file %s recorded as modified in changeset %d couldn't find revision after change", s, revNumber));
274 } 280 }
275 modified.add(new HgFileRevision(repo, nid, null, s, null)); 281 modified.add(new HgFileRevision(repo, nid, null, s, null));
276 } 282 }
277 final Map<Path, Path> copied = r.getCopied(); 283 final Map<Path, Path> copied = r.getCopied();
278 for (Path s : r.getAdded()) { 284 for (Path s : r.getAdded()) {
279 Nodeid nid = r.nodeidAfterChange(s); 285 Nodeid nid = r.nodeidAfterChange(s);
280 if (nid == null) { 286 if (nid == null) {
281 throw new HgException(String.format("For the file %s recorded as added couldn't find revision after change", s)); 287 throw new HgInvalidStateException(String.format("For the file %s recorded as added in changeset %d couldn't find revision after change", s, revNumber));
282 } 288 }
283 added.add(new HgFileRevision(repo, nid, null, s, copied.get(s))); 289 added.add(new HgFileRevision(repo, nid, null, s, copied.get(s)));
284 } 290 }
285 for (Path s : r.getRemoved()) { 291 for (Path s : r.getRemoved()) {
286 // with Path from getRemoved, may just copy 292 // with Path from getRemoved, may just copy