Patchwork [5,of,5] localrepo: add ignoremissing parameter to branchtip

login
register
mail settings
Submitter Sean Farley
Date Jan. 8, 2015, 12:15 a.m.
Message ID <9daf4c4a0221192b5a5d.1420676156@laptop.local>
Download mbox | patch
Permalink /patch/7372/
State Accepted
Headers show

Comments

Sean Farley - Jan. 8, 2015, 12:15 a.m.
# HG changeset patch
# User Sean Farley <sean.michael.farley@gmail.com>
# Date 1413521368 25200
#      Thu Oct 16 21:49:28 2014 -0700
# Node ID 9daf4c4a0221192b5a5dd5625cf2c901ebec2c72
# Parent  b4996d3d9ed2de964a9507b66932084f86bbbc76
localrepo: add ignoremissing parameter to branchtip

Previously, in the namespaces api, the only caller of branchtip was singlenode
which happened to raise the same exception that branchtip raised: KeyError.

This is a minor change but will allow upcoming patches to use repo.branchtip to
not raise an exception if a branch doesn't exist. After that, it will be
possible for extensions to use the namespace api in a stable way.
Pierre-Yves David - Jan. 8, 2015, 11:34 p.m.
On 01/07/2015 04:15 PM, Sean Farley wrote:
> # HG changeset patch
> # User Sean Farley <sean.michael.farley@gmail.com>
> # Date 1413521368 25200
> #      Thu Oct 16 21:49:28 2014 -0700
> # Node ID 9daf4c4a0221192b5a5dd5625cf2c901ebec2c72
> # Parent  b4996d3d9ed2de964a9507b66932084f86bbbc76
> localrepo: add ignoremissing parameter to branchtip

There 5 patches are pushed to the clowncopter. Patch 3 got improved 
inflight by Sean to get more robust.

Patch

diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -716,16 +716,25 @@  class localrepository(object):
         '''returns a dictionary {branch: [branchheads]} with branchheads
         ordered by increasing revision number'''
         branchmap.updatecache(self)
         return self._branchcaches[self.filtername]
 
-    def branchtip(self, branch):
-        '''return the tip node for a given branch'''
+    def branchtip(self, branch, ignoremissing=False):
+        '''return the tip node for a given branch
+
+        If ignoremissing is True, then this method will not raise an error.
+        This is helpful for callers that only expect None for a missing branch
+        (e.g. namespace).
+
+        '''
         try:
             return self.branchmap().branchtip(branch)
         except KeyError:
-            raise error.RepoLookupError(_("unknown branch '%s'") % branch)
+            if not ignoremissing:
+                raise error.RepoLookupError(_("unknown branch '%s'") % branch)
+            else:
+                pass
 
     def lookup(self, key):
         return self[key].node()
 
     def lookupbranch(self, key, remote=None):
diff --git a/mercurial/namespaces.py b/mercurial/namespaces.py
--- a/mercurial/namespaces.py
+++ b/mercurial/namespaces.py
@@ -39,11 +39,11 @@  class namespaces(object):
                lambda repo, name: repo.nodetags(name))
         self.addnamespace(n)
 
         n = ns("branches", "branch",
                lambda repo: repo.branchmap().keys(),
-               lambda repo, name: tolist(repo.branchtip(name)),
+               lambda repo, name: tolist(repo.branchtip(name, True)),
                lambda repo, node: [repo[node].branch()])
         self.addnamespace(n)
 
     def __getitem__(self, namespace):
         """returns the namespace object"""