comparison src/org/tmatesoft/hg/repo/HgDirstate.java @ 490:b3c16d1aede0

Refactoring: move HgRepository's implementation aspects to Internals (which is now its imlementation counterpart and primary repository class to be used by other parts of the library)
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 16 Aug 2012 17:08:34 +0200
parents 909306e412e2
children d2f6ab541330
comparison
equal deleted inserted replaced
489:9c0138cda59a 490:b3c16d1aede0
15 * contact TMate Software at support@hg4j.com 15 * contact TMate Software at support@hg4j.com
16 */ 16 */
17 package org.tmatesoft.hg.repo; 17 package org.tmatesoft.hg.repo;
18 18
19 import static org.tmatesoft.hg.core.Nodeid.NULL; 19 import static org.tmatesoft.hg.core.Nodeid.NULL;
20 import static org.tmatesoft.hg.repo.HgRepositoryFiles.Dirstate;
20 import static org.tmatesoft.hg.util.LogFacility.Severity.Debug; 21 import static org.tmatesoft.hg.util.LogFacility.Severity.Debug;
21 22
22 import java.io.BufferedReader; 23 import java.io.BufferedReader;
23 import java.io.File; 24 import java.io.File;
24 import java.io.FileNotFoundException; 25 import java.io.FileNotFoundException;
31 import java.util.TreeSet; 32 import java.util.TreeSet;
32 33
33 import org.tmatesoft.hg.core.Nodeid; 34 import org.tmatesoft.hg.core.Nodeid;
34 import org.tmatesoft.hg.internal.DataAccess; 35 import org.tmatesoft.hg.internal.DataAccess;
35 import org.tmatesoft.hg.internal.EncodingHelper; 36 import org.tmatesoft.hg.internal.EncodingHelper;
37 import org.tmatesoft.hg.internal.Internals;
36 import org.tmatesoft.hg.util.Pair; 38 import org.tmatesoft.hg.util.Pair;
37 import org.tmatesoft.hg.util.Path; 39 import org.tmatesoft.hg.util.Path;
38 import org.tmatesoft.hg.util.PathRewrite; 40 import org.tmatesoft.hg.util.PathRewrite;
39 import org.tmatesoft.hg.util.LogFacility.Severity; 41 import org.tmatesoft.hg.util.LogFacility.Severity;
40 42
50 52
51 public enum EntryKind { 53 public enum EntryKind {
52 Normal, Added, Removed, Merged, // order is being used in code of this class, don't change unless any use is checked 54 Normal, Added, Removed, Merged, // order is being used in code of this class, don't change unless any use is checked
53 } 55 }
54 56
55 private final HgRepository repo; 57 private final Internals repo;
56 private final File dirstateFile;
57 private final Path.Source pathPool; 58 private final Path.Source pathPool;
58 private final PathRewrite canonicalPathRewrite; 59 private final PathRewrite canonicalPathRewrite;
59 private Map<Path, Record> normal; 60 private Map<Path, Record> normal;
60 private Map<Path, Record> added; 61 private Map<Path, Record> added;
61 private Map<Path, Record> removed; 62 private Map<Path, Record> removed;
65 */ 66 */
66 private Map<Path, Path> canonical2dirstateName; 67 private Map<Path, Path> canonical2dirstateName;
67 private Pair<Nodeid, Nodeid> parents; 68 private Pair<Nodeid, Nodeid> parents;
68 69
69 // canonicalPath may be null if we don't need to check for names other than in dirstate 70 // canonicalPath may be null if we don't need to check for names other than in dirstate
70 /*package-local*/ HgDirstate(HgRepository hgRepo, File dirstate, Path.Source pathSource, PathRewrite canonicalPath) { 71 /*package-local*/ HgDirstate(Internals hgRepo, Path.Source pathSource, PathRewrite canonicalPath) {
71 repo = hgRepo; 72 repo = hgRepo;
72 dirstateFile = dirstate; // XXX decide whether file names shall be kept local to reader (see #branches()) or passed from outside
73 pathPool = pathSource; 73 pathPool = pathSource;
74 canonicalPathRewrite = canonicalPath; 74 canonicalPathRewrite = canonicalPath;
75 } 75 }
76 76
77 /*package-local*/ void read(EncodingHelper encodingHelper) throws HgInvalidControlFileException { 77 /*package-local*/ void read() throws HgInvalidControlFileException {
78 EncodingHelper encodingHelper = repo.buildFileNameEncodingHelper();
78 normal = added = removed = merged = Collections.<Path, Record>emptyMap(); 79 normal = added = removed = merged = Collections.<Path, Record>emptyMap();
79 parents = new Pair<Nodeid,Nodeid>(Nodeid.NULL, Nodeid.NULL); 80 parents = new Pair<Nodeid,Nodeid>(Nodeid.NULL, Nodeid.NULL);
80 if (canonicalPathRewrite != null) { 81 if (canonicalPathRewrite != null) {
81 canonical2dirstateName = new HashMap<Path,Path>(); 82 canonical2dirstateName = new HashMap<Path,Path>();
82 } else { 83 } else {
83 canonical2dirstateName = Collections.emptyMap(); 84 canonical2dirstateName = Collections.emptyMap();
84 } 85 }
86 File dirstateFile = getDirstateFile(repo);
85 if (dirstateFile == null || !dirstateFile.exists()) { 87 if (dirstateFile == null || !dirstateFile.exists()) {
86 return; 88 return;
87 } 89 }
88 DataAccess da = repo.getDataAccess().create(dirstateFile); 90 DataAccess da = repo.getDataAccess().create(dirstateFile);
89 try { 91 try {
168 public Pair<Nodeid,Nodeid> parents() { 170 public Pair<Nodeid,Nodeid> parents() {
169 assert parents != null; // instance not initialized with #read() 171 assert parents != null; // instance not initialized with #read()
170 return parents; 172 return parents;
171 } 173 }
172 174
175 private static File getDirstateFile(Internals repo) {
176 return repo.getFileFromRepoDir(Dirstate.getName());
177 }
178
173 /** 179 /**
174 * @return pair of parents, both {@link Nodeid#NULL} if dirstate is not available 180 * @return pair of parents, both {@link Nodeid#NULL} if dirstate is not available
175 */ 181 */
176 /*package-local*/ static Pair<Nodeid, Nodeid> readParents(HgRepository repo, File dirstateFile) throws HgInvalidControlFileException { 182 /*package-local*/ static Pair<Nodeid, Nodeid> readParents(Internals internalRepo) throws HgInvalidControlFileException {
177 // do not read whole dirstate if all we need is WC parent information 183 // do not read whole dirstate if all we need is WC parent information
184 File dirstateFile = getDirstateFile(internalRepo);
178 if (dirstateFile == null || !dirstateFile.exists()) { 185 if (dirstateFile == null || !dirstateFile.exists()) {
179 return new Pair<Nodeid,Nodeid>(NULL, NULL); 186 return new Pair<Nodeid,Nodeid>(NULL, NULL);
180 } 187 }
181 DataAccess da = repo.getDataAccess().create(dirstateFile); 188 DataAccess da = internalRepo.getDataAccess().create(dirstateFile);
182 try { 189 try {
183 if (da.isEmpty()) { 190 if (da.isEmpty()) {
184 return new Pair<Nodeid,Nodeid>(NULL, NULL); 191 return new Pair<Nodeid,Nodeid>(NULL, NULL);
185 } 192 }
186 return internalReadParents(da); 193 return internalReadParents(da);
193 200
194 /** 201 /**
195 * TODO [post-1.0] it's really not a proper place for the method, need WorkingCopyContainer or similar 202 * TODO [post-1.0] it's really not a proper place for the method, need WorkingCopyContainer or similar
196 * @return branch associated with the working directory 203 * @return branch associated with the working directory
197 */ 204 */
198 /*package-local*/ static String readBranch(HgRepository repo, File branchFile) throws HgInvalidControlFileException { 205 /*package-local*/ static String readBranch(Internals internalRepo) throws HgInvalidControlFileException {
206 File branchFile = internalRepo.getFileFromRepoDir("branch");
199 String branch = HgRepository.DEFAULT_BRANCH_NAME; 207 String branch = HgRepository.DEFAULT_BRANCH_NAME;
200 if (branchFile.exists()) { 208 if (branchFile.exists()) {
201 try { 209 try {
202 BufferedReader r = new BufferedReader(new FileReader(branchFile)); 210 BufferedReader r = new BufferedReader(new FileReader(branchFile));
203 String b = r.readLine(); 211 String b = r.readLine();
205 b = b.trim().intern(); 213 b = b.trim().intern();
206 } 214 }
207 branch = b == null || b.length() == 0 ? HgRepository.DEFAULT_BRANCH_NAME : b; 215 branch = b == null || b.length() == 0 ? HgRepository.DEFAULT_BRANCH_NAME : b;
208 r.close(); 216 r.close();
209 } catch (FileNotFoundException ex) { 217 } catch (FileNotFoundException ex) {
210 repo.getContext().getLog().dump(HgDirstate.class, Debug, ex, null); // log verbose debug, exception might be legal here 218 internalRepo.getContext().getLog().dump(HgDirstate.class, Debug, ex, null); // log verbose debug, exception might be legal here
211 // IGNORE 219 // IGNORE
212 } catch (IOException ex) { 220 } catch (IOException ex) {
213 throw new HgInvalidControlFileException("Error reading file with branch information", ex, branchFile); 221 throw new HgInvalidControlFileException("Error reading file with branch information", ex, branchFile);
214 } 222 }
215 } 223 }