[Pkg-bazaar-commits] ./bzr/unstable r278: - Better workaround for trailing newlines in diffs

Martin Pool mbp at sourcefrog.net
Fri Apr 10 07:51:38 UTC 2009


------------------------------------------------------------
revno: 278
committer: Martin Pool <mbp at sourcefrog.net>
timestamp: Fri 2005-04-15 19:33:28 +1000
message:
  - Better workaround for trailing newlines in diffs
modified:
  NEWS
  bzrlib/commands.py
-------------- next part --------------
=== modified file 'NEWS'
--- a/NEWS	2005-04-15 08:51:11 +0000
+++ b/NEWS	2005-04-15 09:33:28 +0000
@@ -35,6 +35,9 @@
 
     * Some Unicode/locale fixes.
 
+    * Partial workaround for difflib.unified_diff not handling
+      trailing newlines properly.
+
 
   INTERNAL:
 

=== modified file 'bzrlib/commands.py'
--- a/bzrlib/commands.py	2005-04-15 09:02:18 +0000
+++ b/bzrlib/commands.py	2005-04-15 09:33:28 +0000
@@ -374,6 +374,8 @@
 
     TODO: Selected-file diff is inefficient and doesn't show you
           deleted files.
+
+    TODO: This probably handles non-Unix newlines poorly.
 """
 
     ## TODO: Shouldn't be in the cmd function.
@@ -420,13 +422,37 @@
         # with newly-added files.
 
         def diffit(oldlines, newlines, **kw):
+            
             # FIXME: difflib is wrong if there is no trailing newline.
+            # The syntax used by patch seems to be "\ No newline at
+            # end of file" following the last diff line from that
+            # file.  This is not trivial to insert into the
+            # unified_diff output and it might be better to just fix
+            # or replace that function.
+
+            # In the meantime we at least make sure the patch isn't
+            # mangled.
+            
 
             # Special workaround for Python2.3, where difflib fails if
             # both sequences are empty.
-            if oldlines or newlines:
-                sys.stdout.writelines(difflib.unified_diff(oldlines, newlines, **kw))
-            print
+            if not oldlines and not newlines:
+                return
+
+            nonl = False
+
+            if oldlines and (oldlines[-1][-1] != '\n'):
+                oldlines[-1] += '\n'
+                nonl = True
+            if newlines and (newlines[-1][-1] != '\n'):
+                newlines[-1] += '\n'
+                nonl = True
+
+            ud = difflib.unified_diff(oldlines, newlines, **kw)
+            sys.stdout.writelines(ud)
+            if nonl:
+                print "\\ No newline at end of file"
+            sys.stdout.write('\n')
         
         if file_state in ['.', '?', 'I']:
             continue



More information about the Pkg-bazaar-commits mailing list