comparison src/org/tmatesoft/hg/repo/HgBranches.java @ 610:5c68567b3645

Refresh tags, branches, bookmarks and ignore when their files (or csets in the repo) are changed
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 09 May 2013 21:06:48 +0200
parents b3c16d1aede0
children 6526d8adbc0f
comparison
equal deleted inserted replaced
609:e4a71afd3c71 610:5c68567b3645
1 /* 1 /*
2 * Copyright (c) 2011-2012 TMate Software Ltd 2 * Copyright (c) 2011-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 *
36 import java.util.Map; 36 import java.util.Map;
37 import java.util.TreeMap; 37 import java.util.TreeMap;
38 import java.util.regex.Pattern; 38 import java.util.regex.Pattern;
39 39
40 import org.tmatesoft.hg.core.Nodeid; 40 import org.tmatesoft.hg.core.Nodeid;
41 import org.tmatesoft.hg.internal.ChangelogMonitor;
41 import org.tmatesoft.hg.internal.Experimental; 42 import org.tmatesoft.hg.internal.Experimental;
42 import org.tmatesoft.hg.internal.Internals; 43 import org.tmatesoft.hg.internal.Internals;
43 import org.tmatesoft.hg.repo.HgChangelog.RawChangeset; 44 import org.tmatesoft.hg.repo.HgChangelog.RawChangeset;
44 import org.tmatesoft.hg.util.ProgressSupport; 45 import org.tmatesoft.hg.util.ProgressSupport;
45 46
48 * @author Artem Tikhomirov 49 * @author Artem Tikhomirov
49 * @author TMate Software Ltd. 50 * @author TMate Software Ltd.
50 */ 51 */
51 public class HgBranches { 52 public class HgBranches {
52 53
54 private final Internals internalRepo;
55 private final ChangelogMonitor repoChangeTracker;
53 private final Map<String, BranchInfo> branches = new TreeMap<String, BranchInfo>(); 56 private final Map<String, BranchInfo> branches = new TreeMap<String, BranchInfo>();
54 private final HgRepository repo;
55 private final Internals internalRepo;
56 private boolean isCacheActual = false; 57 private boolean isCacheActual = false;
57 58
58 HgBranches(Internals internals) { 59 HgBranches(Internals internals) {
59 internalRepo = internals; 60 internalRepo = internals;
60 repo = internals.getRepo(); // merely a cached value 61 repoChangeTracker = new ChangelogMonitor(internals.getRepo());
61 } 62 }
62 63
63 private int readCache() { 64 private int readCache() {
65 final HgRepository repo = internalRepo.getRepo();
64 File branchheadsCache = getCacheFile(); 66 File branchheadsCache = getCacheFile();
65 int lastInCache = -1; 67 int lastInCache = -1;
66 if (!branchheadsCache.canRead()) { 68 if (!branchheadsCache.canRead()) {
67 return lastInCache; 69 return lastInCache;
68 } 70 }
128 return -1; // deliberately not lastInCache, to avoid anything but -1 when 1st line was read and there's error is in lines 2..end 130 return -1; // deliberately not lastInCache, to avoid anything but -1 when 1st line was read and there's error is in lines 2..end
129 } 131 }
130 132
131 void collect(final ProgressSupport ps) throws HgInvalidControlFileException { 133 void collect(final ProgressSupport ps) throws HgInvalidControlFileException {
132 branches.clear(); 134 branches.clear();
135 final HgRepository repo = internalRepo.getRepo();
133 ps.start(1 + repo.getChangelog().getRevisionCount() * 2); 136 ps.start(1 + repo.getChangelog().getRevisionCount() * 2);
134 // 137 //
135 int lastCached = readCache(); 138 int lastCached = readCache();
136 isCacheActual = lastCached == repo.getChangelog().getLastRevision(); 139 isCacheActual = lastCached == repo.getChangelog().getLastRevision();
137 if (!isCacheActual) { 140 if (!isCacheActual) {
225 final HgChangelog clog = repo.getChangelog(); 228 final HgChangelog clog = repo.getChangelog();
226 final HgRevisionMap<HgChangelog> rmap = new HgRevisionMap<HgChangelog>(clog).init(); 229 final HgRevisionMap<HgChangelog> rmap = new HgRevisionMap<HgChangelog>(clog).init();
227 for (BranchInfo bi : branches.values()) { 230 for (BranchInfo bi : branches.values()) {
228 bi.validate(clog, rmap); 231 bi.validate(clog, rmap);
229 } 232 }
233 repoChangeTracker.touch();
230 ps.done(); 234 ps.done();
231 } 235 }
232 236
233 public List<BranchInfo> getAllBranches() { 237 public List<BranchInfo> getAllBranches() throws HgInvalidControlFileException {
234 return new LinkedList<BranchInfo>(branches.values()); 238 return new LinkedList<BranchInfo>(branches.values());
235 239
236 } 240 }
237 241
238 public BranchInfo getBranch(String name) { 242 public BranchInfo getBranch(String name) throws HgInvalidControlFileException {
239 return branches.get(name); 243 return branches.get(name);
240 } 244 }
241 245
242 /** 246 /**
243 * Writes down information about repository branches in a format Mercurial native client can understand. 247 * Writes down information about repository branches in a format Mercurial native client can understand.
256 branchheadsCache.createNewFile(); 260 branchheadsCache.createNewFile();
257 } 261 }
258 if (!branchheadsCache.canWrite()) { 262 if (!branchheadsCache.canWrite()) {
259 return; 263 return;
260 } 264 }
265 final HgRepository repo = internalRepo.getRepo();
261 final int lastRev = repo.getChangelog().getLastRevision(); 266 final int lastRev = repo.getChangelog().getLastRevision();
262 final Nodeid lastNid = repo.getChangelog().getRevision(lastRev); 267 final Nodeid lastNid = repo.getChangelog().getRevision(lastRev);
263 BufferedWriter bw = new BufferedWriter(new FileWriter(branchheadsCache)); 268 BufferedWriter bw = new BufferedWriter(new FileWriter(branchheadsCache));
264 bw.write(lastNid.toString()); 269 bw.write(lastNid.toString());
265 bw.write((int) ' '); 270 bw.write((int) ' ');
277 } 282 }
278 283
279 private File getCacheFile() { 284 private File getCacheFile() {
280 // prior to 1.8 used to be .hg/branchheads.cache 285 // prior to 1.8 used to be .hg/branchheads.cache
281 return internalRepo.getFileFromRepoDir("cache/branchheads"); 286 return internalRepo.getFileFromRepoDir("cache/branchheads");
287 }
288
289 /*package-local*/ void reloadIfChanged(ProgressSupport ps) throws HgInvalidControlFileException {
290 if (repoChangeTracker.isChanged()) {
291 collect(ps);
292 }
282 } 293 }
283 294
284 public static class BranchInfo { 295 public static class BranchInfo {
285 private final String name; 296 private final String name;
286 private List<Nodeid> heads; 297 private List<Nodeid> heads;