[PATCH 6 of 8] diff: Convert all headers into add*meta functions

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


# HG changeset patch
# User Guillermo Pérez <bisho at fb.com>
# Date 1352618498 28800
# Node ID 66b03c1194b664fee9ddd51d6cead74292a6500d
# Parent  0abfa49cc51122fddab754fe327c89e34dc0a18e
diff: Convert all headers into add*meta functions

We convert the remaining appended headers to the equivalent
add<sometype>meta functions.

diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -1660,18 +1660,36 @@
     ''' Helper header functions '''
     gitmode = {'l': '120000', 'x': '100755', '': '100644'}
 
+    def addrenamemeta(meta, a, b):
+        if opts.git:
+            meta.append('rename from %s' % a)
+            meta.append('rename to %s' % b)
+
+    def addcopymeta(meta, a, b):
+        if opts.git:
+            meta.append('copy from %s' % a)
+            meta.append('copy to %s' % b)
+
+    def addfileaddedmeta(meta, flag):
+        if opts.git:
+            meta.append('new file mode %s' % gitmode[flag])
+
+    def addfiledeletedmeta(meta, flag):
+        if opts.git:
+            meta.append('deleted file mode %s' % gitmode[flag])
+
     def addflagchangemeta(meta, fa, fb):
         if opts.git:
-            meta.append('old mode %s\n' %  gitmode[fa])
-            meta.append('new mode %s\n' %  gitmode[fb])
+            meta.append('old mode %s' %  gitmode[fa])
+            meta.append('new mode %s' %  gitmode[fb])
 
     def addindexmeta(meta, revs):
         if opts.git:
             i = len(revs)
             if i==2:
-                meta.append('index %s..%s\n' % tuple(revs))
+                meta.append('index %s..%s' % tuple(revs))
             elif i==3:
-                meta.append('index %s,%s..%s\n' % tuple(revs))
+                meta.append('index %s,%s..%s' % tuple(revs))
 
     def gitindex(text):
         if not text:
@@ -1727,18 +1745,16 @@
                             a = copyto[f]
                         oflag = man1.flags(a)
                         if a in removed and a not in gone:
-                            op = 'rename'
+                            addrenamemeta(header, join(a), join(f))
                             gone.add(a)
                         else:
-                            op = 'copy'
-                        header.append('%s from %s\n' % (op, join(a)))
-                        header.append('%s to %s\n' % (op, join(f)))
+                            addcopymeta(header, join(a), join(f))
                         to = getfilectx(a, ctx1).data()
                     else:
                         losedatafn(f)
                 else:
                     if opts.git:
-                        header.append('new file mode %s\n' % gitmode[nflag])
+                        addfileaddedmeta(header, nflag)
                     elif ctx2.flags(f):
                         losedatafn(f)
                 # In theory, if tn was copied or renamed we should check
@@ -1761,8 +1777,7 @@
                          and copy[copyto[f]] == f)):
                         dodiff = False
                     else:
-                        header.append('deleted file mode %s\n' %
-                                      gitmode[man1.flags(f)])
+                        addfiledeletedmeta(header, man1.flags(f))
                 elif not to or util.binary(to):
                     # regular diffs cannot represent empty file deletion
                     losedatafn(f)
@@ -1779,8 +1794,6 @@
         if nflag is not None and oflag is not None and nflag != oflag:
             addflagchangemeta(header, oflag, nflag)
         if dodiff:
-            if opts.git or revs:
-                header.insert(0, diffline(join(a), join(b), revs))
             if dodiff == 'binary':
                 text = mdiff.b85diff(to, tn)
                 if text:
@@ -1790,8 +1803,12 @@
                                     # ctx2 date may be dynamic
                                     tn, util.datestr(ctx2.date()),
                                     join(a), join(b), opts=opts)
-            if header and (text or len(header) > 1):
-                yield ''.join(header)
+
+            if text or header:
+                if opts.git or revs:
+                    yield diffline(join(a), join(b), revs)
+                header.append('')
+                yield '\n'.join(header)
             if text:
                 yield text
 


More information about the Mercurial-devel mailing list