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 <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 dc79687e0b70805894d1b4432cef7164ae86e033
4 files changed +41 -3
Documentation/config.txt
+3 -1
@@ -483,7 +483,9 @@ core.excludesfile::
483 '.git/info/exclude', git looks into this file for patterns
484 of files which are not meant to be tracked. "`~/`" is expanded
485 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].
489
490 core.askpass::
491 Some commands (e.g. svn and http interfaces) that interactively
Documentation/gitignore.txt
+3 -1
@@ -50,7 +50,9 @@ the repository but are specific to one user's workflow) should go into
50 the `$GIT_DIR/info/exclude` file. Patterns which a user wants git to
51 ignore in all situations (e.g., backup or temporary files generated by
52 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.
56
57 The underlying git plumbing tools, such as
58 'git ls-files' and 'git read-tree', read
dir.c
+6 -1
@@ -1292,12 +1292,17 @@ int remove_dir_recursively(struct strbuf *path, int flag)
1292 void setup_standard_excludes(struct dir_struct *dir)
1293 {
1294 const char *path;
1295 + char *xdg_path;
1296
1297 dir->exclude_per_dir = ".gitignore";
1298 path = git_path("info/exclude");
1299 + if (!excludes_file) {
1300 + home_config_paths(NULL, &xdg_path, "ignore");
1301 + excludes_file = xdg_path;
1302 + }
1303 if (!access(path, R_OK))
1304 add_excludes_from_file(dir, path);
1300 - if (excludes_file && !access(excludes_file, R_OK))
1305 + if (!access(excludes_file, R_OK))
1306 add_excludes_from_file(dir, excludes_file);
1307 }
1308
t/t1306-xdg-files.sh
+29
@@ -67,4 +67,33 @@ test_expect_success 'read with --list: xdg file exists and ~/.gitconfig exists'
67 '
68
69
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 +
99 test_done