[PATCH 1 of 5] diff: rewrite addmodehdr into addflagchangemeta

Guillermo Pérez bisho at fb.com
Wed Nov 21 17:28:13 UTC 2012


# HG changeset patch
# User Guillermo Pérez <bisho at fb.com>
# Date 1352320114 28800
# Node ID afc2d3d7a9aaac736efe291d5aff087743092467
# Parent  a1f94e2f5da2dee4e107eab035685285d9923999
diff: rewrite addmodehdr into addflagchangemeta

Mercurial handles flags rather than file modes, so it's better
to signal flag changes and let the meta handler function
adapt it to the needed header, file modes for git in particular
but might be different if other patch methods are implemented.

diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -1657,10 +1657,12 @@
     def join(f):
         return os.path.join(prefix, f)
 
-    def addmodehdr(header, omode, nmode):
-        if omode != nmode:
-            header.append('old mode %s\n' % omode)
-            header.append('new mode %s\n' % nmode)
+    gitmode = {'l': '120000', 'x': '100755', '': '100644'}
+
+    def addflagchangemeta(meta, fa, fb):
+        if opts.git:
+            meta.append('old mode %s\n' %  gitmode[fa])
+            meta.append('new mode %s\n' %  gitmode[fb])
 
     def addindexmeta(meta, revs):
         if opts.git:
@@ -1695,7 +1697,6 @@
     man1 = ctx1.manifest()
 
     gone = set()
-    gitmode = {'l': '120000', 'x': '100755', '': '100644'}
 
     copyto = dict([(v, k) for k, v in copy.items()])
 
@@ -1705,6 +1706,8 @@
     for f in sorted(modified + added + removed):
         to = None
         tn = None
+        oflag = None
+        nflag = None
         dodiff = True
         header = []
         if f in man1:
@@ -1714,15 +1717,14 @@
         a, b = f, f
         if opts.git or losedatafn:
             if f in added:
-                mode = gitmode[ctx2.flags(f)]
+                nflag = ctx2.flags(f)
                 if f in copy or f in copyto:
                     if opts.git:
                         if f in copy:
                             a = copy[f]
                         else:
                             a = copyto[f]
-                        omode = gitmode[man1.flags(a)]
-                        addmodehdr(header, omode, mode)
+                        oflag = man1.flags(a)
                         if a in removed and a not in gone:
                             op = 'rename'
                             gone.add(a)
@@ -1735,7 +1737,7 @@
                         losedatafn(f)
                 else:
                     if opts.git:
-                        header.append('new file mode %s\n' % mode)
+                        header.append('new file mode %s\n' % gitmode[nflag])
                     elif ctx2.flags(f):
                         losedatafn(f)
                 # In theory, if tn was copied or renamed we should check
@@ -1768,12 +1770,13 @@
                 nflag = ctx2.flags(f)
                 binary = util.binary(to) or util.binary(tn)
                 if opts.git:
-                    addmodehdr(header, gitmode[oflag], gitmode[nflag])
                     if binary:
                         dodiff = 'binary'
                 elif binary or nflag != oflag:
                     losedatafn(f)
 
+        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))
diff --git a/tests/test-git-export.t b/tests/test-git-export.t
--- a/tests/test-git-export.t
+++ b/tests/test-git-export.t
@@ -75,10 +75,10 @@
   $ hg ci -mrenamemod
   $ hg diff --git -r 6:tip
   diff --git a/src b/dst
+  rename from src
+  rename to dst
   old mode 100755
   new mode 100644
-  rename from src
-  rename to dst
   --- a/src
   +++ b/dst
   @@ -3,3 +3,4 @@


More information about the Mercurial-devel mailing list