changeset 32930:af31d531dda0

changegroup: let callers pass in transaction to apply() (API) I think passing in the transaction makes it a little clearer and more consistent with bundle2.
author Martin von Zweigbergk <martinvonz@google.com>
date Thu, 15 Jun 2017 22:46:38 -0700
parents d11e2343762b
children b08431e1b062
files hgext/histedit.py hgext/shelve.py mercurial/bundle2.py mercurial/changegroup.py mercurial/commands.py mercurial/exchange.py mercurial/repair.py
diffstat 7 files changed, 22 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/histedit.py	Thu Jun 15 23:09:14 2017 -0700
+++ b/hgext/histedit.py	Thu Jun 15 22:46:38 2017 -0700
@@ -1199,7 +1199,7 @@
             gen = exchange.readbundle(ui, f, backupfile)
             with repo.transaction('histedit.abort') as tr:
                 if not isinstance(gen, bundle2.unbundle20):
-                    gen.apply(repo, 'histedit', 'bundle:' + backupfile)
+                    gen.apply(repo, tr, 'histedit', 'bundle:' + backupfile)
                 else:
                     bundle2.applybundle(repo, gen, tr,
                                         source='histedit',
--- a/hgext/shelve.py	Thu Jun 15 23:09:14 2017 -0700
+++ b/hgext/shelve.py	Thu Jun 15 22:46:38 2017 -0700
@@ -127,7 +127,7 @@
         try:
             gen = exchange.readbundle(self.repo.ui, fp, self.fname, self.vfs)
             if not isinstance(gen, bundle2.unbundle20):
-                gen.apply(self.repo, 'unshelve',
+                gen.apply(self.repo, self.repo.currenttransaction(), 'unshelve',
                           'bundle:' + self.vfs.join(self.fname),
                           targetphase=phases.secret)
             else:
--- a/mercurial/bundle2.py	Thu Jun 15 23:09:14 2017 -0700
+++ b/mercurial/bundle2.py	Thu Jun 15 22:46:38 2017 -0700
@@ -1474,12 +1474,7 @@
     This is a very early implementation that will massive rework before being
     inflicted to any end-user.
     """
-    # Make sure we trigger a transaction creation
-    #
-    # The addchangegroup function will get a transaction object by itself, but
-    # we need to make sure we trigger the creation of a transaction object used
-    # for the whole processing scope.
-    op.gettransaction()
+    tr = op.gettransaction()
     unpackerversion = inpart.params.get('version', '01')
     # We should raise an appropriate exception here
     cg = changegroup.getunbundler(unpackerversion, inpart, None)
@@ -1497,7 +1492,8 @@
         op.repo.requirements.add('treemanifest')
         op.repo._applyopenerreqs()
         op.repo._writerequirements()
-    ret = cg.apply(op.repo, 'bundle2', 'bundle2', expectedtotal=nbchangesets)
+    ret = cg.apply(op.repo, tr, 'bundle2', 'bundle2',
+                   expectedtotal=nbchangesets)
     op.records.add('changegroup', {'return': ret})
     if op.reply is not None:
         # This is definitely not the final form of this
@@ -1555,18 +1551,13 @@
 
     real_part = util.digestchecker(url.open(op.ui, raw_url), size, digests)
 
-    # Make sure we trigger a transaction creation
-    #
-    # The addchangegroup function will get a transaction object by itself, but
-    # we need to make sure we trigger the creation of a transaction object used
-    # for the whole processing scope.
-    op.gettransaction()
+    tr = op.gettransaction()
     from . import exchange
     cg = exchange.readbundle(op.repo.ui, real_part, raw_url)
     if not isinstance(cg, changegroup.cg1unpacker):
         raise error.Abort(_('%s: not a bundle version 1.0') %
             util.hidepassword(raw_url))
-    ret = cg.apply(op.repo, 'bundle2', 'bundle2')
+    ret = cg.apply(op.repo, tr, 'bundle2', 'bundle2')
     op.records.add('changegroup', {'return': ret})
     if op.reply is not None:
         # This is definitely not the final form of this
--- a/mercurial/changegroup.py	Thu Jun 15 23:09:14 2017 -0700
+++ b/mercurial/changegroup.py	Thu Jun 15 22:46:38 2017 -0700
@@ -256,7 +256,7 @@
         repo.ui.progress(_('manifests'), None)
         self.callback = None
 
-    def apply(self, repo, srctype, url, emptyok=False,
+    def apply(self, repo, tr, srctype, url, emptyok=False,
               targetphase=phases.draft, expectedtotal=None):
         """Add the changegroup returned by source.read() to this repo.
         srctype is a string like 'push', 'pull', or 'unbundle'.  url is
@@ -279,12 +279,11 @@
         changesets = files = revisions = 0
 
         try:
-            with repo.transaction("\n".join([srctype,
-                                             util.hidepassword(url)])) as tr:
-                # The transaction could have been created before and already
-                # carries source information. In this case we use the top
-                # level data. We overwrite the argument because we need to use
-                # the top level value (if they exist) in this function.
+            if True:
+                # The transaction may already carry source information. In this
+                # case we use the top level data. We overwrite the argument
+                # because we need to use the top level value (if they exist)
+                # in this function.
                 srctype = tr.hookargs.setdefault('source', srctype)
                 url = tr.hookargs.setdefault('url', url)
                 repo.hook('prechangegroup', throw=True, **tr.hookargs)
--- a/mercurial/commands.py	Thu Jun 15 23:09:14 2017 -0700
+++ b/mercurial/commands.py	Thu Jun 15 22:46:38 2017 -0700
@@ -5339,8 +5339,8 @@
                 modheads = changegroup.combineresults(changes)
             else:
                 txnname = 'unbundle\n%s' % util.hidepassword(url)
-                with repo.transaction(txnname):
-                    modheads = gen.apply(repo, 'unbundle', url)
+                with repo.transaction(txnname) as tr:
+                    modheads = gen.apply(repo, tr, 'unbundle', url)
 
     return postincoming(ui, repo, modheads, opts.get(r'update'), None, None)
 
--- a/mercurial/exchange.py	Thu Jun 15 23:09:14 2017 -0700
+++ b/mercurial/exchange.py	Thu Jun 15 22:46:38 2017 -0700
@@ -1430,7 +1430,7 @@
         pullop.repo.ui.status(_("no changes found\n"))
         pullop.cgresult = 0
         return
-    pullop.gettransaction()
+    tr = pullop.gettransaction()
     if pullop.heads is None and list(pullop.common) == [nullid]:
         pullop.repo.ui.status(_("requesting all changes\n"))
     elif pullop.heads is None and pullop.remote.capable('changegroupsubset'):
@@ -1449,7 +1449,7 @@
                            "changegroupsubset."))
     else:
         cg = pullop.remote.changegroupsubset(pullop.fetch, pullop.heads, 'pull')
