From 1e6da9d830c7faf1f92a5bb651b77c45d8045d2e Mon Sep 17 00:00:00 2001 From: Jonas Fonseca Date: Wed, 3 Aug 2016 23:26:48 -0400 Subject: [PATCH] Respect the XDG standard for configuration files From GH #513: Rather than reading the user configuration from `~/.tigrc` by default, instead check `$XDG_CONFIG_HOME/tig/config` and only then look for `~/.tigrc`. Further reading: https://ploum.net/207-modify-your-application-to-use-xdg-folders/ Fixes #513 --- NEWS.adoc | 1 + src/options.c | 14 ++++++++++++-- test/tigrc/xdg-config-home-test | 28 ++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100755 test/tigrc/xdg-config-home-test diff --git a/NEWS.adoc b/NEWS.adoc index c402a85..6ed2bb9 100644 --- a/NEWS.adoc +++ b/NEWS.adoc @@ -34,6 +34,7 @@ Improvements: - Compact relative date display mode. (GH #331) - Add date column option controlling whether to show local date. - Move to parent commit in the main view. (GH #388) + - Respect the XDG standard for configuration files. (GH #513) - Support for custom `strftime(3)` date formats, e.g.: set main-view-date = custom diff --git a/src/options.c b/src/options.c index 8d5f207..8b02a85 100644 --- a/src/options.c +++ b/src/options.c @@ -958,6 +958,7 @@ load_options(void) const char *tig_diff_opts = getenv("TIG_DIFF_OPTS"); const bool diff_opts_from_args = !!opt_diff_options; bool custom_tigrc_system = !!tigrc_system; + char buf[SIZEOF_STR]; opt_file_filter = true; if (!find_option_info_by_value(&opt_diff_context)->seen) @@ -977,8 +978,17 @@ load_options(void) return error("Error in built-in config"); } - if (!tigrc_user) - tigrc_user = TIG_USER_CONFIG; + if (!tigrc_user) { + const char *xdg_config_home = getenv("XDG_CONFIG_HOME"); + + if (!xdg_config_home) + tigrc_user = TIG_USER_CONFIG; + else if (!string_format(buf, "%s/tig/config", xdg_config_home)) + return error("Failed to expand $XDG_CONFIG_HOME"); + else + tigrc_user = buf; + } + load_option_file(tigrc_user); if (!diff_opts_from_args && tig_diff_opts && *tig_diff_opts) { diff --git a/test/tigrc/xdg-config-home-test b/test/tigrc/xdg-config-home-test new file mode 100755 index 0000000..8b9ad3b --- /dev/null +++ b/test/tigrc/xdg-config-home-test @@ -0,0 +1,28 @@ +#!/bin/sh + +. libtest.sh + +tigrc < +" + +#export TIGRC_SYSTEM="should-not-be-loaded" +export XDG_CONFIG_HOME="$HOME/.config" +mkdir -p "$XDG_CONFIG_HOME/tig" + +cat > "$XDG_CONFIG_HOME/tig/config" < @sh -c 'echo ran > ~/RUNME' +EOF + +test_tig status + +assert_equals stderr <