Mercurial > public > mercurial-scm > hg
comparison mercurial/commands.py @ 37621:5537d8f5e989
patch: make extract() a context manager (API)
Previously, this function was creating a temporary file and relying
on callers to unlink it. Yuck.
We convert the function to a context manager and tie the lifetime of
the temporary file to that of the context manager. This changed
indentation not only from the context manager, but also from the
elination of try blocks. It was just easier to split the heart of
extract() into its own function.
The single consumer of this function has been refactored to use it as
a context manager. Code for cleaning up the file in tryimportone()
has also been removed.
.. api::
``patch.extract()`` is now a context manager. Callers no longer have
to worry about deleting the temporary file it creates, as the file is
tied to the lifetime of the context manager.
Differential Revision: https://phab.mercurial-scm.org/D3306
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Thu, 12 Apr 2018 23:14:38 -0700 |
parents | fd1dd79cff20 |
children | 5fc502e149f1 |
comparison
equal
deleted
inserted
replaced
37620:fd1dd79cff20 | 37621:5537d8f5e989 |
---|---|
3087 ui.status(_('applying %s\n') % patchurl) | 3087 ui.status(_('applying %s\n') % patchurl) |
3088 patchfile = hg.openpath(ui, patchurl) | 3088 patchfile = hg.openpath(ui, patchurl) |
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 | 3093 msg, node, rej = cmdutil.tryimportone(ui, repo, patchdata, |
3094 msg, node, rej = cmdutil.tryimportone(ui, repo, patchdata, | 3094 parents, opts, |
3095 parents, opts, | 3095 msgs, hg.clean) |
3096 msgs, hg.clean) | |
3097 if msg: | 3096 if msg: |
3098 haspatch = True | 3097 haspatch = True |
3099 ui.note(msg + '\n') | 3098 ui.note(msg + '\n') |
3100 if update or exact: | 3099 if update or exact: |
3101 parents = repo[None].parents() | 3100 parents = repo[None].parents() |