Patchwork [RFC] cmdtest: add a new command that checks revset matching

login
register
mail settings
Submitter Raphaël Gomès
Date Feb. 13, 2019, 3 p.m.
Message ID <f850e5c85eeee47df95c.1550070034@alphare-carbon.lan>
Download mbox | patch
Permalink /patch/38756/
State New
Headers show

Comments

Raphaël Gomès - Feb. 13, 2019, 3 p.m.
# HG changeset patch
# User Raphaël Gomès <rgomes@octobus.net>
# Date 1550068454 -3600
#      Wed Feb 13 15:34:14 2019 +0100
# Node ID f850e5c85eeee47df95cd4bcb38b4ade5b5af955
# Parent  91701785c2c56a3ee395038488758ad2e1865265
# EXP-Topic cmd-test
cmdtest: add a new command that checks revset matching

This command is meant to ease scripting that need to check revsets validity
on a repository. Right now, doing such revset testing requires abuse of `hg log`
output parsing, and is less than ideal.

It mirrors the `test` shell builtin which is used in almost every shell script
for branching.

This is a first draft of this command, there is of room for adjustement and evolution,
like fileset matching or other arguments.
Pulkit Goyal - Feb. 19, 2019, 12:52 p.m.
On Fri, Feb 15, 2019 at 6:11 PM Raphaël Gomès <raphael.gomes@octobus.net>
wrote:

> # HG changeset patch
> # User Raphaël Gomès <rgomes@octobus.net>
> # Date 1550068454 -3600
> #      Wed Feb 13 15:34:14 2019 +0100
> # Node ID f850e5c85eeee47df95cd4bcb38b4ade5b5af955
> # Parent  91701785c2c56a3ee395038488758ad2e1865265
> # EXP-Topic cmd-test
> cmdtest: add a new command that checks revset matching
>
> This command is meant to ease scripting that need to check revsets validity
> on a repository. Right now, doing such revset testing requires abuse of
> `hg log`
> output parsing, and is less than ideal.
>
> It mirrors the `test` shell builtin which is used in almost every shell
> script
> for branching.
>
> This is a first draft of this command, there is of room for adjustement
> and evolution,
> like fileset matching or other arguments.
>

You can use the `expectsize()` revset which is recently implemented and
pass the size as 0. So if a revset is empty, the operation will succeed and
you will get 0 as return value. If there are revs in the revset, it will
abort.

https://phab.mercurial-scm.org/rHG8185c8abce87b305920bca9ec26ccbfd11279436

