comparison src/org/tmatesoft/hg/internal/PhasesHelper.java @ 649:e79cf9a8130b

Push: phase4 - update local and remote phase information
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 26 Jun 2013 20:52:38 +0200
parents 690e71d29bf6
children 3b275cc2d2aa
comparison
equal deleted inserted replaced
648:690e71d29bf6 649:e79cf9a8130b
17 package org.tmatesoft.hg.internal; 17 package org.tmatesoft.hg.internal;
18 18
19 import static org.tmatesoft.hg.repo.HgPhase.Draft; 19 import static org.tmatesoft.hg.repo.HgPhase.Draft;
20 import static org.tmatesoft.hg.repo.HgPhase.Secret; 20 import static org.tmatesoft.hg.repo.HgPhase.Secret;
21 import static org.tmatesoft.hg.repo.HgRepositoryFiles.Phaseroots; 21 import static org.tmatesoft.hg.repo.HgRepositoryFiles.Phaseroots;
22 import static org.tmatesoft.hg.util.LogFacility.Severity.Info;
23 import static org.tmatesoft.hg.util.LogFacility.Severity.Warn; 22 import static org.tmatesoft.hg.util.LogFacility.Severity.Warn;
24 23
25 import java.io.BufferedReader;
26 import java.io.File; 24 import java.io.File;
27 import java.io.FileReader; 25 import java.io.FileWriter;
28 import java.io.IOException; 26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.Collections; 29 import java.util.Collections;
30 import java.util.HashMap; 30 import java.util.HashMap;
31 import java.util.LinkedList; 31 import java.util.LinkedList;
32 import java.util.List; 32 import java.util.List;
33 33
34 import org.tmatesoft.hg.core.HgChangeset; 34 import org.tmatesoft.hg.core.HgChangeset;
35 import org.tmatesoft.hg.core.HgIOException;
35 import org.tmatesoft.hg.core.Nodeid; 36 import org.tmatesoft.hg.core.Nodeid;
36 import org.tmatesoft.hg.repo.HgChangelog; 37 import org.tmatesoft.hg.repo.HgChangelog;
37 import org.tmatesoft.hg.repo.HgInvalidControlFileException; 38 import org.tmatesoft.hg.repo.HgInvalidControlFileException;
38 import org.tmatesoft.hg.repo.HgParentChildMap; 39 import org.tmatesoft.hg.repo.HgParentChildMap;
39 import org.tmatesoft.hg.repo.HgPhase; 40 import org.tmatesoft.hg.repo.HgPhase;
139 */ 140 */
140 public RevisionSet allSecret() { 141 public RevisionSet allSecret() {
141 return allOf(HgPhase.Secret); 142 return allOf(HgPhase.Secret);
142 } 143 }
143 144
145 /**
146 * @return all revisions with draft phase
147 */
144 public RevisionSet allDraft() { 148 public RevisionSet allDraft() {
145 return allOf(HgPhase.Draft).subtract(allOf(HgPhase.Secret)); 149 return allOf(HgPhase.Draft).subtract(allOf(HgPhase.Secret));
150 }
151
152 public void updateRoots(Collection<Nodeid> draftRoots, Collection<Nodeid> secretRoots) throws HgInvalidControlFileException {
153 draftPhaseRoots = draftRoots.isEmpty() ? Collections.<Nodeid>emptyList() : new ArrayList<Nodeid>(draftRoots);
154 secretPhaseRoots = secretRoots.isEmpty() ? Collections.<Nodeid>emptyList() : new ArrayList<Nodeid>(secretRoots);
155 String fmt = "%d %s\n";
156 File phaseroots = repo.getRepositoryFile(Phaseroots);
157 FileWriter fw = null;
158 try {
159 fw = new FileWriter(phaseroots);
160 for (Nodeid n : secretPhaseRoots) {
161 fw.write(String.format(fmt, HgPhase.Secret.mercurialOrdinal(), n.toString()));
162 }
163 for (Nodeid n : draftPhaseRoots) {
164 fw.write(String.format(fmt, HgPhase.Draft.mercurialOrdinal(), n.toString()));
165 }
166 fw.flush();
167 } catch (IOException ex) {
168 throw new HgInvalidControlFileException(ex.getMessage(), ex, phaseroots);
169 } finally {
170 new FileUtils(repo.getLog()).closeQuietly(fw);
171 }
146 } 172 }
147 173
148 /** 174 /**
149 * For a given phase, collect all revisions with phase that is the same or more private (i.e. for Draft, returns Draft+Secret) 175 * For a given phase, collect all revisions with phase that is the same or more private (i.e. for Draft, returns Draft+Secret)
150 * The reason is not a nice API intention (which is awful, indeed), but an ease of implementation 176 * The reason is not a nice API intention (which is awful, indeed), but an ease of implementation
166 } 192 }
167 } 193 }
168 194
169 private Boolean readRoots() throws HgRuntimeException { 195 private Boolean readRoots() throws HgRuntimeException {
170 File phaseroots = repo.getRepositoryFile(Phaseroots); 196 File phaseroots = repo.getRepositoryFile(Phaseroots);
171 BufferedReader br = null;
172 try { 197 try {
173 if (!phaseroots.exists()) { 198 if (!phaseroots.exists()) {
174 return Boolean.FALSE; 199 return Boolean.FALSE;
175 } 200 }
201 LineReader lr = new LineReader(phaseroots, repo.getLog());
202 final Collection<String> lines = lr.read(new LineReader.SimpleLineCollector(), new LinkedList<String>());
176 HashMap<HgPhase, List<Nodeid>> phase2roots = new HashMap<HgPhase, List<Nodeid>>(); 203 HashMap<HgPhase, List<Nodeid>> phase2roots = new HashMap<HgPhase, List<Nodeid>>();
177 br = new BufferedReader(new FileReader(phaseroots)); 204 for (String line : lines) {
178 String line; 205 String[] lc = line.split("\\s+");
179 while ((line = br.readLine()) != null) {
180 String[] lc = line.trim().split("\\s+");
181 if (lc.length == 0) { 206 if (lc.length == 0) {
182 continue; 207 continue;
183 } 208 }
184 if (lc.length != 2) { 209 if (lc.length != 2) {
185 repo.getSessionContext().getLog().dump(getClass(), Warn, "Bad line in phaseroots:%s", line); 210 repo.getSessionContext().getLog().dump(getClass(), Warn, "Bad line in phaseroots:%s", line);
198 } 223 }
199 roots.add(rootRev); 224 roots.add(rootRev);
200 } 225 }
201 draftPhaseRoots = phase2roots.containsKey(Draft) ? phase2roots.get(Draft) : Collections.<Nodeid>emptyList(); 226 draftPhaseRoots = phase2roots.containsKey(Draft) ? phase2roots.get(Draft) : Collections.<Nodeid>emptyList();
202 secretPhaseRoots = phase2roots.containsKey(Secret) ? phase2roots.get(Secret) : Collections.<Nodeid>emptyList(); 227 secretPhaseRoots = phase2roots.containsKey(Secret) ? phase2roots.get(Secret) : Collections.<Nodeid>emptyList();
203 } catch (IOException ex) { 228 } catch (HgIOException ex) {
204 throw new HgInvalidControlFileException(ex.toString(), ex, phaseroots); 229 throw new HgInvalidControlFileException(ex, true);
205 } finally {
206 if (br != null) {
207 try {
208 br.close();
209 } catch (IOException ex) {
210 repo.getSessionContext().getLog().dump(getClass(), Info, ex, null);
211 // ignore the exception otherwise
212 }
213 }
214 } 230 }
215 return Boolean.TRUE; 231 return Boolean.TRUE;
216 } 232 }
217 233
218 private List<Nodeid> getPhaseRoots(HgPhase phase) { 234 private List<Nodeid> getPhaseRoots(HgPhase phase) {