Skip to contentSkip to conversation

gitignore, re-inclusion fix

PATCH Superseded Nguyễn Thái Ngọc Duy 2 participants 10 messages Jun 15, 2016 Documentation

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover gitignore, re-inclusion fix

This is an old problem. I attempted once [1] and then was reminded [2] with some more comments on the original patch. Let's try again.

The problem is this .gitignore currently does not work, but it should:

/abc
!/abc/def/ghi

This patch fixes that by realizing that the last rule may re-include something in abc/def so it does not exclude "abc" and "abc/def" outright to give the last rule a chance to match.

[1] http://article.gmane.org/gmane.comp.version-control.git/259870
[2] http://thread.gmane.org/gmane.comp.version-control.git/265901/focus=266227

Nguyễn Thái Ngọc Duy (2):
dir.c: make last_exclude_matching_from_list() run til the end dir.c: don't exclude whole dir prematurely if neg pattern may match

 Documentation/gitignore.txt        | 21 ++++++---
 dir.c                              | 89 +++++++++++++++++++++++++++++++++++---
 t/t3001-ls-files-others-exclude.sh | 20 +++++++++
 3 files changed, 118 insertions(+), 12 deletions(-)
Signature · 1 line
2.3.0.rc1.137.g477eb31
#1 lore raw

PATCH 1/2 dir.c: make last_exclude_matching_from_list() run til the end

---
 dir.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
dir.c +10 -5
index c00c7e2..3a7630a 100644
@@ -901,6 +901,7 @@ static struct exclude *last_exclude_matching_from_list(const char *pathname,
 						       int *dtype,
 						       struct exclude_list *el)
 {
+	struct exclude *exc = NULL; /* undecided */
 	int i;
 
 	if (!el->nr)
@@ -922,18 +923,22 @@ static struct exclude *last_exclude_matching_from_list(const char *pathname,
 			if (match_basename(basename,
 					   pathlen - (basename - pathname),
 					   exclude, prefix, x->patternlen,
-					   x->flags))
-				return x;
+					   x->flags)) {
+				exc = x;
+				break;
+			}
 			continue;
 		}
 
 		assert(x->baselen == 0 || x->base[x->baselen - 1] == '/');
 		if (match_pathname(pathname, pathlen,
 				   x->base, x->baselen ? x->baselen - 1 : 0,
-				   exclude, prefix, x->patternlen, x->flags))
-			return x;
+				   exclude, prefix, x->patternlen, x->flags)) {
+			exc = x;
+			break;
+		}
 	}
-	return NULL; /* undecided */
+	return exc;
 }
 
 /*
Signature · 1 line
2.3.0.rc1.137.g477eb31
#2 lore raw

Re: [PATCH 1/2] dir.c: make last_exclude_matching_from_list() run til the end

Quoted 1 line · Nguyễn Thái Ngọc Duy

Because? Title just tells what the patch meant to do (i.e. instead of returning it keeps looping), but does not say why it is a good idea. Besides, this a no-op patch and does not make it keep looping.

dir.c static struct exclude *last_exclude_matching_from_list(const char *pathname,

Quoted 17 lines
---
 dir.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
dir.c +1 -0
index c00c7e2..3a7630a 100644
@@ -901,6 +901,7 @@ static struct exclude *last_exclude_matching_from_list(const char *pathname,
 						       int *dtype,
 						       struct exclude_list *el)
 {
+	struct exclude *exc = NULL; /* undecided */
 	int i;
 
 	if (!el->nr)
@@ -922,18 +923,22 @@ static struct exclude *last_exclude_matching_from_list(const char *pathname,

Note that we are in a big for() loop that scans backwards an array.

dir.c static struct exclude *last_exclude_matching_from_list(const char *pathname, · line 929

Quoted 9 lines
dir.c +4 -2
 			if (match_basename(basename,
 					   pathlen - (basename - pathname),
 					   exclude, prefix, x->patternlen,
-					   x->flags))
-				return x;
+					   x->flags)) {
+				exc = x;
+				break;
+			}

