Mercurial > hg4j
comparison src/org/tmatesoft/hg/repo/HgBranches.java @ 354:5f9073eabf06
Propagate errors with exceptions up to a end client
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Thu, 01 Dec 2011 05:21:40 +0100 |
parents | a0864b2892cd |
children | 189dc6dc1c3e |
comparison
equal
deleted
inserted
replaced
353:0f3687e79f5a | 354:5f9073eabf06 |
---|---|
32 import java.util.List; | 32 import java.util.List; |
33 import java.util.Map; | 33 import java.util.Map; |
34 import java.util.TreeMap; | 34 import java.util.TreeMap; |
35 import java.util.regex.Pattern; | 35 import java.util.regex.Pattern; |
36 | 36 |
37 import org.tmatesoft.hg.core.HgException; | |
38 import org.tmatesoft.hg.core.HgInvalidControlFileException; | |
39 import org.tmatesoft.hg.core.HgInvalidRevisionException; | |
37 import org.tmatesoft.hg.core.Nodeid; | 40 import org.tmatesoft.hg.core.Nodeid; |
38 import org.tmatesoft.hg.internal.Experimental; | 41 import org.tmatesoft.hg.internal.Experimental; |
39 import org.tmatesoft.hg.repo.HgChangelog.RawChangeset; | 42 import org.tmatesoft.hg.repo.HgChangelog.RawChangeset; |
40 import org.tmatesoft.hg.util.ProgressSupport; | 43 import org.tmatesoft.hg.util.ProgressSupport; |
41 | 44 |
100 // log error, but otherwise do nothing | 103 // log error, but otherwise do nothing |
101 repo.getContext().getLog().warn(getClass(), ex, null); | 104 repo.getContext().getLog().warn(getClass(), ex, null); |
102 // FALL THROUGH to return -1 indicating no cache information | 105 // FALL THROUGH to return -1 indicating no cache information |
103 } catch (NumberFormatException ex) { | 106 } catch (NumberFormatException ex) { |
104 repo.getContext().getLog().warn(getClass(), ex, null); | 107 repo.getContext().getLog().warn(getClass(), ex, null); |
108 // FALL THROUGH | |
109 } catch (HgInvalidControlFileException ex) { | |
110 // shall not happen, thus log as error | |
111 repo.getContext().getLog().error(getClass(), ex, null); | |
112 // FALL THROUGH | |
113 } catch (HgInvalidRevisionException ex) { | |
114 repo.getContext().getLog().error(getClass(), ex, null); | |
105 // FALL THROUGH | 115 // FALL THROUGH |
106 } finally { | 116 } finally { |
107 if (br != null) { | 117 if (br != null) { |
108 try { | 118 try { |
109 br.close(); | 119 br.close(); |
227 } | 237 } |
228 | 238 |
229 /** | 239 /** |
230 * Writes down information about repository branches in a format Mercurial native client can understand. | 240 * Writes down information about repository branches in a format Mercurial native client can understand. |
231 * Cache file gets overwritten only if it is out of date (i.e. misses some branch information) | 241 * Cache file gets overwritten only if it is out of date (i.e. misses some branch information) |
242 * @throws IOException if write to cache file failed | |
243 * @throws HgException subclass of {@link HgException} in case of repository access issue | |
232 */ | 244 */ |
233 @Experimental(reason="Usage of cache isn't supposed to be public knowledge") | 245 @Experimental(reason="Usage of cache isn't supposed to be public knowledge") |
234 public void writeCache() { | 246 public void writeCache() throws IOException, HgException { |
235 if (isCacheActual) { | 247 if (isCacheActual) { |
236 return; | 248 return; |
237 } | 249 } |
238 try { | 250 File branchheadsCache = getCacheFile(); |
239 File branchheadsCache = getCacheFile(); | 251 if (!branchheadsCache.exists()) { |
240 if (!branchheadsCache.exists()) { | 252 branchheadsCache.getParentFile().mkdirs(); // just in case cache/ doesn't exist jet |
241 branchheadsCache.getParentFile().mkdirs(); // just in case cache/ doesn't exist jet | 253 branchheadsCache.createNewFile(); |
242 branchheadsCache.createNewFile(); | 254 } |
243 } | 255 if (!branchheadsCache.canWrite()) { |
244 if (!branchheadsCache.canWrite()) { | 256 return; |
245 return; | 257 } |
246 } | 258 final int lastRev = repo.getChangelog().getLastRevision(); |
247 final int lastRev = repo.getChangelog().getLastRevision(); | 259 final Nodeid lastNid = repo.getChangelog().getRevision(lastRev); |
248 final Nodeid lastNid = repo.getChangelog().getRevision(lastRev); | 260 BufferedWriter bw = new BufferedWriter(new FileWriter(branchheadsCache)); |
249 BufferedWriter bw = new BufferedWriter(new FileWriter(branchheadsCache)); | 261 bw.write(lastNid.toString()); |
250 bw.write(lastNid.toString()); | 262 bw.write((int) ' '); |
251 bw.write((int) ' '); | 263 bw.write(Integer.toString(lastRev)); |
252 bw.write(Integer.toString(lastRev)); | 264 bw.write("\n"); |
253 bw.write("\n"); | 265 for (BranchInfo bi : branches.values()) { |
254 for (BranchInfo bi : branches.values()) { | 266 for (Nodeid nid : bi.getHeads()) { |
255 for (Nodeid nid : bi.getHeads()) { | 267 bw.write(nid.toString()); |
256 bw.write(nid.toString()); | 268 bw.write((int) ' '); |
257 bw.write((int) ' '); | 269 bw.write(bi.getName()); |
258 bw.write(bi.getName()); | 270 bw.write("\n"); |
259 bw.write("\n"); | 271 } |
260 } | 272 } |
261 } | 273 bw.close(); |
262 bw.close(); | |
263 } catch (IOException ex) { | |
264 repo.getContext().getLog().error(getClass(), ex, "Error writing branch cache file"); | |
265 } | |
266 } | 274 } |
267 | 275 |
268 private File getCacheFile() { | 276 private File getCacheFile() { |
269 // prior to 1.8 used to be .hg/branchheads.cache | 277 // prior to 1.8 used to be .hg/branchheads.cache |
270 return new File(repo.getRepositoryRoot(), "cache/branchheads"); | 278 return new File(repo.getRepositoryRoot(), "cache/branchheads"); |