# HG changeset patch # User Martin von Zweigbergk # Date 1502778414 25200 # Node ID 158dddc635ff6f29647c610ccab7dd02a9a5e1e4 # Parent 5d286eb7009fde5d070bba656ff4e98e45377896 commit: use context manager with dirstateguard When I wrote 5ac845ca059a (commit: don't let failed commit with --addremove update dirstate (issue5645), 2017-07-31), Durham's 609606d21765 (rebase: use one dirstateguard for when using rebase.singletransaction, 2017-07-20) had not yet landed, so I had to write it in the old-fashioned way. Now that Durham's patch is in, we can simplify by using a context manager. Differential Revision: https://phab.mercurial-scm.org/D406 diff -r 5d286eb7009f -r 158dddc635ff mercurial/cmdutil.py --- a/mercurial/cmdutil.py Mon Aug 14 23:26:51 2017 -0700 +++ b/mercurial/cmdutil.py Mon Aug 14 23:26:54 2017 -0700 @@ -3000,19 +3000,13 @@ # that doesn't support addremove if opts.get('addremove'): dsguard = dirstateguard.dirstateguard(repo, 'commit') - try: + with dsguard or util.nullcontextmanager(): if dsguard: if scmutil.addremove(repo, matcher, "", opts) != 0: raise error.Abort( _("failed to mark all new/missing files as added/removed")) - r = commitfunc(ui, repo, message, matcher, opts) - if dsguard: - dsguard.close() - return r - finally: - if dsguard: - dsguard.release() + return commitfunc(ui, repo, message, matcher, opts) def samefile(f, ctx1, ctx2): if f in ctx1.manifest():