##// END OF EJS Templates
patch: make extract() a context manager (API)...
Gregory Szorc -
r37639:5537d8f5 default
parent child Browse files
Show More
@@ -1379,12 +1379,12 b' def tryimportone(ui, repo, patchdata, pa'
1379 strip = opts["strip"]
1379 strip = opts["strip"]
1380 prefix = opts["prefix"]
1380 prefix = opts["prefix"]
1381 sim = float(opts.get('similarity') or 0)
1381 sim = float(opts.get('similarity') or 0)
1382
1382 if not tmpname:
1383 if not tmpname:
1383 return (None, None, False)
1384 return None, None, False
1384
1385
1385 rejects = False
1386 rejects = False
1386
1387
1387 try:
1388 cmdline_message = logmessage(ui, opts)
1388 cmdline_message = logmessage(ui, opts)
1389 if cmdline_message:
1389 if cmdline_message:
1390 # pickup the cmdline msg
1390 # pickup the cmdline msg
@@ -1511,9 +1511,7 b' def tryimportone(ui, repo, patchdata, pa'
1511 if n:
1511 if n:
1512 # i18n: refers to a short changeset id
1512 # i18n: refers to a short changeset id
1513 msg = _('created %s') % short(n)
1513 msg = _('created %s') % short(n)
1514 return (msg, n, rejects)
1514 return msg, n, rejects
1515 finally:
1516 os.unlink(tmpname)
1517
1515
1518 # facility to let extensions include additional data in an exported patch
1516 # facility to let extensions include additional data in an exported patch
1519 # list of identifiers to be executed in order
1517 # list of identifiers to be executed in order
@@ -3089,8 +3089,7 b' def import_(ui, repo, patch1=None, *patc'
3089
3089
3090 haspatch = False
3090 haspatch = False
3091 for hunk in patch.split(patchfile):
3091 for hunk in patch.split(patchfile):
3092 patchdata = patch.extract(ui, hunk)
3092 with patch.extract(ui, hunk) as patchdata:
3093
3094 msg, node, rej = cmdutil.tryimportone(ui, repo, patchdata,
3093 msg, node, rej = cmdutil.tryimportone(ui, repo, patchdata,
3095 parents, opts,
3094 parents, opts,
3096 msgs, hg.clean)
3095 msgs, hg.clean)
@@ -9,6 +9,7 b''
9 from __future__ import absolute_import, print_function
9 from __future__ import absolute_import, print_function
10
10
11 import collections
11 import collections
12 import contextlib
12 import copy
13 import copy
13 import difflib
14 import difflib
14 import email
15 import email
@@ -192,6 +193,7 b" patchheadermap = [('Date', 'date'),"
192 ('Node ID', 'nodeid'),
193 ('Node ID', 'nodeid'),
193 ]
194 ]
194
195
196 @contextlib.contextmanager
195 def extract(ui, fileobj):
197 def extract(ui, fileobj):
196 '''extract patch from data read from fileobj.
198 '''extract patch from data read from fileobj.
197
199
@@ -209,6 +211,16 b' def extract(ui, fileobj):'
209 Any item can be missing from the dictionary. If filename is missing,
211 Any item can be missing from the dictionary. If filename is missing,
210 fileobj did not contain a patch. Caller must unlink filename when done.'''
212 fileobj did not contain a patch. Caller must unlink filename when done.'''
211
213
214 fd, tmpname = tempfile.mkstemp(prefix='hg-patch-')
215 tmpfp = os.fdopen(fd, r'wb')
216 try:
217 yield _extract(ui, fileobj, tmpname, tmpfp)
218 finally:
219 tmpfp.close()
220 os.unlink(tmpname)
221
222 def _extract(ui, fileobj, tmpname, tmpfp):
223
212 # attempt to detect the start of a patch
224 # attempt to detect the start of a patch
213 # (this heuristic is borrowed from quilt)
225 # (this heuristic is borrowed from quilt)
214 diffre = re.compile(br'^(?:Index:[ \t]|diff[ \t]-|RCS file: |'
226 diffre = re.compile(br'^(?:Index:[ \t]|diff[ \t]-|RCS file: |'
@@ -218,9 +230,7 b' def extract(ui, fileobj):'
218 re.MULTILINE | re.DOTALL)
230 re.MULTILINE | re.DOTALL)
219
231
220 data = {}
232 data = {}
221 fd, tmpname = tempfile.mkstemp(prefix='hg-patch-')
233
222 tmpfp = os.fdopen(fd, r'wb')
223 try:
224 msg = pycompat.emailparser().parse(fileobj)
234 msg = pycompat.emailparser().parse(fileobj)
225
235
226 subject = msg[r'Subject'] and mail.headdecode(msg[r'Subject'])
236 subject = msg[r'Subject'] and mail.headdecode(msg[r'Subject'])
@@ -294,10 +304,6 b' def extract(ui, fileobj):'
294 tmpfp.write('\n')
304 tmpfp.write('\n')
295 elif not diffs_seen and message and content_type == 'text/plain':
305 elif not diffs_seen and message and content_type == 'text/plain':
296 message += '\n' + payload
306 message += '\n' + payload
297 except: # re-raises
298 tmpfp.close()
299 os.unlink(tmpname)
300 raise
301
307
302 if subject and not message.startswith(subject):
308 if subject and not message.startswith(subject):
303 message = '%s\n%s' % (subject, message)
309 message = '%s\n%s' % (subject, message)
@@ -310,8 +316,7 b' def extract(ui, fileobj):'
310
316
311 if diffs_seen:
317 if diffs_seen:
312 data['filename'] = tmpname
318 data['filename'] = tmpname
313 else:
319
314 os.unlink(tmpname)
315 return data
320 return data
316
321
317 class patchmeta(object):
322 class patchmeta(object):
General Comments 0
You need to be logged in to leave comments. Login now