##// END OF EJS Templates
patch: pass an opener to patchfile
Patrick Mezard -
r7391:27d304c8 default
parent child Browse files
Show More
@@ -487,10 +487,10 b' def reposetup(ui, repo):'
487 del wlock, lock
487 del wlock, lock
488
488
489 # monkeypatches
489 # monkeypatches
490 def kwpatchfile_init(orig, self, ui, fname, missing=False):
490 def kwpatchfile_init(orig, self, ui, fname, opener, missing=False):
491 '''Monkeypatch/wrap patch.patchfile.__init__ to avoid
491 '''Monkeypatch/wrap patch.patchfile.__init__ to avoid
492 rejects or conflicts due to expanded keywords in working dir.'''
492 rejects or conflicts due to expanded keywords in working dir.'''
493 orig(self, ui, fname, missing)
493 orig(self, ui, fname, opener, missing)
494 # shrink keywords read from working dir
494 # shrink keywords read from working dir
495 self.lines = kwt.shrinklines(self.fname, self.lines)
495 self.lines = kwt.shrinklines(self.fname, self.lines)
496
496
@@ -228,27 +228,24 b" unidesc = re.compile('@@ -(\\d+)(,(\\d+))?"
228 contextdesc = re.compile('(---|\*\*\*) (\d+)(,(\d+))? (---|\*\*\*)')
228 contextdesc = re.compile('(---|\*\*\*) (\d+)(,(\d+))? (---|\*\*\*)')
229
229
230 class patchfile:
230 class patchfile:
231 def __init__(self, ui, fname, missing=False):
231 def __init__(self, ui, fname, opener, missing=False):
232 self.fname = fname
232 self.fname = fname
233 self.opener = opener
233 self.ui = ui
234 self.ui = ui
234 self.lines = []
235 self.lines = []
235 self.exists = False
236 self.exists = False
236 self.missing = missing
237 self.missing = missing
237 if not missing:
238 if not missing:
238 try:
239 try:
239 fp = file(fname, 'rb')
240 fp = self.opener(fname, 'r')
240 self.lines = fp.readlines()
241 self.lines = fp.readlines()
242 fp.close()
241 self.exists = True
243 self.exists = True
242 except IOError:
244 except IOError:
243 pass
245 pass
244 else:
246 else:
245 self.ui.warn(_("unable to find '%s' for patching\n") % self.fname)
247 self.ui.warn(_("unable to find '%s' for patching\n") % self.fname)
246
248
247 if not self.exists:
248 dirname = os.path.dirname(fname)
249 if dirname and not os.path.isdir(dirname):
250 os.makedirs(dirname)
251
252 self.hash = {}
249 self.hash = {}
253 self.dirty = 0
250 self.dirty = 0
254 self.offset = 0
251 self.offset = 0
@@ -307,32 +304,25 b' class patchfile:'
307 self.ui.warn(
304 self.ui.warn(
308 _("%d out of %d hunks FAILED -- saving rejects to file %s\n") %
305 _("%d out of %d hunks FAILED -- saving rejects to file %s\n") %
309 (len(self.rej), self.hunks, fname))
306 (len(self.rej), self.hunks, fname))
310 fp = file(fname, 'wb')
311 base = os.path.basename(self.fname)
307 base = os.path.basename(self.fname)
308 fp = self.opener(fname, 'w')
312 fp.write("--- %s\n+++ %s\n" % (base, base))
309 fp.write("--- %s\n+++ %s\n" % (base, base))
313 for x in self.rej:
310 for x in self.rej:
314 for l in x.hunk:
311 for l in x.hunk:
315 fp.write(l)
312 fp.write(l)
316 if l[-1] != '\n':
313 if l[-1] != '\n':
317 fp.write("\n\ No newline at end of file\n")
314 fp.write("\n\ No newline at end of file\n")
315 fp.close()
318
316
319 def write(self, dest=None):
317 def write(self, dest=None):
320 if self.dirty:
318 if not self.dirty:
321 if not dest:
319 return
322 dest = self.fname
320 if not dest:
323 st = None
321 dest = self.fname
324 try:
322 fp = self.opener(dest, 'w')
325 st = os.lstat(dest)
323 for l in self.lines:
326 except OSError, inst:
324 fp.write(l)
327 if inst.errno != errno.ENOENT:
325 fp.close()
328 raise
329 if st and st.st_nlink > 1:
330 os.unlink(dest)
331 fp = file(dest, 'wb')
332 if st and st.st_nlink > 1:
333 os.chmod(dest, st.st_mode)
334 fp.writelines(self.lines)
335 fp.close()
336
326
337 def close(self):
327 def close(self):
338 self.write()
328 self.write()
@@ -935,6 +925,7 b' def applydiff(ui, fp, changed, strip=1, '
935 err = 0
925 err = 0
936 current_file = None
926 current_file = None
937 gitpatches = None
927 gitpatches = None
928 opener = util.opener(os.getcwd())
938
929
939 def closefile():
930 def closefile():
940 if not current_file:
931 if not current_file:
@@ -957,11 +948,11 b' def applydiff(ui, fp, changed, strip=1, '
957 afile, bfile, first_hunk = values
948 afile, bfile, first_hunk = values
958 try:
949 try:
959 if sourcefile:
950 if sourcefile:
960 current_file = patchfile(ui, sourcefile)
951 current_file = patchfile(ui, sourcefile, opener)
961 else:
952 else:
962 current_file, missing = selectfile(afile, bfile, first_hunk,
953 current_file, missing = selectfile(afile, bfile, first_hunk,
963 strip, reverse)
954 strip, reverse)
964 current_file = patchfile(ui, current_file, missing)
955 current_file = patchfile(ui, current_file, opener, missing)
965 except PatchError, err:
956 except PatchError, err:
966 ui.warn(str(err) + '\n')
957 ui.warn(str(err) + '\n')
967 current_file, current_hunk = None, None
958 current_file, current_hunk = None, None
General Comments 0
You need to be logged in to leave comments. Login now