changeset 482:6c67debed07e

Distinguish files in wc from files under repo root, use these constants
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 07 Aug 2012 14:27:13 +0200
parents a458f9fb00ce
children e31e85cf4d4c
files src/org/tmatesoft/hg/repo/HgRepository.java src/org/tmatesoft/hg/repo/HgRepositoryFiles.java
diffstat 2 files changed, 67 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/src/org/tmatesoft/hg/repo/HgRepository.java	Tue Aug 07 14:02:28 2012 +0200
+++ b/src/org/tmatesoft/hg/repo/HgRepository.java	Tue Aug 07 14:27:13 2012 +0200
@@ -16,6 +16,7 @@
  */
 package org.tmatesoft.hg.repo;
 
+import static org.tmatesoft.hg.repo.HgRepositoryFiles.*;
 import static org.tmatesoft.hg.util.LogFacility.Severity.*;
 
 import java.io.File;
@@ -190,10 +191,13 @@
 		return manifest;
 	}
 	
+	/**
+	 * @throws HgRuntimeException subclass thereof to indicate issues with the library. <em>Runtime exception</em>
+	 */
 	public HgTags getTags() throws HgInvalidControlFileException {
 		if (tags == null) {
 			tags = new HgTags(this);
-			HgDataFile hgTags = getFileNode(".hgtags");
+			HgDataFile hgTags = getFileNode(HgTags.getPath());
 			if (hgTags.exists()) {
 				for (int i = 0; i <= hgTags.getLastRevision(); i++) { // TODO post-1.0 in fact, would be handy to have walk(start,end) 
 					// method for data files as well, though it looks odd.
@@ -216,9 +220,9 @@
 			}
 			File file2read = null;
 			try {
-				file2read = new File(getWorkingDir(), ".hgtags");
+				file2read = new File(getWorkingDir(), HgTags.getPath());
 				tags.readGlobal(file2read); // XXX replace with HgDataFile.workingCopy
-				file2read = new File(repoDir, "localtags");
+				file2read = new File(repoDir, HgLocalTags.getName());
 				tags.readLocal(file2read);
 			} catch (IOException ex) {
 				getContext().getLog().dump(getClass(), Error, ex, null);
@@ -228,6 +232,9 @@
 		return tags;
 	}
 	
+	/**
+	 * @throws HgRuntimeException subclass thereof to indicate issues with the library. <em>Runtime exception</em>
+	 */
 	public HgBranches getBranches() throws HgInvalidControlFileException {
 		if (branches == null) {
 			branches = new HgBranches(this);
@@ -274,7 +281,7 @@
 	 * @throws HgInvalidControlFileException if attempt to read information about working copy parents from dirstate failed 
 	 */
 	public Pair<Nodeid,Nodeid> getWorkingCopyParents() throws HgInvalidControlFileException {
-		return HgDirstate.readParents(this, new File(repoDir, "dirstate"));
+		return HgDirstate.readParents(this, new File(repoDir, Dirstate.getName()));
 	}
 	
 	/**
@@ -299,6 +306,7 @@
 	 * Provides access to sub-repositories defined in this repository. Enumerated  sub-repositories are those directly
 	 * known, not recursive collection of all nested sub-repositories.
 	 * @return list of all known sub-repositories in this repository, or empty list if none found.
+	 * @throws HgRuntimeException subclass thereof to indicate issues with the library. <em>Runtime exception</em>
 	 */
 	public List<HgSubrepoLocation> getSubrepositories() throws HgInvalidControlFileException {
 		if (subRepos == null) {
@@ -346,7 +354,8 @@
 				}
 			};
 		}
-		HgDirstate ds = new HgDirstate(this, new File(repoDir, "dirstate"), pathFactory, canonicalPath);
+		File dirstateFile = new File(repoDir, Dirstate.getName());
+		HgDirstate ds = new HgDirstate(this, dirstateFile, pathFactory, canonicalPath);
 		ds.read(impl.buildFileNameEncodingHelper());
 		return ds;
 	}
@@ -354,34 +363,35 @@
 	/**
 	 * Access to configured set of ignored files.
 	 * @see HgIgnore#isIgnored(Path)
+	 * @throws HgRuntimeException subclass thereof to indicate issues with the library. <em>Runtime exception</em>
 	 */
-	public HgIgnore getIgnore() /*throws HgInvalidControlFileException */{
+	public HgIgnore getIgnore() throws HgInvalidControlFileException {
 		// TODO read config for additional locations
 		if (ignore == null) {
 			ignore = new HgIgnore(getToRepoPathHelper());
-			File ignoreFile = new File(getWorkingDir(), HgRepositoryFiles.HgIgnore.getPath());
+			File ignoreFile = new File(getWorkingDir(), HgIgnore.getPath());
 			try {
 				final List<String> errors = ignore.read(ignoreFile);
 				if (errors != null) {
 					getContext().getLog().dump(getClass(), Warn, "Syntax errors parsing %s:\n%s", ignoreFile.getName(), Internals.join(errors, ",\n"));
 				}
 			} catch (IOException ex) {
-				final String m = "Error reading .hgignore file";
-				getContext().getLog().dump(getClass(), Warn, ex, m);
-//				throw new HgInvalidControlFileException(m, ex, ignoreFile);
+				final String m = String.format("Error reading %s file", ignoreFile);
+				throw new HgInvalidControlFileException(m, ex, ignoreFile);
 			}
 		}
 		return ignore;
 	}
-	
+
 	/**
 	 * Mercurial saves message user has supplied for a commit to facilitate message re-use in case commit fails.
 	 * This method provides this saved message.
 	 *  
 	 * @return message used for last commit attempt, or <code>null</code> if none
+	 * @throws HgRuntimeException subclass thereof to indicate issues with the library. <em>Runtime exception</em>
 	 */
-	public String getCommitLastMessage() {
-		File lastMessage = new File(getRepositoryRoot(), HgRepositoryFiles.LastMessage.getPath());
+	public String getCommitLastMessage() throws HgInvalidControlFileException {
+		File lastMessage = new File(repoDir, LastMessage.getPath());
 		if (!lastMessage.canRead()) {
 			return null;
 		}
--- a/src/org/tmatesoft/hg/repo/HgRepositoryFiles.java	Tue Aug 07 14:02:28 2012 +0200
+++ b/src/org/tmatesoft/hg/repo/HgRepositoryFiles.java	Tue Aug 07 14:27:13 2012 +0200
@@ -26,17 +26,57 @@
 public enum HgRepositoryFiles {
 
 	HgIgnore(".hgignore"), HgTags(".hgtags"), HgEol(".hgeol"), 
-	Dirstate(".hg/dirstate"), HgLocalTags(".hg/localtags"),
+	Dirstate(false, "dirstate"), HgLocalTags(false, "localtags"),
 	HgSub(".hgsub"), HgSubstate(".hgsubstate"),
-	LastMessage("last-message.txt");
+	LastMessage(false, "last-message.txt");
 
-	private String fname;
+	private final String fname;
+	private final boolean livesInWC; 
 	
 	private HgRepositoryFiles(String filename) {
-		fname = filename;
+		this(true, filename);
 	}
 
+	private HgRepositoryFiles(boolean wcNotRepoRoot, String filename) {
+		fname = filename;
+		livesInWC = wcNotRepoRoot;
+	}
+
+	/**
+	 * Path to the file, relative to the parent it lives in.
+	 * 
+	 * 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.
+	 * @return file location, never <code>null</code>
+	 */
 	public String getPath() {
+		return livesInWC ? getName() : ".hg/" + getName();
+	}
+
+	/**
+	 * File name without any path information
+	 * @return file name, never <code>null</code>
+	 */
+	public String getName() {
 		return fname;
 	}
+
+	/**
+	 * Files that reside under working directory may be accessed like:
+	 * <pre>
+	 *   HgRepository hgRepo = ...;
+	 *   File f = new File(hgRepo.getWorkingDir(), HgRepositoryFiles.HgIgnore.getPath())
+	 * </pre>
+	 * @return <code>true</code> if file lives in working tree
+	 */
+	public boolean residesUnderWorkingDir() {
+		return livesInWC;
+	}
+
+	/**
+	 * @return <code>true</code> if file lives under '.hg/' 
+	 */
+	public boolean residesUnderRepositoryRoot() {
+		return !livesInWC;
+	}
 }