revset: factor out reverse flag of sort() key
authorYuya Nishihara <yuya@tcha.org>
Sat, 14 May 2016 19:46:18 +0900
changeset 29264 22625884b15c
parent 29263 817ee3cfe862
child 29265 3f9e68864ccc
revset: factor out reverse flag of sort() key Prepares for making a table of sort keys. This assumes 'k' has at least one character, which should be guaranteed by keys.split().
mercurial/revset.py
tests/test-revset.t
--- a/mercurial/revset.py	Sat May 28 12:29:59 2016 -0700
+++ b/mercurial/revset.py	Sat May 14 19:46:18 2016 +0900
@@ -1868,28 +1868,22 @@
     # sort() is guaranteed to be stable
     ctxs = [repo[r] for r in revs]
     for k in reversed(keys):
+        fk = k
+        reverse = (k[0] == '-')
+        if reverse:
+            k = k[1:]
         if k == 'rev':
-            ctxs.sort(key=lambda c: c.rev())
-        elif k == '-rev':
-            ctxs.sort(key=lambda c: c.rev(), reverse=True)
+            ctxs.sort(key=lambda c: c.rev(), reverse=reverse)
         elif k == 'branch':
-            ctxs.sort(key=lambda c: c.branch())
-        elif k == '-branch':
-            ctxs.sort(key=lambda c: c.branch(), reverse=True)
+            ctxs.sort(key=lambda c: c.branch(), reverse=reverse)
         elif k == 'desc':
-            ctxs.sort(key=lambda c: c.description())
-        elif k == '-desc':
-            ctxs.sort(key=lambda c: c.description(), reverse=True)
+            ctxs.sort(key=lambda c: c.description(), reverse=reverse)
         elif k in 'user author':
-            ctxs.sort(key=lambda c: c.user())
-        elif k in '-user -author':
-            ctxs.sort(key=lambda c: c.user(), reverse=True)
+            ctxs.sort(key=lambda c: c.user(), reverse=reverse)
         elif k == 'date':
-            ctxs.sort(key=lambda c: c.date()[0])
-        elif k == '-date':
-            ctxs.sort(key=lambda c: c.date()[0], reverse=True)
+            ctxs.sort(key=lambda c: c.date()[0], reverse=reverse)
         else:
-            raise error.ParseError(_("unknown sort key %r") % k)
+            raise error.ParseError(_("unknown sort key %r") % fk)
     return baseset([c.rev() for c in ctxs])
 
 @predicate('subrepo([pattern])')
--- a/tests/test-revset.t	Sat May 28 12:29:59 2016 -0700
+++ b/tests/test-revset.t	Sat May 14 19:46:18 2016 +0900
@@ -962,6 +962,12 @@
   6
   2
 
+test invalid sort keys
+
+  $ log 'sort(all(), -invalid)'
+  hg: parse error: unknown sort key '-invalid'
+  [255]
+
   $ cd ..
 
 test sorting by multiple keys including variable-length strings