changeset 6320:0c780d972350

topic: use check_incompatible_arguments() for --list with --clear or --rev
author Anton Shestakov <av6@dwimlabs.net>
date Thu, 07 Apr 2022 20:42:56 +0300
parents 8c664ed9c103
children 58b856416d2e
files hgext3rd/topic/__init__.py hgext3rd/topic/compat.py tests/test-topic.t
diffstat 3 files changed, 39 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/hgext3rd/topic/__init__.py	Thu Apr 07 20:28:11 2022 +0300
+++ b/hgext3rd/topic/__init__.py	Thu Apr 07 20:42:56 2022 +0300
@@ -895,6 +895,8 @@
     if age and topic:
         raise compat.InputError(_(b"cannot use --age while setting a topic"))
 
+    compat.check_incompatible_arguments(opts, 'list', ('clear', 'rev'))
+
     touchedrevs = set()
     if rev:
         touchedrevs = scmutil.revrange(repo, rev)
@@ -920,8 +922,6 @@
 
     if list:
         ui.pager(b'topics')
-        if clear or rev:
-            raise error.Abort(_(b"cannot use --clear or --rev with --list"))
         if not topic:
             topic = repo.currenttopic
         if not topic:
--- a/hgext3rd/topic/compat.py	Thu Apr 07 20:28:11 2022 +0300
+++ b/hgext3rd/topic/compat.py	Thu Apr 07 20:42:56 2022 +0300
@@ -7,6 +7,7 @@
 """
 from __future__ import absolute_import
 
+from mercurial.i18n import _
 from mercurial import (
     cmdutil,
     error,
@@ -56,3 +57,36 @@
 else:
     # hg <= 5.6 (8d72e29ad1e0)
     InputError = error.Abort
+
+if util.safehasattr(cmdutil, 'check_at_most_one_arg'):
+    def check_at_most_one_arg(opts, *args):
+        return cmdutil.check_at_most_one_arg(opts, *args)
+else:
+    # hg <= 5.2 (d587937600be)
+    def check_at_most_one_arg(opts, *args):
+        def to_display(name):
+            return pycompat.sysbytes(name).replace(b'_', b'-')
+
+        previous = None
+        for x in args:
+            if opts.get(x):
+                if previous:
+                    raise InputError(_(b'cannot specify both --%s and --%s')
+                                     % (to_display(previous), to_display(x)))
+                previous = x
+        return previous
+
+if util.safehasattr(cmdutil, 'check_incompatible_arguments'):
+    code = cmdutil.check_incompatible_arguments.__code__
+    if r'others' in code.co_varnames[:code.co_argcount]:
+        def check_incompatible_arguments(opts, first, others):
+            return cmdutil.check_incompatible_arguments(opts, first, others)
+    else:
+        # hg <= 5.3 (d4c1501225c4)
+        def check_incompatible_arguments(opts, first, others):
+            return cmdutil.check_incompatible_arguments(opts, first, *others)
+else:
+    # hg <= 5.2 (023ad45e2fd2)
+    def check_incompatible_arguments(opts, first, others):
+        for other in others:
+            check_at_most_one_arg(opts, first, other)
--- a/tests/test-topic.t	Thu Apr 07 20:28:11 2022 +0300
+++ b/tests/test-topic.t	Thu Apr 07 20:42:56 2022 +0300
@@ -261,6 +261,9 @@
   $ hg topics --clear somerandomtopic
   abort: cannot use --clear when setting a topic
   [10]
+  $ hg topics --list --clear
+  abort: cannot specify both --list and --clear
+  [10]
 
 Trying some invalid topicnames