config: write to $XDG_CONFIG_HOME/git/config file when appropriate

Teach git to write to $XDG_CONFIG_HOME/git/config if - it already exists, - $HOME/.gitconfig file doesn't, and - The --global option is used. Otherwise, write to $HOME/.gitconfig when the --global option is given, as before. If the user doesn't create $XDG_CONFIG_HOME/git/config, there is absolutely no change. Users can use this new file only if they want. If $XDG_CONFIG_HOME is either not set or empty, $HOME/.config/git/config will be used. Advice for users who often come back to an old version of Git: you shouldn't create this file. Signed-off-by: Huynh Khoi Nguyen Nguyen <Huynh-Khoi-Nguyen.Nguyen@ensimag.imag.fr> Signed-off-by: Valentin Duperray <Valentin.Duperray@ensimag.imag.fr> Signed-off-by: Franck Jonas <Franck.Jonas@ensimag.imag.fr> Signed-off-by: Lucien Kong <Lucien.Kong@ensimag.imag.fr> Signed-off-by: Thomas Nguy <Thomas.Nguy@ensimag.imag.fr> Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Huynh Khoi Nguyen Nguyen committed Jun 22, 2012 at 11:03 UTC 0e8593dc5b812df00400347e88f5707225fe831e
3 files changed +33 -5
Documentation/git-config.txt
+2 -1
@@ -97,7 +97,8 @@ OPTIONS
97
98 --global::
99 For writing options: write to global ~/.gitconfig file rather than
100 - the repository .git/config.
100 + the repository .git/config, write to $XDG_CONFIG_HOME/git/config file
101 + if this file exists and the ~/.gitconfig file doesn't.
102 +
103 For reading options: read only from global ~/.gitconfig and from
104 $XDG_CONFIG_HOME/git/config rather than from all available files.
builtin/config.c
+1 -4
@@ -387,10 +387,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
387
388 home_config_paths(&user_config, &xdg_config, "config");
389
390 - if (access(user_config, R_OK) && !access(xdg_config, R_OK) &&
391 - (actions == ACTION_LIST ||
392 - actions == ACTION_GET_COLOR ||
393 - actions == ACTION_GET_COLORBOOL))
390 + if (access(user_config, R_OK) && !access(xdg_config, R_OK))
391 given_config_file = xdg_config;
392 else if (user_config)
393 given_config_file = user_config;
t/t1306-xdg-files.sh
+30
@@ -125,4 +125,34 @@ test_expect_success 'Checking attributes in a non-XDG global attributes file' '
125 '
126
127
128 +test_expect_success 'write: xdg file exists and ~/.gitconfig doesn'\''t' '
129 + mkdir -p "$HOME"/.config/git &&
130 + >"$HOME"/.config/git/config &&
131 + test_might_fail rm "$HOME"/.gitconfig &&
132 + git config --global user.name "write_config" &&
133 + echo "[user]" >expected &&
134 + echo " name = write_config" >>expected &&
135 + test_cmp expected "$HOME"/.config/git/config
136 +'
137 +
138 +
139 +test_expect_success 'write: xdg file exists and ~/.gitconfig exists' '
140 + >"$HOME"/.gitconfig &&
141 + git config --global user.name "write_gitconfig" &&
142 + echo "[user]" >expected &&
143 + echo " name = write_gitconfig" >>expected &&
144 + test_cmp expected "$HOME"/.gitconfig
145 +'
146 +
147 +
148 +test_expect_success 'write: ~/.config/git/ exists and config file doesn'\''t' '
149 + test_might_fail rm "$HOME"/.gitconfig &&
150 + test_might_fail rm "$HOME"/.config/git/config &&
151 + git config --global user.name "write_gitconfig" &&
152 + echo "[user]" >expected &&
153 + echo " name = write_gitconfig" >>expected &&
154 + test_cmp expected "$HOME"/.gitconfig
155 +'
156 +
157 +
158 test_done