Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/patch.py @ 7151:b5bc5293021c
patch: change functions definition order for readability
author | Patrick Mezard <pmezard@gmail.com> |
---|---|
date | Sat, 18 Oct 2008 23:45:45 +0200 |
parents | 6d1d61bb2984 |
children | f0055cec8446 |
comparison
equal
deleted
inserted
replaced
7150:6d1d61bb2984 | 7151:b5bc5293021c |
---|---|
226 | 226 |
227 if not gitpatches: | 227 if not gitpatches: |
228 dopatch = GP_PATCH | 228 dopatch = GP_PATCH |
229 | 229 |
230 return (dopatch, gitpatches) | 230 return (dopatch, gitpatches) |
231 | |
232 def patch(patchname, ui, strip=1, cwd=None, files={}): | |
233 """apply <patchname> to the working directory. | |
234 returns whether patch was applied with fuzz factor.""" | |
235 patcher = ui.config('ui', 'patch') | |
236 args = [] | |
237 try: | |
238 if patcher: | |
239 return externalpatch(patcher, args, patchname, ui, strip, cwd, | |
240 files) | |
241 else: | |
242 try: | |
243 return internalpatch(patchname, ui, strip, cwd, files) | |
244 except NoHunks: | |
245 patcher = util.find_exe('gpatch') or util.find_exe('patch') | |
246 ui.debug(_('no valid hunks found; trying with %r instead\n') % | |
247 patcher) | |
248 if util.needbinarypatch(): | |
249 args.append('--binary') | |
250 return externalpatch(patcher, args, patchname, ui, strip, cwd, | |
251 files) | |
252 except PatchError, err: | |
253 s = str(err) | |
254 if s: | |
255 raise util.Abort(s) | |
256 else: | |
257 raise util.Abort(_('patch failed to apply')) | |
258 | |
259 def externalpatch(patcher, args, patchname, ui, strip, cwd, files): | |
260 """use <patcher> to apply <patchname> to the working directory. | |
261 returns whether patch was applied with fuzz factor.""" | |
262 | |
263 fuzz = False | |
264 if cwd: | |
265 args.append('-d %s' % util.shellquote(cwd)) | |
266 fp = util.popen('%s %s -p%d < %s' % (patcher, ' '.join(args), strip, | |
267 util.shellquote(patchname))) | |
268 | |
269 for line in fp: | |
270 line = line.rstrip() | |
271 ui.note(line + '\n') | |
272 if line.startswith('patching file '): | |
273 pf = util.parse_patch_output(line) | |
274 printed_file = False | |
275 files.setdefault(pf, (None, None)) | |
276 elif line.find('with fuzz') >= 0: | |
277 fuzz = True | |
278 if not printed_file: | |
279 ui.warn(pf + '\n') | |
280 printed_file = True | |
281 ui.warn(line + '\n') | |
282 elif line.find('saving rejects to file') >= 0: | |
283 ui.warn(line + '\n') | |
284 elif line.find('FAILED') >= 0: | |
285 if not printed_file: | |
286 ui.warn(pf + '\n') | |
287 printed_file = True | |
288 ui.warn(line + '\n') | |
289 code = fp.close() | |
290 if code: | |
291 raise PatchError(_("patch command failed: %s") % | |
292 util.explain_exit(code)[0]) | |
293 return fuzz | |
294 | |
295 def internalpatch(patchobj, ui, strip, cwd, files={}): | |
296 """use builtin patch to apply <patchobj> to the working directory. | |
297 returns whether patch was applied with fuzz factor.""" | |
298 try: | |
299 fp = file(patchobj, 'rb') | |
300 except TypeError: | |
301 fp = patchobj | |
302 if cwd: | |
303 curdir = os.getcwd() | |
304 os.chdir(cwd) | |
305 try: | |
306 ret = applydiff(ui, fp, files, strip=strip) | |
307 finally: | |
308 if cwd: | |
309 os.chdir(curdir) | |
310 if ret < 0: | |
311 raise PatchError | |
312 return ret > 0 | |
313 | 231 |
314 # @@ -start,len +start,len @@ or @@ -start +start @@ if len is 1 | 232 # @@ -start,len +start,len @@ or @@ -start +start @@ if len is 1 |
315 unidesc = re.compile('@@ -(\d+)(,(\d+))? \+(\d+)(,(\d+))? @@') | 233 unidesc = re.compile('@@ -(\d+)(,(\d+))? \+(\d+)(,(\d+))? @@') |
316 contextdesc = re.compile('(---|\*\*\*) (\d+)(,(\d+))? (---|\*\*\*)') | 234 contextdesc = re.compile('(---|\*\*\*) (\d+)(,(\d+))? (---|\*\*\*)') |
317 | 235 |
1116 cmdutil.addremove(repo, cfiles) | 1034 cmdutil.addremove(repo, cfiles) |
1117 files = patches.keys() | 1035 files = patches.keys() |
1118 files.extend([r for r in removes if r not in files]) | 1036 files.extend([r for r in removes if r not in files]) |
1119 return util.sort(files) | 1037 return util.sort(files) |
1120 | 1038 |
1039 def externalpatch(patcher, args, patchname, ui, strip, cwd, files): | |
1040 """use <patcher> to apply <patchname> to the working directory. | |
1041 returns whether patch was applied with fuzz factor.""" | |
1042 | |
1043 fuzz = False | |
1044 if cwd: | |
1045 args.append('-d %s' % util.shellquote(cwd)) | |
1046 fp = util.popen('%s %s -p%d < %s' % (patcher, ' '.join(args), strip, | |
1047 util.shellquote(patchname))) | |
1048 | |
1049 for line in fp: | |
1050 line = line.rstrip() | |
1051 ui.note(line + '\n') | |
1052 if line.startswith('patching file '): | |
1053 pf = util.parse_patch_output(line) | |
1054 printed_file = False | |
1055 files.setdefault(pf, (None, None)) | |
1056 elif line.find('with fuzz') >= 0: | |
1057 fuzz = True | |
1058 if not printed_file: | |
1059 ui.warn(pf + '\n') | |
1060 printed_file = True | |
1061 ui.warn(line + '\n') | |
1062 elif line.find('saving rejects to file') >= 0: | |
1063 ui.warn(line + '\n') | |
1064 elif line.find('FAILED') >= 0: | |
1065 if not printed_file: | |
1066 ui.warn(pf + '\n') | |
1067 printed_file = True | |
1068 ui.warn(line + '\n') | |
1069 code = fp.close() | |
1070 if code: | |
1071 raise PatchError(_("patch command failed: %s") % | |
1072 util.explain_exit(code)[0]) | |
1073 return fuzz | |
1074 | |
1075 def internalpatch(patchobj, ui, strip, cwd, files={}): | |
1076 """use builtin patch to apply <patchobj> to the working directory. | |
1077 returns whether patch was applied with fuzz factor.""" | |
1078 try: | |
1079 fp = file(patchobj, 'rb') | |
1080 except TypeError: | |
1081 fp = patchobj | |
1082 if cwd: | |
1083 curdir = os.getcwd() | |
1084 os.chdir(cwd) | |
1085 try: | |
1086 ret = applydiff(ui, fp, files, strip=strip) | |
1087 finally: | |
1088 if cwd: | |
1089 os.chdir(curdir) | |
1090 if ret < 0: | |
1091 raise PatchError | |
1092 return ret > 0 | |
1093 | |
1094 def patch(patchname, ui, strip=1, cwd=None, files={}): | |
1095 """apply <patchname> to the working directory. | |
1096 returns whether patch was applied with fuzz factor.""" | |
1097 patcher = ui.config('ui', 'patch') | |
1098 args = [] | |
1099 try: | |
1100 if patcher: | |
1101 return externalpatch(patcher, args, patchname, ui, strip, cwd, | |
1102 files) | |
1103 else: | |
1104 try: | |
1105 return internalpatch(patchname, ui, strip, cwd, files) | |
1106 except NoHunks: | |
1107 patcher = util.find_exe('gpatch') or util.find_exe('patch') | |
1108 ui.debug(_('no valid hunks found; trying with %r instead\n') % | |
1109 patcher) | |
1110 if util.needbinarypatch(): | |
1111 args.append('--binary') | |
1112 return externalpatch(patcher, args, patchname, ui, strip, cwd, | |
1113 files) | |
1114 except PatchError, err: | |
1115 s = str(err) | |
1116 if s: | |
1117 raise util.Abort(s) | |
1118 else: | |
1119 raise util.Abort(_('patch failed to apply')) | |
1120 | |
1121 def b85diff(to, tn): | 1121 def b85diff(to, tn): |
1122 '''print base85-encoded binary diff''' | 1122 '''print base85-encoded binary diff''' |
1123 def gitindex(text): | 1123 def gitindex(text): |
1124 if not text: | 1124 if not text: |
1125 return '0' * 40 | 1125 return '0' * 40 |