changeset 647:c75297c17867

Location of repository files as enumeration, use file constants instead of plain names
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 25 Jun 2013 18:53:18 +0200
parents 3b7d51ed4c65
children 690e71d29bf6
files src/org/tmatesoft/hg/internal/FNCacheFile.java src/org/tmatesoft/hg/internal/Internals.java src/org/tmatesoft/hg/internal/PhasesHelper.java src/org/tmatesoft/hg/repo/HgRepository.java src/org/tmatesoft/hg/repo/HgRepositoryFiles.java
diffstat 5 files changed, 51 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/src/org/tmatesoft/hg/internal/FNCacheFile.java	Fri Jun 21 18:30:35 2013 +0200
+++ b/src/org/tmatesoft/hg/internal/FNCacheFile.java	Tue Jun 25 18:53:18 2013 +0200
@@ -16,6 +16,8 @@
  */
 package org.tmatesoft.hg.internal;
 
+import static org.tmatesoft.hg.repo.HgRepositoryFiles.FNCache;
+
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
@@ -78,7 +80,7 @@
 		if (addedDotI.isEmpty() && addedDotD.isEmpty()) {
 			return;
 		}
-		File f = fncacheFile();
+		File f = repo.getRepositoryFile(FNCache);
 		f.getParentFile().mkdirs();
 		final Charset filenameEncoding = repo.getFilenameEncoding();
 		ArrayList<CharBuffer> added = new ArrayList<CharBuffer>();
@@ -112,8 +114,4 @@
 	public void addData(Path p) {
 		addedDotD.add(p);
 	}
-
-	private File fncacheFile() {
-		return repo.getFileFromStoreDir("fncache");
-	}
 }
--- a/src/org/tmatesoft/hg/internal/Internals.java	Fri Jun 21 18:30:35 2013 +0200
+++ b/src/org/tmatesoft/hg/internal/Internals.java	Tue Jun 25 18:53:18 2013 +0200
@@ -150,12 +150,16 @@
 	}
 	
 	public File getRepositoryFile(HgRepositoryFiles f) {
-		return f.residesUnderRepositoryRoot() ? getFileFromRepoDir(f.getName()) : new File(repo.getWorkingDir(), f.getName());
+		switch (f.getHome()) {
+			case Store : return getFileFromStoreDir(f.getName());
+			case Repo : return getFileFromRepoDir(f.getName());
+			default : return new File(repo.getWorkingDir(), f.getName());
+		}
 	}
 
 	/**
 	 * Access files under ".hg/".
-	 * File not necessarily exists, this method is merely a factory for Files at specific, configuration-dependent location. 
+	 * File not necessarily exists, this method is merely a factory for {@link File files} at specific, configuration-dependent location. 
 	 * 
 	 * @param name shall be normalized path
 	 */
--- a/src/org/tmatesoft/hg/internal/PhasesHelper.java	Fri Jun 21 18:30:35 2013 +0200
+++ b/src/org/tmatesoft/hg/internal/PhasesHelper.java	Tue Jun 25 18:53:18 2013 +0200
@@ -18,6 +18,7 @@
 
 import static org.tmatesoft.hg.repo.HgPhase.Draft;
 import static org.tmatesoft.hg.repo.HgPhase.Secret;
+import static org.tmatesoft.hg.repo.HgRepositoryFiles.Phaseroots;
 import static org.tmatesoft.hg.util.LogFacility.Severity.Info;
 import static org.tmatesoft.hg.util.LogFacility.Severity.Warn;
 
