Skip to contentSkip to conversation

gitignore, re-inclusion fix

PATCH In review Nguyễn Thái Ngọc Duy 2 participants 4 messages Jun 15, 2016 1 message not in the archive Documentation

lore
Share & tools
lore
#1 lore raw

v3 cover gitignore, re-inclusion fix

In reply to a message not in the archive

Changes since v2

-- 8< --

Documentation/gitignore.txt +11 -5
index 889a72a..79a1948 100644
@@ -82,9 +82,12 @@ 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.
+   included again.
+   Put a backslash ("`\`") in front of the first "`!`" for patterns
+   that begin with a literal "`!`", for example, "`\!important!.txt`".
+   It is possible to re-include a file if a parent directory of that
+   file is excluded if certain conditions are met. 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
@@ -138,8 +141,11 @@ 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:
+To re-include files or directories when their parent directory is
+excluded, the following conditions must be met:
+
+ - The rules to exclude a directory and re-include a subset back must
+   be in the same .gitignore file.
 
  - The directory part in the re-include rules must be literal (i.e. no
    wildcards)
t/t3001-ls-files-others-exclude.sh +5 -0
index 9de49a6..da257c0 100755
@@ -310,9 +310,14 @@ test_expect_success 'negative patterns' '
 	(
 		cd reinclude &&
 		cat >.gitignore <<-\EOF &&
+		/fooo
 		/foo
 		!foo/bar/bar
 		EOF
+		mkdir fooo &&
+		cat >fooo/.gitignore <<-\EOF &&
+		!/*
+		EOF
 		mkdir -p foo/bar &&
 		touch abc foo/def foo/bar/ghi foo/bar/bar &&
 		git ls-files -o --exclude-standard >../actual &&

-- 8< --

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        | 23 ++++++++--
 dir.c                              | 89 +++++++++++++++++++++++++++++++++++---
 t/t3001-ls-files-others-exclude.sh | 25 +++++++++++
 3 files changed, 127 insertions(+), 10 deletions(-)
Signature · 1 line
2.3.0.rc1.137.g477eb31
#2 lore raw

PATCH v3 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. Keep 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
#3 lore raw

PATCH v3 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        | 23 ++++++++++--
 dir.c                              | 76 +++++++++++++++++++++++++++++++++++++-
 t/t3001-ls-files-others-exclude.sh | 25 +++++++++++++
 3 files changed, 118 insertions(+), 6 deletions(-)
Documentation/gitignore.txt +19 -4
index 473623d..79a1948 100644
@@ -82,12 +82,12 @@ 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.
    Put a backslash ("`\`") in front of the first "`!`" for patterns
    that begin with a literal "`!`", for example, "`\!important!.txt`".
+   It is possible to re-include a file if a parent directory of that
+   file is excluded if certain conditions are met. 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 +141,21 @@ not tracked by Git remain untracked.
 To stop tracking a file that is currently tracked, use
 'git rm --cached'.
 
+To re-include files or directories when their parent directory is
+excluded, the following conditions must be met:
+
+ - The rules to exclude a directory and re-include a subset back must
+   be in the same .gitignore file.
+
+ - 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 +25 -0
index 3fc484e..da257c0 100755
@@ -305,4 +305,29 @@ 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 &&
+		/fooo
+		/foo
+		!foo/bar/bar
+		EOF
+		mkdir fooo &&
+		cat >fooo/.gitignore <<-\EOF &&
+		!/*
+		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