Patchwork [1,of,2] patch: unify check_binary and binary flags

login
register
mail settings
Submitter Yuya Nishihara
Date Feb. 4, 2018, 11:17 a.m.
Message ID <d41b22b06360ceee3a9e.1517743070@mimosa>
Download mbox | patch
Permalink /patch/27277/
State Accepted
Headers show

Comments

Yuya Nishihara - Feb. 4, 2018, 11:17 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1517707683 -32400
#      Sun Feb 04 10:28:03 2018 +0900
# Node ID d41b22b06360ceee3a9e6e66df5f57f267318314
# Parent  a9802c9ecfb5aa20d89480763ae15b03f78f3a88
patch: unify check_binary and binary flags

Follows up 079b27b5a869. If opts.text=True, check_binary is ignored, so we
can just pass the binary flag to unidiff().

perfunidiff now takes any inputs as text files, which I think is a desired
behavior.

Patch

diff --git a/contrib/perf.py b/contrib/perf.py
--- a/contrib/perf.py
+++ b/contrib/perf.py
@@ -1088,7 +1088,7 @@  def perfunidiff(ui, repo, file_, rev=Non
         for left, right in textpairs:
             # The date strings don't matter, so we pass empty strings.
             headerlines, hunks = mdiff.unidiff(
-                left, '', right, '', 'left', 'right')
+                left, '', right, '', 'left', 'right', binary=False)
             # consume iterators in roughly the way patch.py does
             b'\n'.join(headerlines)
             b''.join(sum((list(hlines) for hrange, hlines in hunks), []))
diff --git a/mercurial/mdiff.py b/mercurial/mdiff.py
--- a/mercurial/mdiff.py
+++ b/mercurial/mdiff.py
@@ -236,7 +236,7 @@  def allblocks(text1, text2, opts=None, l
             yield s, type
         yield s1, '='
 
-def unidiff(a, ad, b, bd, fn1, fn2, opts=defaultopts, check_binary=True):
+def unidiff(a, ad, b, bd, fn1, fn2, binary, opts=defaultopts):
     """Return a unified diff as a (headers, hunks) tuple.
 
     If the diff is not null, `headers` is a list with unified diff header
@@ -244,8 +244,7 @@  def unidiff(a, ad, b, bd, fn1, fn2, opts
     (hunkrange, hunklines) coming from _unidiff().
     Otherwise, `headers` and `hunks` are empty.
 
-    Setting `check_binary` to false will skip the binary check, i.e. when
-    it has been done in advance. Files are expected to be text in this case.
+    Set binary=True if either a or b should be taken as a binary file.
     """
     def datetag(date, fn=None):
         if not opts.git and not opts.nodates:
@@ -269,7 +268,7 @@  def unidiff(a, ad, b, bd, fn1, fn2, opts
     fn1 = util.pconvert(fn1)
     fn2 = util.pconvert(fn2)
 
-    if not opts.text and check_binary and (util.binary(a) or util.binary(b)):
+    if binary:
         if a and b and len(a) == len(b) and a == b:
             return sentinel
         headerlines = []
diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -2699,12 +2699,9 @@  def trydiff(repo, revs, ctx1, ctx2, modi
                 flag2 = ctx2.flags(f2)
         # if binary is True, output "summary" or "base85", but not "text diff"
         if opts.text:
-            check_binary = True
             binary = False
         else:
-            check_binary = any(f.isbinary()
-                               for f in [fctx1, fctx2] if f is not None)
-            binary = check_binary
+            binary = any(f.isbinary() for f in [fctx1, fctx2] if f is not None)
 
         if losedatafn and not opts.git:
             if (binary or
@@ -2794,8 +2791,8 @@  def trydiff(repo, revs, ctx1, ctx2, modi
 
             uheaders, hunks = mdiff.unidiff(content1, date1,
                                             content2, date2,
-                                            path1, path2, opts=opts,
-                                            check_binary=check_binary)
+                                            path1, path2,
+                                            binary=binary, opts=opts)
             header.extend(uheaders)
         yield fctx1, fctx2, header, hunks