Patchwork [06,of,12] localrepo: add a method to return labels associated with a node

login
register
mail settings
Submitter Sean Farley
Date Aug. 18, 2014, 9:18 p.m.
Message ID <8e603b1775c5645f0f77.1408396682@1.0.0.127.in-addr.arpa>
Download mbox | patch
Permalink /patch/5464/
State Changes Requested
Headers show

Comments

Sean Farley - Aug. 18, 2014, 9:18 p.m.
# HG changeset patch
# User Sean Farley <sean.michael.farley@gmail.com>
# Date 1396309600 18000
#      Mon Mar 31 18:46:40 2014 -0500
# Node ID 8e603b1775c5645f0f77abe6e0671c9ecc5eb0c6
# Parent  c3f7c84a9f217365800b5d942c875ee54f33c7ae
localrepo: add a method to return labels associated with a node
Pierre-Yves David - Aug. 21, 2014, 7:54 a.m.
On 08/18/2014 02:18 PM, Sean Farley wrote:
> # HG changeset patch
> # User Sean Farley <sean.michael.farley@gmail.com>
> # Date 1396309600 18000
> #      Mon Mar 31 18:46:40 2014 -0500
> # Node ID 8e603b1775c5645f0f77abe6e0671c9ecc5eb0c6
> # Parent  c3f7c84a9f217365800b5d942c875ee54f33c7ae
> localrepo: add a method to return labels associated with a node
>
> diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
> --- a/mercurial/localrepo.py
> +++ b/mercurial/localrepo.py
> @@ -743,10 +743,29 @@ class localrepository(object):
>           l = self._labels.copy()
>           l["tag"] = self.tags()
>           l["bookmark"] = self._bookmarks
>           return l
>
> +    def nodelabels(self, node):
> +        '''Return all labels (sorted) associated with a node.
> +
> +        This is a tuple of (namespace, label) instead of a dictionary
> +        since the results are sorted.
> +        '''
> +        nm = []

I've no idea what your mean by "nm" consider a more verbose name.


> +
> +        # namespace name is plural
> +        nm.extend((b, 'bookmarks') for b in self.nodebookmarks(node))
> +        nm.extend((t, 'tags') for t in self.nodetags(node))
> +
> +        for labeltype, data in self._labels.iteritems():
> +            nm.extend((name, labeltype) for name, n in data.iteritems()
> +            if n == node)

"oneline on multiple line" do a simple for loop instead (and I not super 
fan of this iteration over the whole namespace.

> +        # sort by type of label, then by name
> +        nm = sorted(nm, key=lambda tup: (tup[1], tup[0]))

You could also use:

   lambda tup: tup[::-1]

but I cannot decide what it the worse


> +        return nm
>
>       def branchmap(self):
>           '''returns a dictionary {branch: [branchheads]} with branchheads
>           ordered by increasing revision number'''
>           branchmap.updatecache(self)
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@selenic.com
> http://selenic.com/mailman/listinfo/mercurial-devel
>
Sean Farley - Aug. 21, 2014, 6:20 p.m.
Pierre-Yves David writes:

> On 08/18/2014 02:18 PM, Sean Farley wrote:
>> # HG changeset patch
>> # User Sean Farley <sean.michael.farley@gmail.com>
>> # Date 1396309600 18000
>> #      Mon Mar 31 18:46:40 2014 -0500
>> # Node ID 8e603b1775c5645f0f77abe6e0671c9ecc5eb0c6
>> # Parent  c3f7c84a9f217365800b5d942c875ee54f33c7ae
>> localrepo: add a method to return labels associated with a node
>>
>> diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
>> --- a/mercurial/localrepo.py
>> +++ b/mercurial/localrepo.py
>> @@ -743,10 +743,29 @@ class localrepository(object):
>>           l = self._labels.copy()
>>           l["tag"] = self.tags()
>>           l["bookmark"] = self._bookmarks
>>           return l
>>
>> +    def nodelabels(self, node):
>> +        '''Return all labels (sorted) associated with a node.
>> +
>> +        This is a tuple of (namespace, label) instead of a dictionary
>> +        since the results are sorted.
>> +        '''
>> +        nm = []
>
> I've no idea what your mean by "nm" consider a more verbose name.

Fair enough.

>> +
>> +        # namespace name is plural
>> +        nm.extend((b, 'bookmarks') for b in self.nodebookmarks(node))
>> +        nm.extend((t, 'tags') for t in self.nodetags(node))
>> +
>> +        for labeltype, data in self._labels.iteritems():
>> +            nm.extend((name, labeltype) for name, n in data.iteritems()
>> +            if n == node)
>
> "oneline on multiple line" do a simple for loop instead (and I not super 
> fan of this iteration over the whole namespace.
>
>> +        # sort by type of label, then by name
>> +        nm = sorted(nm, key=lambda tup: (tup[1], tup[0]))
>
> You could also use:
>
>    lambda tup: tup[::-1]
>
> but I cannot decide what it the worse

Now that we have util.sortdict, I guess I could use that instead of
doing this sorting here. Furthermore, I could extend sortdict to include
some implementation of defaultdict.

Patch

diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -743,10 +743,29 @@  class localrepository(object):
         l = self._labels.copy()
         l["tag"] = self.tags()
         l["bookmark"] = self._bookmarks
         return l
 
+    def nodelabels(self, node):
+        '''Return all labels (sorted) associated with a node.
+
+        This is a tuple of (namespace, label) instead of a dictionary
+        since the results are sorted.
+        '''
+        nm = []
+
+        # namespace name is plural
+        nm.extend((b, 'bookmarks') for b in self.nodebookmarks(node))
+        nm.extend((t, 'tags') for t in self.nodetags(node))
+
+        for labeltype, data in self._labels.iteritems():
+            nm.extend((name, labeltype) for name, n in data.iteritems()
+            if n == node)
+
+        # sort by type of label, then by name
+        nm = sorted(nm, key=lambda tup: (tup[1], tup[0]))
+        return nm
 
     def branchmap(self):
         '''returns a dictionary {branch: [branchheads]} with branchheads
         ordered by increasing revision number'''
         branchmap.updatecache(self)