We used to return x immediately; now we store x to exc and break, i.e. leave the loop.

dir.c static struct exclude *last_exclude_matching_from_list(const char *pathname, · line 938

Quoted 11 lines
dir.c +3 -2
 			continue;
 		}
 
 		assert(x->baselen == 0 || x->base[x->baselen - 1] == '/');
 		if (match_pathname(pathname, pathlen,
 				   x->base, x->baselen ? x->baselen - 1 : 0,
-				   exclude, prefix, x->patternlen, x->flags))
-			return x;
+				   exclude, prefix, x->patternlen, x->flags)) {
+			exc = x;
+			break;

We used to return x immediately; now we store x to exc and break, i.e. leave the loop.

dir.c static struct exclude *last_exclude_matching_from_list(const char *pathname, · line 941

Quoted 4 lines
dir.c +2 -1
+		}
 	}
-	return NULL; /* undecided */
+	return exc;

And then we return exc.

Quoted 1 line
dir.c +0 -0
 }
author
#3 lore raw

Re: [PATCH 1/2] dir.c: make last_exclude_matching_from_list() run til the end

Quoted 7 lines · Junio C Hamano
Quoted 1 line · Nguyễn Thái Ngọc Duy

Because? Title just tells what the patch meant to do (i.e. instead of returning it keeps looping), but does not say why it is a good idea. Besides, this a no-op patch and does not make it keep looping.

Because the next patch adds some post processing before returning the value. Having all the paths come to the same point would simplify the code. Will update the commit message.

Signature · 1 line
Duy
#4 lore raw

PATCH 2/2 dir.c: don't exclude whole dir prematurely if neg pattern may match

In reply to Nguyễn Thái Ngọc Duy

If there is a pattern "!foo/bar", this patch makes it not exclude "foo" right away. This gives us a chance to examine "foo" and re-include "foo/bar".

In order for it to detect that the directory under examination should not be excluded right away, in other words it is a parent directory of a negative pattern, the "directory path" of the negative pattern must be literal. Patterns like "!f?o/bar" can't stop "foo" from being excluded.

Basename matching (i.e. "no slashes in the pattern") or must-be-dir matching (i.e. "trailing slash in the pattern") does not work well with this. For example, if we descend in "foo" and are examining "foo/abc", current code for "foo/" pattern will check if path "foo/abc", not "foo", is a directory. The same problem with basename matching. These may need big code reorg to make it work.

---
 Documentation/gitignore.txt        | 21 ++++++++---
 dir.c                              | 76 +++++++++++++++++++++++++++++++++++++-
 t/t3001-ls-files-others-exclude.sh | 20 ++++++++++
 3 files changed, 109 insertions(+), 8 deletions(-)
Documentation/gitignore.txt +15 -6
index 473623d..889a72a 100644
@@ -82,12 +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.
-   Put a backslash ("`\`") in front of the first "`!`" for patterns
-   that begin with a literal "`!`", for example, "`\!important!.txt`".
+   included again. It is possible to re-include a file if a parent
+   directory of that file is excluded, with restrictions. See section
+   NOTES for detail.
 
  - If the pattern ends with a slash, it is removed for the
    purpose of the following description, but it would only find
@@ -141,6 +138,18 @@ not tracked by Git remain untracked.
 To stop tracking a file that is currently tracked, use
 'git rm --cached'.
 
+To re-include a file when its parent directory is excluded, the
+following conditions must match:
+
+ - The directory part in the re-include rules must be literal (i.e. no
+   wildcards)
+
+ - The rules to exclude the parent directory must not end with a
+   trailing slash.
+
+ - The rules to exclude the parent directory must have at least one
+   slash.
+
 EXAMPLES
 --------
 
dir.c +74 -2

100 lines not on this page · raw message

t/t3001-ls-files-others-exclude.sh +20 -0
index 3fc484e..9de49a6 100755
@@ -305,4 +305,24 @@ test_expect_success 'ls-files with "**" patterns and no slashes' '
 	test_cmp expect actual
 '
 
