Patchwork [hggit-ext] bookmarks: compatibility with new applychanges api

login
register
mail settings
Submitter Ryan McElroy
Date July 19, 2017, 12:52 p.m.
Message ID <235fcd456ef74bb9302e.1500468750@devbig314.prn1.facebook.com>
Download mbox | patch
Permalink /patch/22525/
State New, archived
Headers show

Comments

Ryan McElroy - July 19, 2017, 12:52 p.m.
# HG changeset patch
# User Ryan McElroy <rmcelroy@fb.com>
# Date 1500468704 25200
#      Wed Jul 19 05:51:44 2017 -0700
# Node ID 235fcd456ef74bb9302e59dfd014e2a83bb27024
# Parent  78959c8e5e607a00f23e0991760439923bf30dfb
bookmarks: compatibility with new applychanges api

Recent versions of mercuiral issue a devel-warn if the old recordchange api is
used, but we want to remain backwards-compatible, so this patch refactors things
to be forward-compatible and backwards-compatible.
Ryan McElroy - July 19, 2017, 1:38 p.m.
Whoops, this is the wrong list for hg-git. Ignore this, I've just 
re-sent to the proper list.

Cheers,

~Ryan


On 7/19/17 1:52 PM, Ryan McElroy wrote:
> # HG changeset patch
> # User Ryan McElroy <rmcelroy@fb.com>
> # Date 1500468704 25200
> #      Wed Jul 19 05:51:44 2017 -0700
> # Node ID 235fcd456ef74bb9302e59dfd014e2a83bb27024
> # Parent  78959c8e5e607a00f23e0991760439923bf30dfb
> bookmarks: compatibility with new applychanges api
>
> Recent versions of mercuiral issue a devel-warn if the old recordchange api is
> used, but we want to remain backwards-compatible, so this patch refactors things
> to be forward-compatible and backwards-compatible.
>
> diff --git a/hggit/git_handler.py b/hggit/git_handler.py
> --- a/hggit/git_handler.py
> +++ b/hggit/git_handler.py
> @@ -300,8 +300,8 @@ class GitHandler(object):
>                       # make sure the bookmark exists; at the point the remote
>                       # branches has already been set up
>                       suffix = self.branch_bookmark_suffix or ''
> -                    self.repo._bookmarks[rhead + suffix] = rnode
> -                    util.recordbookmarks(self.repo, self.repo._bookmarks)
> +                    changes = [(rhead + suffix, rnode)]
> +                    util.updatebookmarks(self.repo, changes)
>                       bms = [rhead + suffix]
>   
>                   if bms:
> @@ -1372,6 +1372,7 @@ class GitHandler(object):
>                             if ref.startswith('refs/heads/')])
>   
>               suffix = self.branch_bookmark_suffix or ''
> +            changes = []
>               for head, sha in heads.iteritems():
>                   # refs contains all the refs in the server, not just
>                   # the ones we are pulling
> @@ -1381,15 +1382,15 @@ class GitHandler(object):
>                   hgsha = bin(hgsha)
>                   if head not in bms:
>                       # new branch
> -                    bms[head + suffix] = hgsha
> +                    changes.append((head + suffix, hgsha))
>                   else:
>                       bm = self.repo[bms[head]]
>                       if bm.ancestor(self.repo[hgsha]) == bm:
>                           # fast forward
> -                        bms[head + suffix] = hgsha
> +                        changes.append((head + suffix, hgsha))
>   
>               if heads:
> -                util.recordbookmarks(self.repo, bms)
> +                util.updatebookmarks(self.repo, changes)
>   
>           except AttributeError:
>               self.ui.warn(_('creating bookmarks failed, do you have'
> diff --git a/hggit/util.py b/hggit/util.py
> --- a/hggit/util.py
> +++ b/hggit/util.py
> @@ -94,18 +94,28 @@ def isgitsshuri(uri):
>               return True
>       return False
>   
> -def recordbookmarks(repo, bms, name='git_handler'):
> +def updatebookmarks(repo, changes, name='git_handler'):
>       """abstract writing bookmarks for backwards compatibility"""
> +    bms = repo._bookmarks
>       tr = lock = wlock = None
>       try:
>           wlock = repo.wlock()
>           lock = repo.lock()
>           tr = repo.transaction(name)
> -        if hgutil.safehasattr(bms, 'recordchange'):
> -            # recordchange was added in mercurial 3.2
> -            bms.recordchange(tr)
> +        if hgutil.safehasattr(bms, 'applychanges'):
> +            # applychanges was added in mercurial 3.4
> +            bms.applychanges(repo, tr, changes)
>           else:
> -            bms.write()
> +            for name, node in changes:
> +                if node is None:
> +                    del bms[name]
> +                else:
> +                    bms[name] = node
> +            if hgutil.safehasattr(bms, 'recordchange'):
> +                # recordchange was added in mercurial 3.2
> +                bms.recordchange(tr)
> +            else:
> +                bms.write()
>           tr.close()
>       finally:
>           lockmod.release(tr, lock, wlock)

Patch

diff --git a/hggit/git_handler.py b/hggit/git_handler.py
--- a/hggit/git_handler.py
+++ b/hggit/git_handler.py
@@ -300,8 +300,8 @@  class GitHandler(object):
                     # make sure the bookmark exists; at the point the remote
                     # branches has already been set up
                     suffix = self.branch_bookmark_suffix or ''
-                    self.repo._bookmarks[rhead + suffix] = rnode
-                    util.recordbookmarks(self.repo, self.repo._bookmarks)
+                    changes = [(rhead + suffix, rnode)]
+                    util.updatebookmarks(self.repo, changes)
                     bms = [rhead + suffix]
 
                 if bms:
@@ -1372,6 +1372,7 @@  class GitHandler(object):
                           if ref.startswith('refs/heads/')])
 
             suffix = self.branch_bookmark_suffix or ''
