Patchwork [1,of,7] trydiff: make filenames None when they don't exist

login
register
mail settings
Submitter Martin von Zweigbergk
Date Feb. 7, 2015, 1:13 a.m.
Message ID <b10f8481c86598ca1e44.1423271610@martinvonz.mtv.corp.google.com>
Download mbox | patch
Permalink /patch/7732/
State Accepted
Headers show

Comments

Martin von Zweigbergk - Feb. 7, 2015, 1:13 a.m.
# HG changeset patch
# User Martin von Zweigbergk <martinvonz@google.com>
# Date 1421995355 28800
#      Thu Jan 22 22:42:35 2015 -0800
# Node ID b10f8481c86598ca1e4401577e13bcd9ec65415c
# Parent  d1fcff9400c984c521a0af89bdf8eb192824ddd8
trydiff: make filenames None when they don't exist

f1 and f2 are currently set always set to some filename, even for
added or deleted files. Let's instead set them to None to indicate
that one side of the diff doesn't exist. This lets us use the filename
variables instead of the content variables and simplify a bit since
the empty string is not a valid filename. More importantly, it paves
the way for further simplifications.

Patch

diff -r d1fcff9400c9 -r b10f8481c865 mercurial/patch.py
--- a/mercurial/patch.py	Thu Jan 22 22:22:09 2015 -0800
+++ b/mercurial/patch.py	Thu Jan 22 22:42:35 2015 -0800
@@ -1785,6 +1785,7 @@ 
         f1, f2 = f, f
         if opts.git or losedatafn:
             if f in addedset:
+                f1 = None
                 flag2 = ctx2.flags(f)
                 if f in copy:
                     if opts.git:
@@ -1797,6 +1798,7 @@ 
                             copyop = 'copy'
                         content1 = getfilectx(f1, ctx1).data()
             elif f in removedset:
+                f2 = None
                 if opts.git:
                     # have we already reported a copy above?
                     if (f in copyto and copyto[f] in addedset
@@ -1815,25 +1817,24 @@ 
                 # copy/rename
                 f in copy or
                 # empty file creation
-                (content1 is None and not content2) or
+                (not f1 and not content2) or
                 # empty file deletion
-                (not content1 and content2 is None) or
+                (not content1 and not f2) or
                 # create with flags
-                (content1 is None and flag2) or
+                (not f1 and flag2) or
                 # change flags
-                (content1 is not None and content2 is not None and
-                 flag1 != flag2)):
+                (f1 and f2 and flag1 != flag2)):
                 losedatafn(f)
 
-        path1 = posixpath.join(prefix, f1)
-        path2 = posixpath.join(prefix, f2)
+        path1 = posixpath.join(prefix, f1 or f2)
+        path2 = posixpath.join(prefix, f2 or f1)
         header = []
         if opts.git:
             header.append('diff --git %s%s %s%s' %
                           (aprefix, path1, bprefix, path2))
-            if content1 is None: # added
+            if not f1: # added
                 header.append('new file mode %s' % gitmode[flag2])
-            elif content2 is None: # removed
+            elif not f2: # removed
                 header.append('deleted file mode %s' % gitmode[flag1])
             else:  # modified/copied/renamed
                 mode1, mode2 = gitmode[flag1], gitmode[flag2]