changeset 22423:edf07a804ac4

files: add new command unifying locate and manifest functionality
author Matt Mackall <mpm@selenic.com>
date Fri, 12 Sep 2014 18:32:46 -0500
parents 75bb7c702317
children 1f72226064b8
files mercurial/commands.py tests/test-completion.t tests/test-globalopts.t tests/test-help.t tests/test-locate.t tests/test-manifest.t
diffstat 6 files changed, 107 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/commands.py	Tue Sep 02 14:42:30 2014 -0400
+++ b/mercurial/commands.py	Fri Sep 12 18:32:46 2014 -0500
@@ -3061,6 +3061,81 @@
                  switch_parent=opts.get('switch_parent'),
                  opts=patch.diffopts(ui, opts))
 
+@command('files',
+    [('r', 'rev', '', _('search the repository as it is in REV'), _('REV')),
+     ('0', 'print0', None, _('end filenames with NUL, for use with xargs')),
+    ] + walkopts,
+    _('[OPTION]... [PATTERN]...'))
+def files(ui, repo, *pats, **opts):
+    """list tracked files
+
+    Print files under Mercurial control in the working directory or
+    specified revision whose names match the given patterns (excluding
+    removed files).
+
+    If no patterns are given to match, this command prints the names
+    of all files under Mercurial control in the working copy.
+
+    .. container:: verbose
+
+      Examples:
+
+      - list all files under the current directory::
+
+          hg files .
+
+      - shows sizes and flags for current revision::
+
+          hg files -vr .
+
+      - list all files named README::
+
+          hg files -I "**/README"
+
+      - list all binary files::
+
+          hg files "set:binary()"
+
+      - find files containing a regular expression:
+
+          hg files "set:grep('bob')"
+
+      - search tracked file contents with xargs and grep::
+
+          hg files -0 | xargs -0 grep foo
+
+    See :hg:'help pattern' and :hg:'help revsets' for more information
+    on specifying file patterns.
+
+    Returns 0 if a match is found, 1 otherwise.
+
+    """
+    ctx = scmutil.revsingle(repo, opts.get('rev'), None)
+    rev = ctx.rev()
+    ret = 1
+
+    end = '\n'
+    if opts.get('print0'):
+        end = '\0'
+    fm = ui.formatter('status', opts)
+    fmt = '%s' + end
+
+    m = scmutil.match(ctx, pats, opts)
+    for f in ctx.walk(m):
+        if rev is None and repo.dirstate[f] in 'R?!':
+            continue
+        fm.startitem()
+        if ui.verbose:
+            fc = ctx[f]
+            fm.write('size flags', '% 10d % 1s ', fc.size(), fc.flags())
+        fm.data(abspath=f)
+        fm.write('path', fmt, m.rel(f))
+        ret = 0
+
+    fm.end()
+
+    return ret
+
 @command('^forget', walkopts, _('[OPTION]... FILE...'), inferrepo=True)
 def forget(ui, repo, *pats, **opts):
     """forget the specified files on the next commit
--- a/tests/test-completion.t	Tue Sep 02 14:42:30 2014 -0400
+++ b/tests/test-completion.t	Fri Sep 12 18:32:46 2014 -0500
@@ -17,6 +17,7 @@
   copy
   diff
   export
+  files
   forget
   graft
   grep
@@ -257,6 +258,7 @@
   debugsuccessorssets: 
   debugwalk: include, exclude
   debugwireargs: three, four, five, ssh, remotecmd, insecure
+  files: rev, print0, include, exclude
   graft: rev, continue, edit, log, force, currentdate, currentuser, date, user, tool, dry-run
   grep: print0, all, text, follow, ignore-case, files-with-matches, line-number, rev, user, date, include, exclude
   heads: rev, topo, active, closed, style, template
--- a/tests/test-globalopts.t	Tue Sep 02 14:42:30 2014 -0400
+++ b/tests/test-globalopts.t	Fri Sep 12 18:32:46 2014 -0500
@@ -301,6 +301,7 @@
    copy          mark files as copied for the next commit
    diff          diff repository (or selected files)
    export        dump the header and diffs for one or more changesets
+   files         list tracked files
    forget        forget the specified files on the next commit
    graft         copy changes from other branches onto the current branch
    grep          search for a pattern in specified files and revisions
@@ -383,6 +384,7 @@
    copy          mark files as copied for the next commit
    diff          diff repository (or selected files)
    export        dump the header and diffs for one or more changesets
+   files         list tracked files
    forget        forget the specified files on the next commit
    graft         copy changes from other branches onto the current branch
    grep          search for a pattern in specified files and revisions
--- a/tests/test-help.t	Tue Sep 02 14:42:30 2014 -0400
+++ b/tests/test-help.t	Fri Sep 12 18:32:46 2014 -0500
@@ -66,6 +66,7 @@
    copy          mark files as copied for the next commit
    diff          diff repository (or selected files)
    export        dump the header and diffs for one or more changesets
+   files         list tracked files
    forget        forget the specified files on the next commit
    graft         copy changes from other branches onto the current branch
    grep          search for a pattern in specified files and revisions
@@ -142,6 +143,7 @@
    copy          mark files as copied for the next commit
    diff          diff repository (or selected files)
    export        dump the header and diffs for one or more changesets
+   files         list tracked files
    forget        forget the specified files on the next commit
    graft         copy changes from other branches onto the current branch
    grep          search for a pattern in specified files and revisions
@@ -684,6 +686,7 @@
    copy          mark files as copied for the next commit
    diff          diff repository (or selected files)
    export        dump the header and diffs for one or more changesets
+   files         list tracked files
    forget        forget the specified files on the next commit
    graft         copy changes from other branches onto the current branch
    grep          search for a pattern in specified files and revisions
@@ -1444,6 +1447,13 @@
   mark files as copied for the next commit
   </td></tr>
   <tr><td>
+  <a href="/help/files">
+  files
+  </a>
+  </td><td>
+  list tracked files
+  </td></tr>
+  <tr><td>
   <a href="/help/graft">
   graft
   </a>
--- a/tests/test-locate.t	Tue Sep 02 14:42:30 2014 -0400
+++ b/tests/test-locate.t	Fri Sep 12 18:32:46 2014 -0500
@@ -92,6 +92,16 @@
   t/e.h (glob)
   t/x (glob)
 
+  $ hg files
+  b
+  dir.h/foo
+  t.h
+  t/b
+  t/e.h
+  t/x
+  $ hg files b
+  b
+
   $ mkdir otherdir
   $ cd otherdir
 
--- a/tests/test-manifest.t	Tue Sep 02 14:42:30 2014 -0400
+++ b/tests/test-manifest.t	Fri Sep 12 18:32:46 2014 -0500
@@ -24,6 +24,14 @@
   b/a
   l
 
+  $ hg files -vr .
+           2   a
+           2 x b/a
+           1 l l
+  $ hg files -r . -X b
+  a
+  l
+
   $ hg manifest -v
   644   a
   755 * b/a