A tiny side-effect is that the transaction is now closed after saving
the commit message.
Details
- Reviewers
- None
- Group Reviewers
hg-reviewers - Commits
- rHG2ceea1554d1e: import: use context manager for lock, dirstateguard, transaction
Diff Detail
- Repository
- rHG Mercurial
- Lint
Lint Skipped - Unit
Unit Tests Skipped
Event Timeline
+ if not opts.get('no_commit'):
+ lock = repo.lock()
+ tr = repo.transaction('import')
+ dsguard = util.nullcontextmanager()
+ else:
+ lock = util.nullcontextmanager()
+ tr = util.nullcontextmanager()
+ dsguard = dirstateguard.dirstateguard(repo, 'import')
+ with lock, tr, dsguard:
Doesn't it leave a stale lock if repo.transaction() raises exception?
Good catch. I have often wished that our transactions and locks respected the context manager protocol and did the locking in enter. How do you feel about this workaround (see updated code)?
+ if not opts.get('no_commit'):
+ lock = repo.lock()
+ try:tr = repo.transaction('import')
- else:
- dsguard = dirstateguard.dirstateguard(repo, 'import')
+ except:
+ lock.release()
Needs to re-raise.
+ dsguard = util.nullcontextmanager()
+ else:
+ lock = util.nullcontextmanager()
+ tr = util.nullcontextmanager()
+ dsguard = dirstateguard.dirstateguard(repo, 'import')
+ with lock, tr, dsguard:
Another option is to wrap transaction and dirstateguard by lambda or partial,
and call them with "with".
with lock(), tr(), dsguard():