bookmarks: use context managers for locks and transaction in pushbookmark()
authorMartin von Zweigbergk <martinvonz@google.com>
Wed, 10 Jan 2018 10:49:12 -0800
changeset 35577 ac7ee75ee664
parent 35576 b9a0de08110e
child 35578 86f9aabed67b
bookmarks: use context managers for locks and transaction in pushbookmark() Differential Revision: https://phab.mercurial-scm.org/D1840
mercurial/bookmarks.py
--- a/mercurial/bookmarks.py	Wed Jan 10 10:47:13 2018 -0800
+++ b/mercurial/bookmarks.py	Wed Jan 10 10:49:12 2018 -0800
@@ -20,7 +20,6 @@
 from . import (
     encoding,
     error,
-    lock as lockmod,
     obsutil,
     pycompat,
     scmutil,
@@ -414,11 +413,7 @@
     return d
 
 def pushbookmark(repo, key, old, new):
-    w = l = tr = None
-    try:
-        w = repo.wlock()
-        l = repo.lock()
-        tr = repo.transaction('bookmarks')
+    with repo.wlock(), repo.lock(), repo.transaction('bookmarks') as tr:
         marks = repo._bookmarks
         existing = hex(marks.get(key, ''))
         if existing != old and existing != new:
@@ -430,10 +425,7 @@
                 return False
             changes = [(key, repo[new].node())]
         marks.applychanges(repo, tr, changes)
-        tr.close()
         return True
-    finally:
-        lockmod.release(tr, l, w)
 
 def comparebookmarks(repo, srcmarks, dstmarks, targets=None):
     '''Compare bookmarks between srcmarks and dstmarks