[PATCH 2 of 8] diff: Move diffline to patch module

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


# HG changeset patch
# User Guillermo Pérez <bisho at fb.com>
# Date 1352587507 28800
# Node ID ebdfe79a0649f0fdb28734680c06be59bec7dd22
# Parent  ea3d7cd2c934251e3e70870de647c1aea3640c9a
diff: Move diffline to patch module

diffline is not part of diff computation, so makes more sense to
place it with other header generation in patch module, and remove
the call to this from mdiff.unidiff so keeps just responsability
over diff generation.

In upcoming patches we will generalize this approach for
all headers added in the patch, including the git index
header.

diff --git a/mercurial/mdiff.py b/mercurial/mdiff.py
--- a/mercurial/mdiff.py
+++ b/mercurial/mdiff.py
@@ -143,20 +143,7 @@
             yield s, type
         yield s1, '='
 
-def diffline(revs, a, b, opts):
-    parts = ['diff']
-    if opts.git:
-        parts.append('--git')
-    if revs and not opts.git:
-        parts.append(' '.join(["-r %s" % rev for rev in revs]))
-    if opts.git:
-        parts.append('a/%s' % a)
-        parts.append('b/%s' % b)
-    else:
-        parts.append(a)
-    return ' '.join(parts) + '\n'
-
-def unidiff(a, ad, b, bd, fn1, fn2, r=None, opts=defaultopts):
+def unidiff(a, ad, b, bd, fn1, fn2, opts=defaultopts):
     def datetag(date, fn=None):
         if not opts.git and not opts.nodates:
             return '\t%s\n' % date
@@ -207,9 +194,6 @@
         if l[ln][-1] != '\n':
             l[ln] += "\n\ No newline at end of file\n"
 
-    if r:
-        l.insert(0, diffline(r, fn1, fn2, opts))
-
     return "".join(l)
 
 # creates a headerless unified diff
diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -1653,17 +1653,31 @@
     return difflabel(diff, *args, **kw)
 
 
-def _addmodehdr(header, omode, nmode):
-    if omode != nmode:
-        header.append('old mode %s\n' % omode)
-        header.append('new mode %s\n' % nmode)
-
 def trydiff(repo, revs, ctx1, ctx2, modified, added, removed,
             copy, getfilectx, opts, losedatafn, prefix):
 
     def join(f):
         return os.path.join(prefix, f)
 
+    ''' Helper header functions '''
+    def addmodehdr(header, omode, nmode):
+        if omode != nmode:
+            header.append('old mode %s\n' % omode)
+            header.append('new mode %s\n' % nmode)
+
+    def diffline(revs, a, b, opts):
+        parts = ['diff']
+        if opts.git:
+            parts.append('--git')
+        if revs and not opts.git:
+            parts.append(' '.join(["-r %s" % rev for rev in revs]))
+        if opts.git:
+            parts.append('a/%s' % a)
+            parts.append('b/%s' % b)
+        else:
+            parts.append(a)
+        return ' '.join(parts) + '\n'
+
     date1 = util.datestr(ctx1.date())
     man1 = ctx1.manifest()
 
@@ -1695,7 +1709,7 @@
                         else:
                             a = copyto[f]
                         omode = gitmode[man1.flags(a)]
-                        _addmodehdr(header, omode, mode)
+                        addmodehdr(header, omode, mode)
                         if a in removed and a not in gone:
                             op = 'rename'
                             gone.add(a)
@@ -1741,22 +1755,22 @@
                 nflag = ctx2.flags(f)
                 binary = util.binary(to) or util.binary(tn)
                 if opts.git:
-                    _addmodehdr(header, gitmode[oflag], gitmode[nflag])
+                    addmodehdr(header, gitmode[oflag], gitmode[nflag])
                     if binary:
                         dodiff = 'binary'
                 elif binary or nflag != oflag:
                     losedatafn(f)
-            if opts.git:
-                header.insert(0, mdiff.diffline(revs, join(a), join(b), opts))
 
         if dodiff:
+            if opts.git or revs:
+                header.insert(0, diffline(revs, join(a), join(b), opts))
             if dodiff == 'binary':
                 text = mdiff.b85diff(to, tn)
             else:
                 text = mdiff.unidiff(to, date1,
                                     # ctx2 date may be dynamic
                                     tn, util.datestr(ctx2.date()),
-                                    join(a), join(b), revs, opts=opts)
+                                    join(a), join(b), opts=opts)
             if header and (text or len(header) > 1):
                 yield ''.join(header)
             if text:


More information about the Mercurial-devel mailing list