changeset 45486:ac7a3da0dbb6

config: add `--shared` flag to edit config file of shared source With `format.exp-share-safe` enabled, we now read the `.hg/hgrc` of the shared source also. This patch adds `--shared` flag to `hg config` command which can be used to edit that shared source config file. It only works if the repository is shared one and is shared using the safe method. Differential Revision: https://phab.mercurial-scm.org/D8659
author Pulkit Goyal <7895pulkit@gmail.com>
date Thu, 17 Sep 2020 18:49:57 -0700
parents b71858b42963
children 78f0bb37f52d
files mercurial/commands.py tests/test-completion.t tests/test-share-safe.t
diffstat 3 files changed, 40 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/commands.py	Thu Jul 02 16:23:36 2020 +0530
+++ b/mercurial/commands.py	Thu Sep 17 18:49:57 2020 -0700
@@ -55,6 +55,7 @@
     pycompat,
     rcutil,
     registrar,
+    requirements,
     revsetlang,
     rewriteutil,
     scmutil,
@@ -66,6 +67,7 @@
     ui as uimod,
     util,
     verify as verifymod,
+    vfs as vfsmod,
     wireprotoserver,
 )
 from .utils import (
@@ -2141,6 +2143,12 @@
         (b'u', b'untrusted', None, _(b'show untrusted configuration options')),
         (b'e', b'edit', None, _(b'edit user config')),
         (b'l', b'local', None, _(b'edit repository config')),
+        (
+            b'',
+            b'shared',
+            None,
+            _(b'edit shared source repository config (EXPERIMENTAL)'),
+        ),
         (b'g', b'global', None, _(b'edit global config')),
     ]
     + formatteropts,
@@ -2179,22 +2187,37 @@
       :source:  String. Filename and line number where the item is defined.
       :value:   String. Config value.
 
+      The --shared flag can be used to edit the config file of shared source
+      repository. It only works when you have shared using the experimental
+      share safe feature.
+
     Returns 0 on success, 1 if NAME does not exist.
 
     """
 
     opts = pycompat.byteskwargs(opts)
-    editopts = (b'edit', b'local', b'global')
+    editopts = (b'edit', b'local', b'global', b'shared')
     if any(opts.get(o) for o in editopts):
-        if opts.get(b'local') and opts.get(b'global'):
-            raise error.Abort(_(b"can't use --local and --global together"))
-
+        cmdutil.check_at_most_one_arg(opts, *editopts[1:])
         if opts.get(b'local'):
             if not repo:
                 raise error.Abort(_(b"can't use --local outside a repository"))
             paths = [repo.vfs.join(b'hgrc')]
         elif opts.get(b'global'):
             paths = rcutil.systemrcpath()
+        elif opts.get(b'shared'):
+            if not repo.shared():
+                raise error.Abort(
+                    _(b"repository is not shared; can't use --shared")
+                )
+                if requirements.SHARESAFE_REQUIREMENT not in repo.requirements:
+                    raise error.Abort(
+                        _(
+                            b"share safe feature not unabled; "
+                            b"unable to edit shared source repository config"
+                        )
+                    )
+            paths = [vfsmod.vfs(repo.sharedpath).join(b'hgrc')]
         else:
             paths = rcutil.userrcpath()
 
--- a/tests/test-completion.t	Thu Jul 02 16:23:36 2020 +0530
+++ b/tests/test-completion.t	Thu Sep 17 18:49:57 2020 -0700
@@ -258,7 +258,7 @@
   cat: output, rev, decode, include, exclude, template
   clone: noupdate, updaterev, rev, branch, pull, uncompressed, stream, ssh, remotecmd, insecure
   commit: addremove, close-branch, amend, secret, edit, force-close-branch, interactive, include, exclude, message, logfile, date, user, subrepos
-  config: untrusted, edit, local, global, template
+  config: untrusted, edit, local, shared, global, template
   continue: dry-run
   copy: forget, after, at-rev, force, include, exclude, dry-run
   debugancestor: 
--- a/tests/test-share-safe.t	Thu Jul 02 16:23:36 2020 +0530
+++ b/tests/test-share-safe.t	Thu Sep 17 18:49:57 2020 -0700
@@ -33,6 +33,10 @@
   $ hg ci -Aqm "added a"
   $ echo b > b
   $ hg ci -Aqm "added b"
+
+  $ HGEDITOR=cat hg config --shared
+  abort: repository is not shared; can't use --shared
+  [255]
   $ cd ..
 
 Create a shared repo and check the requirements are shared and read correctly
@@ -85,6 +89,14 @@
   $ hg showconfig ui.curses
   false
 
+  $ HGEDITOR=cat hg config --shared
+  [ui]
+  curses=true
+
+  $ HGEDITOR=cat hg config --local
+  [ui]
+  curses=false
+
 Testing that hooks set in source repository also runs in shared repo
 
   $ cd ../source