changeset 29265:3f9e68864ccc

revset: define table of sort() key functions This should be more readable than big "if" branch.
author Yuya Nishihara <yuya@tcha.org>
date Sat, 14 May 2016 19:52:00 +0900
parents 22625884b15c
children b3a677c82a35
files mercurial/revset.py
diffstat 1 files changed, 12 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/revset.py	Sat May 14 19:46:18 2016 +0900
+++ b/mercurial/revset.py	Sat May 14 19:52:00 2016 +0900
@@ -1834,6 +1834,15 @@
         return True
     return subset & s.filter(filter, condrepr='<roots>')
 
+_sortkeyfuncs = {
+    'rev': lambda c: c.rev(),
+    'branch': lambda c: c.branch(),
+    'desc': lambda c: c.description(),
+    'user': lambda c: c.user(),
+    'author': lambda c: c.user(),
+    'date': lambda c: c.date()[0],
+}
+
 @predicate('sort(set[, [-]key...])', safe=True)
 def sort(repo, subset, x):
     """Sort set by keys. The default sort order is ascending, specify a key
@@ -1872,17 +1881,9 @@
         reverse = (k[0] == '-')
         if reverse:
             k = k[1:]
-        if k == 'rev':
-            ctxs.sort(key=lambda c: c.rev(), reverse=reverse)
-        elif k == 'branch':
-            ctxs.sort(key=lambda c: c.branch(), reverse=reverse)
-        elif k == 'desc':
-            ctxs.sort(key=lambda c: c.description(), reverse=reverse)
-        elif k in 'user author':
-            ctxs.sort(key=lambda c: c.user(), reverse=reverse)
-        elif k == 'date':
-            ctxs.sort(key=lambda c: c.date()[0], reverse=reverse)
-        else:
+        try:
+            ctxs.sort(key=_sortkeyfuncs[k], reverse=reverse)
+        except KeyError:
             raise error.ParseError(_("unknown sort key %r") % fk)
     return baseset([c.rev() for c in ctxs])