Patchwork completion: add a debuglabelcomplete command

login
register
mail settings
Submitter Bryan O'Sullivan
Date March 21, 2013, 4:28 p.m.
Message ID <9335c605b08ae8c53277.1363883338@australite.local>
Download mbox | patch
Permalink /patch/1148/
State Accepted
Commit 1e28a7f58f33729994be240335ba490a3f92e5d5
Headers show

Comments

Bryan O'Sullivan - March 21, 2013, 4:28 p.m.
# HG changeset patch
# User Bryan O'Sullivan <bryano@fb.com>
# Date 1363883331 25200
# Node ID 9335c605b08ae8c5327711ddf47a42e8f729211d
# Parent  fff3a811451082b9f47909c38e7c6d3b15177b34
completion: add a debuglabelcomplete command

When completing a "label" (a symbolic name for a commit), the
bash_completion script currently has to invoke hg three times. For
a large repository, the cost of starting up and loading all the
necessary context over and over is very high.

For instance, in mozilla-central:

  time (export HGPLAIN=1; hg tags -q; hg bookmarks -q; hg branches) >/dev/null
  0.446 sec

Compare with the debuglabelcomplete command that this commit adds:

  time hg debuglabelcomplete >/dev/null
  0.148 sec

This greatly helps responsiveness.
Bryan O'Sullivan - April 8, 2013, 8:09 p.m.
On Mon, Apr 8, 2013 at 5:33 AM, Nikolaj Sjujskij <sterkrig@myopera.com>wrote:

>  Works weirdly here on Gentoo Linux (confirmed by marmoute on Debian
> Wheezy), on own mercurial repo:
>

Thanks - I just sent a patch that fixes this.

Patch

diff --git a/contrib/bash_completion b/contrib/bash_completion
--- a/contrib/bash_completion
+++ b/contrib/bash_completion
@@ -88,20 +88,6 @@  shopt -s extglob
     COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$files' -- "$cur"))
 }
 
-_hg_tags()
-{
-    local tags="$(_hg_cmd tags -q)"
-    local IFS=$'\n'
-    COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$tags' -- "$cur"))
-}
-
-_hg_branches()
-{
-    local branches="$(_hg_cmd branches -q)"
-    local IFS=$'\n'
-    COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$branches' -- "$cur"))
-}
-
 _hg_bookmarks()
 {
     local bookmarks="$(_hg_cmd bookmarks -q)"
@@ -111,9 +97,9 @@  shopt -s extglob
 
 _hg_labels()
 {
-    _hg_tags
-    _hg_branches
-    _hg_bookmarks
+    local labels="$(_hg_cmd debuglabelcomplete "$cur")"
+    local IFS=$'\n'
+    COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$labels' -- "$cur"))
 }
 
 # this is "kind of" ugly...
diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -2072,6 +2072,22 @@  def debugknown(ui, repopath, *ids, **opt
     flags = repo.known([bin(s) for s in ids])
     ui.write("%s\n" % ("".join([f and "1" or "0" for f in flags])))
 
+@command('debuglabelcomplete', [], _('LABEL...'))
+def debuglabelcomplete(ui, repo, *args):
+    '''complete "labels" - tags, open branch names, bookmark names'''
+
+    labels = set()
+    labels.update(t[0] for t in repo.tagslist())
+    labels.update(repo[n].branch() for n in repo.heads())
+    labels.update(repo._bookmarks.keys())
+    completions = set()
+    if not args:
+        args = ['']
+    for a in args:
+        completions.update(l for l in labels if l.startswith(a))
+    ui.write('\n'.join(sorted(completions)))
+    ui.write('\n')
+
 @command('debugobsolete',
         [('', 'flags', 0, _('markers flag')),
         ] + commitopts2,