+            changes = []
             for head, sha in heads.iteritems():
                 # refs contains all the refs in the server, not just
                 # the ones we are pulling
@@ -1381,15 +1382,15 @@  class GitHandler(object):
                 hgsha = bin(hgsha)
                 if head not in bms:
                     # new branch
-                    bms[head + suffix] = hgsha
+                    changes.append((head + suffix, hgsha))
                 else:
                     bm = self.repo[bms[head]]
                     if bm.ancestor(self.repo[hgsha]) == bm:
                         # fast forward
-                        bms[head + suffix] = hgsha
+                        changes.append((head + suffix, hgsha))
 
             if heads:
-                util.recordbookmarks(self.repo, bms)
+                util.updatebookmarks(self.repo, changes)
 
         except AttributeError:
             self.ui.warn(_('creating bookmarks failed, do you have'
diff --git a/hggit/util.py b/hggit/util.py
--- a/hggit/util.py
+++ b/hggit/util.py
@@ -94,18 +94,28 @@  def isgitsshuri(uri):
             return True
     return False
 
-def recordbookmarks(repo, bms, name='git_handler'):
+def updatebookmarks(repo, changes, name='git_handler'):
     """abstract writing bookmarks for backwards compatibility"""
+    bms = repo._bookmarks
     tr = lock = wlock = None
     try:
         wlock = repo.wlock()
         lock = repo.lock()
         tr = repo.transaction(name)
-        if hgutil.safehasattr(bms, 'recordchange'):
-            # recordchange was added in mercurial 3.2
-            bms.recordchange(tr)
+        if hgutil.safehasattr(bms, 'applychanges'):
+            # applychanges was added in mercurial 3.4
+            bms.applychanges(repo, tr, changes)
         else:
-            bms.write()
+            for name, node in changes:
+                if node is None:
+                    del bms[name]
+                else:
+                    bms[name] = node
+            if hgutil.safehasattr(bms, 'recordchange'):
+                # recordchange was added in mercurial 3.2
+                bms.recordchange(tr)
+            else:
+                bms.write()
         tr.close()
     finally:
         lockmod.release(tr, lock, wlock)