comparison src/org/tmatesoft/hg/internal/PhasesHelper.java @ 493:ba36f66c32b4

Refactor to keep knowledge about repository control files and their location in respect to .hg/ in a single place (facilitate future adoption of shared repositories)
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 18 Oct 2012 18:36:13 +0200
parents b3c16d1aede0
children d2f6ab541330
comparison
equal deleted inserted replaced
492:e4eaa23e3442 493:ba36f66c32b4
31 import java.util.List; 31 import java.util.List;
32 32
33 import org.tmatesoft.hg.core.HgChangeset; 33 import org.tmatesoft.hg.core.HgChangeset;
34 import org.tmatesoft.hg.core.Nodeid; 34 import org.tmatesoft.hg.core.Nodeid;
35 import org.tmatesoft.hg.repo.HgChangelog; 35 import org.tmatesoft.hg.repo.HgChangelog;
36 import org.tmatesoft.hg.repo.HgInternals;
37 import org.tmatesoft.hg.repo.HgInvalidControlFileException; 36 import org.tmatesoft.hg.repo.HgInvalidControlFileException;
38 import org.tmatesoft.hg.repo.HgParentChildMap; 37 import org.tmatesoft.hg.repo.HgParentChildMap;
39 import org.tmatesoft.hg.repo.HgPhase; 38 import org.tmatesoft.hg.repo.HgPhase;
40 import org.tmatesoft.hg.repo.HgRepository; 39 import org.tmatesoft.hg.repo.HgRepository;
41 40
48 * @author Artem Tikhomirov 47 * @author Artem Tikhomirov
49 * @author TMate Software Ltd. 48 * @author TMate Software Ltd.
50 */ 49 */
51 public final class PhasesHelper { 50 public final class PhasesHelper {
52 51
53 private final HgRepository repo; 52 private final Internals repo;
54 private final HgParentChildMap<HgChangelog> parentHelper; 53 private final HgParentChildMap<HgChangelog> parentHelper;
55 private Boolean repoSupporsPhases; 54 private Boolean repoSupporsPhases;
56 private List<Nodeid> draftPhaseRoots; 55 private List<Nodeid> draftPhaseRoots;
57 private List<Nodeid> secretPhaseRoots; 56 private List<Nodeid> secretPhaseRoots;
58 private RevisionDescendants[][] phaseDescendants = new RevisionDescendants[HgPhase.values().length][]; 57 private RevisionDescendants[][] phaseDescendants = new RevisionDescendants[HgPhase.values().length][];
59 58
60 public PhasesHelper(HgRepository hgRepo) { 59 public PhasesHelper(Internals internalRepo) {
61 this(hgRepo, null); 60 this(internalRepo, null);
62 } 61 }
63 62
64 public PhasesHelper(HgRepository hgRepo, HgParentChildMap<HgChangelog> pw) { 63 public PhasesHelper(Internals internalRepo, HgParentChildMap<HgChangelog> pw) {
65 repo = hgRepo; 64 repo = internalRepo;
66 parentHelper = pw; 65 parentHelper = pw;
67 } 66 }
68 67
69 public HgRepository getRepo() { 68 public HgRepository getRepo() {
70 return repo; 69 return repo.getRepo();
71 } 70 }
72 71
73 public boolean isCapableOfPhases() throws HgInvalidControlFileException { 72 public boolean isCapableOfPhases() throws HgInvalidControlFileException {
74 if (null == repoSupporsPhases) { 73 if (null == repoSupporsPhases) {
75 repoSupporsPhases = readRoots(); 74 repoSupporsPhases = readRoots();
88 if (!isCapableOfPhases()) { 87 if (!isCapableOfPhases()) {
89 return HgPhase.Undefined; 88 return HgPhase.Undefined;
90 } 89 }
91 // csetRev is only used when parentHelper is available 90 // csetRev is only used when parentHelper is available
92 if (parentHelper != null && (csetRev == null || csetRev.isNull())) { 91 if (parentHelper != null && (csetRev == null || csetRev.isNull())) {
93 csetRev = repo.getChangelog().getRevision(csetRevIndex); 92 csetRev = getRepo().getChangelog().getRevision(csetRevIndex);
94 } 93 }
95 94
96 for (HgPhase phase : new HgPhase[] {HgPhase.Secret, HgPhase.Draft }) { 95 for (HgPhase phase : new HgPhase[] {HgPhase.Secret, HgPhase.Draft }) {
97 List<Nodeid> roots = getPhaseRoots(phase); 96 List<Nodeid> roots = getPhaseRoots(phase);
98 if (roots.isEmpty()) { 97 if (roots.isEmpty()) {
119 return HgPhase.Public; 118 return HgPhase.Public;
120 119
121 } 120 }
122 121
123 private Boolean readRoots() throws HgInvalidControlFileException { 122 private Boolean readRoots() throws HgInvalidControlFileException {
124 // FIXME shall access phaseroots through HgRepository#repoPathHelper 123 File phaseroots = repo.getFileFromStoreDir("phaseroots");
125 File phaseroots = new File(HgInternals.getRepositoryDir(repo), "store/phaseroots");
126 BufferedReader br = null; 124 BufferedReader br = null;
127 try { 125 try {
128 if (!phaseroots.exists()) { 126 if (!phaseroots.exists()) {
129 return Boolean.FALSE; 127 return Boolean.FALSE;
130 } 128 }
135 String[] lc = line.trim().split("\\s+"); 133 String[] lc = line.trim().split("\\s+");
136 if (lc.length == 0) { 134 if (lc.length == 0) {
137 continue; 135 continue;
138 } 136 }
139 if (lc.length != 2) { 137 if (lc.length != 2) {
140 repo.getSessionContext().getLog().dump(getClass(), Warn, "Bad line in phaseroots:%s", line); 138 repo.getContext().getLog().dump(getClass(), Warn, "Bad line in phaseroots:%s", line);
141 continue; 139 continue;
142 } 140 }
143 int phaseIndex = Integer.parseInt(lc[0]); 141 int phaseIndex = Integer.parseInt(lc[0]);
144 Nodeid rootRev = Nodeid.fromAscii(lc[1]); 142 Nodeid rootRev = Nodeid.fromAscii(lc[1]);
145 if (!repo.getChangelog().isKnown(rootRev)) { 143 if (!getRepo().getChangelog().isKnown(rootRev)) {
146 repo.getSessionContext().getLog().dump(getClass(), Warn, "Phase(%d) root node %s doesn't exist in the repository, ignored.", phaseIndex, rootRev); 144 repo.getContext().getLog().dump(getClass(), Warn, "Phase(%d) root node %s doesn't exist in the repository, ignored.", phaseIndex, rootRev);
147 continue; 145 continue;
148 } 146 }
149 HgPhase phase = HgPhase.parse(phaseIndex); 147 HgPhase phase = HgPhase.parse(phaseIndex);
150 List<Nodeid> roots = phase2roots.get(phase); 148 List<Nodeid> roots = phase2roots.get(phase);
151 if (roots == null) { 149 if (roots == null) {
160 } finally { 158 } finally {
161 if (br != null) { 159 if (br != null) {
162 try { 160 try {
163 br.close(); 161 br.close();
164 } catch (IOException ex) { 162 } catch (IOException ex) {
165 repo.getSessionContext().getLog().dump(getClass(), Info, ex, null); 163 repo.getContext().getLog().dump(getClass(), Info, ex, null);
166 // ignore the exception otherwise 164 // ignore the exception otherwise
167 } 165 }
168 } 166 }
169 } 167 }
170 return Boolean.TRUE; 168 return Boolean.TRUE;
189 187
190 private RevisionDescendants[] buildPhaseDescendants(HgPhase phase) throws HgInvalidControlFileException { 188 private RevisionDescendants[] buildPhaseDescendants(HgPhase phase) throws HgInvalidControlFileException {
191 int[] roots = toIndexes(getPhaseRoots(phase)); 189 int[] roots = toIndexes(getPhaseRoots(phase));
192 RevisionDescendants[] rv = new RevisionDescendants[roots.length]; 190 RevisionDescendants[] rv = new RevisionDescendants[roots.length];
193 for (int i = 0; i < roots.length; i++) { 191 for (int i = 0; i < roots.length; i++) {
194 rv[i] = new RevisionDescendants(repo, roots[i]); 192 rv[i] = new RevisionDescendants(getRepo(), roots[i]);
195 rv[i].build(); 193 rv[i].build();
196 } 194 }
197 return rv; 195 return rv;
198 } 196 }
199 197
200 private int[] toIndexes(List<Nodeid> roots) throws HgInvalidControlFileException { 198 private int[] toIndexes(List<Nodeid> roots) throws HgInvalidControlFileException {
201 int[] rv = new int[roots.size()]; 199 int[] rv = new int[roots.size()];
202 for (int i = 0; i < rv.length; i++) { 200 for (int i = 0; i < rv.length; i++) {
203 rv[i] = repo.getChangelog().getRevisionIndex(roots.get(i)); 201 rv[i] = getRepo().getChangelog().getRevisionIndex(roots.get(i));
204 } 202 }
205 return rv; 203 return rv;
206 } 204 }
207 } 205 }