Patchwork show: config option to register aliases for views

login
register
mail settings
Submitter Gregory Szorc
Date June 26, 2017, 5:26 a.m.
Message ID <6546ab6d7a9aaa4266d1.1498454812@ubuntu-vm-main>
Download mbox | patch
Permalink /patch/21719/
State Accepted
Headers show

Comments

Gregory Szorc - June 26, 2017, 5:26 a.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# Date 1498454437 25200
#      Sun Jun 25 22:20:37 2017 -0700
# Node ID 6546ab6d7a9aaa4266d1decf3da1501153be796b
# Parent  ef46d432e2e4cfecafb1faa4765254bf0650d4ef
show: config option to register aliases for views

As part of using `hg show` in my daily workflow, I've found it slightly
annoying to have to type full view names, complete with a space. I've
locally registered an alias for "swork = show work."

I think others will have this same complaint and could benefit from
some automation to streamline the creation of aliases. So, this
commit introduces a config option that allows `hg show` views to be
automatically aliased using a given prefix. e.g. a value of "s"
will automatically register "swork" and "sbookmarks." Multiple
values can be given for ultimate flexibility. This arguably isn't
needed now. But since we don't register aliases if there will be
a collision and we're bound to have a collision, it makes sense to
allow multiple prefixes so specific views can avoid collisions by
using different prefixes.
Yuya Nishihara - June 28, 2017, 3:33 p.m.
On Sun, 25 Jun 2017 22:26:52 -0700, Gregory Szorc wrote:
> # HG changeset patch
> # User Gregory Szorc <gregory.szorc@gmail.com>
> # Date 1498454437 25200
> #      Sun Jun 25 22:20:37 2017 -0700
> # Node ID 6546ab6d7a9aaa4266d1decf3da1501153be796b
> # Parent  ef46d432e2e4cfecafb1faa4765254bf0650d4ef
> show: config option to register aliases for views
> 
> As part of using `hg show` in my daily workflow, I've found it slightly
> annoying to have to type full view names, complete with a space. I've
> locally registered an alias for "swork = show work."
> 
> I think others will have this same complaint and could benefit from
> some automation to streamline the creation of aliases. So, this
> commit introduces a config option that allows `hg show` views to be
> automatically aliased using a given prefix. e.g. a value of "s"
> will automatically register "swork" and "sbookmarks." Multiple
> values can be given for ultimate flexibility. This arguably isn't
> needed now. But since we don't register aliases if there will be
> a collision and we're bound to have a collision, it makes sense to
> allow multiple prefixes so specific views can avoid collisions by
> using different prefixes.

I think this change is okay. (I don't queue it now just because it's late.)

Somewhat related to this, I've lost why we decided to make "hg show <cmd>"
at the cost of managing sub-sub commands. If we want a shorter command name,
I think "hg work" is even better.

> +def extsetup(ui):
> +    # Alias `hg <prefix><view>` to `hg show <view>`.
> +    for prefix in ui.configlist('commands', 'show.aliasprefix'):
> +        for view in showview._table:
> +            name = '%s%s' % (prefix, view)
> +
> +            choice, allcommands = cmdutil.findpossible(name, commands.table,
> +                                                       strict=True)
> +
> +            # This alias is already a command name. Don't set it.
> +            if name in choice:
> +                continue
> +
> +            # Same for aliases.
> +            if ui.config('alias', name):
> +                continue
> +
> +            ui.setconfig('alias', name, 'show %s' % view, source='show')

Setting config in ui/extsetup() doesn't work in general, but we're lucky that
addaliases() takes lui not ui.
Yuya Nishihara - June 29, 2017, 1:54 p.m.
On Sun, 25 Jun 2017 22:26:52 -0700, Gregory Szorc wrote:
> # HG changeset patch
> # User Gregory Szorc <gregory.szorc@gmail.com>
> # Date 1498454437 25200
> #      Sun Jun 25 22:20:37 2017 -0700
> # Node ID 6546ab6d7a9aaa4266d1decf3da1501153be796b
> # Parent  ef46d432e2e4cfecafb1faa4765254bf0650d4ef
> show: config option to register aliases for views

