[PATCH stable] diff: use binary diff when copy source is binary

Martin von Zweigbergk martinvonz at google.com
Mon Jan 19 06:16:55 UTC 2015


# HG changeset patch
# User Martin von Zweigbergk <martinvonz at google.com>
# Date 1421535821 28800
#      Sat Jan 17 15:03:41 2015 -0800
# Node ID 7e444c8e80c1dbee2c145ff7022547ac10d7a56e
# Parent  049a9e3a078d7c988cb12ed456aad6ec2779ea69
diff: use binary diff when copy source is binary

When a binary source has been copied or renamed into a non-binary
file, we don't check whether the copy source was binary. There is a
code comment explaining that a git mode will be forced anyway in order
to capture the copy record (i.e. losedatafn() will be called). This is
true, but forcing git mode is not the only effect binary files have:
when git mode was already requested, we use the binary-ness to tell us
whether to use a regular unified diff or a git binary diff. The user
sees this as a "Binary file $file has changed" instead of the binary
diff in the output from "hg diff --git" when the copy source is binary
and the copy target is not.

Fix by also checking whether the copy source is binary. Note that
util.binary() is safe to call with None (which 'to' would be in the
non-copy/non-rename plain addition case).

diff -r 049a9e3a078d -r 7e444c8e80c1 mercurial/patch.py
--- a/mercurial/patch.py	Wed Jan 14 22:40:39 2015 -0500
+++ b/mercurial/patch.py	Sat Jan 17 15:03:41 2015 -0800
@@ -1831,10 +1831,7 @@
                         header.append('new file mode %s\n' % mode)
                     elif ctx2.flags(f):
                         losedatafn(f)
-                # In theory, if tn was copied or renamed we should check
-                # if the source is binary too but the copy record already
-                # forces git mode.
-                if util.binary(tn):
+                if util.binary(to) or util.binary(tn):
                     if opts.git:
                         binarydiff = True
                     else:
diff -r 049a9e3a078d -r 7e444c8e80c1 tests/test-diff-binary-file.t
--- a/tests/test-diff-binary-file.t	Wed Jan 14 22:40:39 2015 -0500
+++ b/tests/test-diff-binary-file.t	Sat Jan 17 15:03:41 2015 -0800
@@ -9,6 +9,9 @@
 
   $ hg revert -r 0 binfile.bin
   $ hg ci -m 'revert binfile.bin'
+  $ hg cp binfile.bin nonbinfile
+  $ echo text > nonbinfile
+  $ hg ci -m 'make non-binary copy of binary file'
 
   $ hg diff --nodates -r 0 -r 1
   diff -r 48b371597640 -r acea2ab458c8 binfile.bin
@@ -41,4 +44,14 @@
   diff --git a/binfile.bin b/binfile.bin
   Binary file binfile.bin has changed
 
+  $ hg diff --git -r 2 -r 3
+  diff --git a/binfile.bin b/nonbinfile
+  copy from binfile.bin
+  copy to nonbinfile
+  index 37ba3d1c6f17137d9c5f5776fa040caf5fe73ff9..8e27be7d6154a1f68ea9160ef0e18691d20560dc
+  GIT binary patch
+  literal 5
+  Mc$_OqttjCF00uV!&;S4c
+  
+
   $ cd ..


More information about the Mercurial-devel mailing list