[PATCH 3 of 8] diff: Rewrite diffline

Guillermo Pérez bisho at fb.com
Tue Nov 13 22:25:36 UTC 2012


# HG changeset patch
# User Guillermo Pérez <bisho at fb.com>
# Date 1352307040 28800
# Node ID 6dde23de905ecf20b64487b28e8b69cbce6c0b77
# Parent  ebdfe79a0649f0fdb28734680c06be59bec7dd22
diff: Rewrite diffline

Modifications:
- Before, quiet mode produced not diffline header for mercurial
  as a side effect of not populating "revs", weird side effect
  and we will always need revs for git index header that will
  be added in upcoming patches, so now we just check ui.quiet
  from diffline directly.
- More readable, diffline format is a string with placeholders
  rather than appending to a list from many ifs.
- Args order swapped, since a, b are more important than revs
  that is only used on non-git format.
- Read opts from context

diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -1584,9 +1584,8 @@
         return []
 
     revs = None
-    if not repo.ui.quiet:
-        hexfunc = repo.ui.debugflag and hex or short
-        revs = [hexfunc(node) for node in [node1, node2] if node]
+    hexfunc = repo.ui.debugflag and hex or short
+    revs = [hexfunc(node) for node in [node1, node2] if node]
 
     copy = {}
     if opts.git or opts.upgrade:
@@ -1665,18 +1664,18 @@
             header.append('old mode %s\n' % omode)
             header.append('new mode %s\n' % nmode)
 
-    def diffline(revs, a, b, opts):
-        parts = ['diff']
+    def diffline(a, b, revs):
         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)
+            line = 'diff --git a/%s b/%s\n' % (a, b)
+        elif not repo.ui.quiet:
+            if revs:
+                revinfo = ' '.join(["-r %s" % rev for rev in revs])
+                line = 'diff %s %s\n' % (revinfo, a)
+            else:
+                line = 'diff %s\n' % a
         else:
-            parts.append(a)
-        return ' '.join(parts) + '\n'
+            line = ''
+        return line
 
     date1 = util.datestr(ctx1.date())
     man1 = ctx1.manifest()
@@ -1763,7 +1762,7 @@
 
         if dodiff:
             if opts.git or revs:
-                header.insert(0, diffline(revs, join(a), join(b), opts))
+                header.insert(0, diffline(join(a), join(b), revs))
             if dodiff == 'binary':
                 text = mdiff.b85diff(to, tn)
             else:


More information about the Mercurial-devel mailing list