.gitignore sub-dir exclusions not overriding '*'
The behaviour below is seen with both git 1.8.3.2 and git 2.1.3; I am not subscribed to the vger list, please keep me in the CC list.
Use-case: git repo which only holds PGP-encrypted files with a .asc extension, no matter which sub-directory they're in, or if in the top directory.
Simple layout; for demo purposes names starting 'incl' should end up included, those starting 'excl' should end up excluded, but not based on those prefices: they're success result indicators; so:
cd wherever
git init
mkdir foo
touch incl.asc excl excl.txt foo/incl.asc foo/excl.txt foo/excl $EDITOR .gitignore
Expected to work as .gitignore in top-level of repo:
*
!**/*.asc
!.gitignoreWith that, git status ignores the contents of foo/ thusly:
$ git check-ignore -v foo/incl.asc
.gitignore:1:* foo/incl.ascCommenting out the '*' line and removing the '!' from the second, the **/*.asc clearly matches. The only way I can make this style work is to set the first line to '**/*.*' which fails to exclude the plain 'excl' files (no extension).
It seems that there's some magic around '*' as the entire final path component of a pattern which causes it to match against the entire directory, and excludes of the directory can not be overriden by matches against '*.ext' within the directory, even when they come later in the same config file at the same precedence.
This does not seem to my reading to match the behaviour described by git help gitignore (checked in both versions of git) and so seems to me to be a bug, but if it's a failure of my understanding, please help me to understand where I messed up.
Thanks,
-Phil
Quoted 6 lines · Phil Pennock
Expected to work as .gitignore in top-level of repo:
* !**/*.asc !.gitignore
gitignore man page has this "It is not possible to re-include a file if a parent directory of that file is excluded". In this case, directory "foo" is ignored by "*". Although it makes sense for this particular case to re-include something in foo because we can clearly see there are rules to re-include. It's on my todo list, but I don't know when it will be implemented.
Signature · 1 line
Duy
PATCH 1/1 dir.c: allow re-include after a dir is excluded in some cases
If we exclude a directory and have no knowledge in advance if there will be any negative rules on that directory, then it makes no sense to enter the directory and search for those negative rules. That defeats the purpose of excluding in the first place.
However there are cases where we know in advance about such negative rules. One of them is when the rule to exclude a dir and negative rules on that dir appear on the same .gitignore file. This case be easily detected.
Note that I do not support this case
*
!bar
because I fear of performance degradation because people may not realize "!bar" will force recursing indefinitely. So you need at least a slash some where to nail your pattern to some directory. "!/bar" is fine (but pointless), "bar/*.c" or "**/foo" may be seen more often.
This is a quick prototype. I know the two statements in add_exclude() is not good enough. I should not count the last trailing slash because that one means different thing.
---
Quoted 9 lines · Duy Nguyen
Quoted 6 lines · Phil Pennock
Expected to work as .gitignore in top-level of repo:
*
!**/*.asc
!.gitignore
Quoted 6 lines
gitignore man page has this "It is not possible to re-include a file if a parent directory of that file is excluded". In this case, directory "foo" is ignored by "*". Although it makes sense for this particular case to re-include something in foo because we can clearly see there are rules to re-include. It's on my todo list, but I don't know when it will be implemented.
It turns out not so hard to do. I just needed a push like your report. I'll try to find some time to finish this up, if people don't object this idea.
Documentation/gitignore.txt | 13 +++++++++---- dir.c | 27 +++++++++++++++++++++------ dir.h | 2 ++ 3 files changed, 32 insertions(+), 10 deletions(-)
Documentation/gitignore.txt +9 -4
@@ -82,10 +82,9 @@ PATTERN FORMAT - An optional prefix "`!`" which negates the pattern; any matching file excluded by a previous pattern will become - included again. It is not possible to re-include a file if a parent - directory of that file is excluded. Git doesn't list excluded - directories for performance reasons, so any patterns on contained - files have no effect, no matter where they are defined. + included again. + It is usually not possible to re-include a file if a parent + directory of that file is excluded. See NOTES for details. Put a backslash ("`\`") in front of the first "`!`" for patterns that begin with a literal "`!`", for example, "`\!important!.txt`".
@@ -144,6 +143,12 @@ use 'git update-index {litdd}assume-unchanged'. To stop tracking a file that is currently tracked, use 'git rm --cached'. +It is usually not possible to re-include a file if a parent directory +of that file is excluded because of performance reasons. However, if +there are negative rules in the same .gitignore file that contains the +rule to ignore a specific directory, and those negative rules contain +a slash, then re-inclusion is possible. + EXAMPLES --------
dir.c +21 -6
@@ -443,6 +443,8 @@ void add_exclude(const char *string, const char *base,int flags;int nowildcardlen; +if (string[0 ] =='!' &&strchr (string +1 ,'/' )) + el->flags |=EXC_FLAG_MAY_REINCLUDE ;parse_exclude_pattern (&string, &patternlen, &flags, &nowildcardlen);if (flags &EXC_FLAG_MUSTBEDIR ) {char *s;
@@ -710,14 +712,17 @@ static struct exclude *last_exclude_matching_from_list(const char *pathname, struct exclude_list *el) {int i; +struct exclude *x; +const char *exclude; +int prefix;if (!el->nr)return NULL ;/* undefined */ for (i = el->nr -1 ;0 <= i; i--) { -struct exclude *x = el->excludes[i]; -const char *exclude = x->pattern; -int prefix = x->nowildcardlen; + x = el->excludes[i]; + exclude = x->pattern; + prefix = x->nowildcardlen;if (x->flags &EXC_FLAG_MUSTBEDIR ) {if (*dtype ==DT_UNKNOWN )
@@ -731,7 +736,7 @@ static struct exclude *last_exclude_matching_from_list(const char *pathname, pathlen - (basename - pathname), exclude, prefix, x->patternlen, x->flags)) - return x; + goto done;continue ; }
@@ -739,9 +744,19 @@ static struct exclude *last_exclude_matching_from_list(const char *pathname,if (match_pathname (pathname, pathlen, x->base, x->baselen ? x->baselen -1 :0 , exclude, prefix, x->patternlen, x->flags)) -return x; + goto done; + } + x =NULL ;/* undecided */ +done: +if (x && !(x->flags &EXC_FLAG_NEGATIVE ) && + (el->flags &EXC_FLAG_MAY_REINCLUDE )) { +if (*dtype ==DT_UNKNOWN ) + *dtype =get_dtype (NULL , pathname, pathlen); +if (*dtype ==DT_DIR ) + x =NULL ; } -return NULL ;/* undecided */ + +return x; }/*
dir.h +2 -0
@@ -14,6 +14,7 @@ struct dir_entry {#define EXC_FLAG_ENDSWITH 4#define EXC_FLAG_MUSTBEDIR 8#define EXC_FLAG_NEGATIVE 16 +#define EXC_FLAG_MAY_REINCLUDE 32struct exclude {/*
@@ -46,6 +47,7 @@ struct exclude {struct exclude_list {int nr;int alloc; +int flags;/* remember pointer to exclude file contents so we can free() */ char *filebuf;
Signature · 1 line
2.1.0.rc0.78.gc0d8480
Re: [PATCH] dir.c: allow re-include after a dir is excluded in some cases
Quoted 19 lines · Nguyễn Thái Ngọc Duy
Documentation/gitignore.txt +3
-4@@ -82,10 +82,9 @@ PATTERN FORMAT - An optional prefix "`!`" which negates the pattern; any matching file excluded by a previous pattern will become - included again. It is not possible to re-include a file if a parent - directory of that file is excluded. Git doesn't list excluded - directories for performance reasons, so any patterns on contained - files have no effect, no matter where they are defined. + included again. + It is usually not possible to re-include a file if a parent + directory of that file is excluded. See NOTES for details. Put a backslash ("`\`") in front of the first "`!`" for patterns that begin with a literal "`!`", for example, "`\!important!.txt`".
Documentation/gitignore.txt use 'git update-index {litdd}assume-unchanged'. · line 150
Quoted 8 lines
Documentation/gitignore.txt +5
-0@@ -144,6 +143,12 @@ use 'git update-index {litdd}assume-unchanged'. To stop tracking a file that is currently tracked, use 'git rm --cached'. +It is usually not possible to re-include a file if a parent directory +of that file is excluded because of performance reasons. However, if +there are negative rules in the same .gitignore file that contains the +rule to ignore a specific directory, and those negative rules contain +a slash, then re-inclusion is possible.
Does that mean "performance reasons" goes out the window???
What trade-off are the users making by choosing to do so? Is it explained in the documentation well enough to allow them to make an informed decision?
Re: [PATCH] dir.c: allow re-include after a dir is excluded in some cases
Quoted 32 lines · Junio C Hamano
Quoted 19 lines · Nguyễn Thái Ngọc Duy
Documentation/gitignore.txt +3
-4@@ -82,10 +82,9 @@ PATTERN FORMAT - An optional prefix "`!`" which negates the pattern; any matching file excluded by a previous pattern will become - included again. It is not possible to re-include a file if a parent - directory of that file is excluded. Git doesn't list excluded - directories for performance reasons, so any patterns on contained - files have no effect, no matter where they are defined. + included again. + It is usually not possible to re-include a file if a parent + directory of that file is excluded. See NOTES for details. Put a backslash ("`\`") in front of the first "`!`" for patterns that begin with a literal "`!`", for example, "`\!important!.txt`".Documentation/gitignore.txt use 'git update-index {litdd}assume-unchanged'. · line 150
Quoted 8 lines
Documentation/gitignore.txt +5
-0@@ -144,6 +143,12 @@ use 'git update-index {litdd}assume-unchanged'. To stop tracking a file that is currently tracked, use 'git rm --cached'. +It is usually not possible to re-include a file if a parent directory +of that file is excluded because of performance reasons. However, if +there are negative rules in the same .gitignore file that contains the +rule to ignore a specific directory, and those negative rules contain +a slash, then re-inclusion is possible.Does that mean "performance reasons" goes out the window???
Quoted 3 lines
What trade-off are the users making by choosing to do so? Is it explained in the documentation well enough to allow them to make an informed decision?
If they put "!**/foo" there, I think it's obvious for users that all dirs must be looked at. "!a*/**/foo" may be expected to only look at a subset of dirs recursively, instead of everything. "!*/def" may be expected that only one more level of directories are looked at. I didn't cover the last two cases well and I don't think it'll be easy to do. Which makes this patch less appealing. (so much for a not-thoroughly-thought-through quick prototype)
Perhaps I look at it from a wrong angle. If we have a way to say "these patterns do not apply to directories", then the user can selectively exclude certain directories and let others through. It would give the user more control and make the travelling cost more apparent (assuming we mention somewhere that the more dirs we examine, the longer it will take).
Signature · 1 line
Duy
Quoted 9 lines · Duy Nguyen
Quoted 6 lines · Phil Pennock
Expected to work as .gitignore in top-level of repo:
* !**/*.asc !.gitignore
Quoted 6 lines
gitignore man page has this "It is not possible to re-include a file if a parent directory of that file is excluded". In this case, directory "foo" is ignored by "*". Although it makes sense for this particular case to re-include something in foo because we can clearly see there are rules to re-include. It's on my todo list, but I don't know when it will be implemented.
Thanks for this and the patches and discussion which follow.
I didn't cover it in my report, but one of the scenarios I tried was to explicitly re-include directories, to make them candidates again, and either use directory-matching patterns in the top-level .gitignore or to use per-directory .gitignore to handle those directories.
Looking fresh today, I see that I failed to compare baseline behaviour without a .gitignore when using git status as a baseline for comparison. So a .gitignore like this:
*
!*/
!*.ascappeared to not work; even within the foo/ sub-directory, git status shows no candidates for inclusion. But this is true even without a .gitignore. *sigh*
In fact, it looks like the simple three lines above work, without any .gitignore in sub-directories.
The behaviour which confused me between this simplified test-case and the original was that git status shows files in the top-level directory which are untracked, and in untracked files sub-directories where some other file in that directory is already tracked, but if no file in the sub-directory is already tracked, then git status does not report the files for inclusion, even if the cwd is inside that directory.
I tied myself in knots trying to avoid adding unencrypted files to the repo.
Thanks,
-Phil
Quoted 28 lines · Phil Pennock
Quoted 9 lines · Duy Nguyen
Quoted 6 lines · Phil Pennock
Expected to work as .gitignore in top-level of repo:
* !**/*.asc !.gitignoreQuoted 6 lines
gitignore man page has this "It is not possible to re-include a file if a parent directory of that file is excluded". In this case, directory "foo" is ignored by "*". Although it makes sense for this particular case to re-include something in foo because we can clearly see there are rules to re-include. It's on my todo list, but I don't know when it will be implemented.
Thanks for this and the patches and discussion which follow.
I didn't cover it in my report, but one of the scenarios I tried was to explicitly re-include directories, to make them candidates again, and either use directory-matching patterns in the top-level .gitignore or to use per-directory .gitignore to handle those directories.
Looking fresh today, I see that I failed to compare baseline behaviour without a .gitignore when using
git statusas a baseline for comparison. So a .gitignore like this:
Quoted 7 lines
* !*/ !*.ascappeared to not work; even within the
foo/sub-directory,git statusshows no candidates for inclusion. But this is true even without a .gitignore. *sigh*
I should have read this mail before replying to Junio in the other email :( Yeah the "!*/" would re-include dirs back. I'm not sure if there are any side effects by doing this, no time to think about this yet. Maybe we can put this in the example section in gitignore man page with more explanation.
Quoted 16 lines
In fact, it looks like the simple three lines above work, without any .gitignore in sub-directories.
The behaviour which confused me between this simplified test-case and the original was that
git statusshows files in the top-level directory which are untracked, and in untracked files sub-directories where some other file in that directory is already tracked, but if no file in the sub-directory is already tracked, thengit statusdoes not report the files for inclusion, even if the cwd is inside that directory.I tied myself in knots trying to avoid adding unencrypted files to the repo.
Thanks,
-Phil
Signature · 1 line
Duy