comparison mercurial/bookmarks.py @ 35577:ac7ee75ee664

bookmarks: use context managers for locks and transaction in pushbookmark() Differential Revision: https://phab.mercurial-scm.org/D1840
author Martin von Zweigbergk <martinvonz@google.com>
date Wed, 10 Jan 2018 10:49:12 -0800
parents b9a0de08110e
children 7336ac5e786e
comparison
equal deleted inserted replaced
35576:b9a0de08110e 35577:ac7ee75ee664
18 wdirid, 18 wdirid,
19 ) 19 )
20 from . import ( 20 from . import (
21 encoding, 21 encoding,
22 error, 22 error,
23 lock as lockmod,
24 obsutil, 23 obsutil,
25 pycompat, 24 pycompat,
26 scmutil, 25 scmutil,
27 txnutil, 26 txnutil,
28 util, 27 util,
412 for book, node in listbinbookmarks(repo): 411 for book, node in listbinbookmarks(repo):
413 d[book] = hex(node) 412 d[book] = hex(node)
414 return d 413 return d
415 414
416 def pushbookmark(repo, key, old, new): 415 def pushbookmark(repo, key, old, new):
417 w = l = tr = None 416 with repo.wlock(), repo.lock(), repo.transaction('bookmarks') as tr:
418 try:
419 w = repo.wlock()
420 l = repo.lock()
421 tr = repo.transaction('bookmarks')
422 marks = repo._bookmarks 417 marks = repo._bookmarks
423 existing = hex(marks.get(key, '')) 418 existing = hex(marks.get(key, ''))
424 if existing != old and existing != new: 419 if existing != old and existing != new:
425 return False 420 return False
426 if new == '': 421 if new == '':
428 else: 423 else:
429 if new not in repo: 424 if new not in repo:
430 return False 425 return False
431 changes = [(key, repo[new].node())] 426 changes = [(key, repo[new].node())]
432 marks.applychanges(repo, tr, changes) 427 marks.applychanges(repo, tr, changes)
433 tr.close()
434 return True 428 return True
435 finally:
436 lockmod.release(tr, l, w)
437 429
438 def comparebookmarks(repo, srcmarks, dstmarks, targets=None): 430 def comparebookmarks(repo, srcmarks, dstmarks, targets=None):
439 '''Compare bookmarks between srcmarks and dstmarks 431 '''Compare bookmarks between srcmarks and dstmarks
440 432
441 This returns tuple "(addsrc, adddst, advsrc, advdst, diverge, 433 This returns tuple "(addsrc, adddst, advsrc, advdst, diverge,