mercurial/patch.py
changeset 4965 4106dde15aed
parent 4961 126f527b3ba3
parent 4904 59b8ff35c4ed
child 5033 1b07668b8cc3
equal deleted inserted replaced
4964:ee983d0dbea8 4965:4106dde15aed
   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 
   775         pathlen = len(path)
   775         pathlen = len(path)
   776         i = 0
   776         i = 0
   777         if count == 0:
   777         if count == 0:
   778             return path.rstrip()
   778             return path.rstrip()
   779         while count > 0:
   779         while count > 0:
   780             i = path.find(os.sep, i)
   780             i = path.find('/', i)
   781             if i == -1:
   781             if i == -1:
   782                 raise PatchError(_("unable to strip away %d dirs from %s") %
   782                 raise PatchError(_("unable to strip away %d dirs from %s") %
   783                                  (count, path))
   783                                  (count, path))
   784             i += 1
   784             i += 1
   785             # consume '//' in the path
   785             # consume '//' in the path
   786             while i < pathlen - 1 and path[i] == os.sep:
   786             while i < pathlen - 1 and path[i] == '/':
   787                 i += 1
   787                 i += 1
   788             count -= 1
   788             count -= 1
   789         return path[i:].rstrip()
   789         return path[i:].rstrip()
   790 
   790 
   791     nulla = afile_orig == "/dev/null"
   791     nulla = afile_orig == "/dev/null"