[PATCH 2 of 2] commands: add a --filenameonly flag to hg diff --stat

Laurent Charignon lcharignon at fb.com
Tue Aug 11 08:03:26 CDT 2015


# HG changeset patch
# User Laurent Charignon <lcharignon at fb.com>
# Date 1439295216 25200
#      Tue Aug 11 05:13:36 2015 -0700
# Node ID 2a42df446fd8a59199b9d7f7dfc28b5efb784e17
# Parent  e3166b55525d48d0b69231d793ef2918046320e0
commands: add a --filenameonly flag to hg diff --stat

This series of patches adds a --filenameonly flag to hg diff --stat to only
display the filenames and omit the number of changes and summary line.

I had the need for this feature trying to compute the list of files that changed
between two branches with:

hg log -T {files % "{file}\\n"} -r 'only(r1,r2)'
 and having to pipe the result to sort, uniq to get the list of file that
 changed between the branches without duplicates

hg diff -r r1 -r r2 --stat
 gave the right result but required parsing the output to eliminate the +,- and
 summary line

This series solves this issues with a new flag that combines with --stat:
 hg diff -r r1 -r r2 --stat --filenameonly

diff --git a/mercurial/cmdutil.py b/mercurial/cmdutil.py
--- a/mercurial/cmdutil.py
+++ b/mercurial/cmdutil.py
@@ -1078,6 +1078,7 @@ def diffordiffstat(ui, repo, diffopts, n
 
     if stat:
         diffopts = diffopts.copy(context=0)
+        filenameonly = diffopts.filenameonly
         width = 80
         if not ui.plain():
             width = ui.termwidth()
@@ -1085,7 +1086,8 @@ def diffordiffstat(ui, repo, diffopts, n
                             prefix=prefix, relroot=relroot)
         for chunk, label in patch.diffstatui(util.iterlines(chunks),
                                              width=width,
-                                             git=diffopts.git):
+                                             git=diffopts.git,
+                                             filenameonly=filenameonly):
             write(chunk, label=label)
     else:
         for chunk, label in patch.diffui(repo, node1, node2, match,
diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -154,6 +154,7 @@ diffopts2 = [
     ('U', 'unified', '',
      _('number of lines of context to show'), _('NUM')),
     ('', 'stat', None, _('output diffstat-style summary of changes')),
+    ('', 'filenameonly', None, _('with --stat, only show filenames')),
     ('', 'root', '', _('produce diffs relative to subdirectory'), _('DIR')),
 ]
 
@@ -3173,8 +3174,13 @@ def diff(ui, repo, *pats, **opts):
     revs = opts.get('rev')
     change = opts.get('change')
     stat = opts.get('stat')
+    filenameonly = opts.get('filenameonly')
     reverse = opts.get('reverse')
 
+    if filenameonly and not stat:
+        msg = _('cannot specify --filenameonly without --stat')
+        raise util.Abort(msg)
+
     if revs and change:
         msg = _('cannot specify --rev and --change at the same time')
         raise util.Abort(msg)
diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -2494,7 +2494,7 @@ def diffstatdata(lines):
     addresult()
     return results
 
-def diffstat(lines, width=80, git=False):
+def diffstat(lines, width=80, git=False, filenameonly=False):
     output = []
     stats = diffstatdata(lines)
     maxname, maxtotal, totaladds, totalremoves, hasbinary = diffstatsum(stats)
@@ -2521,11 +2521,15 @@ def diffstat(lines, width=80, git=False)
             count = adds + removes
         pluses = '+' * scale(adds)
         minuses = '-' * scale(removes)
-        output.append(' %s%s |  %*s %s%s\n' %
-                      (filename, ' ' * (maxname - encoding.colwidth(filename)),
-                       countwidth, count, pluses, minuses))
+        if filenameonly:
+            output.append('%s\n' % filename)
+        else:
+            output.append(' %s%s |  %*s %s%s\n' %
+                          (filename,
+                           ' ' * (maxname - encoding.colwidth(filename)),
+                           countwidth, count, pluses, minuses))
 
-    if stats:
+    if stats and not filenameonly:
         output.append(_(' %d files changed, %d insertions(+), '
                         '%d deletions(-)\n')
                       % (len(stats), totaladds, totalremoves))
diff --git a/tests/test-completion.t b/tests/test-completion.t
--- a/tests/test-completion.t
+++ b/tests/test-completion.t
@@ -204,7 +204,7 @@ Show all commands + options
   annotate: rev, follow, no-follow, text, user, file, date, number, changeset, line-number, ignore-all-space, ignore-space-change, ignore-blank-lines, include, exclude, template
   clone: noupdate, updaterev, rev, branch, pull, uncompressed, ssh, remotecmd, insecure
   commit: addremove, close-branch, amend, secret, edit, interactive, include, exclude, message, logfile, date, user, subrepos
-  diff: rev, change, text, git, nodates, noprefix, show-function, reverse, ignore-all-space, ignore-space-change, ignore-blank-lines, unified, stat, root, include, exclude, subrepos
+  diff: rev, change, text, git, nodates, noprefix, show-function, reverse, ignore-all-space, ignore-space-change, ignore-blank-lines, unified, stat, filenameonly, root, include, exclude, subrepos
   export: output, switch-parent, rev, text, git, nodates
   forget: include, exclude
   init: ssh, remotecmd, insecure
diff --git a/tests/test-diffstat.t b/tests/test-diffstat.t
--- a/tests/test-diffstat.t
+++ b/tests/test-diffstat.t
@@ -82,6 +82,13 @@ diffstat within directories:
    dir2/new |  1 +
    2 files changed, 2 insertions(+), 0 deletions(-)
 
+  $ hg diff -c . --filenameonly
+  abort: cannot specify --filenameonly without --stat
+  [255]
+  $ hg diff --stat --filenameonly
+  dir1/new
+  dir2/new
+
   $ hg diff --stat --root dir1
    new |  1 +
    1 files changed, 1 insertions(+), 0 deletions(-)
diff --git a/tests/test-help.t b/tests/test-help.t
--- a/tests/test-help.t
+++ b/tests/test-help.t
@@ -510,6 +510,7 @@ Test command without options
    -B --ignore-blank-lines  ignore changes whose lines are all blank
    -U --unified NUM         number of lines of context to show
       --stat                output diffstat-style summary of changes
+      --filenameonly        with --stat, only show filenames
       --root DIR            produce diffs relative to subdirectory
    -I --include PATTERN [+] include names matching the given patterns
    -X --exclude PATTERN [+] exclude names matching the given patterns


More information about the Mercurial-devel mailing list