+test_expect_success 'negative patterns' '
+	git init reinclude &&
+	(
+		cd reinclude &&
+		cat >.gitignore <<-\EOF &&
+		/foo
+		!foo/bar/bar
+		EOF
+		mkdir -p foo/bar &&
+		touch abc foo/def foo/bar/ghi foo/bar/bar &&
+		git ls-files -o --exclude-standard >../actual &&
+		cat >../expected <<-\EOF &&
+		.gitignore
+		abc
+		foo/bar/bar
+		EOF
+		test_cmp ../expected ../actual
+	)
+'
+
 test_done
Signature · 1 line
2.3.0.rc1.137.g477eb31
#5 lore raw

v2 cover gitignore, re-inclusion fix

In reply to Nguyễn Thái Ngọc Duy

No code change. Explain why 1/2 is needed in the commit message.

Nguyễn Thái Ngọc Duy (2):
dir.c: make last_exclude_matching_from_list() run til the end dir.c: don't exclude whole dir prematurely if neg pattern may match

 Documentation/gitignore.txt        | 21 ++++++---
 dir.c                              | 89 +++++++++++++++++++++++++++++++++++---
 t/t3001-ls-files-others-exclude.sh | 20 +++++++++
 3 files changed, 118 insertions(+), 12 deletions(-)
Signature · 1 line
2.3.0.rc1.137.g477eb31
#6 lore raw

PATCH v2 1/2 dir.c: make last_exclude_matching_from_list() run til the end

The next patch adds some post processing to the result value before it's returned to the caller. Make all branches reach the end of the function, so we can do it all in one place.

---
 dir.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
dir.c +10 -5
index c00c7e2..3a7630a 100644
@@ -901,6 +901,7 @@ static struct exclude *last_exclude_matching_from_list(const char *pathname,
 						       int *dtype,
 						       struct exclude_list *el)
 {
+	struct exclude *exc = NULL; /* undecided */
 	int i;
 
 	if (!el->nr)
@@ -922,18 +923,22 @@ static struct exclude *last_exclude_matching_from_list(const char *pathname,
 			if (match_basename(basename,
 					   pathlen - (basename - pathname),
 					   exclude, prefix, x->patternlen,
-					   x->flags))
-				return x;
+					   x->flags)) {
+				exc = x;
+				break;
+			}
 			continue;
 		}
 
 		assert(x->baselen == 0 || x->base[x->baselen - 1] == '/');
 		if (match_pathname(pathname, pathlen,
 				   x->base, x->baselen ? x->baselen - 1 : 0,
-				   exclude, prefix, x->patternlen, x->flags))
-			return x;
+				   exclude, prefix, x->patternlen, x->flags)) {
+			exc = x;
+			break;
+		}
 	}
