[PATCH 1 of 2] completion: add a debugpathcomplete command

Bryan O'Sullivan bos at serpentine.com
Thu Mar 21 13:17:18 CDT 2013


# HG changeset patch
# User Bryan O'Sullivan <bryano at fb.com>
# Date 1363889763 25200
# Node ID d91fe1c607511dc57cb51e557229c243908a6b39
# Parent  1e28a7f58f33729994be240335ba490a3f92e5d5
completion: add a debugpathcomplete command

The bash_completion code uses "hg status" to generate a list of
possible completions for commands that operate on files in the
working directory. In a large working directory, this can result
in a single tab-completion being very slow (several seconds) as a
result of checking the status of every file, even when there is no
need to check status or no possible matches.

The new debugpathcomplete command gains performance in a few simple
ways:

* Allow completion to operate on just a single directory. When used
  to complete the right commands, this considerably reduces the
  number of completions returned, at no loss in functionality.

* Never check the status of files. For completions that really must
  know if a file is modified, it is faster to use status:

  hg status -nm 'glob:myprefix**'

Performance:

Here are the commands used by bash_completion to complete, run in
the root of the mozilla-central working dir (~77,000 files) and
another repo (~165,000 files):

All "normal state" files (used by e.g. remove, revert):

                            mozilla    other
  status -nmcd 'glob:**'       1.77     4.10 sec
  debugpathcomplete -f -n      0.53     1.26
  debugpathcomplete -n         0.17     0.41

("-f" means "complete full paths", rather than the current directory)

Tracked files matching "a":

                            mozilla    other
  status -nmcd 'glob:a**'      0.26     0.47
  debugpathcomplete -f -n a    0.10     0.24
  debugpathcomplete -n a       0.10     0.22

We should be able to further improve completion performance once
the critbit work lands. Right now, our performance is limited by
the need to iterate over all keys in the dirstate.

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -2137,6 +2137,73 @@ def debugobsolete(ui, repo, precursor=No
                                          sorted(m.metadata().items()))))
             ui.write('\n')
 
+ at command('debugpathcomplete',
+         [('f', 'full', None, _('complete an entire path')),
+          ('n', 'normal', None, _('show only normal files')),
+          ('a', 'added', None, _('show only added files')),
+          ('r', 'removed', None, _('show only removed files'))],
+         _('FILESPEC...'))
+def debugpathcomplete(ui, repo, *specs, **opts):
+    '''complete part or all of a tracked path
+
+    This command supports shells that offer path name completion. It
+    currently completes only files already known to the dirstate.
+
+    Completion extends only to the next path segment unless
+    --full is specified, in which case entire paths are used.'''
+
+    def complete(path, acceptable):
+        dirstate = repo.dirstate
+        spec = os.path.normpath(os.path.join(os.getcwd(), path))
+        rootdir = repo.root + os.sep
+        if spec != repo.root and not spec.startswith(rootdir):
+            return [], []
+        if os.path.isdir(spec):
+            spec += '/'
+        spec = spec[len(rootdir):]
+        fixpaths = os.sep != '/'
+        if fixpaths:
+            spec = spec.replace(os.sep, '/')
+        speclen = len(spec)
+        fullpaths = opts['full']
+        files, dirs = set(), set()
+        adddir, addfile = dirs.add, files.add
+        for f, st in dirstate.iteritems():
+            if f.startswith(spec) and st[0] in acceptable:
+                if fixpaths:
+                    f = f.replace('/', os.sep)
+                if fullpaths:
+                    addfile(f)
+                    continue
+                s = f.find(os.sep, speclen)
+                if s >= 0:
+                    adddir(f[:s+1])
+                else:
+                    addfile(f)
+        return files, dirs
+
+    acceptable = ''
+    if opts['normal']:
+        acceptable += 'nm'
+    if opts['added']:
+        acceptable += 'a'
+    if opts['removed']:
+        acceptable += 'r'
+    cwd = repo.getcwd()
+    if not specs:
+        specs = ['.']
+
+    files, dirs = set(), set()
+    for spec in specs:
+        f, d = complete(spec, acceptable or 'nmar')
+        files.update(f)
+        dirs.update(d)
+    for d in dirs:
+        files.add(d + 'a')
+        files.add(d + 'b')
+    ui.write('\n'.join(repo.pathto(p, cwd) for p in sorted(files)))
+    ui.write('\n')
+
 @command('debugpushkey', [], _('REPO NAMESPACE [KEY OLD NEW]'))
 def debugpushkey(ui, repopath, namespace, *keyinfo, **opts):
     '''access the pushkey key/value protocol
diff --git a/mercurial/dirstate.py b/mercurial/dirstate.py
--- a/mercurial/dirstate.py
+++ b/mercurial/dirstate.py
@@ -223,6 +223,9 @@ class dirstate(object):
         for x in sorted(self._map):
             yield x
 
+    def iteritems(self):
+        return self._map.iteritems()
+
     def parents(self):
         return [self._validate(p) for p in self._pl]
 


More information about the Mercurial-devel mailing list