unbundle: move most of the logic on cmdutil to help debug::unbundle reuse
authorPierre-Yves David <pierre-yves.david@octobus.net>
Wed, 27 Mar 2024 17:46:23 +0000
changeset 51555 15e680a44502
parent 51554 a151fd01e98c
child 51556 f1512dbfee9f
unbundle: move most of the logic on cmdutil to help debug::unbundle reuse This make sure `hg debug::unbundle` focus on the core logic.
mercurial/cmdutil.py
mercurial/commands.py
mercurial/debugcommands.py
tests/test-completion.t
tests/test-debugcommands.t
--- a/mercurial/cmdutil.py	Wed Mar 27 17:29:48 2024 +0000
+++ b/mercurial/cmdutil.py	Wed Mar 27 17:46:23 2024 +0000
@@ -35,11 +35,13 @@
 
 from . import (
     bookmarks,
+    bundle2,
     changelog,
     copies,
     crecord as crecordmod,
     encoding,
     error,
+    exchange,
     formatter,
     logcmdutil,
     match as matchmod,
@@ -56,6 +58,7 @@
     rewriteutil,
     scmutil,
     state as statemod,
+    streamclone,
     subrepoutil,
     templatekw,
     templater,
@@ -66,6 +69,7 @@
 from .utils import (
     dateutil,
     stringutil,
+    urlutil,
 )
 
 from .revlogutils import (
@@ -4178,3 +4182,47 @@
     elif not ui.configbool(b'commands', b'update.requiredest'):
         ui.status(_(b"(run 'hg update' to get a working copy)\n"))
     return False
+
+
+def unbundle_files(ui, repo, fnames, unbundle_source=b'unbundle'):
+    """utility for `hg unbundle` and `hg debug::unbundle`"""
+    assert fnames
+    # avoid circular import
+    from . import hg
+
+    with repo.lock():
+        for fname in fnames:
+            f = hg.openpath(ui, fname)
+            gen = exchange.readbundle(ui, f, fname)
+            if isinstance(gen, streamclone.streamcloneapplier):
+                raise error.InputError(
+                    _(
+                        b'packed bundles cannot be applied with '
+                        b'"hg unbundle"'
+                    ),
+                    hint=_(b'use "hg debugapplystreamclonebundle"'),
+                )
+            url = b'bundle:' + fname
+            try:
+                txnname = b'unbundle'
+                if not isinstance(gen, bundle2.unbundle20):
+                    txnname = b'unbundle\n%s' % urlutil.hidepassword(url)
+                with repo.transaction(txnname) as tr:
+                    op = bundle2.applybundle(
+                        repo,
+                        gen,
+                        tr,
+                        source=unbundle_source,  # used by debug::unbundle
+                        url=url,
+                    )
+            except error.BundleUnknownFeatureError as exc:
+                raise error.Abort(
+                    _(b'%s: unknown bundle feature, %s') % (fname, exc),
+                    hint=_(
+                        b"see https://mercurial-scm.org/"
+                        b"wiki/BundleFeature for more "
+                        b"information"
+                    ),
+                )
+            modheads = bundle2.combinechangegroupresults(op)
+    return modheads
--- a/mercurial/commands.py	Wed Mar 27 17:29:48 2024 +0000
+++ b/mercurial/commands.py	Wed Mar 27 17:46:23 2024 +0000
@@ -60,7 +60,6 @@
     server,
     shelve as shelvemod,
     state as statemod,
-    streamclone,
     tags as tagsmod,
     ui as uimod,
     util,
@@ -7692,7 +7691,7 @@
     _(b'[-u] FILE...'),
     helpcategory=command.CATEGORY_IMPORT_EXPORT,
 )
-def unbundle(ui, repo, fname1, *fnames, _unbundle_source=b'unbundle', **opts):
+def unbundle(ui, repo, fname1, *fnames, **opts):
     """apply one or more bundle files
 
     Apply one or more bundle files generated by :hg:`bundle`.
@@ -7700,42 +7699,7 @@
     Returns 0 on success, 1 if an update has unresolved files.
     """
     fnames = (fname1,) + fnames
-
-    with repo.lock():
-        for fname in fnames:
-            f = hg.openpath(ui, fname)
-            gen = exchange.readbundle(ui, f, fname)
-            if isinstance(gen, streamclone.streamcloneapplier):
-                raise error.InputError(
-                    _(
-                        b'packed bundles cannot be applied with '
-                        b'"hg unbundle"'
-                    ),
-                    hint=_(b'use "hg debugapplystreamclonebundle"'),
-                )
-            url = b'bundle:' + fname
-            try:
-                txnname = b'unbundle'
-                if not isinstance(gen, bundle2.unbundle20):
-                    txnname = b'unbundle\n%s' % urlutil.hidepassword(url)
-                with repo.transaction(txnname) as tr:
-                    op = bundle2.applybundle(
-                        repo,
-                        gen,
-                        tr,
-                        source=_unbundle_source,  # used by debug::unbundle
-                        url=url,
-                    )
-            except error.BundleUnknownFeatureError as exc:
-                raise error.Abort(
-                    _(b'%s: unknown bundle feature, %s') % (fname, exc),
-                    hint=_(
-                        b"see https://mercurial-scm.org/"
-                        b"wiki/BundleFeature for more "
-                        b"information"
-                    ),
-                )
-            modheads = bundle2.combinechangegroupresults(op)
+    modheads = cmdutil.unbundle_files(ui, repo, fnames)
 
     if cmdutil.postincoming(ui, repo, modheads, opts.get('update'), None, None):
         return 1
--- a/mercurial/debugcommands.py	Wed Mar 27 17:29:48 2024 +0000
+++ b/mercurial/debugcommands.py	Wed Mar 27 17:46:23 2024 +0000
@@ -4078,26 +4078,17 @@
 
 @command(
     b'debug::unbundle',
-    [
-        (
-            b'u',
-            b'update',
-            None,
-            _(b'update to new branch head if changesets were unbundled'),
-        )
-    ],
-    _(b'[-u] FILE...'),
+    [],
+    _(b'FILE...'),
     helpcategory=command.CATEGORY_IMPORT_EXPORT,
 )
-def debugunbundle(ui, repo, *args, **kwargs):
+def debugunbundle(ui, repo, fname1, *fnames):
     """same as `hg unbundle`, but pretent to come from a push
 
     This is useful to debug behavior and performance change in this case.
     """
-    from . import commands  # avoid cycle
-
-    unbundle = cmdutil.findcmd(b'unbundle', commands.table)[1][0]
-    return unbundle(ui, repo, *args, _unbundle_source=b'push', **kwargs)
+    fnames = (fname1,) + fnames
+    cmdutil.unbundle_files(ui, repo, fnames)
 
 
 @command(
--- a/tests/test-completion.t	Wed Mar 27 17:29:48 2024 +0000
+++ b/tests/test-completion.t	Wed Mar 27 17:46:23 2024 +0000
@@ -284,7 +284,7 @@
   debug-revlog-stats: changelog, manifest, filelogs, template
   debug::stable-tail-sort: template
   debug::stable-tail-sort-leaps: template, specific
-  debug::unbundle: update
+  debug::unbundle: 
   debugancestor: 
   debugantivirusrunning: 
   debugapplystreamclonebundle: 
--- a/tests/test-debugcommands.t	Wed Mar 27 17:29:48 2024 +0000
+++ b/tests/test-debugcommands.t	Wed Mar 27 17:46:23 2024 +0000
@@ -663,9 +663,6 @@
   adding manifests
   adding file changes
   added 0 changesets with 0 changes to 1 files (no-pure !)
-  9 local changesets published (no-pure !)
-  3 local changesets published (pure !)
-  (run 'hg update' to get a working copy)
 
 Test debugcolor