diff mercurial/patch.py @ 24845:8133494accf1 stable

record: edit patch of newly added files (issue4304) I tried to fix this issue in the past and had to revert the fix. This is a second attempt without the regression we found with the first one. record defines special headers (of file) as headers whose hunk are not shown to the user for editing, they are used to represent deleted, moved and new files. Since we want to authorize editing the patch of newly added file we make the newly added file with some content not special anymore. This entails that we have to save their content before applying the backup to be able to revert it if the patch does not apply properly. We reintroduce the test showing that newly added files can be edited and that their content is shown to the user.
author Laurent Charignon <lcharignon@fb.com>
date Thu, 23 Apr 2015 14:27:26 -0700
parents edf907bd8144
children 0ca8410ea345
line wrap: on
line diff
--- a/mercurial/patch.py	Thu Apr 23 16:59:11 2015 -0700
+++ b/mercurial/patch.py	Thu Apr 23 14:27:26 2015 -0700
@@ -820,9 +820,10 @@
     """
     diffgit_re = re.compile('diff --git a/(.*) b/(.*)$')
     diff_re = re.compile('diff -r .* (.*)$')
-    allhunks_re = re.compile('(?:index|new file|deleted file) ')
+    allhunks_re = re.compile('(?:index|deleted file) ')
     pretty_re = re.compile('(?:new file|deleted file) ')
-    special_re = re.compile('(?:index|new|deleted|copy|rename) ')
+    special_re = re.compile('(?:index|deleted|copy|rename) ')
+    newfile_re = re.compile('(?:new file)')
 
     def __init__(self, header):
         self.header = header
@@ -870,8 +871,21 @@
     def __repr__(self):
         return '<header %s>' % (' '.join(map(repr, self.files())))
 
+    def isnewfile(self):
+        return util.any(self.newfile_re.match(h) for h in self.header)
+
     def special(self):
-        return util.any(self.special_re.match(h) for h in self.header)
+        # Special files are shown only at the header level and not at the hunk
+        # level for example a file that has been deleted is a special file.
+        # The user cannot change the content of the operation, in the case of
+        # the deleted file he has to take the deletion or not take it, he
+        # cannot take some of it.
+        # Newly added files are special if they are empty, they are not special
+        # if they have some content as we want to be able to change it
+        nocontent = len(self.header) == 2
+        emptynewfile = self.isnewfile() and nocontent
+        return emptynewfile or \
+                util.any(self.special_re.match(h) for h in self.header)
 
 class recordhunk(object):
     """patch hunk