comparison mercurial/patch.py @ 4923:59b8ff35c4ed

patch: patches should be read and written in binary mode when possible.
author Patrick Mezard <pmezard@gmail.com>
date Tue, 17 Jul 2007 23:35:24 +0200
parents 020ee9c781cf
children 4106dde15aed
comparison
equal deleted inserted replaced
4922:020ee9c781cf 4923:59b8ff35c4ed
279 return fuzz 279 return fuzz
280 280
281 def internalpatch(patchname, ui, strip, cwd, files): 281 def internalpatch(patchname, ui, strip, cwd, files):
282 """use builtin patch to apply <patchname> to the working directory. 282 """use builtin patch to apply <patchname> to the working directory.
283 returns whether patch was applied with fuzz factor.""" 283 returns whether patch was applied with fuzz factor."""
284 fp = file(patchname) 284 fp = file(patchname, 'rb')
285 if cwd: 285 if cwd:
286 curdir = os.getcwd() 286 curdir = os.getcwd()
287 os.chdir(cwd) 287 os.chdir(cwd)
288 try: 288 try:
289 ret = applydiff(ui, fp, files, strip=strip) 289 ret = applydiff(ui, fp, files, strip=strip)
301 class patchfile: 301 class patchfile:
302 def __init__(self, ui, fname): 302 def __init__(self, ui, fname):
303 self.fname = fname 303 self.fname = fname
304 self.ui = ui 304 self.ui = ui
305 try: 305 try:
306 fp = file(fname, 'r') 306 fp = file(fname, 'rb')
307 self.lines = fp.readlines() 307 self.lines = fp.readlines()
308 self.exists = True 308 self.exists = True
309 except IOError: 309 except IOError:
310 dirname = os.path.dirname(fname) 310 dirname = os.path.dirname(fname)
311 if dirname and not os.path.isdir(dirname): 311 if dirname and not os.path.isdir(dirname):
381 _("%d out of %d hunk%s FAILED -- saving rejects to file %s\n") % 381 _("%d out of %d hunk%s FAILED -- saving rejects to file %s\n") %
382 (len(self.rej), self.hunks, hunkstr, fname)) 382 (len(self.rej), self.hunks, hunkstr, fname))
383 try: os.unlink(fname) 383 try: os.unlink(fname)
384 except: 384 except:
385 pass 385 pass
386 fp = file(fname, 'w') 386 fp = file(fname, 'wb')
387 base = os.path.basename(self.fname) 387 base = os.path.basename(self.fname)
388 fp.write("--- %s\n+++ %s\n" % (base, base)) 388 fp.write("--- %s\n+++ %s\n" % (base, base))
389 for x in self.rej: 389 for x in self.rej:
390 for l in x.hunk: 390 for l in x.hunk:
391 fp.write(l) 391 fp.write(l)
400 try: 400 try:
401 st = os.lstat(dest) 401 st = os.lstat(dest)
402 if st.st_nlink > 1: 402 if st.st_nlink > 1:
403 os.unlink(dest) 403 os.unlink(dest)
404 except: pass 404 except: pass
405 fp = file(dest, 'w') 405 fp = file(dest, 'wb')
406 if st: 406 if st:
407 os.chmod(dest, st.st_mode) 407 os.chmod(dest, st.st_mode)
408 fp.writelines(self.lines) 408 fp.writelines(self.lines)
409 fp.close() 409 fp.close()
410 410