[PATCH] dir.c: allow re-include after a dir is excluded in some cases

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@xxxxxxxxx>
---
 On Wed, Nov 19, 2014 at 4:48 PM, Duy Nguyen <pclouds@xxxxxxxxx> wrote:
 > On Wed, Nov 19, 2014 at 10:40 AM, Phil Pennock
 > <phil-gitml@xxxxxxxxxxxxxxxxx> wrote:
 >> 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.

 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(-)

diff --git a/Documentation/gitignore.txt b/Documentation/gitignore.txt
index 09e82c3..0340c44 100644
--- a/Documentation/gitignore.txt
+++ b/Documentation/gitignore.txt
@@ -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
 --------
 
diff --git a/dir.c b/dir.c
index 3f7a025..df6d940 100644
--- a/dir.c
+++ b/dir.c
@@ -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;
 }
 
 /*
diff --git a/dir.h b/dir.h
index 6c45e9d..1ace1cf 100644
--- a/dir.h
+++ b/dir.h
@@ -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 32
 
 struct 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;
-- 
2.1.0.rc0.78.gc0d8480

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]