Queued, thanks.
Gregory Szorc - July 2, 2017, 3:12 a.m.
On Wed, Jun 28, 2017 at 8:33 AM, Yuya Nishihara <yuya@tcha.org> wrote:

> On Sun, 25 Jun 2017 22:26:52 -0700, Gregory Szorc wrote:
> > # HG changeset patch
> > # User Gregory Szorc <gregory.szorc@gmail.com>
> > # Date 1498454437 25200
> > #      Sun Jun 25 22:20:37 2017 -0700
> > # Node ID 6546ab6d7a9aaa4266d1decf3da1501153be796b
> > # Parent  ef46d432e2e4cfecafb1faa4765254bf0650d4ef
> > show: config option to register aliases for views
> >
> > As part of using `hg show` in my daily workflow, I've found it slightly
> > annoying to have to type full view names, complete with a space. I've
> > locally registered an alias for "swork = show work."
> >
> > I think others will have this same complaint and could benefit from
> > some automation to streamline the creation of aliases. So, this
> > commit introduces a config option that allows `hg show` views to be
> > automatically aliased using a given prefix. e.g. a value of "s"
> > will automatically register "swork" and "sbookmarks." Multiple
> > values can be given for ultimate flexibility. This arguably isn't
> > needed now. But since we don't register aliases if there will be
> > a collision and we're bound to have a collision, it makes sense to
> > allow multiple prefixes so specific views can avoid collisions by
> > using different prefixes.
>
> I think this change is okay. (I don't queue it now just because it's late.)
>
> Somewhat related to this, I've lost why we decided to make "hg show <cmd>"
> at the cost of managing sub-sub commands. If we want a shorter command
> name,
> I think "hg work" is even better.
>

A big reason is that `hg help show` can be a gateway to all available
views, thus fostering understanding. Also, `hg show` is guaranteed to be
read-only, so any command under it is safe to run. That means we shouldn't
scare people away from running `hg show` like other commands would.


>
> > +def extsetup(ui):
> > +    # Alias `hg <prefix><view>` to `hg show <view>`.
> > +    for prefix in ui.configlist('commands', 'show.aliasprefix'):
> > +        for view in showview._table:
> > +            name = '%s%s' % (prefix, view)
> > +
> > +            choice, allcommands = cmdutil.findpossible(name,
> commands.table,
> > +                                                       strict=True)
> > +
> > +            # This alias is already a command name. Don't set it.
> > +            if name in choice:
> > +                continue
> > +
> > +            # Same for aliases.
> > +            if ui.config('alias', name):
> > +                continue
> > +
> > +            ui.setconfig('alias', name, 'show %s' % view, source='show')
>
> Setting config in ui/extsetup() doesn't work in general, but we're lucky
> that
> addaliases() takes lui not ui.
>
Yuya Nishihara - July 3, 2017, 2:55 p.m.
On Sat, 1 Jul 2017 20:12:42 -0700, Gregory Szorc wrote:
> On Wed, Jun 28, 2017 at 8:33 AM, Yuya Nishihara <yuya@tcha.org> wrote:
> > On Sun, 25 Jun 2017 22:26:52 -0700, Gregory Szorc wrote:
> > > # HG changeset patch
> > > # User Gregory Szorc <gregory.szorc@gmail.com>
> > > # Date 1498454437 25200
> > > #      Sun Jun 25 22:20:37 2017 -0700
> > > # Node ID 6546ab6d7a9aaa4266d1decf3da1501153be796b
> > > # Parent  ef46d432e2e4cfecafb1faa4765254bf0650d4ef
> > > show: config option to register aliases for views
> > >
> > > As part of using `hg show` in my daily workflow, I've found it slightly
> > > annoying to have to type full view names, complete with a space. I've
> > > locally registered an alias for "swork = show work."
> > >
> > > I think others will have this same complaint and could benefit from
> > > some automation to streamline the creation of aliases. So, this
> > > commit introduces a config option that allows `hg show` views to be
> > > automatically aliased using a given prefix. e.g. a value of "s"
> > > will automatically register "swork" and "sbookmarks." Multiple
> > > values can be given for ultimate flexibility. This arguably isn't
> > > needed now. But since we don't register aliases if there will be
> > > a collision and we're bound to have a collision, it makes sense to
> > > allow multiple prefixes so specific views can avoid collisions by
> > > using different prefixes.
> >
> > I think this change is okay. (I don't queue it now just because it's late.)
> >
> > Somewhat related to this, I've lost why we decided to make "hg show <cmd>"
> > at the cost of managing sub-sub commands. If we want a shorter command
> > name,
> > I think "hg work" is even better.
> 
> A big reason is that `hg help show` can be a gateway to all available
> views, thus fostering understanding. Also, `hg show` is guaranteed to be
> read-only, so any command under it is safe to run. That means we shouldn't
> scare people away from running `hg show` like other commands would.