>
> diff -r 91701785c2c5 -r f850e5c85eee mercurial/commands.py
> --- a/mercurial/commands.py     Mon Feb 11 11:18:37 2019 -0500
> +++ b/mercurial/commands.py     Wed Feb 13 15:34:14 2019 +0100
> @@ -5942,6 +5942,22 @@
>          fm.plain('\n')
>      fm.end()
>
> +@command('test', [], _('REVS'), helpcategory=command.CATEGORY_MISC)
> +def cmdtest(ui, repo, *revs):
> +    """check if revset match anything
> +
> +    Helper command that mirrors the shell builtin of the same name.
> +    If revset match anything, exits with 0, else exits with 1.
> +    """
> +    # XXX if `hg test` is used in scripting, using user aliases may
> +    # XXX be a security issue.
> +    revs = scmutil.revrange(repo, revs)
> +
> +    if revs:
> +        return 0
> +    else:
> +        return 1
> +
>  @command('tip',
>      [('p', 'patch', None, _('show patch')),
>      ('g', 'git', None, _('use git extended diff format')),
> diff -r 91701785c2c5 -r f850e5c85eee tests/test-cmdtest.t
> --- /dev/null   Thu Jan 01 00:00:00 1970 +0000
> +++ b/tests/test-cmdtest.t      Wed Feb 13 15:34:14 2019 +0100
> @@ -0,0 +1,31 @@
> +================================
> +Testing of the `hg test` command
> +================================
> +
> +Initial setup
> +=============
> +
> +  $ hg init cmdtest
> +  $ cd cmdtest
> +  $ hg debugbuilddag .+10
> +
> +
> +Basic expression
> +================
> +
> +Simple successful
> +
> +  $ hg test 0
> +
> +Simple empty revset
> +
> +  $ hg test "(0 and 1)"
> +  [1]
> +
> +Simple invalid revset
> +
> +  $ hg test "(0"
> +  hg: parse error at 2: unexpected token: end
> +  ((0
> +     ^ here)
> +  [255]
> diff -r 91701785c2c5 -r f850e5c85eee tests/test-completion.t
> --- a/tests/test-completion.t   Mon Feb 11 11:18:37 2019 -0500
> +++ b/tests/test-completion.t   Wed Feb 13 15:34:14 2019 +0100
> @@ -49,6 +49,7 @@
>    summary
>    tag
>    tags
> +  test
>    tip
>    unbundle
>    update
> @@ -340,6 +341,7 @@
>    summary: remote
>    tag: force, local, rev, remove, edit, message, date, user
>    tags: template
> +  test:
>    tip: patch, git, style, template
>    unbundle: update
>    update: clean, check, merge, date, rev, tool
> diff -r 91701785c2c5 -r f850e5c85eee tests/test-globalopts.t
> --- a/tests/test-globalopts.t   Mon Feb 11 11:18:37 2019 -0500
> +++ b/tests/test-globalopts.t   Wed Feb 13 15:34:14 2019 +0100
> @@ -380,6 +380,10 @@
>     help          show help for a given topic or a help overview
>     version       output version and copyright information
>
> +  Miscellaneous commands:
> +
> +   test          check if revset match anything
> +
>    additional help topics:
>
>    Mercurial identifiers:
> @@ -510,6 +514,10 @@
>     help          show help for a given topic or a help overview
>     version       output version and copyright information
>
> +  Miscellaneous commands:
> +
> +   test          check if revset match anything
> +
>    additional help topics:
>
>    Mercurial identifiers:
> diff -r 91701785c2c5 -r f850e5c85eee tests/test-help-hide.t
> --- a/tests/test-help-hide.t    Mon Feb 11 11:18:37 2019 -0500
> +++ b/tests/test-help-hide.t    Wed Feb 13 15:34:14 2019 +0100
> @@ -84,6 +84,10 @@
>     help          show help for a given topic or a help overview
>     version       output version and copyright information
>
> +  Miscellaneous commands:
> +
> +   test          check if revset match anything
> +
>    additional help topics:
>
>    Mercurial identifiers:
> @@ -218,6 +222,10 @@
>     help          show help for a given topic or a help overview
>     version       output version and copyright information
>
> +  Miscellaneous commands:
> +
> +   test          check if revset match anything
> +
>    additional help topics:
>
>    Mercurial identifiers:
> diff -r 91701785c2c5 -r f850e5c85eee tests/test-help.t
> --- a/tests/test-help.t Mon Feb 11 11:18:37 2019 -0500
> +++ b/tests/test-help.t Wed Feb 13 15:34:14 2019 +0100
> @@ -136,6 +136,10 @@
>     help          show help for a given topic or a help overview
>     version       output version and copyright information
>
> +  Miscellaneous commands:
> +
> +   test          check if revset match anything
> +
>    additional help topics:
>
>    Mercurial identifiers:
> @@ -262,6 +266,10 @@
>     help          show help for a given topic or a help overview
>     version       output version and copyright information
>
> +  Miscellaneous commands:
> +
> +   test          check if revset match anything
> +
>    additional help topics:
>
>    Mercurial identifiers:
> @@ -2698,6 +2706,13 @@
>    list repository tags
>    </td></tr>
>    <tr><td>
> +  <a href="/help/test">
> +  test
> +  </a>
> +  </td><td>
> +  check if revset match anything
> +  </td></tr>
> +  <tr><td>
>    <a href="/help/unbundle">
>    unbundle
>    </a>
> diff -r 91701785c2c5 -r f850e5c85eee tests/test-hgweb-json.t
> --- a/tests/test-hgweb-json.t   Mon Feb 11 11:18:37 2019 -0500
> +++ b/tests/test-hgweb-json.t   Wed Feb 13 15:34:14 2019 +0100
> @@ -2065,6 +2065,10 @@
>          "topic": "tags"
>        },
>        {
> +        "summary": "check if revset match anything",
> +        "topic": "test"
> +      },
> +      {
>          "summary": "apply one or more bundle files",
>          "topic": "unbundle"
>        },
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
>
Boris Feld - Feb. 19, 2019, 6:17 p.m.
On 19/02/2019 13:52, Pulkit Goyal wrote:
>
>
> On Fri, Feb 15, 2019 at 6:11 PM Raphaël Gomès
> <raphael.gomes@octobus.net <mailto:raphael.gomes@octobus.net>> wrote:
>
>     # HG changeset patch
>     # User Raphaël Gomès <rgomes@octobus.net <mailto:rgomes@octobus.net>>
>     # Date 1550068454 -3600
>     #      Wed Feb 13 15:34:14 2019 +0100
>     # Node ID f850e5c85eeee47df95cd4bcb38b4ade5b5af955
>     # Parent  91701785c2c56a3ee395038488758ad2e1865265
>     # EXP-Topic cmd-test
>     cmdtest: add a new command that checks revset matching
>
>     This command is meant to ease scripting that need to check revsets
>     validity
>     on a repository. Right now, doing such revset testing requires
>     abuse of `hg log`
>     output parsing, and is less than ideal.
>
>     It mirrors the `test` shell builtin which is used in almost every
>     shell script
>     for branching.
>
>     This is a first draft of this command, there is of room for
>     adjustement and evolution,
>     like fileset matching or other arguments.
>
>
> You can use the `expectsize()` revset which is recently implemented
> and pass the size as 0. So if a revset is empty, the operation will
> succeed and you will get 0 as return value. If there are revs in the
> revset, it will abort.
The expectsize revset is less flexible, and provide an ambiguous return
code when it does not match (same return as an invalid revset). The
revset seems useful in itself, but having a clear and extensible command
will make scripting simpler, clearer and less error-prone.
Yuya Nishihara - Feb. 20, 2019, 3:38 a.m.
On Tue, 19 Feb 2019 19:17:03 +0100, Boris FELD wrote:
> On 19/02/2019 13:52, Pulkit Goyal wrote:
> >
> >
> > On Fri, Feb 15, 2019 at 6:11 PM Raphaël Gomès
> > <raphael.gomes@octobus.net <mailto:raphael.gomes@octobus.net>> wrote:
> >
> >     # HG changeset patch
> >     # User Raphaël Gomès <rgomes@octobus.net <mailto:rgomes@octobus.net>>
> >     # Date 1550068454 -3600
> >     #      Wed Feb 13 15:34:14 2019 +0100
> >     # Node ID f850e5c85eeee47df95cd4bcb38b4ade5b5af955
> >     # Parent  91701785c2c56a3ee395038488758ad2e1865265
> >     # EXP-Topic cmd-test
> >     cmdtest: add a new command that checks revset matching
> >
> >     This command is meant to ease scripting that need to check revsets
> >     validity
> >     on a repository. Right now, doing such revset testing requires
> >     abuse of `hg log`
> >     output parsing, and is less than ideal.
> >
> >     It mirrors the `test` shell builtin which is used in almost every
> >     shell script
> >     for branching.
> >
> >     This is a first draft of this command, there is of room for
> >     adjustement and evolution,
> >     like fileset matching or other arguments.
> >
> >
> > You can use the `expectsize()` revset which is recently implemented
> > and pass the size as 0. So if a revset is empty, the operation will
> > succeed and you will get 0 as return value. If there are revs in the
> > revset, it will abort.
> The expectsize revset is less flexible, and provide an ambiguous return
> code when it does not match (same return as an invalid revset). The
> revset seems useful in itself, but having a clear and extensible command
> will make scripting simpler, clearer and less error-prone.

Maybe add some flag to debugrevspec command to disambiguate RepoLookupError
from the other errors?

I don't know what kind of other features the script would need, but
inspecting revset/filset syntax would belong to debugrevspec/fileset commands.

Patch

diff -r 91701785c2c5 -r f850e5c85eee mercurial/commands.py
--- a/mercurial/commands.py	Mon Feb 11 11:18:37 2019 -0500
+++ b/mercurial/commands.py	Wed Feb 13 15:34:14 2019 +0100
@@ -5942,6 +5942,22 @@ 
         fm.plain('\n')
     fm.end()
 
+@command('test', [], _('REVS'), helpcategory=command.CATEGORY_MISC)
+def cmdtest(ui, repo, *revs):
+    """check if revset match anything
+
+    Helper command that mirrors the shell builtin of the same name.
+    If revset match anything, exits with 0, else exits with 1.
+    """
+    # XXX if `hg test` is used in scripting, using user aliases may
+    # XXX be a security issue.
+    revs = scmutil.revrange(repo, revs)
+
+    if revs:
+        return 0
+    else:
+        return 1
+
 @command('tip',
     [('p', 'patch', None, _('show patch')),
     ('g', 'git', None, _('use git extended diff format')),
diff -r 91701785c2c5 -r f850e5c85eee tests/test-cmdtest.t
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-cmdtest.t	Wed Feb 13 15:34:14 2019 +0100
@@ -0,0 +1,31 @@ 
+================================
+Testing of the `hg test` command
+================================
+
+Initial setup
+=============
+
+  $ hg init cmdtest
+  $ cd cmdtest
+  $ hg debugbuilddag .+10
+
+
+Basic expression
+================
+
+Simple successful
+
+  $ hg test 0
+
+Simple empty revset
+
+  $ hg test "(0 and 1)"
+  [1]
+
+Simple invalid revset
+
+  $ hg test "(0"
+  hg: parse error at 2: unexpected token: end
+  ((0
+     ^ here)
+  [255]
diff -r 91701785c2c5 -r f850e5c85eee tests/test-completion.t
--- a/tests/test-completion.t	Mon Feb 11 11:18:37 2019 -0500
+++ b/tests/test-completion.t	Wed Feb 13 15:34:14 2019 +0100
@@ -49,6 +49,7 @@ 
   summary
   tag
   tags
+  test
   tip
   unbundle
   update
@@ -340,6 +341,7 @@ 
   summary: remote
   tag: force, local, rev, remove, edit, message, date, user
   tags: template
+  test: 
   tip: patch, git, style, template
   unbundle: update
   update: clean, check, merge, date, rev, tool
diff -r 91701785c2c5 -r f850e5c85eee tests/test-globalopts.t
--- a/tests/test-globalopts.t	Mon Feb 11 11:18:37 2019 -0500
+++ b/tests/test-globalopts.t	Wed Feb 13 15:34:14 2019 +0100
@@ -380,6 +380,10 @@ 
    help          show help for a given topic or a help overview
    version       output version and copyright information
   
+  Miscellaneous commands:
+  
+   test          check if revset match anything
+  
   additional help topics:
   
   Mercurial identifiers:
@@ -510,6 +514,10 @@ 
    help          show help for a given topic or a help overview
    version       output version and copyright information
   
+  Miscellaneous commands:
+  
+   test          check if revset match anything
+  
   additional help topics:
   
   Mercurial identifiers:
diff -r 91701785c2c5 -r f850e5c85eee tests/test-help-hide.t
--- a/tests/test-help-hide.t	Mon Feb 11 11:18:37 2019 -0500
+++ b/tests/test-help-hide.t	Wed Feb 13 15:34:14 2019 +0100
@@ -84,6 +84,10 @@ 
    help          show help for a given topic or a help overview
    version       output version and copyright information
   
+  Miscellaneous commands:
+  
+   test          check if revset match anything
+  
   additional help topics:
   
   Mercurial identifiers:
@@ -218,6 +222,10 @@ 
    help          show help for a given topic or a help overview
    version       output version and copyright information
   
+  Miscellaneous commands:
+  
+   test          check if revset match anything
+  
   additional help topics:
   
   Mercurial identifiers:
diff -r 91701785c2c5 -r f850e5c85eee tests/test-help.t
--- a/tests/test-help.t	Mon Feb 11 11:18:37 2019 -0500
+++ b/tests/test-help.t	Wed Feb 13 15:34:14 2019 +0100
@@ -136,6 +136,10 @@ 
    help          show help for a given topic or a help overview
    version       output version and copyright information
   
+  Miscellaneous commands:
+  
+   test          check if revset match anything
+  
   additional help topics:
   
   Mercurial identifiers:
@@ -262,6 +266,10 @@ 
    help          show help for a given topic or a help overview
    version       output version and copyright information
   
+  Miscellaneous commands:
+  
+   test          check if revset match anything
+  
   additional help topics:
   
   Mercurial identifiers:
@@ -2698,6 +2706,13 @@ 
   list repository tags
   </td></tr>
   <tr><td>
+  <a href="/help/test">
+  test
+  </a>
+  </td><td>
+  check if revset match anything
+  </td></tr>
+  <tr><td>
   <a href="/help/unbundle">
   unbundle
   </a>
diff -r 91701785c2c5 -r f850e5c85eee tests/test-hgweb-json.t
--- a/tests/test-hgweb-json.t	Mon Feb 11 11:18:37 2019 -0500
+++ b/tests/test-hgweb-json.t	Wed Feb 13 15:34:14 2019 +0100
@@ -2065,6 +2065,10 @@ 
         "topic": "tags"
       },
       {
+        "summary": "check if revset match anything",
+        "topic": "test"
+      },
+      {
         "summary": "apply one or more bundle files",
         "topic": "unbundle"
       },