diff: add --binary option for git mode diffs
authorAlexander Fomin <afomin@fb.com>
Wed, 05 Apr 2017 15:31:08 -0700
changeset 31822 fde4822b0102
parent 31821 66a9faadbc83
child 31823 f6d77af84ef3
diff: add --binary option for git mode diffs This patch adds --binary option to `hg diff` and `hg export` to allow more control about when binary diffs are displayed in Git mode as well as some tests to verify it behaves correctly (issue5510).
mercurial/commands.py
mercurial/patch.py
tests/test-completion.t
tests/test-diff-binary-file.t
tests/test-help.t
--- a/mercurial/commands.py	Wed Apr 05 15:15:06 2017 -0700
+++ b/mercurial/commands.py	Wed Apr 05 15:31:08 2017 -0700
@@ -162,6 +162,7 @@
 diffopts = [
     ('a', 'text', None, _('treat all files as text')),
     ('g', 'git', None, _('use git extended diff format')),
+    ('', 'binary', None, _('generate binary diffs in git mode (default)')),
     ('', 'nodates', None, _('omit dates from diff headers'))
 ]
 
--- a/mercurial/patch.py	Wed Apr 05 15:15:06 2017 -0700
+++ b/mercurial/patch.py	Wed Apr 05 15:31:08 2017 -0700
@@ -2210,7 +2210,9 @@
                                             'ignoreblanklines')
     if formatchanging:
         buildopts['text'] = opts and opts.get('text')
-        buildopts['nobinary'] = get('nobinary', forceplain=False)
+        binary = None if opts is None else opts.get('binary')
+        buildopts['nobinary'] = (not binary if binary is not None
+                                 else get('nobinary', forceplain=False))
         buildopts['noprefix'] = get('noprefix', forceplain=False)
 
     return mdiff.diffopts(**pycompat.strkwargs(buildopts))
--- a/tests/test-completion.t	Wed Apr 05 15:15:06 2017 -0700
+++ b/tests/test-completion.t	Wed Apr 05 15:31:08 2017 -0700
@@ -216,8 +216,8 @@
   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
-  export: output, switch-parent, rev, text, git, nodates
+  diff: rev, change, text, git, binary, nodates, noprefix, show-function, reverse, ignore-all-space, ignore-space-change, ignore-blank-lines, unified, stat, root, include, exclude, subrepos
+  export: output, switch-parent, rev, text, git, binary, nodates
   forget: include, exclude
   init: ssh, remotecmd, insecure
   log: follow, follow-first, date, copies, keyword, rev, removed, only-merges, user, only-branch, branch, prune, patch, git, limit, no-merges, stat, graph, style, template, include, exclude
--- a/tests/test-diff-binary-file.t	Wed Apr 05 15:15:06 2017 -0700
+++ b/tests/test-diff-binary-file.t	Wed Apr 05 15:31:08 2017 -0700
@@ -108,4 +108,37 @@
   \ No newline at end of file
   +\x00\x01\x02\x03 (esc)
 
+Test binary mode with extended git-style diff format
+  $ hg diff --no-binary -r 0 -r 1
+  diff -r fb45f71337ad -r 9ca112d1a3c1 binfile.bin
+  Binary file binfile.bin has changed
+
+  $ hg diff --git --no-binary -r 0 -r 1
+  diff --git a/binfile.bin b/binfile.bin
+  Binary file binfile.bin has changed
+
+  $ hg diff --git --binary -r 0 -r 1
+  diff --git a/binfile.bin b/binfile.bin
+  index eaf36c1daccfdf325514461cd1a2ffbc139b5464..ba71a782e93f3fb63a428383706065e3ec2828e9
+  GIT binary patch
+  literal 5
+  Mc${NkWMbw50018V5dZ)H
+  
+  $ hg diff --git --binary --config diff.nobinary=True -r 0 -r 1
+  diff --git a/binfile.bin b/binfile.bin
+  index eaf36c1daccfdf325514461cd1a2ffbc139b5464..ba71a782e93f3fb63a428383706065e3ec2828e9
+  GIT binary patch
+  literal 5
+  Mc${NkWMbw50018V5dZ)H
+  
+
+  $ hg diff --git --binary --text -r 0 -r 1
+  diff --git a/binfile.bin b/binfile.bin
+  --- a/binfile.bin
+  +++ b/binfile.bin
+  @@ -1,1 +1,1 @@
+  -\x00\x01\x02\x03 (esc)
+  \ No newline at end of file
+  +\x00\x01\x02\x03 (esc)
+
   $ cd ..
--- a/tests/test-help.t	Wed Apr 05 15:15:06 2017 -0700
+++ b/tests/test-help.t	Wed Apr 05 15:31:08 2017 -0700
@@ -545,6 +545,7 @@
    -c --change REV          change made by revision
    -a --text                treat all files as text
    -g --git                 use git extended diff format
+      --binary              generate binary diffs in git mode (default)
       --nodates             omit dates from diff headers
       --noprefix            omit a/ and b/ prefixes from filenames
    -p --show-function       show which function each change is in