changeset 5652:e90e72c6b4c7

patch: write rej files for missing targets (issue 853)
author Patrick Mezard <pmezard@gmail.com>
date Mon, 17 Dec 2007 23:42:46 +0100
parents e11940d84606
children 1b35bc1c1968
files mercurial/patch.py tests/test-mq-missingfiles tests/test-mq-missingfiles.out
diffstat 3 files changed, 67 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/patch.py	Mon Dec 17 23:06:04 2007 +0100
+++ b/mercurial/patch.py	Mon Dec 17 23:42:46 2007 +0100
@@ -302,14 +302,23 @@
 contextdesc = re.compile('(---|\*\*\*) (\d+)(,(\d+))? (---|\*\*\*)')
 
 class patchfile:
-    def __init__(self, ui, fname):
+    def __init__(self, ui, fname, missing=False):
         self.fname = fname
         self.ui = ui
-        try:
-            fp = file(fname, 'rb')
-            self.lines = fp.readlines()
-            self.exists = True
-        except IOError:
+        self.lines = []
+        self.exists = False
+        self.missing = missing
+        if not missing:
+            try:
+                fp = file(fname, 'rb')
+                self.lines = fp.readlines()
+                self.exists = True
+            except IOError:
+                pass
+        else:
+            self.ui.warn(_("unable to find '%s' for patching\n") % self.fname)
+
+        if not self.exists:
             dirname = os.path.dirname(fname)
             if dirname and not os.path.isdir(dirname):
                 dirs = dirname.split(os.path.sep)
@@ -318,8 +327,6 @@
                     d = os.path.join(d, x)
                     if not os.path.isdir(d):
                         os.mkdir(d)
-            self.lines = []
-            self.exists = False
 
         self.hash = {}
         self.dirty = 0
@@ -427,6 +434,10 @@
         if reverse:
             h.reverse()
 
+        if self.missing:
+            self.rej.append(h)
+            return -1
+
         if self.exists and h.createfile():
             self.ui.warn(_("file %s already exists\n") % self.fname)
             self.rej.append(h)
@@ -805,22 +816,23 @@
     createfunc = hunk.createfile
     if reverse:
         createfunc = hunk.rmfile
-    if not goodb and not gooda and not createfunc():
-        raise PatchError(_("unable to find %s or %s for patching") %
-                         (afile, bfile))
-    if gooda and goodb:
-        fname = bfile
-        if afile in bfile:
+    missing = not goodb and not gooda and not createfunc()
+    fname = None
+    if not missing:
+        if gooda and goodb:
+            fname = (afile in bfile) and afile or bfile
+        elif gooda:
             fname = afile
-    elif gooda:
-        fname = afile
-    elif not nullb:
-        fname = bfile
-        if afile in bfile:
+    
+    if not fname:
+        if not nullb:
+            fname = (afile in bfile) and afile or bfile
+        elif not nulla:
             fname = afile
-    elif not nulla:
-        fname = afile
-    return fname
+        else:
+            raise PatchError(_("undefined source and destination files"))
+        
+    return fname, missing
 
 class linereader:
     # simple class to allow pushing lines back into the input stream
@@ -1009,9 +1021,9 @@
                 if sourcefile:
                     current_file = patchfile(ui, sourcefile)
                 else:
-                    current_file = selectfile(afile, bfile, first_hunk,
+                    current_file, missing = selectfile(afile, bfile, first_hunk,
                                             strip, reverse)
-                    current_file = patchfile(ui, current_file)
+                    current_file = patchfile(ui, current_file, missing)
             except PatchError, err:
                 ui.warn(str(err) + '\n')
                 current_file, current_hunk = None, None
--- a/tests/test-mq-missingfiles	Mon Dec 17 23:06:04 2007 +0100
+++ b/tests/test-mq-missingfiles	Mon Dec 17 23:42:46 2007 +0100
@@ -41,6 +41,8 @@
 echo % display added files
 cat a
 cat c
+echo % display rejections
+cat b.rej
 cd ..
 
 
@@ -65,5 +67,7 @@
 echo % display added files
 cat a
 cat c
+echo % display rejections
+cat b.rej
 cd ..
 
--- a/tests/test-mq-missingfiles.out	Mon Dec 17 23:06:04 2007 +0100
+++ b/tests/test-mq-missingfiles.out	Mon Dec 17 23:42:46 2007 +0100
@@ -2,23 +2,48 @@
 Patch queue now empty
 % push patch with missing target
 applying changeb
-unable to find b or b for patching
+unable to find 'b' for patching
+2 out of 2 hunks FAILED -- saving rejects to file b.rej
 patch failed, unable to continue (try -v)
 patch failed, rejects left in working dir
 Errors during apply, please fix and refresh changeb
 % display added files
 a
 c
+% display rejections
+--- b
++++ b
+@@ -1,3 +1,5 @@ a
++b
++b
+ a
+ a
+ a
+@@ -8,3 +10,5 @@ a
+ a
+ a
+ a
++c
++c
 adding b
 Patch queue now empty
 % push git patch with missing target
 applying changeb
-unable to find b or b for patching
+unable to find 'b' for patching
+1 out of 1 hunk FAILED -- saving rejects to file b.rej
 patch failed, unable to continue (try -v)
 b: No such file or directory
 b not tracked!
 patch failed, rejects left in working dir
 Errors during apply, please fix and refresh changeb
+? b.rej
 % display added files
 a
 c
+% display rejections
+--- b
++++ b
+GIT binary patch
+literal 2
+Jc${No0000400IC2
+