changeset 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 0e01f9182e16
children 2fb439375ddc
files cmdline/org/tmatesoft/hg/console/Main.java src/org/tmatesoft/hg/core/Nodeid.java src/org/tmatesoft/hg/repo/HgBranches.java
diffstat 3 files changed, 62 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/cmdline/org/tmatesoft/hg/console/Main.java	Thu Jun 23 16:58:38 2011 +0200
+++ b/cmdline/org/tmatesoft/hg/console/Main.java	Tue Jul 12 02:48:34 2011 +0200
@@ -72,7 +72,7 @@
 
 	public static void main(String[] args) throws Exception {
 		Main m = new Main(args);
-		m.testRevisionMap();
+//		m.testRevisionMap();
 //		m.testSubrepos();
 //		m.testReadWorkingCopy();
 //		m.testParents();
@@ -80,7 +80,7 @@
 //		m.testCatAtCsetRevision();
 //		m.testMergeState();
 //		m.testFileStatus();
-//		m.dumpBranches();
+		m.dumpBranches();
 //		m.inflaterLengthException();
 //		m.dumpIgnored();
 //		m.dumpDirstate();
@@ -259,6 +259,7 @@
 			System.out.print(" ");
 			System.out.println(bi.getHeads());
 		}
+		b.writeCache();
 //		final long start = System.currentTimeMillis();
 //		for (int i = 0; i < 10; i++) {
 //			b.collect(ProgressSupport.Factory.get(null));
--- a/src/org/tmatesoft/hg/core/Nodeid.java	Thu Jun 23 16:58:38 2011 +0200
+++ b/src/org/tmatesoft/hg/core/Nodeid.java	Tue Jul 12 02:48:34 2011 +0200
@@ -95,7 +95,10 @@
 		}
 		return 0;
 	}
-	
+
+	/**
+	 * Complete string representation of this Nodeid.
+	 */
 	@Override
 	public String toString() {
 		// XXX may want to output just single 0 for the NULL id?
--- a/src/org/tmatesoft/hg/repo/HgBranches.java	Thu Jun 23 16:58:38 2011 +0200
+++ b/src/org/tmatesoft/hg/repo/HgBranches.java	Tue Jul 12 02:48:34 2011 +0200
@@ -17,8 +17,10 @@
 package org.tmatesoft.hg.repo;
 
 import java.io.BufferedReader;
+import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileReader;
+import java.io.FileWriter;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -33,6 +35,7 @@
 import java.util.regex.Pattern;
 
 import org.tmatesoft.hg.core.Nodeid;
+import org.tmatesoft.hg.internal.Experimental;
 import org.tmatesoft.hg.repo.HgChangelog.RawChangeset;
 import org.tmatesoft.hg.util.ProgressSupport;
 
@@ -45,13 +48,14 @@
 	
 	private final Map<String, BranchInfo> branches = new TreeMap<String, BranchInfo>();
 	private final HgRepository repo;
-	
+	private boolean isCacheActual = false;
+
 	HgBranches(HgRepository hgRepo) {
 		repo = hgRepo;
 	}
 
 	private int readCache() {
-		File branchheadsCache = new File(repo.getRepositoryRoot(), "branchheads.cache");
+		File branchheadsCache = getCacheFile();
 		int lastInCache = -1;
 		if (!branchheadsCache.canRead()) {
 			return lastInCache;
@@ -69,7 +73,7 @@
 			// XXX may want to check if nodeid of cset from repo.getChangelog() of lastInCache index match cacheIdentity[0]
 			//
 			while ((line = br.readLine()) != null) {
-				String[] elements = line.trim().split(" ");
+				String[] elements = spacePattern.split(line.trim());
 				if (elements.length < 2) {
 					// bad entry
 					continue;
@@ -162,7 +166,8 @@
 			}
 		}
 		*/
-		if (lastCached != repo.getChangelog().getLastRevision()) {
+		isCacheActual = lastCached == repo.getChangelog().getLastRevision();
+		if (!isCacheActual) {
 			final HgChangelog.ParentWalker pw = repo.getChangelog().new ParentWalker();
 			pw.init();
 			ps.worked(repo.getChangelog().getRevisionCount());
@@ -243,7 +248,52 @@
 	public BranchInfo getBranch(String name) {
 		return branches.get(name);
 	}
-	
+
+	/**
+	 * Writes down information about repository branches in a format Mercurial native client can understand.
+	 * Cache file gets overwritten only if it is out of date (i.e. misses some branch information)
+	 */
+	@Experimental(reason="Usage of cache isn't supposed to be public knowledge")
+	public void writeCache() {
+		if (isCacheActual) {
+			return;
+		}
+		try {
+			File branchheadsCache = getCacheFile();
+			if (!branchheadsCache.exists()) {
+				branchheadsCache.getParentFile().mkdirs(); // just in case cache/ doesn't exist jet
+				branchheadsCache.createNewFile();
+			}
+			if (!branchheadsCache.canWrite()) {
+				return;
+			}
+			final int lastRev = repo.getChangelog().getLastRevision();
+			final Nodeid lastNid = repo.getChangelog().getRevision(lastRev);
+			BufferedWriter bw = new BufferedWriter(new FileWriter(branchheadsCache));
+			bw.write(lastNid.toString());
+			bw.write((int) ' ');
+			bw.write(Integer.toString(lastRev));
+			bw.write("\n");
+			for (BranchInfo bi : branches.values()) {
+				for (Nodeid nid : bi.getHeads()) {
+					bw.write(nid.toString());
+					bw.write((int) ' ');
+					bw.write(bi.getName());
+					bw.write("\n");
+				}
+			}
+			bw.close();
+		} catch (IOException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+	}
+
+	private File getCacheFile() {
+		// prior to 1.8 used to be .hg/branchheads.cache
+		return new File(repo.getRepositoryRoot(), "cache/branchheads");
+	}
+
 	public static class BranchInfo {
 		private final String name;
 		private final List<Nodeid> heads;