[PATCH 4 of 8] diff: Move index header generation to patch

Guillermo Pérez bisho at fb.com
Tue Nov 13 16:25:37 CST 2012


# HG changeset patch
# User Guillermo Pérez <bisho at fb.com>
# Date 1352310712 28800
# Node ID 5eb067d51f97cf66845d9d76555f2b0570228d4d
# Parent  6dde23de905ecf20b64487b28e8b69cbce6c0b77
diff: Move index header generation to patch

In an upcoming patch, we will add index information to all git diffs, not
only binary diffs, so this code needs to be moved to a more appropriate
place.

Also, since this information is used for patch headers, it makes more
sense to be in the patch module, along with other patch-related metadata.

diff --git a/mercurial/mdiff.py b/mercurial/mdiff.py
--- a/mercurial/mdiff.py
+++ b/mercurial/mdiff.py
@@ -8,7 +8,6 @@
 from i18n import _
 import bdiff, mpatch, util
 import re, struct, base85, zlib
-from node import hex, nullid
 
 def splitnewlines(text):
     '''like str.splitlines, but only split on newlines.'''
@@ -301,14 +300,6 @@
 
 def b85diff(to, tn):
     '''print base85-encoded binary diff'''
-    def gitindex(text):
-        if not text:
-            return hex(nullid)
-        l = len(text)
-        s = util.sha1('blob %d\0' % l)
-        s.update(text)
-        return s.hexdigest()
-
     def fmtline(line):
         l = len(line)
         if l <= 26:
@@ -324,17 +315,22 @@
             yield text[i:i + csize]
             i += csize
 
-    tohash = gitindex(to)
-    tnhash = gitindex(tn)
-    if tohash == tnhash:
-        return ""
+    if to is None:
+        to = ''
+    if tn is None:
+        tn = ''
+
+    if to == tn:
+        return ''
 
     # TODO: deltas
-    ret = ['index %s..%s\nGIT binary patch\nliteral %s\n' %
-           (tohash, tnhash, len(tn))]
+    ret = []
+    ret.append('GIT binary patch\n')
+    ret.append('literal %s\n' % len(tn))
     for l in chunk(zlib.compress(tn)):
         ret.append(fmtline(l))
     ret.append('\n')
+
     return ''.join(ret)
 
 def patchtext(bin):
diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -10,7 +10,7 @@
 import tempfile, zlib, shutil
 
 from i18n import _
-from node import hex, short
+from node import hex, nullid, short
 import base85, mdiff, scmutil, util, diffhelpers, copies, encoding, error
 import context
 
@@ -1651,7 +1651,6 @@
     '''like diff(), but yields 2-tuples of (output, label) for ui.write()'''
     return difflabel(diff, *args, **kw)
 
-
 def trydiff(repo, revs, ctx1, ctx2, modified, added, removed,
             copy, getfilectx, opts, losedatafn, prefix):
 
@@ -1664,6 +1663,22 @@
             header.append('old mode %s\n' % omode)
             header.append('new mode %s\n' % nmode)
 
+    def addindexmeta(meta, revs):
+        if opts.git:
+            i = len(revs)
+            if i==2:
+                meta.append('index %s..%s\n' % tuple(revs))
+            elif i==3:
+                meta.append('index %s,%s..%s\n' % tuple(revs))
+
+    def gitindex(text):
+        if not text:
+            return hex(nullid)
+        l = len(text)
+        s = util.sha1('blob %d\0' % l)
+        s.update(text)
+        return s.hexdigest()
+
     def diffline(a, b, revs):
         if opts.git:
             line = 'diff --git a/%s b/%s\n' % (a, b)
@@ -1765,6 +1780,8 @@
                 header.insert(0, diffline(join(a), join(b), revs))
             if dodiff == 'binary':
                 text = mdiff.b85diff(to, tn)
+                if text:
+                    addindexmeta(header, [gitindex(to), gitindex(tn)])
             else:
                 text = mdiff.unidiff(to, date1,
                                     # ctx2 date may be dynamic


More information about the Mercurial-devel mailing list