@@ -134,7 +135,7 @@
 	}
 
 	private Boolean readRoots() throws HgRuntimeException {
-		File phaseroots = repo.getFileFromStoreDir("phaseroots"); // TODO into HgRepositoryFiles
+		File phaseroots = repo.getRepositoryFile(Phaseroots);
 		BufferedReader br = null;
 		try {
 			if (!phaseroots.exists()) {
--- a/src/org/tmatesoft/hg/repo/HgRepository.java	Fri Jun 21 18:30:35 2013 +0200
+++ b/src/org/tmatesoft/hg/repo/HgRepository.java	Tue Jun 25 18:53:18 2013 +0200
@@ -16,7 +16,7 @@
  */
 package org.tmatesoft.hg.repo;
 
-import static org.tmatesoft.hg.repo.HgRepositoryFiles.LastMessage;
+import static org.tmatesoft.hg.repo.HgRepositoryFiles.*;
 import static org.tmatesoft.hg.util.LogFacility.Severity.Warn;
 
 import java.io.File;
@@ -389,7 +389,7 @@
 	public HgRepositoryLock getWorkingDirLock() {
 		if (wdLock == null) {
 			int timeout = getLockTimeout();
-			File lf = impl.getFileFromRepoDir("wlock");
+			File lf = impl.getRepositoryFile(WorkingCopyLock);
 			synchronized (this) {
 				if (wdLock == null) {
 					wdLock = new HgRepositoryLock(lf, timeout);
@@ -407,7 +407,7 @@
 	public HgRepositoryLock getStoreLock() {
 		if (storeLock == null) {
 			int timeout = getLockTimeout();
-			File fl = impl.getFileFromStoreDir("lock");
+			File fl = impl.getRepositoryFile(StoreLock);
 			synchronized (this) {
 				if (storeLock == null) {
 					storeLock = new HgRepositoryLock(fl, timeout);
--- a/src/org/tmatesoft/hg/repo/HgRepositoryFiles.java	Fri Jun 21 18:30:35 2013 +0200
+++ b/src/org/tmatesoft/hg/repo/HgRepositoryFiles.java	Tue Jun 25 18:53:18 2013 +0200
@@ -25,35 +25,46 @@
  */
 public enum HgRepositoryFiles {
 
-	HgIgnore(".hgignore"), HgTags(".hgtags"), HgEol(".hgeol"), 
-	Dirstate(false, "dirstate"), HgLocalTags(false, "localtags"),
-	HgSub(".hgsub"), HgSubstate(".hgsubstate"),
-	LastMessage(false, "last-message.txt"),
-	Bookmarks(false, "bookmarks"), BookmarksCurrent(false, "bookmarks.current"),
-	Branch(false, "branch"), 
-	UndoBranch(false, "undo.branch"), UndoDirstate(false, "undo.dirstate");
+	HgIgnore(Home.Root, ".hgignore"), HgTags(Home.Root, ".hgtags"), HgEol(Home.Root, ".hgeol"), 
+	Dirstate(Home.Repo, "dirstate"), HgLocalTags(Home.Repo, "localtags"),
+	HgSub(Home.Root, ".hgsub"), HgSubstate(Home.Root, ".hgsubstate"),
+	LastMessage(Home.Repo, "last-message.txt"),
+	Bookmarks(Home.Repo, "bookmarks"), BookmarksCurrent(Home.Repo, "bookmarks.current"),
+	Branch(Home.Repo, "branch"), 
+	UndoBranch(Home.Repo, "undo.branch"), UndoDirstate(Home.Repo, "undo.dirstate"),
+	Phaseroots(Home.Store, "phaseroots"), FNCache(Home.Store, "fncache"),
+	WorkingCopyLock(Home.Repo, "wlock"), StoreLock(Home.Store, "lock");
+
+	/**
+	 * Possible file locations 
+	 */
+	public enum Home {
+		Root, Repo, Store
+	}
 
 	private final String fname;
-	private final boolean livesInWC; 
+	private final Home residesIn; 
 	
-	private HgRepositoryFiles(String filename) {
-		this(true, filename);
-	}
-
-	private HgRepositoryFiles(boolean wcNotRepoRoot, String filename) {
+	private HgRepositoryFiles(Home home, String filename) {
 		fname = filename;
-		livesInWC = wcNotRepoRoot;
+		residesIn = home;
 	}
 
 	/**
-	 * Path to the file, relative to the parent it lives in.
+	 * Path to the file, relative to the repository root.
 	 * 
 	 * For repository files that reside in working directory, return their location relative to the working dir.
-	 * For files that reside under repository root, path returned would include '.hg/' prefix.
+	 * For files that reside under repository root, path returned includes '.hg/' prefix.
+	 * For files from {@link Home#Store} storage area, path starts with '.hg/store/', although actual use of 'store' folder
+	 * is controlled by repository requirements. Returned value shall be deemed as 'most likely' path in a general environment.
 	 * @return file location, never <code>null</code>
 	 */
 	public String getPath() {
-		return livesInWC ? getName() : ".hg/" + getName();
+		switch (residesIn) {
+			case Store : return ".hg/store/" + getName();
+			case Repo : return ".hg/" + getName();
+			default : return getName();
+		}
 	}
 
 	/**
@@ -73,13 +84,20 @@
 	 * @return <code>true</code> if file lives in working tree
 	 */
 	public boolean residesUnderWorkingDir() {
-		return livesInWC;
+		return residesIn == Home.Root;
 	}
 
 	/**
 	 * @return <code>true</code> if file lives under '.hg/' 
 	 */
 	public boolean residesUnderRepositoryRoot() {
-		return !livesInWC;
+		return residesIn == Home.Repo;
+	}
+	
+	/**
+	 * Identify a root the file lives under
+	 */
+	public Home getHome() {
+		return residesIn;
 	}
 }