I think that could also be achieved by naming all commands as 'show-<x>'
(plus aliases e.g. 'show-stack|stack' if needed), and adding help topic
'show'. I don't mean it is better than the gateway command. I'm just afraid
of reinventing fullstack command dispatcher for 'show' subcommands.
Gregory Szorc - July 3, 2017, 3:39 p.m.
> On Jul 3, 2017, at 07:55, Yuya Nishihara <yuya@tcha.org> wrote:
> 
>> On Sat, 1 Jul 2017 20:12:42 -0700, Gregory Szorc wrote:
>>> On Wed, Jun 28, 2017 at 8:33 AM, Yuya Nishihara <yuya@tcha.org> wrote:
>>>> On Sun, 25 Jun 2017 22:26:52 -0700, Gregory Szorc wrote:
>>>> # HG changeset patch
>>>> # User Gregory Szorc <gregory.szorc@gmail.com>
>>>> # Date 1498454437 25200
>>>> #      Sun Jun 25 22:20:37 2017 -0700
>>>> # Node ID 6546ab6d7a9aaa4266d1decf3da1501153be796b
>>>> # Parent  ef46d432e2e4cfecafb1faa4765254bf0650d4ef
>>>> show: config option to register aliases for views
>>>> 
>>>> As part of using `hg show` in my daily workflow, I've found it slightly
>>>> annoying to have to type full view names, complete with a space. I've
>>>> locally registered an alias for "swork = show work."
>>>> 
>>>> I think others will have this same complaint and could benefit from
>>>> some automation to streamline the creation of aliases. So, this
>>>> commit introduces a config option that allows `hg show` views to be
>>>> automatically aliased using a given prefix. e.g. a value of "s"
>>>> will automatically register "swork" and "sbookmarks." Multiple
>>>> values can be given for ultimate flexibility. This arguably isn't
>>>> needed now. But since we don't register aliases if there will be
>>>> a collision and we're bound to have a collision, it makes sense to
>>>> allow multiple prefixes so specific views can avoid collisions by
>>>> using different prefixes.
>>> 
>>> I think this change is okay. (I don't queue it now just because it's late.)
>>> 
>>> Somewhat related to this, I've lost why we decided to make "hg show <cmd>"
>>> at the cost of managing sub-sub commands. If we want a shorter command
>>> name,
>>> I think "hg work" is even better.
>> 
>> A big reason is that `hg help show` can be a gateway to all available
>> views, thus fostering understanding. Also, `hg show` is guaranteed to be
>> read-only, so any command under it is safe to run. That means we shouldn't
>> scare people away from running `hg show` like other commands would.
> 
> I think that could also be achieved by naming all commands as 'show-<x>'
> (plus aliases e.g. 'show-stack|stack' if needed), and adding help topic
> 'show'. I don't mean it is better than the gateway command. I'm just afraid
> of reinventing fullstack command dispatcher for 'show' subcommands.