-    pullop.cgresult = cg.apply(pullop.repo, 'pull', pullop.remote.url())
+    pullop.cgresult = cg.apply(pullop.repo, tr, 'pull', pullop.remote.url())
 
 def _pullphase(pullop):
     # Get remote phases data from remote
@@ -1735,8 +1735,8 @@
         if not isinstance(cg, bundle2.unbundle20):
             # legacy case: bundle1 (changegroup 01)
             txnname = "\n".join([source, util.hidepassword(url)])
-            with repo.lock(), repo.transaction(txnname):
-                r = cg.apply(repo, source, url)
+            with repo.lock(), repo.transaction(txnname) as tr:
+                r = cg.apply(repo, tr, source, url)
         else:
             r = None
             try:
@@ -2001,7 +2001,7 @@
             elif isinstance(cg, streamclone.streamcloneapplier):
                 cg.apply(repo)
             else:
-                cg.apply(repo, 'clonebundles', url)
+                cg.apply(repo, tr, 'clonebundles', url)
             return True
         except urlerr.httperror as e:
             ui.warn(_('HTTP error fetching bundle: %s\n') % str(e))
--- a/mercurial/repair.py	Thu Jun 15 23:09:14 2017 -0700
+++ b/mercurial/repair.py	Thu Jun 15 22:46:38 2017 -0700
@@ -208,8 +208,8 @@
                                         url=tmpbundleurl)
             else:
                 txnname = "strip\n%s" % util.hidepassword(tmpbundleurl)
-                with repo.transaction(txnname):
-                    gen.apply(repo, 'strip', tmpbundleurl, True)
+                with repo.transaction(txnname) as tr:
+                    gen.apply(repo, tr, 'strip', tmpbundleurl, True)
             if not repo.ui.verbose:
                 repo.ui.popbuffer()
             f.close()