hg: allow usage of XDG_CONFIG_HOME/hg/hgrc
authorDavid Demelier <demelier.david@gmail.com>
Tue, 07 Feb 2017 17:33:35 +0100
changeset 30941 354020079723
parent 30940 c8081ea63591
child 30942 65a3b4d67a65
hg: allow usage of XDG_CONFIG_HOME/hg/hgrc Modern applications must use the following paths to store configuration files: - $XDG_CONFIG_HOME/hg/hgrc, - $HOME/.config/hg/hgrc (if XDG_CONFIG_HOME is unset or not absolute). For backward compatibility, ~/.hgrc is still created if no hgrc exist using hg config --edit. See https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
mercurial/help/config.txt
mercurial/scmposix.py
tests/test-xdg.t
--- a/mercurial/help/config.txt	Wed Feb 01 17:47:35 2017 +0100
+++ b/mercurial/help/config.txt	Tue Feb 07 17:33:35 2017 +0100
@@ -56,6 +56,7 @@
 
   - ``<repo>/.hg/hgrc`` (per-repository)
   - ``$HOME/.hgrc`` (per-user)
+  - ``${XDG_CONFIG_HOME:-$HOME/.config}/hg/hgrc`` (per-user)
   - ``<install-root>/etc/mercurial/hgrc`` (per-installation)
   - ``<install-root>/etc/mercurial/hgrc.d/*.rc`` (per-installation)
   - ``/etc/mercurial/hgrc`` (per-system)
--- a/mercurial/scmposix.py	Wed Feb 01 17:47:35 2017 +0100
+++ b/mercurial/scmposix.py	Tue Feb 07 17:33:35 2017 +0100
@@ -40,8 +40,15 @@
 def userrcpath():
     if pycompat.sysplatform == 'plan9':
         return [encoding.environ['home'] + '/lib/hgrc']
+    elif pycompat.sysplatform == 'darwin':
+        return [os.path.expanduser('~/.hgrc')]
     else:
-        return [os.path.expanduser('~/.hgrc')]
+        confighome = encoding.environ.get('XDG_CONFIG_HOME')
+        if confighome is None or not os.path.isabs(confighome):
+            confighome = os.path.expanduser('~/.config')
+
+        return [os.path.expanduser('~/.hgrc'),
+                os.path.join(confighome, 'hg', 'hgrc')]
 
 def termsize(ui):
     try:
--- /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 no-osx
+
+  $ mkdir -p xdgconf/hg
+  $ echo '[ui]' > xdgconf/hg/hgrc
+  $ echo 'username = foobar' >> xdgconf/hg/hgrc
+  $ XDG_CONFIG_HOME="`pwd`/xdgconf" ; export XDG_CONFIG_HOME
+  $ unset HGRCPATH
+  $ hg config ui.username
+  foobar
+
+#endif