This seems like a reasonable approach.

At some point we'll need to add arguments to individual views. I also question whether it is worth introducing the concept/complexity of sub-commands for.

Patch

diff --git a/hgext/show.py b/hgext/show.py
--- a/hgext/show.py
+++ b/hgext/show.py
@@ -10,6 +10,19 @@ 
 This extension provides the :hg:`show` command, which provides a central
 command for displaying commonly-accessed repository data and views of that
 data.
+
+The following config options can influence operation.
+
+``commands``
+------------
+
+``show.aliasprefix``
+   List of strings that will register aliases for views. e.g. ``s`` will
+   effectively set config options ``alias.s<view> = show <view>`` for all
+   views. i.e. `hg swork` would execute `hg show work`.
+
+   Aliases that would conflict with existing registrations will not be
+   performed.
 """
 
 from __future__ import absolute_import
@@ -18,6 +31,7 @@  from mercurial.i18n import _
 from mercurial.node import nullrev
 from mercurial import (
     cmdutil,
+    commands,
     error,
     formatter,
     graphmod,
@@ -218,6 +232,25 @@  def showwork(ui, repo, displayer):
     ui.setconfig('experimental', 'graphshorten', True)
     cmdutil.displaygraph(ui, repo, revdag, displayer, graphmod.asciiedges)
 
+def extsetup(ui):
+    # Alias `hg <prefix><view>` to `hg show <view>`.
+    for prefix in ui.configlist('commands', 'show.aliasprefix'):
+        for view in showview._table:
+            name = '%s%s' % (prefix, view)
+
+            choice, allcommands = cmdutil.findpossible(name, commands.table,
+                                                       strict=True)
+
+            # This alias is already a command name. Don't set it.
+            if name in choice:
+                continue
+
+            # Same for aliases.
+            if ui.config('alias', name):
+                continue
+
+            ui.setconfig('alias', name, 'show %s' % view, source='show')
+
 # Adjust the docstring of the show command so it shows all registered views.
 # This is a bit hacky because it runs at the end of module load. When moved
 # into core or when another extension wants to provide a view, we'll need
diff --git a/tests/test-show.t b/tests/test-show.t
--- a/tests/test-show.t
+++ b/tests/test-show.t
@@ -127,4 +127,42 @@  JSON works with no bookmarks
   [
   ]
 
+commands.show.aliasprefix aliases values to `show <view>`
+
+  $ hg --config commands.show.aliasprefix=s sbookmarks
+  (no bookmarks set)
+
+  $ hg --config commands.show.aliasprefix=sh shwork
+  @  7b570 commit for book2
+  o  b757f commit for book1
+  o  ba592 initial
+
+  $ hg --config commands.show.aliasprefix='s sh' swork
+  @  7b570 commit for book2
+  o  b757f commit for book1
+  o  ba592 initial
+
+  $ hg --config commands.show.aliasprefix='s sh' shwork
+  @  7b570 commit for book2
+  o  b757f commit for book1
+  o  ba592 initial
+
+The aliases don't appear in `hg config`
+
+  $ hg --config commands.show.aliasprefix=s config alias
+  [1]
+
+Doesn't overwrite existing alias
+
+  $ hg --config alias.swork='log -r .' --config commands.show.aliasprefix=s swork
+  changeset:   2:7b5709ab64cb
+  tag:         tip
+  user:        test
+  date:        Thu Jan 01 00:00:00 1970 +0000
+  summary:     commit for book2
+  
+
+  $ hg --config alias.swork='log -r .' --config commands.show.aliasprefix=s config alias
+  alias.swork=log -r .
+
   $ cd ..