# HG changeset patch # User Artem Tikhomirov # Date 1313427101 -7200 # Node ID a6d19adc2636e5fdfb2153a83b966d73844648f5 # Parent 8c951645bea0869f95beda6c265c8c2f5538e37c HgRepository.getWorkingCopyBranchName() to retrieve branch associated with working directory diff -r 8c951645bea0 -r a6d19adc2636 cmdline/org/tmatesoft/hg/console/Main.java --- a/cmdline/org/tmatesoft/hg/console/Main.java Fri Aug 12 19:12:04 2011 +0200 +++ b/cmdline/org/tmatesoft/hg/console/Main.java Mon Aug 15 18:51:41 2011 +0200 @@ -75,9 +75,9 @@ // m.testRevisionMap(); // m.testSubrepos(); // m.testReadWorkingCopy(); -// m.testParents(); + m.testParents(); // m.testEffectiveFileLog(); - m.testCatAtCsetRevision(); +// m.testCatAtCsetRevision(); // m.testMergeState(); // m.testFileStatus(); // m.dumpBranches(); @@ -173,6 +173,7 @@ int x = clog.getLocalRevision(wcParents.second()); cmd.range(x, x).execute(dump); } + System.out.println("Branch:" + hgRepo.getWorkingCopyBranchName()); } /* diff -r 8c951645bea0 -r a6d19adc2636 src/org/tmatesoft/hg/repo/HgChangelog.java --- a/src/org/tmatesoft/hg/repo/HgChangelog.java Fri Aug 12 19:12:04 2011 +0200 +++ b/src/org/tmatesoft/hg/repo/HgChangelog.java Mon Aug 15 18:51:41 2011 +0200 @@ -259,8 +259,9 @@ Date _time = new Date(unixTime * 1000); String _extras = space2 < _timeString.length() ? _timeString.substring(space2 + 1) : null; Map _extrasMap; + final String extras_branch_key = "branch"; if (_extras == null) { - _extrasMap = Collections.singletonMap("branch", "default"); + _extrasMap = Collections.singletonMap(extras_branch_key, HgRepository.DEFAULT_BRANCH_NAME); } else { _extrasMap = new HashMap(); for (String pair : _extras.split("\00")) { @@ -268,8 +269,8 @@ // FIXME need to decode key/value, @see changelog.py:decodeextra _extrasMap.put(pair.substring(0, eq), pair.substring(eq + 1)); } - if (!_extrasMap.containsKey("branch")) { - _extrasMap.put("branch", "default"); + if (!_extrasMap.containsKey(extras_branch_key)) { + _extrasMap.put(extras_branch_key, HgRepository.DEFAULT_BRANCH_NAME); } _extrasMap = Collections.unmodifiableMap(_extrasMap); } diff -r 8c951645bea0 -r a6d19adc2636 src/org/tmatesoft/hg/repo/HgDirstate.java --- a/src/org/tmatesoft/hg/repo/HgDirstate.java Fri Aug 12 19:12:04 2011 +0200 +++ b/src/org/tmatesoft/hg/repo/HgDirstate.java Mon Aug 15 18:51:41 2011 +0200 @@ -16,7 +16,9 @@ */ package org.tmatesoft.hg.repo; +import java.io.BufferedReader; import java.io.File; +import java.io.FileReader; import java.io.IOException; import java.util.Collections; import java.util.LinkedHashMap; @@ -26,7 +28,6 @@ import org.tmatesoft.hg.core.HgBadStateException; import org.tmatesoft.hg.core.Nodeid; import org.tmatesoft.hg.internal.DataAccess; -import org.tmatesoft.hg.internal.DataAccessProvider; import org.tmatesoft.hg.util.Path; @@ -37,9 +38,9 @@ * @author Artem Tikhomirov * @author TMate Software Ltd. */ -class HgDirstate { +class HgDirstate /* XXX RepoChangeListener */{ - private final DataAccessProvider accessProvider; + private final HgRepository repo; private final File dirstateFile; // deliberate String, not Path as it seems useless to keep Path here private Map normal; @@ -47,16 +48,11 @@ private Map removed; private Map merged; private Nodeid p1, p2; - - /*package-local*/ HgDirstate() { - // empty instance - accessProvider = null; - dirstateFile = null; - } - - public HgDirstate(DataAccessProvider dap, File dirstate) { - accessProvider = dap; - dirstateFile = dirstate; + private String currentBranch; + + public HgDirstate(HgRepository hgRepo, File dirstate) { + repo = hgRepo; + dirstateFile = dirstate; // XXX decide whether file names shall be kept local to reader (see #branches()) or passed from outside } private void read() { @@ -64,7 +60,7 @@ if (dirstateFile == null || !dirstateFile.exists()) { return; } - DataAccess da = accessProvider.create(dirstateFile); + DataAccess da = repo.getDataAccess().create(dirstateFile); if (da.isEmpty()) { return; } @@ -124,7 +120,7 @@ if (dirstateFile == null || !dirstateFile.exists()) { return; } - DataAccess da = accessProvider.create(dirstateFile); + DataAccess da = repo.getDataAccess().create(dirstateFile); if (da.isEmpty()) { return; } @@ -153,6 +149,31 @@ rv[1] = p2; return rv; } + + /** + * @return branch associated with the working directory + */ + public String branch() { + if (currentBranch == null) { + currentBranch = HgRepository.DEFAULT_BRANCH_NAME; + File branchFile = new File(repo.getRepositoryRoot(), "branch"); + if (branchFile.exists()) { + try { + BufferedReader r = new BufferedReader(new FileReader(branchFile)); + String b = r.readLine(); + if (b != null) { + b = b.trim().intern(); + } + currentBranch = b == null || b.length() == 0 ? HgRepository.DEFAULT_BRANCH_NAME : b; + r.close(); + } catch (IOException ex) { + ex.printStackTrace(); // XXX log verbose debug, exception might be legal here (i.e. FileNotFound) + // IGNORE + } + } + } + return currentBranch; + } // new, modifiable collection /*package-local*/ TreeSet all() { diff -r 8c951645bea0 -r a6d19adc2636 src/org/tmatesoft/hg/repo/HgRepository.java --- a/src/org/tmatesoft/hg/repo/HgRepository.java Fri Aug 12 19:12:04 2011 +0200 +++ b/src/org/tmatesoft/hg/repo/HgRepository.java Mon Aug 15 18:51:41 2011 +0200 @@ -57,6 +57,8 @@ public static final int TIP = -3; public static final int BAD_REVISION = Integer.MIN_VALUE; public static final int WORKING_COPY = -2; + + public static final String DEFAULT_BRANCH_NAME = "default"; // temp aux marker method public static IllegalStateException notImplemented() { @@ -180,7 +182,7 @@ } } } - tags.readGlobal(new File(repoDir.getParentFile(), ".hgtags")); // XXX replace with HgDataFile.workingCopy + tags.readGlobal(new File(getWorkingDir(), ".hgtags")); // XXX replace with HgDataFile.workingCopy tags.readLocal(new File(repoDir, "localtags")); } catch (IOException ex) { ex.printStackTrace(); // FIXME log or othewise report @@ -236,6 +238,13 @@ Nodeid[] p = loadDirstate().parents(); return new Pair(NULL == p[0] ? null : p[0], NULL == p[1] ? null : p[1]); } + + /** + * @return name of the branch associated with working directory, never null. + */ + public String getWorkingCopyBranchName() { + return loadDirstate().branch(); + } /** * @return location where user files (shall) reside @@ -263,7 +272,7 @@ // XXX package-local, unless there are cases when required from outside (guess, working dir/revision walkers may hide dirstate access and no public visibility needed) /*package-local*/ final HgDirstate loadDirstate() { - return new HgDirstate(getDataAccess(), new File(repoDir, "dirstate")); + return new HgDirstate(this, new File(repoDir, "dirstate")); } // package-local, see comment for loadDirstate @@ -272,7 +281,7 @@ if (ignore == null) { ignore = new HgIgnore(); try { - File ignoreFile = new File(repoDir.getParentFile(), ".hgignore"); + File ignoreFile = new File(getWorkingDir(), ".hgignore"); ignore.read(ignoreFile); } catch (IOException ex) { ex.printStackTrace(); // log warn