Patchwork [1,of,2] revset: factor out reverse flag of sort() key

login
register
mail settings
Submitter Yuya Nishihara
Date May 25, 2016, 2:37 p.m.
Message ID <63b2515cfc673094cc08.1464187074@mimosa>
Download mbox | patch
Permalink /patch/15195/
State Accepted
Headers show

Comments

Yuya Nishihara - May 25, 2016, 2:37 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1463222778 -32400
#      Sat May 14 19:46:18 2016 +0900
# Node ID 63b2515cfc673094cc085ef929aeb34ace042e74
# Parent  1b7d907ec18a7e2ff244a30e8a63ac46cd121588
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().

Patch

diff --git a/mercurial/revset.py b/mercurial/revset.py
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -1866,28 +1866,22 @@  def sort(repo, subset, x):
     # 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])')
diff --git a/tests/test-revset.t b/tests/test-revset.t
--- a/tests/test-revset.t
+++ b/tests/test-revset.t
@@ -962,6 +962,12 @@  test sorting two sorted collections in d
   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