[PATCH 5 of 5 V2] diff: add a fast path to avoid loading binary contents

Jun Wu quark at fb.com
Fri May 5 21:27:22 EDT 2017


# HG changeset patch
# User Jun Wu <quark at fb.com>
# Date 1493880641 25200
#      Wed May 03 23:50:41 2017 -0700
# Node ID 3ca1eefa3e97630313da6b719b4882e38d4f1f0e
# Parent  43b7e046f5e1b36041c8ce124a27e5c5f3ba27fd
# Available At https://bitbucket.org/quark-zju/hg-draft
#              hg pull https://bitbucket.org/quark-zju/hg-draft -r 3ca1eefa3e97
diff: add a fast path to avoid loading binary contents

When diffing binary contents, with certain configs, we can show
"Binary file <name> has changed" without actual content.

That allows a fast path where we could avoid providing actual binary
contents. Note: in that case we still need to test if two contents are the
same, that's done by using "filectx.cmp", which could have its own fast
path.

diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -2533,10 +2533,8 @@ def trydiff(repo, revs, ctx1, ctx2, modi
         if f1:
             fctx1 = getfilectx(f1, ctx1)
-            content1 = fctx1.data()
             if opts.git or losedatafn:
                 flag1 = ctx1.flags(f1)
         if f2:
             fctx2 = getfilectx(f2, ctx2)
-            content2 = fctx2.data()
             if opts.git or losedatafn:
                 flag2 = ctx2.flags(f2)
@@ -2596,4 +2594,23 @@ def trydiff(repo, revs, ctx1, ctx2, modi
         #  no       | *    *        *   *     | text diff | yes
         # [1]: hash(fctx.data()) is outputted. so fctx.data() cannot be faked
+        if binary and (not opts.git or (opts.git and opts.nobinary and not
+                                        opts.index)):
+            # fast path: no binary content will be displayed, content1 and
+            # content2 are only used for equivalent test. cmp() could have a
+            # fast path.
+            if fctx1 is not None:
+                content1 = b'\0'
+            if fctx2 is not None:
+                if fctx1 is not None and not fctx1.cmp(fctx2):
+                    content2 = b'\0' # not different
+                else:
+                    content2 = b'\0\0'
+        else:
+            # normal path: load contents
+            if fctx1 is not None:
+                content1 = fctx1.data()
+            if fctx2 is not None:
+                content2 = fctx2.data()
+
         if binary and opts.git and not opts.nobinary:
             text = mdiff.b85diff(content1, content2)


More information about the Mercurial-devel mailing list