compat: add a compatibility layer for bookmark move
authorPierre-Yves David <pierre-yves.david@octobus.net>
Fri, 21 Jul 2017 10:07:15 +0200
changeset 2752 4457aa1d81aa
parent 2751 4f560f117fff
child 2753 b265900a9650
compat: add a compatibility layer for bookmark move The API was updated in 4.3 (712a85b3677f) we adapt to that.
hgext3rd/evolve/__init__.py
hgext3rd/evolve/compat.py
hgext3rd/evolve/evocommands.py
--- a/hgext3rd/evolve/__init__.py	Fri Jul 21 09:50:26 2017 +0200
+++ b/hgext3rd/evolve/__init__.py	Fri Jul 21 10:07:15 2017 +0200
@@ -2004,8 +2004,8 @@
                 lock = repo.lock()
                 tr = repo.transaction('previous')
                 if bookmark is not None:
-                    repo._bookmarks[bookmark] = target.node()
-                    repo._bookmarks.recordchange(tr)
+                    bmchanges = [(bookmark, target.node())]
+                    compat.bookmarkapplychanges(repo, tr, bmchanges)
                 else:
                     bookmarksmod.deactivate(repo)
                 tr.close()
@@ -2145,8 +2145,8 @@
                         lock = repo.lock()
                         tr = repo.transaction('next')
                         if shouldmove:
-                            repo._bookmarks[bm] = c.node()
-                            repo._bookmarks.recordchange(tr)
+                            bmchanges = [(bm, c.node())]
+                            compat.bookmarkapplychanges(repo, tr, bmchanges)
                         else:
                             bookmarksmod.deactivate(repo)
                         tr.close()
@@ -2226,9 +2226,10 @@
         wlock = repo.wlock()
         lock = repo.lock()
         tr = repo.transaction('prune')
+        bmchanges = []
         for bookmark in bookmarks:
-            del repomarks[bookmark]
-        repomarks.recordchange(tr)
+            bmchanges.append((bookmark, None))
+        compat.bookmarkapplychanges(repo, tr, bmchanges)
         tr.close()
         for bookmark in sorted(bookmarks):
             repo.ui.write(_("bookmark '%s' deleted\n") % bookmark)
@@ -2393,8 +2394,8 @@
                 movebookmark = bookactive and not bookmarks
                 if movebookmark:
                     bookmarksmod.deactivate(repo)
-                    repo._bookmarks[bookactive] = newnode.node()
-                    repo._bookmarks.recordchange(tr)
+                    bmchanges = [(bookactive, newnode.node())]
+                    compat.bookmarkapplychanges(repo, tr, bmchanges)
                 commands.update(ui, repo, newnode.rev())
                 ui.status(_('working directory now at %s\n')
                           % ui.label(str(newnode), 'evolve.node'))
@@ -2452,15 +2453,16 @@
                 markers.append((old, (new,)))
             if markers:
                 obsolete.createmarkers(repo, markers)
+            bmchanges = []
             for book in oldbookmarks:
-                repo._bookmarks[book] = new.node()
+                bmchanges.append((book, new.node()))
             if oldbookmarks:
                 if not wlock:
                     wlock = repo.wlock()
                 if not lock:
                     lock = repo.lock()
                 tr = repo.transaction('commit')
-                repo._bookmarks.recordchange(tr)
+                compat.bookmarkapplychanges(repo, tr, bmchanges)
                 tr.close()
         return result
     finally:
@@ -3056,20 +3058,22 @@
     nodesrc = orig.node()
     destphase = repo[nodesrc].phase()
     oldbookmarks = repo.nodebookmarks(nodesrc)
+    bmchanges = []
+
     if nodenew is not None:
         phases.retractboundary(repo, tr, destphase, [nodenew])
         obsolete.createmarkers(repo, [(repo[nodesrc], (repo[nodenew],))])
         for book in oldbookmarks:
-            repo._bookmarks[book] = nodenew
+            bmchanges.append((book, nodenew))
     else:
         obsolete.createmarkers(repo, [(repo[nodesrc], ())])
         # Behave like rebase, move bookmarks to dest
         for book in oldbookmarks:
-            repo._bookmarks[book] = dest.node()
+            bmchanges.append((book, dest.node()))
     for book in destbookmarks: # restore bookmark that rebase move
-        repo._bookmarks[book] = dest.node()
-    if oldbookmarks or destbookmarks:
-        repo._bookmarks.recordchange(tr)
+        bmchanges.append((book, dest.node()))
+    if bmchanges:
+        compat.bookmarkapplychanges(repo, tr, bmchanges)
 
 evolvestateversion = 0
 
--- a/hgext3rd/evolve/compat.py	Fri Jul 21 09:50:26 2017 +0200
+++ b/hgext3rd/evolve/compat.py	Fri Jul 21 10:07:15 2017 +0200
@@ -76,3 +76,17 @@
     if func is None:
         func = obsolete.allprecursors
     return func(*args, **kwargs)
+
+# compatibility layer for mercurial < 4.3
+def bookmarkapplychanges(repo, tr, changes):
+    """Apply a list of changes to bookmarks
+    """
+    bookmarks = repo._bookmarks
+    if util.safehasattr(bookmarks, 'applychanges'):
+        return bookmarks.applychanges(repo, tr, changes)
+    for name, node in changes:
+        if node is None:
+            bookmarks._del(name)
+        else:
+            bookmarks._set(name, node)
+    bookmarks.recordchange(tr)
--- a/hgext3rd/evolve/evocommands.py	Fri Jul 21 09:50:26 2017 +0200
+++ b/hgext3rd/evolve/evocommands.py	Fri Jul 21 10:07:15 2017 +0200
@@ -31,6 +31,7 @@
 
 from . import (
     exthelper,
+    compat,
 )
 
 eh = exthelper.exthelper()
@@ -68,14 +69,10 @@
     and bookmarks bound to oldid to newid.
     """
     def updatebookmarks(newid):
-        dirty = False
         oldbookmarks = repo.nodebookmarks(oldid)
-        if oldbookmarks:
-            for b in oldbookmarks:
-                repo._bookmarks[b] = newid
-            dirty = True
-        if dirty:
-            repo._bookmarks.recordchange(tr)
+        bmchanges = [(b, newid) for b in oldbookmarks]
+        if bmchanges:
+            compat.bookmarkapplychanges(repo, tr, bmchanges)
     return updatebookmarks
 
 @eh.command(