diff --git a/hgext/fetch.py b/hgext/fetch.py --- a/hgext/fetch.py +++ b/hgext/fetch.py @@ -38,7 +38,10 @@ def fetch(ui, repo, source='default', ** parent, p2 = repo.dirstate.parents() branch = repo.dirstate.branch() - branchnode = repo.branchtags().get(branch) + try: + branchnode = repo.branchtip(branch) + except error.RepoLookupError: + branchnode = None if parent != branchnode: raise util.Abort(_('working dir not at branch tip ' '(use "hg update" to check out branch tip)')) diff --git a/mercurial/bookmarks.py b/mercurial/bookmarks.py --- a/mercurial/bookmarks.py +++ b/mercurial/bookmarks.py @@ -7,7 +7,7 @@ from mercurial.i18n import _ from mercurial.node import hex -from mercurial import encoding, util +from mercurial import encoding, error, util import errno, os def valid(mark): @@ -140,8 +140,8 @@ def unsetcurrent(repo): def updatecurrentbookmark(repo, oldnode, curbranch): try: - return update(repo, oldnode, repo.branchtags()[curbranch]) - except KeyError: + return update(repo, oldnode, repo.branchtip(curbranch)) + except error.RepoLookupError: if curbranch == "default": # no default branch! return update(repo, oldnode, repo.lookup("tip")) else: diff --git a/mercurial/commands.py b/mercurial/commands.py --- a/mercurial/commands.py +++ b/mercurial/commands.py @@ -827,7 +827,7 @@ def bookmark(ui, repo, mark=None, rev=No if mark in marks and not force: raise util.Abort(_("bookmark '%s' already exists " "(use -f to force)") % mark) - if ((mark in repo.branchtags() or mark == repo.dirstate.branch()) + if ((mark in repo.branchmap() or mark == repo.dirstate.branch()) and not force): raise util.Abort( _("a bookmark cannot have the name of an existing branch")) @@ -903,7 +903,7 @@ def branch(ui, repo, label=None, **opts) repo.dirstate.setbranch(label) ui.status(_('reset working directory to branch %s\n') % label) elif label: - if not opts.get('force') and label in repo.branchtags(): + if not opts.get('force') and label in repo.branchmap(): if label not in [p.branch() for p in repo.parents()]: raise util.Abort(_('a branch of the same name already' ' exists'), diff --git a/mercurial/context.py b/mercurial/context.py --- a/mercurial/context.py +++ b/mercurial/context.py @@ -78,10 +78,12 @@ class changectx(object): self._node = repo._tagscache.tags[changeid] self._rev = repo.changelog.rev(self._node) return - if changeid in repo.branchtags(): - self._node = repo.branchtags()[changeid] + try: + self._node = repo.branchtip(changeid) self._rev = repo.changelog.rev(self._node) return + except error.RepoLookupError: + pass self._node = repo.changelog._partialmatch(changeid) if self._node is not None: diff --git a/mercurial/hgweb/webcommands.py b/mercurial/hgweb/webcommands.py --- a/mercurial/hgweb/webcommands.py +++ b/mercurial/hgweb/webcommands.py @@ -794,7 +794,11 @@ def graph(web, req, tmpl): desc = cgi.escape(templatefilters.nonempty(desc)) user = cgi.escape(templatefilters.person(ctx.user())) branch = ctx.branch() - branch = branch, web.repo.branchtags().get(branch) == ctx.node() + try: + branchnode = web.repo.branchtip(branch) + except error.RepoLookupError: + branchnode = None + branch = branch, branchnode == ctx.node() data.append((node, vtx, edges, desc, user, age, branch, ctx.tags(), ctx.bookmarks())) diff --git a/mercurial/hgweb/webutil.py b/mercurial/hgweb/webutil.py --- a/mercurial/hgweb/webutil.py +++ b/mercurial/hgweb/webutil.py @@ -98,16 +98,23 @@ def nodebranchdict(repo, ctx): branches = [] branch = ctx.branch() # If this is an empty repo, ctx.node() == nullid, - # ctx.branch() == 'default', but branchtags() is - # an empty dict. Using dict.get avoids a traceback. - if repo.branchtags().get(branch) == ctx.node(): + # ctx.branch() == 'default'. + try: + branchnode = repo.branchtip(branch) + except error.RepoLookupError: + branchnode = None + if branchnode == ctx.node(): branches.append({"name": branch}) return branches def nodeinbranch(repo, ctx): branches = [] branch = ctx.branch() - if branch != 'default' and repo.branchtags().get(branch) != ctx.node(): + try: + branchnode = repo.branchtip(branch) + except error.RepoLookupError: + branchnode = None + if branch != 'default' and branchnode != ctx.node(): branches.append({"name": branch}) return branches diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py --- a/mercurial/localrepo.py +++ b/mercurial/localrepo.py @@ -507,17 +507,27 @@ class localrepository(repo.repository): self.updatebranchcache() return self._branchcache + def _branchtip(self, heads): + '''return the tipmost branch head in heads''' + tip = heads[-1] + for h in reversed(heads): + if 'close' not in self.changelog.read(h)[5]: + tip = h + break + return tip + + def branchtip(self, branch): + '''return the tip node for a given branch''' + if branch not in self.branchmap(): + raise error.RepoLookupError(_("unknown branch '%s'") % branch) + return self._branchtip(self.branchmap()[branch]) + def branchtags(self): '''return a dict where branch names map to the tipmost head of the branch, open heads come before closed''' bt = {} for bn, heads in self.branchmap().iteritems(): - tip = heads[-1] - for h in reversed(heads): - if 'close' not in self.changelog.read(h)[5]: - tip = h - break - bt[bn] = tip + bt[bn] = self._branchtip(heads) return bt def _readbranchcache(self): diff --git a/mercurial/merge.py b/mercurial/merge.py --- a/mercurial/merge.py +++ b/mercurial/merge.py @@ -7,7 +7,7 @@ from node import nullid, nullrev, hex, bin from i18n import _ -import scmutil, util, filemerge, copies, subrepo +import error, scmutil, util, filemerge, copies, subrepo import errno, os, shutil class mergestate(object): @@ -529,8 +529,8 @@ def update(repo, node, branchmerge, forc if node is None: # tip of current branch try: - node = repo.branchtags()[wc.branch()] - except KeyError: + node = repo.branchtip(wc.branch()) + except error.RepoLookupError: if wc.branch() == "default": # no default branch! node = repo.lookup("tip") # update to tip else: