Patchwork [v2] hg: allow usage of XDG_CONFIG_HOME and $HOME/.config/hgrc

login
register
mail settings
Submitter David Demelier
Date Feb. 7, 2017, 10:59 p.m.
Message ID <880021eb7ec0be00c767.1486508399@localhost.localdomain>
Download mbox | patch
Permalink /patch/18347/
State Superseded
Headers show

Comments

David Demelier - Feb. 7, 2017, 10:59 p.m.
# HG changeset patch
# User David Demelier <demelier.david@gmail.com>
# Date 1486485215 -3600
#      Tue Feb 07 17:33:35 2017 +0100
# Node ID 880021eb7ec0be00c76739cb647e0f5712420c81
# Parent  1f51b4658f21bbb797e922d155c1046eddccf91d
hg: allow usage of XDG_CONFIG_HOME and $HOME/.config/hgrc

Modern applications must use the following paths to store configuration files:

  - $XDG_CONFIG_HOME/hgrc
  - $HOME/.config/hgrc (if XDG_CONFIG_HOME is not set)

For convenience, these paths are now evaluated first and the old $HOME/.hgrc is
used as a fallback.

See https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
Augie Fackler - Feb. 8, 2017, 4:01 p.m.
> On Feb 7, 2017, at 17:59, David Demelier <demelier.david@gmail.com> wrote:
> 
> # HG changeset patch
> # User David Demelier <demelier.david@gmail.com>
> # Date 1486485215 -3600
> #      Tue Feb 07 17:33:35 2017 +0100
> # Node ID 880021eb7ec0be00c76739cb647e0f5712420c81
> # Parent  1f51b4658f21bbb797e922d155c1046eddccf91d
> hg: allow usage of XDG_CONFIG_HOME and $HOME/.config/hgrc
> 
> Modern applications must use the following paths to store configuration files:

"must" is such a strong word. I kinda disagree with the whole spirit of moving things inside XDG_CONFIG_HOME, but whatever.

That said, pip looks at $XDG_CONFIG_HOME/pip, so I suspect (the spec is unclear) that we should be storing things in $XDG_CONFIG_HOME/hg - that's also consistent with what git does (search for XDG_CONFIG on https://git-scm.com/docs/git-config)

