Mercurial > jhg
comparison src/org/tmatesoft/hg/repo/HgDirstate.java @ 252:a6d19adc2636
HgRepository.getWorkingCopyBranchName() to retrieve branch associated with working directory
| author | Artem Tikhomirov <tikhomirov.artem@gmail.com> | 
|---|---|
| date | Mon, 15 Aug 2011 18:51:41 +0200 | 
| parents | 1792b37650f2 | 
| children | 35125450c804 | 
   comparison
  equal
  deleted
  inserted
  replaced
| 251:8c951645bea0 | 252:a6d19adc2636 | 
|---|---|
| 14 * the terms of a license other than GNU General Public License | 14 * the terms of a license other than GNU General Public License | 
| 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 java.io.BufferedReader; | |
| 19 import java.io.File; | 20 import java.io.File; | 
| 21 import java.io.FileReader; | |
| 20 import java.io.IOException; | 22 import java.io.IOException; | 
| 21 import java.util.Collections; | 23 import java.util.Collections; | 
| 22 import java.util.LinkedHashMap; | 24 import java.util.LinkedHashMap; | 
| 23 import java.util.Map; | 25 import java.util.Map; | 
| 24 import java.util.TreeSet; | 26 import java.util.TreeSet; | 
| 25 | 27 | 
| 26 import org.tmatesoft.hg.core.HgBadStateException; | 28 import org.tmatesoft.hg.core.HgBadStateException; | 
| 27 import org.tmatesoft.hg.core.Nodeid; | 29 import org.tmatesoft.hg.core.Nodeid; | 
| 28 import org.tmatesoft.hg.internal.DataAccess; | 30 import org.tmatesoft.hg.internal.DataAccess; | 
| 29 import org.tmatesoft.hg.internal.DataAccessProvider; | |
| 30 import org.tmatesoft.hg.util.Path; | 31 import org.tmatesoft.hg.util.Path; | 
| 31 | 32 | 
| 32 | 33 | 
| 33 /** | 34 /** | 
| 34 * @see http://mercurial.selenic.com/wiki/DirState | 35 * @see http://mercurial.selenic.com/wiki/DirState | 
| 35 * @see http://mercurial.selenic.com/wiki/FileFormats#dirstate | 36 * @see http://mercurial.selenic.com/wiki/FileFormats#dirstate | 
| 36 * | 37 * | 
| 37 * @author Artem Tikhomirov | 38 * @author Artem Tikhomirov | 
| 38 * @author TMate Software Ltd. | 39 * @author TMate Software Ltd. | 
| 39 */ | 40 */ | 
| 40 class HgDirstate { | 41 class HgDirstate /* XXX RepoChangeListener */{ | 
| 41 | 42 | 
| 42 private final DataAccessProvider accessProvider; | 43 private final HgRepository repo; | 
| 43 private final File dirstateFile; | 44 private final File dirstateFile; | 
| 44 // deliberate String, not Path as it seems useless to keep Path here | 45 // deliberate String, not Path as it seems useless to keep Path here | 
| 45 private Map<String, Record> normal; | 46 private Map<String, Record> normal; | 
| 46 private Map<String, Record> added; | 47 private Map<String, Record> added; | 
| 47 private Map<String, Record> removed; | 48 private Map<String, Record> removed; | 
| 48 private Map<String, Record> merged; | 49 private Map<String, Record> merged; | 
| 49 private Nodeid p1, p2; | 50 private Nodeid p1, p2; | 
| 50 | 51 private String currentBranch; | 
| 51 /*package-local*/ HgDirstate() { | 52 | 
| 52 // empty instance | 53 public HgDirstate(HgRepository hgRepo, File dirstate) { | 
| 53 accessProvider = null; | 54 repo = hgRepo; | 
| 54 dirstateFile = null; | 55 dirstateFile = dirstate; // XXX decide whether file names shall be kept local to reader (see #branches()) or passed from outside | 
| 55 } | |
| 56 | |
| 57 public HgDirstate(DataAccessProvider dap, File dirstate) { | |
| 58 accessProvider = dap; | |
| 59 dirstateFile = dirstate; | |
| 60 } | 56 } | 
| 61 | 57 | 
| 62 private void read() { | 58 private void read() { | 
| 63 normal = added = removed = merged = Collections.<String, Record>emptyMap(); | 59 normal = added = removed = merged = Collections.<String, Record>emptyMap(); | 
| 64 if (dirstateFile == null || !dirstateFile.exists()) { | 60 if (dirstateFile == null || !dirstateFile.exists()) { | 
| 65 return; | 61 return; | 
| 66 } | 62 } | 
| 67 DataAccess da = accessProvider.create(dirstateFile); | 63 DataAccess da = repo.getDataAccess().create(dirstateFile); | 
| 68 if (da.isEmpty()) { | 64 if (da.isEmpty()) { | 
| 69 return; | 65 return; | 
| 70 } | 66 } | 
| 71 // not sure linked is really needed here, just for ease of debug | 67 // not sure linked is really needed here, just for ease of debug | 
| 72 normal = new LinkedHashMap<String, Record>(); | 68 normal = new LinkedHashMap<String, Record>(); | 
| 122 // do not read whole dirstate if all we need is WC parent information | 118 // do not read whole dirstate if all we need is WC parent information | 
| 123 private void readParents() { | 119 private void readParents() { | 
| 124 if (dirstateFile == null || !dirstateFile.exists()) { | 120 if (dirstateFile == null || !dirstateFile.exists()) { | 
| 125 return; | 121 return; | 
| 126 } | 122 } | 
| 127 DataAccess da = accessProvider.create(dirstateFile); | 123 DataAccess da = repo.getDataAccess().create(dirstateFile); | 
| 128 if (da.isEmpty()) { | 124 if (da.isEmpty()) { | 
| 129 return; | 125 return; | 
| 130 } | 126 } | 
| 131 try { | 127 try { | 
| 132 byte[] parents = new byte[40]; | 128 byte[] parents = new byte[40]; | 
| 151 Nodeid[] rv = new Nodeid[2]; | 147 Nodeid[] rv = new Nodeid[2]; | 
| 152 rv[0] = p1; | 148 rv[0] = p1; | 
| 153 rv[1] = p2; | 149 rv[1] = p2; | 
| 154 return rv; | 150 return rv; | 
| 155 } | 151 } | 
| 152 | |
| 153 /** | |
| 154 * @return branch associated with the working directory | |
| 155 */ | |
| 156 public String branch() { | |
| 157 if (currentBranch == null) { | |
| 158 currentBranch = HgRepository.DEFAULT_BRANCH_NAME; | |
| 159 File branchFile = new File(repo.getRepositoryRoot(), "branch"); | |
| 160 if (branchFile.exists()) { | |
| 161 try { | |
| 162 BufferedReader r = new BufferedReader(new FileReader(branchFile)); | |
| 163 String b = r.readLine(); | |
| 164 if (b != null) { | |
| 165 b = b.trim().intern(); | |
| 166 } | |
| 167 currentBranch = b == null || b.length() == 0 ? HgRepository.DEFAULT_BRANCH_NAME : b; | |
| 168 r.close(); | |
| 169 } catch (IOException ex) { | |
| 170 ex.printStackTrace(); // XXX log verbose debug, exception might be legal here (i.e. FileNotFound) | |
| 171 // IGNORE | |
| 172 } | |
| 173 } | |
| 174 } | |
| 175 return currentBranch; | |
| 176 } | |
| 156 | 177 | 
| 157 // new, modifiable collection | 178 // new, modifiable collection | 
| 158 /*package-local*/ TreeSet<String> all() { | 179 /*package-local*/ TreeSet<String> all() { | 
| 159 read(); | 180 read(); | 
| 160 TreeSet<String> rv = new TreeSet<String>(); | 181 TreeSet<String> rv = new TreeSet<String>(); | 
