changeset 49194:e4b31016e194

auto-upgrade: introduce a way to auto-upgrade to/from tracked-hint This is similar to what we introduced for `share-safe`, but apply to the tracked-hint feature. Differential Revision: https://phab.mercurial-scm.org/D12613
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Tue, 05 Apr 2022 05:20:05 +0200
parents 566066826e7c
children 411d591e0a27
files mercurial/configitems.py mercurial/helptext/config.txt mercurial/upgrade_utils/auto_upgrade.py rust/hg-core/src/requirements.rs rust/rhg/src/main.rs tests/test-help.t tests/test-share-safe.t tests/test-status-tracked-key.t
diffstat 8 files changed, 176 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/configitems.py	Mon Apr 04 19:30:32 2022 +0200
+++ b/mercurial/configitems.py	Tue Apr 05 05:20:05 2022 +0200
@@ -1290,6 +1290,12 @@
 )
 coreconfigitem(
     b'format',
+    b'use-dirstate-tracked-hint.automatic-upgrade-of-mismatching-repositories',
+    default=False,
+    experimental=True,
+)
+coreconfigitem(
+    b'format',
     b'dotencode',
     default=True,
 )
--- a/mercurial/helptext/config.txt	Mon Apr 04 19:30:32 2022 +0200
+++ b/mercurial/helptext/config.txt	Tue Apr 05 05:20:05 2022 +0200
@@ -976,6 +976,27 @@
 
     2) storing the value and comparing it to a later value.
 
+
+``use-dirstate-tracked-hint.automatic-upgrade-of-mismatching-repositories``
+   When enabled, an automatic upgrade will be triggered when a repository format
+   does not match its `use-dirstate-tracked-hint` config.
+
+   This is an advanced behavior that most users will not need. We recommend you
+   don't use this unless you are a seasoned administrator of a Mercurial install
+   base.
+
+   Automatic upgrade means that any process accessing the repository will
+   upgrade the repository format to use `dirstate-tracked-hint`. This only
+   triggers if a change is needed. This also applies to operations that would
+   have been read-only (like hg status).
+
+   This configuration will apply for moves in any direction, either adding the
+   `dirstate-tracked-hint` format if `format.use-dirstate-tracked-hint=yes` or
+   removing the `dirstate-tracked-hint` requirement if
+   `format.use-dirstate-tracked-hint=no`. So we recommend setting both this
+   value and `format.use-dirstate-tracked-hint` at the same time.
+
+
 ``use-persistent-nodemap``
     Enable or disable the "persistent-nodemap" feature which improves
     performance if the Rust extensions are available.
--- a/mercurial/upgrade_utils/auto_upgrade.py	Mon Apr 04 19:30:32 2022 +0200
+++ b/mercurial/upgrade_utils/auto_upgrade.py	Tue Apr 05 05:20:05 2022 +0200
@@ -12,6 +12,24 @@
     scmutil,
 )
 
