changeset 7391:27d304c8cc03

patch: pass an opener to patchfile
author Patrick Mezard <pmezard@gmail.com>
date Wed, 19 Nov 2008 13:27:57 +0100
parents 0d1c770c6be1
children 564326a6ef9c
files hgext/keyword.py mercurial/patch.py
diffstat 2 files changed, 19 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/keyword.py	Tue Nov 18 13:20:55 2008 +0100
+++ b/hgext/keyword.py	Wed Nov 19 13:27:57 2008 +0100
@@ -487,10 +487,10 @@
                 del wlock, lock
 
     # monkeypatches
-    def kwpatchfile_init(orig, self, ui, fname, missing=False):
+    def kwpatchfile_init(orig, self, ui, fname, opener, missing=False):
         '''Monkeypatch/wrap patch.patchfile.__init__ to avoid
         rejects or conflicts due to expanded keywords in working dir.'''
-        orig(self, ui, fname, missing)
+        orig(self, ui, fname, opener, missing)
         # shrink keywords read from working dir
         self.lines = kwt.shrinklines(self.fname, self.lines)
 
--- a/mercurial/patch.py	Tue Nov 18 13:20:55 2008 +0100
+++ b/mercurial/patch.py	Wed Nov 19 13:27:57 2008 +0100
@@ -228,27 +228,24 @@
 contextdesc = re.compile('(---|\*\*\*) (\d+)(,(\d+))? (---|\*\*\*)')
 
 class patchfile:
-    def __init__(self, ui, fname, missing=False):
+    def __init__(self, ui, fname, opener, missing=False):
         self.fname = fname
+        self.opener = opener
         self.ui = ui
         self.lines = []
         self.exists = False
         self.missing = missing
         if not missing:
             try:
-                fp = file(fname, 'rb')
+                fp = self.opener(fname, 'r')
                 self.lines = fp.readlines()
+                fp.close()
                 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):
-                os.makedirs(dirname)
-
         self.hash = {}
         self.dirty = 0
         self.offset = 0
@@ -307,32 +304,25 @@
         self.ui.warn(
             _("%d out of %d hunks FAILED -- saving rejects to file %s\n") %
             (len(self.rej), self.hunks, fname))
-        fp = file(fname, 'wb')
         base = os.path.basename(self.fname)
+        fp = self.opener(fname, 'w')        
         fp.write("--- %s\n+++ %s\n" % (base, base))
         for x in self.rej:
             for l in x.hunk:
                 fp.write(l)
                 if l[-1] != '\n':
                     fp.write("\n\ No newline at end of file\n")
+        fp.close()
 
     def write(self, dest=None):
-        if self.dirty:
-            if not dest:
-                dest = self.fname
-            st = None
-            try:
-                st = os.lstat(dest)
-            except OSError, inst:
-                if inst.errno != errno.ENOENT:
-                    raise
-            if st and st.st_nlink > 1:
-                os.unlink(dest)
-            fp = file(dest, 'wb')
-            if st and st.st_nlink > 1:
-                os.chmod(dest, st.st_mode)
-            fp.writelines(self.lines)
-            fp.close()
+        if not self.dirty:
+            return
+        if not dest:
+            dest = self.fname
+        fp = self.opener(dest, 'w')
+        for l in self.lines:
+            fp.write(l)
+        fp.close()
 
     def close(self):
         self.write()
@@ -935,6 +925,7 @@
     err = 0
     current_file = None
     gitpatches = None
+    opener = util.opener(os.getcwd())
 
     def closefile():
         if not current_file:
@@ -957,11 +948,11 @@
             afile, bfile, first_hunk = values
             try:
                 if sourcefile:
-                    current_file = patchfile(ui, sourcefile)
+                    current_file = patchfile(ui, sourcefile, opener)
                 else:
                     current_file, missing = selectfile(afile, bfile, first_hunk,
                                             strip, reverse)
-                    current_file = patchfile(ui, current_file, missing)
+                    current_file = patchfile(ui, current_file, opener, missing)
             except PatchError, err:
                 ui.warn(str(err) + '\n')
                 current_file, current_hunk = None, None