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