Skip to content

Commit dc79687

Browse files
Huynh Khoi Nguyen Nguyengitster
authored andcommitted
Let core.excludesfile default to $XDG_CONFIG_HOME/git/ignore
To use the feature of core.excludesfile, the user needs: 1. to create such a file, 2. and add configuration variable to point at it. Instead, we can make this a one-step process by choosing a default value which points to a filename in the user's $HOME, that is unlikely to already exist on the system, and only use the presence of the file as a cue that the user wants to use that feature. And we use "${XDG_CONFIG_HOME:-$HOME/.config/git}/ignore" as such a file, in the same directory as the newly added configuration file ("${XDG_CONFIG_HOME:-$HOME/.config/git}/config). The use of this directory is in line with XDG specification as a location to store such application specific files. 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 21cf322 commit dc79687

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

Documentation/config.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,9 @@ core.excludesfile::
483483
'.git/info/exclude', git looks into this file for patterns
484484
of files which are not meant to be tracked. "`~/`" is expanded
485485
to the value of `$HOME` and "`~user/`" to the specified user's
486-
home directory. See linkgit:gitignore[5].
486+
home directory. Its default value is $XDG_CONFIG_HOME/git/ignore.
487+
If $XDG_CONFIG_HOME is either not set or empty, $HOME/.config/git/ignore
488+
is used instead. See linkgit:gitignore[5].
487489

488490
core.askpass::
489491
Some commands (e.g. svn and http interfaces) that interactively

Documentation/gitignore.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ the repository but are specific to one user's workflow) should go into
5050
the `$GIT_DIR/info/exclude` file. Patterns which a user wants git to
5151
ignore in all situations (e.g., backup or temporary files generated by
5252
the user's editor of choice) generally go into a file specified by
53-
`core.excludesfile` in the user's `~/.gitconfig`.
53+
`core.excludesfile` in the user's `~/.gitconfig`. Its default value is
54+
$XDG_CONFIG_HOME/git/ignore. If $XDG_CONFIG_HOME is either not set or empty,
55+
$HOME/.config/git/ignore is used instead.
5456

5557
The underlying git plumbing tools, such as
5658
'git ls-files' and 'git read-tree', read

dir.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1292,12 +1292,17 @@ int remove_dir_recursively(struct strbuf *path, int flag)
12921292
void setup_standard_excludes(struct dir_struct *dir)
12931293
{
12941294
const char *path;
1295+
char *xdg_path;
12951296

12961297
dir->exclude_per_dir = ".gitignore";
12971298
path = git_path("info/exclude");
1299+
if (!excludes_file) {
1300+
home_config_paths(NULL, &xdg_path, "ignore");
1301+
excludes_file = xdg_path;
1302+
}
12981303
if (!access(path, R_OK))
12991304
add_excludes_from_file(dir, path);
1300-
if (excludes_file && !access(excludes_file, R_OK))
1305+
if (!access(excludes_file, R_OK))
13011306
add_excludes_from_file(dir, excludes_file);
13021307
}
13031308

t/t1306-xdg-files.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,33 @@ test_expect_success 'read with --list: xdg file exists and ~/.gitconfig exists'
6767
'
6868

6969

70+
test_expect_success 'Setup' '
71+
git init git &&
72+
cd git &&
73+
echo foo >to_be_excluded
74+
'
75+
76+
77+
test_expect_success 'Exclusion of a file in the XDG ignore file' '
78+
mkdir -p "$HOME"/.config/git/ &&
79+
echo to_be_excluded >"$HOME"/.config/git/ignore &&
80+
test_must_fail git add to_be_excluded
81+
'
82+
83+
84+
test_expect_success 'Exclusion in both XDG and local ignore files' '
85+
echo to_be_excluded >.gitignore &&
86+
test_must_fail git add to_be_excluded
87+
'
88+
89+
90+
test_expect_success 'Exclusion in a non-XDG global ignore file' '
91+
rm .gitignore &&
92+
echo >"$HOME"/.config/git/ignore &&
93+
echo to_be_excluded >"$HOME"/my_gitignore &&
94+
git config core.excludesfile "$HOME"/my_gitignore &&
95+
test_must_fail git add to_be_excluded
96+
'
97+
98+
7099
test_done

0 commit comments

Comments
 (0)