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

Sean Farley sean.michael.farley at gmail.com
Thu Aug 21 18:20:06 UTC 2014


Pierre-Yves David writes:

> On 08/18/2014 02:18 PM, Sean Farley wrote:
>> # HG changeset patch
>> # User Sean Farley <sean.michael.farley at 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.



More information about the Mercurial-devel mailing list