Skip to content

Commit 0e8593d

Browse files
Huynh Khoi Nguyen Nguyengitster
authored andcommitted
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 <[email protected]> Signed-off-by: Valentin Duperray <[email protected]> Signed-off-by: Franck Jonas <[email protected]> Signed-off-by: Lucien Kong <[email protected]> Signed-off-by: Thomas Nguy <[email protected]> Signed-off-by: Matthieu Moy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 684e40f commit 0e8593d

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

Documentation/git-config.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ OPTIONS
9797

9898
--global::
9999
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.
101102
+
102103
For reading options: read only from global ~/.gitconfig and from
103104
$XDG_CONFIG_HOME/git/config rather than from all available files.

builtin/config.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
387387

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

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))
394391
given_config_file = xdg_config;
395392
else if (user_config)
396393
given_config_file = user_config;

t/t1306-xdg-files.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,34 @@ test_expect_success 'Checking attributes in a non-XDG global attributes file' '
125125
'
126126

127127

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+
128158
test_done

0 commit comments

Comments
 (0)