Mercurial > hg4j
comparison src/org/tmatesoft/hg/repo/HgBranches.java @ 244:4b661efb9374
Use updated location of cache files (cache/ folder instead of .cache filename extension). Provide means to update (write down) cache for subsequent uses
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Tue, 12 Jul 2011 02:48:34 +0200 |
parents | 883300108179 |
children | 981f9f50bb6c |
comparison
equal
deleted
inserted
replaced
243:0e01f9182e16 | 244:4b661efb9374 |
---|---|
15 * contact TMate Software at support@hg4j.com | 15 * contact TMate Software at support@hg4j.com |
16 */ | 16 */ |
17 package org.tmatesoft.hg.repo; | 17 package org.tmatesoft.hg.repo; |
18 | 18 |
19 import java.io.BufferedReader; | 19 import java.io.BufferedReader; |
20 import java.io.BufferedWriter; | |
20 import java.io.File; | 21 import java.io.File; |
21 import java.io.FileReader; | 22 import java.io.FileReader; |
23 import java.io.FileWriter; | |
22 import java.io.IOException; | 24 import java.io.IOException; |
23 import java.util.ArrayList; | 25 import java.util.ArrayList; |
24 import java.util.Arrays; | 26 import java.util.Arrays; |
25 import java.util.Collections; | 27 import java.util.Collections; |
26 import java.util.HashMap; | 28 import java.util.HashMap; |
31 import java.util.Map; | 33 import java.util.Map; |
32 import java.util.TreeMap; | 34 import java.util.TreeMap; |
33 import java.util.regex.Pattern; | 35 import java.util.regex.Pattern; |
34 | 36 |
35 import org.tmatesoft.hg.core.Nodeid; | 37 import org.tmatesoft.hg.core.Nodeid; |
38 import org.tmatesoft.hg.internal.Experimental; | |
36 import org.tmatesoft.hg.repo.HgChangelog.RawChangeset; | 39 import org.tmatesoft.hg.repo.HgChangelog.RawChangeset; |
37 import org.tmatesoft.hg.util.ProgressSupport; | 40 import org.tmatesoft.hg.util.ProgressSupport; |
38 | 41 |
39 /** | 42 /** |
40 * | 43 * |
43 */ | 46 */ |
44 public class HgBranches { | 47 public class HgBranches { |
45 | 48 |
46 private final Map<String, BranchInfo> branches = new TreeMap<String, BranchInfo>(); | 49 private final Map<String, BranchInfo> branches = new TreeMap<String, BranchInfo>(); |
47 private final HgRepository repo; | 50 private final HgRepository repo; |
48 | 51 private boolean isCacheActual = false; |
52 | |
49 HgBranches(HgRepository hgRepo) { | 53 HgBranches(HgRepository hgRepo) { |
50 repo = hgRepo; | 54 repo = hgRepo; |
51 } | 55 } |
52 | 56 |
53 private int readCache() { | 57 private int readCache() { |
54 File branchheadsCache = new File(repo.getRepositoryRoot(), "branchheads.cache"); | 58 File branchheadsCache = getCacheFile(); |
55 int lastInCache = -1; | 59 int lastInCache = -1; |
56 if (!branchheadsCache.canRead()) { | 60 if (!branchheadsCache.canRead()) { |
57 return lastInCache; | 61 return lastInCache; |
58 } | 62 } |
59 BufferedReader br = null; | 63 BufferedReader br = null; |
67 String[] cacheIdentity = spacePattern.split(line.trim()); | 71 String[] cacheIdentity = spacePattern.split(line.trim()); |
68 lastInCache = Integer.parseInt(cacheIdentity[1]); | 72 lastInCache = Integer.parseInt(cacheIdentity[1]); |
69 // XXX may want to check if nodeid of cset from repo.getChangelog() of lastInCache index match cacheIdentity[0] | 73 // XXX may want to check if nodeid of cset from repo.getChangelog() of lastInCache index match cacheIdentity[0] |
70 // | 74 // |
71 while ((line = br.readLine()) != null) { | 75 while ((line = br.readLine()) != null) { |
72 String[] elements = line.trim().split(" "); | 76 String[] elements = spacePattern.split(line.trim()); |
73 if (elements.length < 2) { | 77 if (elements.length < 2) { |
74 // bad entry | 78 // bad entry |
75 continue; | 79 continue; |
76 } | 80 } |
77 Nodeid[] branchHeads = new Nodeid[elements.length - 1]; | 81 Nodeid[] branchHeads = new Nodeid[elements.length - 1]; |
160 System.out.println("Updated branch " + bi.getName()); | 164 System.out.println("Updated branch " + bi.getName()); |
161 branches.put(bi.getName(), new BranchInfo(bi.getName(), first, bi.getHeads().toArray(new Nodeid[0]), closedHeads.size() == bi.getHeads().size())); | 165 branches.put(bi.getName(), new BranchInfo(bi.getName(), first, bi.getHeads().toArray(new Nodeid[0]), closedHeads.size() == bi.getHeads().size())); |
162 } | 166 } |
163 } | 167 } |
164 */ | 168 */ |
165 if (lastCached != repo.getChangelog().getLastRevision()) { | 169 isCacheActual = lastCached == repo.getChangelog().getLastRevision(); |
170 if (!isCacheActual) { | |
166 final HgChangelog.ParentWalker pw = repo.getChangelog().new ParentWalker(); | 171 final HgChangelog.ParentWalker pw = repo.getChangelog().new ParentWalker(); |
167 pw.init(); | 172 pw.init(); |
168 ps.worked(repo.getChangelog().getRevisionCount()); | 173 ps.worked(repo.getChangelog().getRevisionCount()); |
169 final HashMap<String, Nodeid> branchStart = new HashMap<String, Nodeid>(); | 174 final HashMap<String, Nodeid> branchStart = new HashMap<String, Nodeid>(); |
170 final HashMap<String, Nodeid> branchLastSeen = new HashMap<String, Nodeid>(); | 175 final HashMap<String, Nodeid> branchLastSeen = new HashMap<String, Nodeid>(); |
241 } | 246 } |
242 | 247 |
243 public BranchInfo getBranch(String name) { | 248 public BranchInfo getBranch(String name) { |
244 return branches.get(name); | 249 return branches.get(name); |
245 } | 250 } |
246 | 251 |
252 /** | |
253 * Writes down information about repository branches in a format Mercurial native client can understand. | |
254 * Cache file gets overwritten only if it is out of date (i.e. misses some branch information) | |
255 */ | |
256 @Experimental(reason="Usage of cache isn't supposed to be public knowledge") | |
257 public void writeCache() { | |
258 if (isCacheActual) { | |
259 return; | |
260 } | |
261 try { | |
262 File branchheadsCache = getCacheFile(); | |
263 if (!branchheadsCache.exists()) { | |
264 branchheadsCache.getParentFile().mkdirs(); // just in case cache/ doesn't exist jet | |
265 branchheadsCache.createNewFile(); | |
266 } | |
267 if (!branchheadsCache.canWrite()) { | |
268 return; | |
269 } | |
270 final int lastRev = repo.getChangelog().getLastRevision(); | |
271 final Nodeid lastNid = repo.getChangelog().getRevision(lastRev); | |
272 BufferedWriter bw = new BufferedWriter(new FileWriter(branchheadsCache)); | |
273 bw.write(lastNid.toString()); | |
274 bw.write((int) ' '); | |
275 bw.write(Integer.toString(lastRev)); | |
276 bw.write("\n"); | |
277 for (BranchInfo bi : branches.values()) { | |
278 for (Nodeid nid : bi.getHeads()) { | |
279 bw.write(nid.toString()); | |
280 bw.write((int) ' '); | |
281 bw.write(bi.getName()); | |
282 bw.write("\n"); | |
283 } | |
284 } | |
285 bw.close(); | |
286 } catch (IOException e) { | |
287 // TODO Auto-generated catch block | |
288 e.printStackTrace(); | |
289 } | |
290 } | |
291 | |
292 private File getCacheFile() { | |
293 // prior to 1.8 used to be .hg/branchheads.cache | |
294 return new File(repo.getRepositoryRoot(), "cache/branchheads"); | |
295 } | |
296 | |
247 public static class BranchInfo { | 297 public static class BranchInfo { |
248 private final String name; | 298 private final String name; |
249 private final List<Nodeid> heads; | 299 private final List<Nodeid> heads; |
250 private final boolean closed; | 300 private final boolean closed; |
251 private final Nodeid start; | 301 private final Nodeid start; |