> 
>  - $XDG_CONFIG_HOME/hgrc
>  - $HOME/.config/hgrc (if XDG_CONFIG_HOME is not set)
> 
> For convenience, these paths are now evaluated first and the old $HOME/.hgrc is
> used as a fallback.
> 
> See https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
> 
> diff -r 1f51b4658f21 -r 880021eb7ec0 mercurial/help/config.txt
> --- a/mercurial/help/config.txt	Thu Feb 02 14:19:48 2017 +0100
> +++ b/mercurial/help/config.txt	Tue Feb 07 17:33:35 2017 +0100
> @@ -55,6 +55,8 @@
>   On Unix, the following files are consulted:
> 
>   - ``<repo>/.hg/hgrc`` (per-repository)
> +  - ``$XDG_CONFIG_HOME/hgrc`` (per-user)
> +  - ``$HOME/.config/hgrc`` (per-user)
>   - ``$HOME/.hgrc`` (per-user)
>   - ``<install-root>/etc/mercurial/hgrc`` (per-installation)
>   - ``<install-root>/etc/mercurial/hgrc.d/*.rc`` (per-installation)
> diff -r 1f51b4658f21 -r 880021eb7ec0 mercurial/scmposix.py
> --- a/mercurial/scmposix.py	Thu Feb 02 14:19:48 2017 +0100
> +++ b/mercurial/scmposix.py	Tue Feb 07 17:33:35 2017 +0100
> @@ -41,7 +41,15 @@
>     if pycompat.sysplatform == 'plan9':
>         return [encoding.environ['home'] + '/lib/hgrc']
>     else:
> -        return [os.path.expanduser('~/.hgrc')]
> +        xdg = encoding.environ.get("XDG_CONFIG_HOME")

Much as this isn't consulted on plan9, it also shouldn't be consulted on sys.platform == 'darwin'.

> +        if xdg is not None:
> +            return [os.path.join(xdg, "hgrc")]

You don't check if this file exists, so if someone sets XDG_CONFIG_HOME, but has an existing ~/.hgrc you've just broken them?

> +        else:
> +            cfg = os.path.expanduser("~/.config/hgrc")
> +            if os.path.isfile(cfg):
> +                return [cfg]
> +            else:
> +                return [os.path.expanduser('~/.hgrc')]
> 
> def termsize(ui):
>     try:
> diff -r 1f51b4658f21 -r 880021eb7ec0 tests/test-xdg.t
> --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
> +++ b/tests/test-xdg.t	Tue Feb 07 17:33:35 2017 +0100
> @@ -0,0 +1,11 @@
> +#if no-windows
> +
> +  $ mkdir xdgconf
> +  $ echo '[ui]' > xdgconf/hgrc
> +  $ echo 'username = foobar' >> xdgconf/hgrc
> +  $ XDG_CONFIG_HOME=xdgconf; export XDG_CONFIG_HOME
> +  $ unset HGRCPATH
> +  $ hg config ui.username
> +  foobar
> +
> +#endif
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
Augie Fackler - Feb. 8, 2017, 4:21 p.m.
> On Feb 8, 2017, at 11:01, Augie Fackler <raf@durin42.com> wrote:
> 
> Much as this isn't consulted on plan9, it also shouldn't be consulted on sys.platform == 'darwin'.

Though it appears ~/.config/git/config might be respected on macOS with git (I didn't test), so maybe we should reserve the right to start consulting this on OS X in the future.
Raffaele Salmaso - Feb. 8, 2017, 5:13 p.m.
On Wed, Feb 8, 2017 at 5:01 PM, Augie Fackler <raf@durin42.com> wrote:

> > hg: allow usage of XDG_CONFIG_HOME and $HOME/.config/hgrcThat said, pip
> looks at $XDG_CONFIG_HOME/pip, so I suspect (the spec is unclear) that we
> should be storing things in $XDG_CONFIG_HOME/hg - that's also consistent
> with what git does (search for XDG_CONFIG on https://git-scm.com/docs/git-
> config)
>
Yes, I use this configuration (export
HGRCPATH=$HOME/.local/mercurial/hgrc), and put in $HOME/.local/mercurial my
personal style and some custom extensions (evolve...).

Patch

diff -r 1f51b4658f21 -r 880021eb7ec0 mercurial/help/config.txt
--- a/mercurial/help/config.txt	Thu Feb 02 14:19:48 2017 +0100
+++ b/mercurial/help/config.txt	Tue Feb 07 17:33:35 2017 +0100
@@ -55,6 +55,8 @@ 
   On Unix, the following files are consulted:
 
   - ``<repo>/.hg/hgrc`` (per-repository)
+  - ``$XDG_CONFIG_HOME/hgrc`` (per-user)
+  - ``$HOME/.config/hgrc`` (per-user)
   - ``$HOME/.hgrc`` (per-user)
   - ``<install-root>/etc/mercurial/hgrc`` (per-installation)
   - ``<install-root>/etc/mercurial/hgrc.d/*.rc`` (per-installation)
diff -r 1f51b4658f21 -r 880021eb7ec0 mercurial/scmposix.py
--- a/mercurial/scmposix.py	Thu Feb 02 14:19:48 2017 +0100
+++ b/mercurial/scmposix.py	Tue Feb 07 17:33:35 2017 +0100
@@ -41,7 +41,15 @@ 
     if pycompat.sysplatform == 'plan9':
         return [encoding.environ['home'] + '/lib/hgrc']
     else:
-        return [os.path.expanduser('~/.hgrc')]
+        xdg = encoding.environ.get("XDG_CONFIG_HOME")
+        if xdg is not None:
+            return [os.path.join(xdg, "hgrc")]
+        else:
+            cfg = os.path.expanduser("~/.config/hgrc")
+            if os.path.isfile(cfg):
+                return [cfg]
+            else:
+                return [os.path.expanduser('~/.hgrc')]
 
 def termsize(ui):
     try:
diff -r 1f51b4658f21 -r 880021eb7ec0 tests/test-xdg.t
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test-xdg.t	Tue Feb 07 17:33:35 2017 +0100
@@ -0,0 +1,11 @@ 
+#if no-windows
+
+  $ mkdir xdgconf
+  $ echo '[ui]' > xdgconf/hgrc
+  $ echo 'username = foobar' >> xdgconf/hgrc
+  $ XDG_CONFIG_HOME=xdgconf; export XDG_CONFIG_HOME
+  $ unset HGRCPATH
+  $ hg config ui.username
+  foobar
+
+#endif