changeset 44127:59b3fe1e2021

phabricator: use .arcconfig for the callsign if not set locally (issue6243) This makes things easier for people working with more than one repository because this file can be committed to each repository. The bug report asks to read <repo>/.arcrc, but AFAICT, that file lives in ~/ and holds the credentials. And we already track an .arcconfig file. Any callsign set globally is still used if that is all that is present, but .arcconfig will override it if available. The idea behind letting the local hgrc override .arcconfig is that the developer may need to do testing against another server, and not dirty the working directory. Originally I was going to just try to read the callsign in `getrepophid()` if it wasn't present in the hg config. That works fine, but I think it also makes sense to read the URL from this file too. That would have worked less well because `readurltoken()` doesn't have access to the repo object to know where to find the file. Supplimenting the config mechanism is less magical because it reports the source and value of the properties used, and it doesn't need to read the file twice. Invalid hgrc files generally cause the program to abort. I only flagged it as a warning here because it's not our config file, not crucial to the whole program operating, and really shouldn't be corrupt in the typical case where it is checked into the repo. Differential Revision: https://phab.mercurial-scm.org/D7934
author Matt Harbison <matt_harbison@yahoo.com>
date Fri, 17 Jan 2020 14:21:40 -0500
parents e2278581b1c6
children ff396501e841
files hgext/phabricator.py tests/test-phabricator.t
diffstat 2 files changed, 67 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/phabricator.py	Fri Jan 17 13:29:47 2020 -0500
+++ b/hgext/phabricator.py	Fri Jan 17 14:21:40 2020 -0500
@@ -66,6 +66,7 @@
     exthelper,
     graphmod,
     httpconnection as httpconnectionmod,
+    localrepo,
     logcmdutil,
     match,
     mdiff,
@@ -101,6 +102,7 @@
 command = eh.command
 configtable = eh.configtable
 templatekeyword = eh.templatekeyword
+uisetup = eh.finaluisetup
 
 # developer config: phabricator.batchsize
 eh.configitem(
@@ -152,6 +154,39 @@
 ]
 
 
+@eh.wrapfunction(localrepo, "loadhgrc")
+def _loadhgrc(orig, ui, wdirvfs, hgvfs, requirements):
+    """Load ``.arcconfig`` content into a ui instance on repository open.
+    """
+    result = False
+    arcconfig = {}
+
+    try:
+        # json.loads only accepts bytes from 3.6+
+        rawparams = encoding.unifromlocal(wdirvfs.read(b".arcconfig"))
+        # json.loads only returns unicode strings
+        arcconfig = pycompat.rapply(
+            lambda x: encoding.unitolocal(x)
+            if isinstance(x, pycompat.unicode)
+            else x,
+            pycompat.json_loads(rawparams),
+        )
+
+        result = True
+    except ValueError:
+        ui.warn(_(b"invalid JSON in %s\n") % wdirvfs.join(b".arcconfig"))
+    except IOError:
+        pass
+
+    if b"repository.callsign" in arcconfig:
+        ui.applyconfig(
+            {(b"phabricator", b"callsign"): arcconfig[b"repository.callsign"]},
+            source=wdirvfs.join(b".arcconfig"),
+        )
+
+    return orig(ui, wdirvfs, hgvfs, requirements) or result  # Load .hg/hgrc
+
+
 def vcrcommand(name, flags, spec, helpcategory=None, optionalrepo=False):
     fullflags = flags + _VCR_FLAGS
 
--- a/tests/test-phabricator.t	Fri Jan 17 13:29:47 2020 -0500
+++ b/tests/test-phabricator.t	Fri Jan 17 14:21:40 2020 -0500
@@ -210,5 +210,37 @@
            extensions.loadall(self.ui)
    
   
+A bad .arcconfig doesn't error out
+  $ echo 'garbage' > .arcconfig
+  $ hg config phabricator --debug
+  invalid JSON in $TESTTMP/repo/.arcconfig
+  read config from: */.hgrc (glob)
+  $TESTTMP/repo/.hg/hgrc:*: phabricator.url=https://phab.mercurial-scm.org/ (glob)
+  $TESTTMP/repo/.hg/hgrc:*: phabricator.callsign=HG (glob)
+
+The .arcconfig content overrides global config
+  $ cat >> $HGRCPATH << EOF
+  > [phabricator]
+  > url = global
+  > callsign = global
+  > EOF
+  $ cp $TESTDIR/../.arcconfig .
+  $ mv .hg/hgrc .hg/hgrc.bak
+  $ hg config phabricator --debug
+  read config from: */.hgrc (glob)
+  */.hgrc:*: phabricator.url=global (glob)
+  $TESTTMP/repo/.arcconfig: phabricator.callsign=HG
+
+But it doesn't override local config
+  $ cat >> .hg/hgrc << EOF
+  > [phabricator]
+  > url = local
+  > callsign = local
+  > EOF
+  $ hg config phabricator --debug
+  read config from: */.hgrc (glob)
+  $TESTTMP/repo/.hg/hgrc:*: phabricator.url=local (glob)
+  $TESTTMP/repo/.hg/hgrc:*: phabricator.callsign=local (glob)
+  $ mv .hg/hgrc.bak .hg/hgrc
 
   $ cd ..