comparison src/org/tmatesoft/hg/internal/PhasesHelper.java @ 628:6526d8adbc0f

Explicit HgRuntimeException to facilitate easy switch from runtime to checked exceptions
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 22 May 2013 15:52:31 +0200
parents d2f6ab541330
children c75297c17867
comparison
equal deleted inserted replaced
627:5153eb73b18d 628:6526d8adbc0f
1 /* 1 /*
2 * Copyright (c) 2012 TMate Software Ltd 2 * Copyright (c) 2012-2013 TMate Software Ltd
3 * 3 *
4 * This program is free software; you can redistribute it and/or modify 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 5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License. 6 * the Free Software Foundation; version 2 of the License.
7 * 7 *
35 import org.tmatesoft.hg.repo.HgChangelog; 35 import org.tmatesoft.hg.repo.HgChangelog;
36 import org.tmatesoft.hg.repo.HgInvalidControlFileException; 36 import org.tmatesoft.hg.repo.HgInvalidControlFileException;
37 import org.tmatesoft.hg.repo.HgParentChildMap; 37 import org.tmatesoft.hg.repo.HgParentChildMap;
38 import org.tmatesoft.hg.repo.HgPhase; 38 import org.tmatesoft.hg.repo.HgPhase;
39 import org.tmatesoft.hg.repo.HgRepository; 39 import org.tmatesoft.hg.repo.HgRepository;
40 import org.tmatesoft.hg.repo.HgRuntimeException;
40 41
41 /** 42 /**
42 * Support to deal with Mercurial phases feature (as of Mercurial version 2.1) 43 * Support to deal with Mercurial phases feature (as of Mercurial version 2.1)
43 * 44 *
44 * @see http://mercurial.selenic.com/wiki/Phases 45 * @see http://mercurial.selenic.com/wiki/Phases
67 68
68 public HgRepository getRepo() { 69 public HgRepository getRepo() {
69 return repo.getRepo(); 70 return repo.getRepo();
70 } 71 }
71 72
72 public boolean isCapableOfPhases() throws HgInvalidControlFileException { 73 public boolean isCapableOfPhases() throws HgRuntimeException {
73 if (null == repoSupporsPhases) { 74 if (null == repoSupporsPhases) {
74 repoSupporsPhases = readRoots(); 75 repoSupporsPhases = readRoots();
75 } 76 }
76 return repoSupporsPhases.booleanValue(); 77 return repoSupporsPhases.booleanValue();
77 } 78 }
78 79
79 80
80 public HgPhase getPhase(HgChangeset cset) throws HgInvalidControlFileException { 81 /**
82 * @param cset revision to query
83 * @return phase of the changeset, never <code>null</code>
84 * @throws HgInvalidControlFileException if failed to access revlog index/data entry. <em>Runtime exception</em>
85 * @throws HgRuntimeException subclass thereof to indicate other issues with the library. <em>Runtime exception</em>
86 */
87 public HgPhase getPhase(HgChangeset cset) throws HgRuntimeException {
81 final Nodeid csetRev = cset.getNodeid(); 88 final Nodeid csetRev = cset.getNodeid();
82 final int csetRevIndex = cset.getRevisionIndex(); 89 final int csetRevIndex = cset.getRevisionIndex();
83 return getPhase(csetRevIndex, csetRev); 90 return getPhase(csetRevIndex, csetRev);
84 } 91 }
85 92
86 public HgPhase getPhase(final int csetRevIndex, Nodeid csetRev) throws HgInvalidControlFileException { 93 /**
94 * @param csetRevIndex revision index to query
95 * @param csetRev revision nodeid, optional
96 * @return phase of the changeset, never <code>null</code>
97 * @throws HgInvalidControlFileException if failed to access revlog index/data entry. <em>Runtime exception</em>
98 * @throws HgRuntimeException subclass thereof to indicate other issues with the library. <em>Runtime exception</em>
99 */
100 public HgPhase getPhase(final int csetRevIndex, Nodeid csetRev) throws HgRuntimeException {
87 if (!isCapableOfPhases()) { 101 if (!isCapableOfPhases()) {
88 return HgPhase.Undefined; 102 return HgPhase.Undefined;
89 } 103 }
90 // csetRev is only used when parentHelper is available 104 // csetRev is only used when parentHelper is available
91 if (parentHelper != null && (csetRev == null || csetRev.isNull())) { 105 if (parentHelper != null && (csetRev == null || csetRev.isNull())) {
117 } 131 }
118 return HgPhase.Public; 132 return HgPhase.Public;
119 133
120 } 134 }
121 135
122 private Boolean readRoots() throws HgInvalidControlFileException { 136 private Boolean readRoots() throws HgRuntimeException {
123 File phaseroots = repo.getFileFromStoreDir("phaseroots"); 137 File phaseroots = repo.getFileFromStoreDir("phaseroots"); // TODO into HgRepositoryFiles
124 BufferedReader br = null; 138 BufferedReader br = null;
125 try { 139 try {
126 if (!phaseroots.exists()) { 140 if (!phaseroots.exists()) {
127 return Boolean.FALSE; 141 return Boolean.FALSE;
128 } 142 }
175 } 189 }
176 return Collections.emptyList(); 190 return Collections.emptyList();
177 } 191 }
178 192
179 193
180 private RevisionDescendants[] getPhaseDescendants(HgPhase phase) throws HgInvalidControlFileException { 194 private RevisionDescendants[] getPhaseDescendants(HgPhase phase) throws HgRuntimeException {
181 int ordinal = phase.ordinal(); 195 int ordinal = phase.ordinal();
182 if (phaseDescendants[ordinal] == null) { 196 if (phaseDescendants[ordinal] == null) {
183 phaseDescendants[ordinal] = buildPhaseDescendants(phase); 197 phaseDescendants[ordinal] = buildPhaseDescendants(phase);
184 } 198 }
185 return phaseDescendants[ordinal]; 199 return phaseDescendants[ordinal];
186 } 200 }
187 201
188 private RevisionDescendants[] buildPhaseDescendants(HgPhase phase) throws HgInvalidControlFileException { 202 private RevisionDescendants[] buildPhaseDescendants(HgPhase phase) throws HgRuntimeException {
189 int[] roots = toIndexes(getPhaseRoots(phase)); 203 int[] roots = toIndexes(getPhaseRoots(phase));
190 RevisionDescendants[] rv = new RevisionDescendants[roots.length]; 204 RevisionDescendants[] rv = new RevisionDescendants[roots.length];
191 for (int i = 0; i < roots.length; i++) { 205 for (int i = 0; i < roots.length; i++) {
192 rv[i] = new RevisionDescendants(getRepo(), roots[i]); 206 rv[i] = new RevisionDescendants(getRepo(), roots[i]);
193 rv[i].build(); 207 rv[i].build();
194 } 208 }
195 return rv; 209 return rv;
196 } 210 }
197 211
198 private int[] toIndexes(List<Nodeid> roots) throws HgInvalidControlFileException { 212 private int[] toIndexes(List<Nodeid> roots) throws HgRuntimeException {
199 int[] rv = new int[roots.size()]; 213 int[] rv = new int[roots.size()];
200 for (int i = 0; i < rv.length; i++) { 214 for (int i = 0; i < rv.length; i++) {
201 rv[i] = getRepo().getChangelog().getRevisionIndex(roots.get(i)); 215 rv[i] = getRepo().getChangelog().getRevisionIndex(roots.get(i));
202 } 216 }
203 return rv; 217 return rv;