Skip to content

Commit

Permalink
config: write to $XDG_CONFIG_HOME/git/config file when appropriate
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Huynh Khoi Nguyen Nguyen authored and Junio C Hamano committed Jun 25, 2012
1 parent 684e40f commit 0e8593d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
3 changes: 2 additions & 1 deletion Documentation/git-config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ OPTIONS

--global::
For writing options: write to global ~/.gitconfig file rather than
the repository .git/config.
the repository .git/config, write to $XDG_CONFIG_HOME/git/config file
if this file exists and the ~/.gitconfig file doesn't.
+
For reading options: read only from global ~/.gitconfig and from
$XDG_CONFIG_HOME/git/config rather than from all available files.
Expand Down
5 changes: 1 addition & 4 deletions builtin/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)

home_config_paths(&user_config, &xdg_config, "config");

if (access(user_config, R_OK) && !access(xdg_config, R_OK) &&
(actions == ACTION_LIST ||
actions == ACTION_GET_COLOR ||
actions == ACTION_GET_COLORBOOL))
if (access(user_config, R_OK) && !access(xdg_config, R_OK))
given_config_file = xdg_config;
else if (user_config)
given_config_file = user_config;
Expand Down
30 changes: 30 additions & 0 deletions t/t1306-xdg-files.sh
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,34 @@ test_expect_success 'Checking attributes in a non-XDG global attributes file' '
'


test_expect_success 'write: xdg file exists and ~/.gitconfig doesn'\''t' '
mkdir -p "$HOME"/.config/git &&
>"$HOME"/.config/git/config &&
test_might_fail rm "$HOME"/.gitconfig &&
git config --global user.name "write_config" &&
echo "[user]" >expected &&
echo " name = write_config" >>expected &&
test_cmp expected "$HOME"/.config/git/config
'


test_expect_success 'write: xdg file exists and ~/.gitconfig exists' '
>"$HOME"/.gitconfig &&
git config --global user.name "write_gitconfig" &&
echo "[user]" >expected &&
echo " name = write_gitconfig" >>expected &&
test_cmp expected "$HOME"/.gitconfig
'


test_expect_success 'write: ~/.config/git/ exists and config file doesn'\''t' '
test_might_fail rm "$HOME"/.gitconfig &&
test_might_fail rm "$HOME"/.config/git/config &&
git config --global user.name "write_gitconfig" &&
echo "[user]" >expected &&
echo " name = write_gitconfig" >>expected &&
test_cmp expected "$HOME"/.gitconfig
'


test_done

0 comments on commit 0e8593d

Please sign in to comment.