diff 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
line wrap: on
line diff
--- a/src/org/tmatesoft/hg/repo/HgBranches.java	Wed May 08 17:11:45 2013 +0200
+++ b/src/org/tmatesoft/hg/repo/HgBranches.java	Thu May 09 21:06:48 2013 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011-2012 TMate Software Ltd
+ * Copyright (c) 2011-2013 TMate Software Ltd
  *  
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -38,6 +38,7 @@
 import java.util.regex.Pattern;
 
 import org.tmatesoft.hg.core.Nodeid;
+import org.tmatesoft.hg.internal.ChangelogMonitor;
 import org.tmatesoft.hg.internal.Experimental;
 import org.tmatesoft.hg.internal.Internals;
 import org.tmatesoft.hg.repo.HgChangelog.RawChangeset;
@@ -50,17 +51,18 @@
  */
 public class HgBranches {
 	
+	private final Internals internalRepo;
+	private final ChangelogMonitor repoChangeTracker;
 	private final Map<String, BranchInfo> branches = new TreeMap<String, BranchInfo>();
-	private final HgRepository repo;
-	private final Internals internalRepo;
 	private boolean isCacheActual = false;
 
 	HgBranches(Internals internals) {
 		internalRepo = internals;
-		repo = internals.getRepo(); // merely a cached value
+		repoChangeTracker = new ChangelogMonitor(internals.getRepo());
 	}
 
 	private int readCache() {
+		final HgRepository repo = internalRepo.getRepo();
 		File branchheadsCache = getCacheFile();
 		int lastInCache = -1;
 		if (!branchheadsCache.canRead()) {
@@ -130,6 +132,7 @@
 
 	void collect(final ProgressSupport ps) throws HgInvalidControlFileException {
 		branches.clear();
+		final HgRepository repo = internalRepo.getRepo();
 		ps.start(1 + repo.getChangelog().getRevisionCount() * 2);
 		//
 		int lastCached = readCache();
@@ -227,15 +230,16 @@
 		for (BranchInfo bi : branches.values()) {
 			bi.validate(clog, rmap);
 		}
+		repoChangeTracker.touch();
 		ps.done();
 	}
 
-	public List<BranchInfo> getAllBranches() {
+	public List<BranchInfo> getAllBranches() throws HgInvalidControlFileException {
 		return new LinkedList<BranchInfo>(branches.values());
 				
 	}
 
-	public BranchInfo getBranch(String name) {
+	public BranchInfo getBranch(String name) throws HgInvalidControlFileException {
 		return branches.get(name);
 	}
 
@@ -258,6 +262,7 @@
 		if (!branchheadsCache.canWrite()) {
 			return;
 		}
+		final HgRepository repo = internalRepo.getRepo();
 		final int lastRev = repo.getChangelog().getLastRevision();
 		final Nodeid lastNid = repo.getChangelog().getRevision(lastRev);
 		BufferedWriter bw = new BufferedWriter(new FileWriter(branchheadsCache));
@@ -280,6 +285,12 @@
 		// prior to 1.8 used to be .hg/branchheads.cache
 		return internalRepo.getFileFromRepoDir("cache/branchheads");
 	}
+	
+	/*package-local*/ void reloadIfChanged(ProgressSupport ps) throws HgInvalidControlFileException {
+		if (repoChangeTracker.isChanged()) {
+			collect(ps);
+		}
+	}
 
 	public static class BranchInfo {
 		private final String name;