-	return NULL; /* undecided */
+	return exc;
 }
 
 /*
Signature · 1 line
2.3.0.rc1.137.g477eb31
#7 lore raw

PATCH v2 2/2 dir.c: don't exclude whole dir prematurely if neg pattern may match

In reply to Nguyễn Thái Ngọc Duy

If there is a pattern "!foo/bar", this patch makes it not exclude "foo" right away. This gives us a chance to examine "foo" and re-include "foo/bar".

In order for it to detect that the directory under examination should not be excluded right away, in other words it is a parent directory of a negative pattern, the "directory path" of the negative pattern must be literal. Patterns like "!f?o/bar" can't stop "foo" from being excluded.

Basename matching (i.e. "no slashes in the pattern") or must-be-dir matching (i.e. "trailing slash in the pattern") does not work well with this. For example, if we descend in "foo" and are examining "foo/abc", current code for "foo/" pattern will check if path "foo/abc", not "foo", is a directory. The same problem with basename matching. These may need big code reorg to make it work.

---
 Documentation/gitignore.txt        | 21 ++++++++---
 dir.c                              | 76 +++++++++++++++++++++++++++++++++++++-
 t/t3001-ls-files-others-exclude.sh | 20 ++++++++++
 3 files changed, 109 insertions(+), 8 deletions(-)
Documentation/gitignore.txt +15 -6
index 473623d..889a72a 100644
@@ -82,12 +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.
-   Put a backslash ("`\`") in front of the first "`!`" for patterns
-   that begin with a literal "`!`", for example, "`\!important!.txt`".
+   included again. It is possible to re-include a file if a parent
+   directory of that file is excluded, with restrictions. See section
+   NOTES for detail.
 
  - If the pattern ends with a slash, it is removed for the
    purpose of the following description, but it would only find
@@ -141,6 +138,18 @@ not tracked by Git remain untracked.
 To stop tracking a file that is currently tracked, use
 'git rm --cached'.
 
+To re-include a file when its parent directory is excluded, the
+following conditions must be met:
+
+ - The directory part in the re-include rules must be literal (i.e. no
+   wildcards)
+
+ - The rules to exclude the parent directory must not end with a
+   trailing slash.
+
+ - The rules to exclude the parent directory must have at least one
+   slash.
+
 EXAMPLES
 --------
 
dir.c +74 -2

100 lines not on this page · raw message

t/t3001-ls-files-others-exclude.sh +20 -0
index 3fc484e..9de49a6 100755
@@ -305,4 +305,24 @@ test_expect_success 'ls-files with "**" patterns and no slashes' '
 	test_cmp expect actual
 '
 
+test_expect_success 'negative patterns' '
+	git init reinclude &&
+	(
+		cd reinclude &&
+		cat >.gitignore <<-\EOF &&
+		/foo
+		!foo/bar/bar
+		EOF
+		mkdir -p foo/bar &&
+		touch abc foo/def foo/bar/ghi foo/bar/bar &&
+		git ls-files -o --exclude-standard >../actual &&
+		cat >../expected <<-\EOF &&
+		.gitignore
+		abc
+		foo/bar/bar
+		EOF
+		test_cmp ../expected ../actual
+	)
+'
+
 test_done
Signature · 1 line
2.3.0.rc1.137.g477eb31
#8 lore raw

Re: [PATCH v2 2/2] dir.c: don't exclude whole dir prematurely if neg pattern may match

Documentation/gitignore.txt PATTERN FORMAT · line 87

Quoted 11 lines · Nguyễn Thái Ngọc Duy
Documentation/gitignore.txt +3 -0
index 473623d..889a72a 100644
@@ -82,12 +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 possible to re-include a file if a parent
+   directory of that file is excluded, with restrictions. See section
+   NOTES for detail.

Sounds like a very useful thing.

Quoted 8 lines
Documentation/gitignore.txt +2 -0
  - If the pattern ends with a slash, it is removed for the
    purpose of the following description, but it would only find
@@ -141,6 +138,18 @@ not tracked by Git remain untracked.
 To stop tracking a file that is currently tracked, use
 'git rm --cached'.
 
+To re-include a file when its parent directory is excluded, the
+following conditions must be met:

Documentation/gitignore.txt not tracked by Git remain untracked. · line 152

Quoted 10 lines
Documentation/gitignore.txt +10 -0
+
+ - The directory part in the re-include rules must be literal (i.e. no
+   wildcards)
+
+ - The rules to exclude the parent directory must not end with a
+   trailing slash.
+
+ - The rules to exclude the parent directory must have at least one
+   slash.
+

In this bulletted list, don't the readers also need to be told that having "/abc" in .gitignore (but not "!/abc/anything" in .gitignore) and "!foo" in abc/.gitignore would not cause us to descend into "/abc" just to examine "abc/.gitignore" with pessimistic assumption that there might be some exclusion in there?

Quoted 17 lines
t/t3001-ls-files-others-exclude.sh +9 -0
index 3fc484e..9de49a6 100755
@@ -305,4 +305,24 @@ test_expect_success 'ls-files with "**" patterns and no slashes' '
 	test_cmp expect actual
 '
 
+test_expect_success 'negative patterns' '
+	git init reinclude &&
+	(
+		cd reinclude &&
+		cat >.gitignore <<-\EOF &&
+		/foo
+		!foo/bar/bar
+		EOF
+		mkdir -p foo/bar &&

t/t3001-ls-files-others-exclude.sh test_expect_success 'ls-files with "**" patterns and no slashes' ' · line 326

Quoted 10 lines
t/t3001-ls-files-others-exclude.sh +10 -0
+		touch abc foo/def foo/bar/ghi foo/bar/bar &&
+		git ls-files -o --exclude-standard >../actual &&
+		cat >../expected <<-\EOF &&
+		.gitignore
+		abc
+		foo/bar/bar
+		EOF
+		test_cmp ../expected ../actual
+	)
+'

And another test here may want to explicitly ensure that we are not overly pessimising the ignore processing, so that later changes will not break it, I think. Or do we already have such a case covered by an existing test?

Thanks.

author
#9 lore raw

Re: [PATCH v2 2/2] dir.c: don't exclude whole dir prematurely if neg pattern may match

Quoted 32 lines · Junio C Hamano

Documentation/gitignore.txt PATTERN FORMAT · line 87

Quoted 11 lines · Nguyễn Thái Ngọc Duy
Documentation/gitignore.txt +3 -0
index 473623d..889a72a 100644
@@ -82,12 +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 possible to re-include a file if a parent
+   directory of that file is excluded, with restrictions. See section
+   NOTES for detail.

Sounds like a very useful thing.

Quoted 16 lines
Documentation/gitignore.txt +10 -0
  - If the pattern ends with a slash, it is removed for the
    purpose of the following description, but it would only find
@@ -141,6 +138,18 @@ not tracked by Git remain untracked.
 To stop tracking a file that is currently tracked, use
 'git rm --cached'.
 
+To re-include a file when its parent directory is excluded, the
+following conditions must be met:
+
+ - The directory part in the re-include rules must be literal (i.e. no
+   wildcards)
+
+ - The rules to exclude the parent directory must not end with a
+   trailing slash.
+
+ - The rules to exclude the parent directory must have at least one
Quoted 8 lines

Documentation/gitignore.txt not tracked by Git remain untracked. · line 152

Quoted 2 lines
Documentation/gitignore.txt +2 -0
+   slash.
+

In this bulletted list, don't the readers also need to be told that having "/abc" in .gitignore (but not "!/abc/anything" in .gitignore) and "!foo" in abc/.gitignore would not cause us to descend into "/abc" just to examine "abc/.gitignore" with pessimistic assumption that there might be some exclusion in there?

Yeah I thought it was already mentioned. I guess it's just in my mind. Will update.

Quoted 23 lines
Quoted 23 lines
t/t3001-ls-files-others-exclude.sh +15 -0
index 3fc484e..9de49a6 100755
@@ -305,4 +305,24 @@ test_expect_success 'ls-files with "**" patterns and no slashes' '
      test_cmp expect actual
 '
 
+test_expect_success 'negative patterns' '
+     git init reinclude &&
+     (
+             cd reinclude &&
+             cat >.gitignore <<-\EOF &&
+             /foo
+             !foo/bar/bar
+             EOF
+             mkdir -p foo/bar &&
+             touch abc foo/def foo/bar/ghi foo/bar/bar &&
+             git ls-files -o --exclude-standard >../actual &&
+             cat >../expected <<-\EOF &&
+             .gitignore
+             abc
+             foo/bar/bar
Quoted 8 lines

t/t3001-ls-files-others-exclude.sh test_expect_success 'ls-files with "**" patterns and no slashes' ' · line 326

Quoted 4 lines
t/t3001-ls-files-others-exclude.sh +4 -0
+             EOF
+             test_cmp ../expected ../actual
+     )
+'

And another test here may want to explicitly ensure that we are not overly pessimising the ignore processing, so that later changes will not break it, I think.

Yep. Will do.

Signature · 1 line
Duy