+from . import (
+    actions,
+    engine,
+)
+
+
+class AutoUpgradeOperation(actions.BaseOperation):
+    """A limited Upgrade Operation used to run simple auto upgrade task
+
+    (Expand it as needed in the future)
+    """
+
+    def __init__(self, req):
+        super().__init__(
+            new_requirements=req,
+            backup_store=False,
+        )
+
 
 def get_share_safe_action(repo):
     """return an automatic-upgrade action for `share-safe` if applicable
@@ -66,8 +84,61 @@
     return action
 
 
+def get_tracked_hint_action(repo):
+    """return an automatic-upgrade action for `tracked-hint` if applicable
+
+    If no action is needed, return None, otherwise return a callback to upgrade
+    or downgrade the repository according the configuration and repository
+    format.
+    """
+    ui = repo.ui
+    requirements = set(repo.requirements)
+    auto_upgrade_tracked_hint = ui.configbool(
+        b'format',
+        b'use-dirstate-tracked-hint.automatic-upgrade-of-mismatching-repositories',
+    )
+
+    action = None
+
+    if auto_upgrade_tracked_hint:
+        th_config = ui.configbool(b'format', b'use-dirstate-tracked-hint')
+        th_local = requirementsmod.DIRSTATE_TRACKED_HINT_V1 in requirements
+        if th_config and not th_local:
+            msg = _(
+                b"automatically upgrading repository to the `tracked-hint`"
+                b" feature\n"
+            )
+            hint = b"(see `hg help config.format.use-dirstate-tracked-hint` for details)\n"
+
+            def action():
+                if not ui.quiet:
+                    ui.write_err(msg)
+                    ui.write_err(hint)
+                requirements.add(requirementsmod.DIRSTATE_TRACKED_HINT_V1)
+                op = AutoUpgradeOperation(requirements)
+                engine.upgrade_tracked_hint(ui, repo, op, add=True)
+
+        elif th_local and not th_config:
+            msg = _(
+                b"automatically downgrading repository from the `tracked-hint`"
+                b" feature\n"
+            )
+            hint = b"(see `hg help config.format.use-dirstate-tracked-hint` for details)\n"
+
+            def action():
+                if not ui.quiet:
+                    ui.write_err(msg)
+                    ui.write_err(hint)
+                requirements.discard(requirementsmod.DIRSTATE_TRACKED_HINT_V1)
+                op = AutoUpgradeOperation(requirements)
+                engine.upgrade_tracked_hint(ui, repo, op, add=False)
+
+    return action
+
+
 AUTO_UPGRADE_ACTIONS = [
     get_share_safe_action,
+    get_tracked_hint_action,
 ]
 
 
--- a/rust/hg-core/src/requirements.rs	Mon Apr 04 19:30:32 2022 +0200
+++ b/rust/hg-core/src/requirements.rs	Tue Apr 05 05:20:05 2022 +0200
@@ -100,6 +100,10 @@
 
 pub const DIRSTATE_V2_REQUIREMENT: &str = "dirstate-v2";
 
+/// A repository that uses the tracked hint dirstate file
+#[allow(unused)]
+pub const DIRSTATE_TRACKED_HINT_V1: &str = "dirstate-tracked-key-v1";
+
 /// When narrowing is finalized and no longer subject to format changes,
 /// we should move this to just "narrow" or similar.
 #[allow(unused)]
--- a/rust/rhg/src/main.rs	Mon Apr 04 19:30:32 2022 +0200
+++ b/rust/rhg/src/main.rs	Tue Apr 05 05:20:05 2022 +0200
@@ -731,6 +731,11 @@
         ("format", "use-share-safe"),
         requirements::SHARESAFE_REQUIREMENT,
     ),
+    (
+        ("format", "use-dirstate-tracked-hint.automatic-upgrade-of-mismatching-repositories"),
+        ("format", "use-dirstate-tracked-hint"),
+        requirements::DIRSTATE_TRACKED_HINT_V1,
+    ),
 ];
 
 /// Mercurial allows users to automatically upgrade their repository.
--- a/tests/test-help.t	Mon Apr 04 19:30:32 2022 +0200
+++ b/tests/test-help.t	Tue Apr 05 05:20:05 2022 +0200
@@ -1602,6 +1602,8 @@
   
       "use-dirstate-tracked-hint"
   
+      "use-dirstate-tracked-hint.automatic-upgrade-of-mismatching-repositories"
+  
       "use-persistent-nodemap"
   
       "use-share-safe"
--- a/tests/test-share-safe.t	Mon Apr 04 19:30:32 2022 +0200
+++ b/tests/test-share-safe.t	Tue Apr 05 05:20:05 2022 +0200
@@ -603,3 +603,36 @@
   |
   o  f3ba8b99bb6f897c87bbc1c07b75c6ddf43a4f77: added foo
   
+
+Test automatique upgrade/downgrade of main-repository
+------------------------------------------------------
+
+create an initial repository
+
+  $ hg init auto-upgrade \
+  > --config format.use-share-safe=no
+  $ hg debugbuilddag -R auto-upgrade --new-file .+5
+  $ hg -R auto-upgrade update
+  6 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  $ hg debugformat -R auto-upgrade | grep share-safe
+  share-safe:          no
+
+upgrade it to share-safe automatically
+
+  $ hg status -R auto-upgrade \
+  >     --config format.use-share-safe.automatic-upgrade-of-mismatching-repositories=yes \
+  >     --config format.use-share-safe=yes
+  automatically upgrading repository to the `share-safe` feature
+  (see `hg help config.format.use-share-safe` for details)
+  $ hg debugformat -R auto-upgrade | grep share-safe
+  share-safe:         yes
+
+downgrade it from share-safe automatically
+
+  $ hg status -R auto-upgrade \
+  >     --config format.use-share-safe.automatic-upgrade-of-mismatching-repositories=yes \
+  >     --config format.use-share-safe=no
+  automatically downgrading repository from the `share-safe` feature
+  (see `hg help config.format.use-share-safe` for details)
+  $ hg debugformat -R auto-upgrade | grep share-safe
+  share-safe:          no
--- a/tests/test-status-tracked-key.t	Mon Apr 04 19:30:32 2022 +0200
+++ b/tests/test-status-tracked-key.t	Tue Apr 05 05:20:05 2022 +0200
@@ -202,3 +202,37 @@
   .hg/dirstate-tracked-hint
   $ hg debugrequires | grep 'tracked'
   dirstate-tracked-key-v1
+  $ cd ..
+
+Test automatic upgrade and downgrade
+------------------------------------
+
+create an initial repository
+
+  $ hg init auto-upgrade \
+  > --config format.use-dirstate-tracked-hint=no
+  $ hg debugbuilddag -R auto-upgrade --new-file .+5
+  $ hg -R auto-upgrade update
+  6 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  $ hg debugformat -R auto-upgrade | grep tracked
+  tracked-hint:        no
+
+upgrade it to dirstate-tracked-hint automatically
+
+  $ hg status -R auto-upgrade \
+  > --config format.use-dirstate-tracked-hint.automatic-upgrade-of-mismatching-repositories=yes \
+  > --config format.use-dirstate-tracked-hint=yes
+  automatically upgrading repository to the `tracked-hint` feature
+  (see `hg help config.format.use-dirstate-tracked-hint` for details)
+  $ hg debugformat -R auto-upgrade | grep tracked
+  tracked-hint:       yes
+
+downgrade it from dirstate-tracked-hint automatically
+
+  $ hg status -R auto-upgrade \
+  > --config format.use-dirstate-tracked-hint.automatic-upgrade-of-mismatching-repositories=yes \
+  > --config format.use-dirstate-tracked-hint=no
+  automatically downgrading repository from the `tracked-hint` feature
+  (see `hg help config.format.use-dirstate-tracked-hint` for details)
+  $ hg debugformat -R auto-upgrade | grep tracked
+  tracked-hint:        no