{"thread":{"id":"40160","subject":"[PATCH 0/2] gitignore, re-inclusion fix","startedAt":"2015-08-23T12:50:12Z","lastAt":"2015-09-17T13:21:51Z","messageCount":10,"participants":["Nguyễn Thái Ngọc Duy","Junio C Hamano","Duy Nguyen"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"268527","messageId":"1440334214-32131-1-git-send-email-pclouds@gmail.com","threadId":"40160","inReplyTo":null,"subject":"[PATCH 0/2] gitignore, re-inclusion fix","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2015-08-23T12:50:12Z","receivedAt":"2015-08-23T12:50:12Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"This is an old problem. I attempted once [1] and then was reminded [2]\nwith some more comments on the original patch. Let's try again.\n\nThe problem is this .gitignore currently does not work, but it should:\n\n/abc\n!/abc/def/ghi\n\nThis patch fixes that by realizing that the last rule may re-include\nsomething in abc/def so it does not exclude \"abc\" and \"abc/def\"\noutright to give the last rule a chance to match.\n\n[1] http://article.gmane.org/gmane.comp.version-control.git/259870\n[2] http://thread.gmane.org/gmane.comp.version-control.git/265901/focus=266227\n\nNguyễn Thái Ngọc Duy (2):\n  dir.c: make last_exclude_matching_from_list() run til the end\n  dir.c: don't exclude whole dir prematurely if neg pattern may match\n\n Documentation/gitignore.txt        | 21 ++++++---\n dir.c                              | 89 +++++++++++++++++++++++++++++++++++---\n t/t3001-ls-files-others-exclude.sh | 20 +++++++++\n 3 files changed, 118 insertions(+), 12 deletions(-)\n\n-- \n2.3.0.rc1.137.g477eb31\n"},{"id":"268528","messageId":"1440334214-32131-2-git-send-email-pclouds@gmail.com","threadId":"40160","inReplyTo":"1440334214-32131-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 1/2] dir.c: make last_exclude_matching_from_list() run til the end","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2015-08-23T12:50:13Z","receivedAt":"2015-08-23T12:50:13Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n dir.c | 15 ++++++++++-----\n 1 file changed, 10 insertions(+), 5 deletions(-)\n\ndiff --git a/dir.c b/dir.c\nindex c00c7e2..3a7630a 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -901,6 +901,7 @@ static struct exclude *last_exclude_matching_from_list(const char *pathname,\n \t\t\t\t\t\t       int *dtype,\n \t\t\t\t\t\t       struct exclude_list *el)\n {\n+\tstruct exclude *exc = NULL; /* undecided */\n \tint i;\n \n \tif (!el->nr)\n@@ -922,18 +923,22 @@ static struct exclude *last_exclude_matching_from_list(const char *pathname,\n \t\t\tif (match_basename(basename,\n \t\t\t\t\t   pathlen - (basename - pathname),\n \t\t\t\t\t   exclude, prefix, x->patternlen,\n-\t\t\t\t\t   x->flags))\n-\t\t\t\treturn x;\n+\t\t\t\t\t   x->flags)) {\n+\t\t\t\texc = x;\n+\t\t\t\tbreak;\n+\t\t\t}\n \t\t\tcontinue;\n \t\t}\n \n \t\tassert(x->baselen == 0 || x->base[x->baselen - 1] == '/');\n \t\tif (match_pathname(pathname, pathlen,\n \t\t\t\t   x->base, x->baselen ? x->baselen - 1 : 0,\n-\t\t\t\t   exclude, prefix, x->patternlen, x->flags))\n-\t\t\treturn x;\n+\t\t\t\t   exclude, prefix, x->patternlen, x->flags)) {\n+\t\t\texc = x;\n+\t\t\tbreak;\n+\t\t}\n \t}\n-\treturn NULL; /* undecided */\n+\treturn exc;\n }\n \n /*\n-- \n2.3.0.rc1.137.g477eb31\n"},{"id":"268529","messageId":"1440334214-32131-3-git-send-email-pclouds@gmail.com","threadId":"40160","inReplyTo":"1440334214-32131-1-git-send-email-pclouds@gmail.com","subject":"[PATCH 2/2] dir.c: don't exclude whole dir prematurely if neg pattern may match","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2015-08-23T12:50:14Z","receivedAt":"2015-08-23T12:50:14Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"If there is a pattern \"!foo/bar\", this patch makes it not exclude \"foo\"\nright away. This gives us a chance to examine \"foo\" and re-include\n\"foo/bar\".\n\nIn order for it to detect that the directory under examination should\nnot be excluded right away, in other words it is a parent directory of a\nnegative pattern, the \"directory path\" of the negative pattern must be\nliteral. Patterns like \"!f?o/bar\" can't stop \"foo\" from being excluded.\n\nBasename matching (i.e. \"no slashes in the pattern\") or must-be-dir\nmatching (i.e. \"trailing slash in the pattern\") does not work well with\nthis. For example, if we descend in \"foo\" and are examining \"foo/abc\",\ncurrent code for \"foo/\" pattern will check if path \"foo/abc\", not \"foo\",\nis a directory. The same problem with basename matching. These may need\nbig code reorg to make it work.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n Documentation/gitignore.txt        | 21 ++++++++---\n dir.c                              | 76 +++++++++++++++++++++++++++++++++++++-\n t/t3001-ls-files-others-exclude.sh | 20 ++++++++++\n 3 files changed, 109 insertions(+), 8 deletions(-)\n\ndiff --git a/Documentation/gitignore.txt b/Documentation/gitignore.txt\nindex 473623d..889a72a 100644\n--- a/Documentation/gitignore.txt\n+++ b/Documentation/gitignore.txt\n@@ -82,12 +82,9 @@ PATTERN FORMAT\n \n  - An optional prefix \"`!`\" which negates the pattern; any\n    matching file excluded by a previous pattern will become\n-   included again. It is not possible to re-include a file if a parent\n-   directory of that file is excluded. Git doesn't list excluded\n-   directories for performance reasons, so any patterns on contained\n-   files have no effect, no matter where they are defined.\n-   Put a backslash (\"`\\`\") in front of the first \"`!`\" for patterns\n-   that begin with a literal \"`!`\", for example, \"`\\!important!.txt`\".\n+   included again. It is possible to re-include a file if a parent\n+   directory of that file is excluded, with restrictions. See section\n+   NOTES for detail.\n \n  - If the pattern ends with a slash, it is removed for the\n    purpose of the following description, but it would only find\n@@ -141,6 +138,18 @@ not tracked by Git remain untracked.\n To stop tracking a file that is currently tracked, use\n 'git rm --cached'.\n \n+To re-include a file when its parent directory is excluded, the\n+following conditions must match:\n+\n+ - The directory part in the re-include rules must be literal (i.e. no\n+   wildcards)\n+\n+ - The rules to exclude the parent directory must not end with a\n+   trailing slash.\n+\n+ - The rules to exclude the parent directory must have at least one\n+   slash.\n+\n EXAMPLES\n --------\n \ndiff --git a/dir.c b/dir.c\nindex 3a7630a..a1f711c 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -882,6 +882,25 @@ int match_pathname(const char *pathname, int pathlen,\n \t\t */\n \t\tif (!patternlen && !namelen)\n \t\t\treturn 1;\n+\t\t/*\n+\t\t * This can happen when we ignore some exclude rules\n+\t\t * on directories in other to see if negative rules\n+\t\t * may match. E.g.\n+\t\t *\n+\t\t * /abc\n+\t\t * !/abc/def/ghi\n+\t\t *\n+\t\t * The pattern of interest is \"/abc\". On the first\n+\t\t * try, we should match path \"abc\" with this pattern\n+\t\t * in the \"if\" statement right above, but the caller\n+\t\t * ignores it.\n+\t\t *\n+\t\t * On the second try with paths within \"abc\",\n+\t\t * e.g. \"abc/xyz\", we come here and try to match it\n+\t\t * with \"/abc\".\n+\t\t */\n+\t\tif (!patternlen && namelen && *name == '/')\n+\t\t\treturn 1;\n \t}\n \n \treturn fnmatch_icase_mem(pattern, patternlen,\n@@ -890,6 +909,48 @@ int match_pathname(const char *pathname, int pathlen,\n }\n \n /*\n+ * Return non-zero if pathname is a directory and an ancestor of the\n+ * literal path in a (negative) pattern. This is used to keep\n+ * descending in \"foo\" and \"foo/bar\" when the pattern is\n+ * \"!foo/bar/.gitignore\". \"foo/notbar\" will not be descended however.\n+ */\n+static int match_neg_path(const char *pathname, int pathlen, int *dtype,\n+\t\t\t  const char *base, int baselen,\n+\t\t\t  const char *pattern, int prefix, int patternlen,\n+\t\t\t  int flags)\n+{\n+\tassert((flags & EXC_FLAG_NEGATIVE) && !(flags & EXC_FLAG_NODIR));\n+\n+\tif (*dtype == DT_UNKNOWN)\n+\t\t*dtype = get_dtype(NULL, pathname, pathlen);\n+\tif (*dtype != DT_DIR)\n+\t\treturn 0;\n+\n+\tif (*pattern == '/') {\n+\t\tpattern++;\n+\t\tpatternlen--;\n+\t\tprefix--;\n+\t}\n+\n+\tif (baselen) {\n+\t\tif (((pathlen < baselen && base[pathlen] == '/') ||\n+\t\t     pathlen == baselen) &&\n+\t\t    !strncmp_icase(pathname, base, pathlen))\n+\t\t\treturn 1;\n+\t\tpathname += baselen + 1;\n+\t\tpathlen  -= baselen + 1;\n+\t}\n+\n+\n+\tif (prefix &&\n+\t    ((pathlen < prefix && pattern[pathlen] == '/') &&\n+\t     !strncmp_icase(pathname, pattern, pathlen)))\n+\t\treturn 1;\n+\n+\treturn 0;\n+}\n+\n+/*\n  * Scan the given exclude list in reverse to see whether pathname\n  * should be ignored.  The first match (i.e. the last on the list), if\n  * any, determines the fate.  Returns the exclude_list element which\n@@ -902,7 +963,7 @@ static struct exclude *last_exclude_matching_from_list(const char *pathname,\n \t\t\t\t\t\t       struct exclude_list *el)\n {\n \tstruct exclude *exc = NULL; /* undecided */\n-\tint i;\n+\tint i, matched_negative_path = 0;\n \n \tif (!el->nr)\n \t\treturn NULL;\t/* undefined */\n@@ -937,7 +998,18 @@ static struct exclude *last_exclude_matching_from_list(const char *pathname,\n \t\t\texc = x;\n \t\t\tbreak;\n \t\t}\n-\t}\n+\n+\t\tif ((x->flags & EXC_FLAG_NEGATIVE) && !matched_negative_path &&\n+\t\t    match_neg_path(pathname, pathlen, dtype, x->base,\n+\t\t\t\t   x->baselen ? x->baselen - 1 : 0,\n+\t\t\t\t   exclude, prefix, x->patternlen, x->flags))\n+\t\t\tmatched_negative_path = 1;\n+\t}\n+\tif (exc &&\n+\t    !(exc->flags & EXC_FLAG_NEGATIVE) &&\n+\t    !(exc->flags & EXC_FLAG_NODIR) &&\n+\t    matched_negative_path)\n+\t\texc = NULL;\n \treturn exc;\n }\n \ndiff --git a/t/t3001-ls-files-others-exclude.sh b/t/t3001-ls-files-others-exclude.sh\nindex 3fc484e..9de49a6 100755\n--- a/t/t3001-ls-files-others-exclude.sh\n+++ b/t/t3001-ls-files-others-exclude.sh\n@@ -305,4 +305,24 @@ test_expect_success 'ls-files with \"**\" patterns and no slashes' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'negative patterns' '\n+\tgit init reinclude &&\n+\t(\n+\t\tcd reinclude &&\n+\t\tcat >.gitignore <<-\\EOF &&\n+\t\t/foo\n+\t\t!foo/bar/bar\n+\t\tEOF\n+\t\tmkdir -p foo/bar &&\n+\t\ttouch abc foo/def foo/bar/ghi foo/bar/bar &&\n+\t\tgit ls-files -o --exclude-standard >../actual &&\n+\t\tcat >../expected <<-\\EOF &&\n+\t\t.gitignore\n+\t\tabc\n+\t\tfoo/bar/bar\n+\t\tEOF\n+\t\ttest_cmp ../expected ../actual\n+\t)\n+'\n+\n test_done\n-- \n2.3.0.rc1.137.g477eb31\n"},{"id":"268689","messageId":"xmqqmvxfxgdj.fsf@gitster.dls.corp.google.com","threadId":"40160","inReplyTo":"1440334214-32131-2-git-send-email-pclouds@gmail.com","subject":"Re: [PATCH 1/2] dir.c: make last_exclude_matching_from_list() run til the end","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2015-08-25T20:28:08Z","receivedAt":"2015-08-25T20:28:08Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:\n\n> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n\nBecause?  Title just tells what the patch meant to do (i.e. instead\nof returning it keeps looping), but does not say why it is a good\nidea.  Besides, this a no-op patch and does not make it keep looping.\n\n> ---\n>  dir.c | 15 ++++++++++-----\n>  1 file changed, 10 insertions(+), 5 deletions(-)\n>\n> diff --git a/dir.c b/dir.c\n> index c00c7e2..3a7630a 100644\n> --- a/dir.c\n> +++ b/dir.c\n> @@ -901,6 +901,7 @@ static struct exclude *last_exclude_matching_from_list(const char *pathname,\n>  \t\t\t\t\t\t       int *dtype,\n>  \t\t\t\t\t\t       struct exclude_list *el)\n>  {\n> +\tstruct exclude *exc = NULL; /* undecided */\n>  \tint i;\n>  \n>  \tif (!el->nr)\n> @@ -922,18 +923,22 @@ static struct exclude *last_exclude_matching_from_list(const char *pathname,\n\nNote that we are in a big for() loop that scans backwards an array.\n\n>  \t\t\tif (match_basename(basename,\n>  \t\t\t\t\t   pathlen - (basename - pathname),\n>  \t\t\t\t\t   exclude, prefix, x->patternlen,\n> -\t\t\t\t\t   x->flags))\n> -\t\t\t\treturn x;\n> +\t\t\t\t\t   x->flags)) {\n> +\t\t\t\texc = x;\n> +\t\t\t\tbreak;\n> +\t\t\t}\n\nWe used to return x immediately; now we store x to exc and break,\ni.e. leave the loop.\n\n>  \t\t\tcontinue;\n>  \t\t}\n>  \n>  \t\tassert(x->baselen == 0 || x->base[x->baselen - 1] == '/');\n>  \t\tif (match_pathname(pathname, pathlen,\n>  \t\t\t\t   x->base, x->baselen ? x->baselen - 1 : 0,\n> -\t\t\t\t   exclude, prefix, x->patternlen, x->flags))\n> -\t\t\treturn x;\n> +\t\t\t\t   exclude, prefix, x->patternlen, x->flags)) {\n> +\t\t\texc = x;\n> +\t\t\tbreak;\n\nWe used to return x immediately; now we store x to exc and break,\ni.e. leave the loop.\n\n> +\t\t}\n>  \t}\n> -\treturn NULL; /* undecided */\n> +\treturn exc;\n\nAnd then we return exc.\n\n>  }\n"},{"id":"268978","messageId":"CACsJy8AwV9J9hs4uyZ0eCy0vFMat4dYaQy+6mVkcK7KvRw2bgg@mail.gmail.com","threadId":"40160","inReplyTo":"xmqqmvxfxgdj.fsf@gitster.dls.corp.google.com","subject":"Re: [PATCH 1/2] dir.c: make last_exclude_matching_from_list() run til the end","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2015-08-31T10:13:26Z","receivedAt":"2015-08-31T10:13:26Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Wed, Aug 26, 2015 at 3:28 AM, Junio C Hamano <gitster@pobox.com> wrote:\n> Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:\n>\n>> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n>\n> Because?  Title just tells what the patch meant to do (i.e. instead\n> of returning it keeps looping), but does not say why it is a good\n> idea.  Besides, this a no-op patch and does not make it keep looping.\n\nBecause the next patch adds some post processing before returning the\nvalue. Having all the paths come to the same point would simplify the\ncode. Will update the commit message.\n-- \nDuy\n"},{"id":"269850","messageId":"1442107141-24265-1-git-send-email-pclouds@gmail.com","threadId":"40160","inReplyTo":"1440334214-32131-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 0/2] gitignore, re-inclusion fix","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2015-09-13T01:18:59Z","receivedAt":"2015-09-13T01:18:59Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"No code change. Explain why 1/2 is needed in the commit message.\n\nNguyễn Thái Ngọc Duy (2):\n  dir.c: make last_exclude_matching_from_list() run til the end\n  dir.c: don't exclude whole dir prematurely if neg pattern may match\n\n Documentation/gitignore.txt        | 21 ++++++---\n dir.c                              | 89 +++++++++++++++++++++++++++++++++++---\n t/t3001-ls-files-others-exclude.sh | 20 +++++++++\n 3 files changed, 118 insertions(+), 12 deletions(-)\n\n-- \n2.3.0.rc1.137.g477eb31\n"},{"id":"269851","messageId":"1442107141-24265-2-git-send-email-pclouds@gmail.com","threadId":"40160","inReplyTo":"1442107141-24265-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 1/2] dir.c: make last_exclude_matching_from_list() run til the end","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2015-09-13T01:19:00Z","receivedAt":"2015-09-13T01:19:00Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"The next patch adds some post processing to the result value before it's\nreturned to the caller. Make all branches reach the end of the function,\nso we can do it all in one place.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n dir.c | 15 ++++++++++-----\n 1 file changed, 10 insertions(+), 5 deletions(-)\n\ndiff --git a/dir.c b/dir.c\nindex c00c7e2..3a7630a 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -901,6 +901,7 @@ static struct exclude *last_exclude_matching_from_list(const char *pathname,\n \t\t\t\t\t\t       int *dtype,\n \t\t\t\t\t\t       struct exclude_list *el)\n {\n+\tstruct exclude *exc = NULL; /* undecided */\n \tint i;\n \n \tif (!el->nr)\n@@ -922,18 +923,22 @@ static struct exclude *last_exclude_matching_from_list(const char *pathname,\n \t\t\tif (match_basename(basename,\n \t\t\t\t\t   pathlen - (basename - pathname),\n \t\t\t\t\t   exclude, prefix, x->patternlen,\n-\t\t\t\t\t   x->flags))\n-\t\t\t\treturn x;\n+\t\t\t\t\t   x->flags)) {\n+\t\t\t\texc = x;\n+\t\t\t\tbreak;\n+\t\t\t}\n \t\t\tcontinue;\n \t\t}\n \n \t\tassert(x->baselen == 0 || x->base[x->baselen - 1] == '/');\n \t\tif (match_pathname(pathname, pathlen,\n \t\t\t\t   x->base, x->baselen ? x->baselen - 1 : 0,\n-\t\t\t\t   exclude, prefix, x->patternlen, x->flags))\n-\t\t\treturn x;\n+\t\t\t\t   exclude, prefix, x->patternlen, x->flags)) {\n+\t\t\texc = x;\n+\t\t\tbreak;\n+\t\t}\n \t}\n-\treturn NULL; /* undecided */\n+\treturn exc;\n }\n \n /*\n-- \n2.3.0.rc1.137.g477eb31\n"},{"id":"269852","messageId":"1442107141-24265-3-git-send-email-pclouds@gmail.com","threadId":"40160","inReplyTo":"1442107141-24265-1-git-send-email-pclouds@gmail.com","subject":"[PATCH v2 2/2] dir.c: don't exclude whole dir prematurely if neg pattern may match","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2015-09-13T01:19:01Z","receivedAt":"2015-09-13T01:19:01Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"If there is a pattern \"!foo/bar\", this patch makes it not exclude \"foo\"\nright away. This gives us a chance to examine \"foo\" and re-include\n\"foo/bar\".\n\nIn order for it to detect that the directory under examination should\nnot be excluded right away, in other words it is a parent directory of a\nnegative pattern, the \"directory path\" of the negative pattern must be\nliteral. Patterns like \"!f?o/bar\" can't stop \"foo\" from being excluded.\n\nBasename matching (i.e. \"no slashes in the pattern\") or must-be-dir\nmatching (i.e. \"trailing slash in the pattern\") does not work well with\nthis. For example, if we descend in \"foo\" and are examining \"foo/abc\",\ncurrent code for \"foo/\" pattern will check if path \"foo/abc\", not \"foo\",\nis a directory. The same problem with basename matching. These may need\nbig code reorg to make it work.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n---\n Documentation/gitignore.txt        | 21 ++++++++---\n dir.c                              | 76 +++++++++++++++++++++++++++++++++++++-\n t/t3001-ls-files-others-exclude.sh | 20 ++++++++++\n 3 files changed, 109 insertions(+), 8 deletions(-)\n\ndiff --git a/Documentation/gitignore.txt b/Documentation/gitignore.txt\nindex 473623d..889a72a 100644\n--- a/Documentation/gitignore.txt\n+++ b/Documentation/gitignore.txt\n@@ -82,12 +82,9 @@ PATTERN FORMAT\n \n  - An optional prefix \"`!`\" which negates the pattern; any\n    matching file excluded by a previous pattern will become\n-   included again. It is not possible to re-include a file if a parent\n-   directory of that file is excluded. Git doesn't list excluded\n-   directories for performance reasons, so any patterns on contained\n-   files have no effect, no matter where they are defined.\n-   Put a backslash (\"`\\`\") in front of the first \"`!`\" for patterns\n-   that begin with a literal \"`!`\", for example, \"`\\!important!.txt`\".\n+   included again. It is possible to re-include a file if a parent\n+   directory of that file is excluded, with restrictions. See section\n+   NOTES for detail.\n \n  - If the pattern ends with a slash, it is removed for the\n    purpose of the following description, but it would only find\n@@ -141,6 +138,18 @@ not tracked by Git remain untracked.\n To stop tracking a file that is currently tracked, use\n 'git rm --cached'.\n \n+To re-include a file when its parent directory is excluded, the\n+following conditions must be met:\n+\n+ - The directory part in the re-include rules must be literal (i.e. no\n+   wildcards)\n+\n+ - The rules to exclude the parent directory must not end with a\n+   trailing slash.\n+\n+ - The rules to exclude the parent directory must have at least one\n+   slash.\n+\n EXAMPLES\n --------\n \ndiff --git a/dir.c b/dir.c\nindex 3a7630a..a1f711c 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -882,6 +882,25 @@ int match_pathname(const char *pathname, int pathlen,\n \t\t */\n \t\tif (!patternlen && !namelen)\n \t\t\treturn 1;\n+\t\t/*\n+\t\t * This can happen when we ignore some exclude rules\n+\t\t * on directories in other to see if negative rules\n+\t\t * may match. E.g.\n+\t\t *\n+\t\t * /abc\n+\t\t * !/abc/def/ghi\n+\t\t *\n+\t\t * The pattern of interest is \"/abc\". On the first\n+\t\t * try, we should match path \"abc\" with this pattern\n+\t\t * in the \"if\" statement right above, but the caller\n+\t\t * ignores it.\n+\t\t *\n+\t\t * On the second try with paths within \"abc\",\n+\t\t * e.g. \"abc/xyz\", we come here and try to match it\n+\t\t * with \"/abc\".\n+\t\t */\n+\t\tif (!patternlen && namelen && *name == '/')\n+\t\t\treturn 1;\n \t}\n \n \treturn fnmatch_icase_mem(pattern, patternlen,\n@@ -890,6 +909,48 @@ int match_pathname(const char *pathname, int pathlen,\n }\n \n /*\n+ * Return non-zero if pathname is a directory and an ancestor of the\n+ * literal path in a (negative) pattern. This is used to keep\n+ * descending in \"foo\" and \"foo/bar\" when the pattern is\n+ * \"!foo/bar/.gitignore\". \"foo/notbar\" will not be descended however.\n+ */\n+static int match_neg_path(const char *pathname, int pathlen, int *dtype,\n+\t\t\t  const char *base, int baselen,\n+\t\t\t  const char *pattern, int prefix, int patternlen,\n+\t\t\t  int flags)\n+{\n+\tassert((flags & EXC_FLAG_NEGATIVE) && !(flags & EXC_FLAG_NODIR));\n+\n+\tif (*dtype == DT_UNKNOWN)\n+\t\t*dtype = get_dtype(NULL, pathname, pathlen);\n+\tif (*dtype != DT_DIR)\n+\t\treturn 0;\n+\n+\tif (*pattern == '/') {\n+\t\tpattern++;\n+\t\tpatternlen--;\n+\t\tprefix--;\n+\t}\n+\n+\tif (baselen) {\n+\t\tif (((pathlen < baselen && base[pathlen] == '/') ||\n+\t\t     pathlen == baselen) &&\n+\t\t    !strncmp_icase(pathname, base, pathlen))\n+\t\t\treturn 1;\n+\t\tpathname += baselen + 1;\n+\t\tpathlen  -= baselen + 1;\n+\t}\n+\n+\n+\tif (prefix &&\n+\t    ((pathlen < prefix && pattern[pathlen] == '/') &&\n+\t     !strncmp_icase(pathname, pattern, pathlen)))\n+\t\treturn 1;\n+\n+\treturn 0;\n+}\n+\n+/*\n  * Scan the given exclude list in reverse to see whether pathname\n  * should be ignored.  The first match (i.e. the last on the list), if\n  * any, determines the fate.  Returns the exclude_list element which\n@@ -902,7 +963,7 @@ static struct exclude *last_exclude_matching_from_list(const char *pathname,\n \t\t\t\t\t\t       struct exclude_list *el)\n {\n \tstruct exclude *exc = NULL; /* undecided */\n-\tint i;\n+\tint i, matched_negative_path = 0;\n \n \tif (!el->nr)\n \t\treturn NULL;\t/* undefined */\n@@ -937,7 +998,18 @@ static struct exclude *last_exclude_matching_from_list(const char *pathname,\n \t\t\texc = x;\n \t\t\tbreak;\n \t\t}\n-\t}\n+\n+\t\tif ((x->flags & EXC_FLAG_NEGATIVE) && !matched_negative_path &&\n+\t\t    match_neg_path(pathname, pathlen, dtype, x->base,\n+\t\t\t\t   x->baselen ? x->baselen - 1 : 0,\n+\t\t\t\t   exclude, prefix, x->patternlen, x->flags))\n+\t\t\tmatched_negative_path = 1;\n+\t}\n+\tif (exc &&\n+\t    !(exc->flags & EXC_FLAG_NEGATIVE) &&\n+\t    !(exc->flags & EXC_FLAG_NODIR) &&\n+\t    matched_negative_path)\n+\t\texc = NULL;\n \treturn exc;\n }\n \ndiff --git a/t/t3001-ls-files-others-exclude.sh b/t/t3001-ls-files-others-exclude.sh\nindex 3fc484e..9de49a6 100755\n--- a/t/t3001-ls-files-others-exclude.sh\n+++ b/t/t3001-ls-files-others-exclude.sh\n@@ -305,4 +305,24 @@ test_expect_success 'ls-files with \"**\" patterns and no slashes' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'negative patterns' '\n+\tgit init reinclude &&\n+\t(\n+\t\tcd reinclude &&\n+\t\tcat >.gitignore <<-\\EOF &&\n+\t\t/foo\n+\t\t!foo/bar/bar\n+\t\tEOF\n+\t\tmkdir -p foo/bar &&\n+\t\ttouch abc foo/def foo/bar/ghi foo/bar/bar &&\n+\t\tgit ls-files -o --exclude-standard >../actual &&\n+\t\tcat >../expected <<-\\EOF &&\n+\t\t.gitignore\n+\t\tabc\n+\t\tfoo/bar/bar\n+\t\tEOF\n+\t\ttest_cmp ../expected ../actual\n+\t)\n+'\n+\n test_done\n-- \n2.3.0.rc1.137.g477eb31\n"},{"id":"269939","messageId":"xmqqd1xkylb4.fsf@gitster.mtv.corp.google.com","threadId":"40160","inReplyTo":"1442107141-24265-3-git-send-email-pclouds@gmail.com","subject":"Re: [PATCH v2 2/2] dir.c: don't exclude whole dir prematurely if neg pattern may match","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2015-09-14T17:15:11Z","receivedAt":"2015-09-14T17:15:11Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:\n\n> diff --git a/Documentation/gitignore.txt b/Documentation/gitignore.txt\n> index 473623d..889a72a 100644\n> --- a/Documentation/gitignore.txt\n> +++ b/Documentation/gitignore.txt\n> @@ -82,12 +82,9 @@ PATTERN FORMAT\n>  \n>   - An optional prefix \"`!`\" which negates the pattern; any\n>     matching file excluded by a previous pattern will become\n> +   included again. It is possible to re-include a file if a parent\n> +   directory of that file is excluded, with restrictions. See section\n> +   NOTES for detail.\n\nSounds like a very useful thing.\n\n>   - If the pattern ends with a slash, it is removed for the\n>     purpose of the following description, but it would only find\n> @@ -141,6 +138,18 @@ not tracked by Git remain untracked.\n>  To stop tracking a file that is currently tracked, use\n>  'git rm --cached'.\n>  \n> +To re-include a file when its parent directory is excluded, the\n> +following conditions must be met:\n> +\n> + - The directory part in the re-include rules must be literal (i.e. no\n> +   wildcards)\n> +\n> + - The rules to exclude the parent directory must not end with a\n> +   trailing slash.\n> +\n> + - The rules to exclude the parent directory must have at least one\n> +   slash.\n> +\n\nIn this bulletted list, don't the readers also need to be told that\nhaving \"/abc\" in .gitignore (but not \"!/abc/anything\" in .gitignore)\nand \"!foo\" in abc/.gitignore would not cause us to descend into\n\"/abc\" just to examine \"abc/.gitignore\" with pessimistic assumption\nthat there might be some exclusion in there?\n\n> diff --git a/t/t3001-ls-files-others-exclude.sh b/t/t3001-ls-files-others-exclude.sh\n> index 3fc484e..9de49a6 100755\n> --- a/t/t3001-ls-files-others-exclude.sh\n> +++ b/t/t3001-ls-files-others-exclude.sh\n> @@ -305,4 +305,24 @@ test_expect_success 'ls-files with \"**\" patterns and no slashes' '\n>  \ttest_cmp expect actual\n>  '\n>  \n> +test_expect_success 'negative patterns' '\n> +\tgit init reinclude &&\n> +\t(\n> +\t\tcd reinclude &&\n> +\t\tcat >.gitignore <<-\\EOF &&\n> +\t\t/foo\n> +\t\t!foo/bar/bar\n> +\t\tEOF\n> +\t\tmkdir -p foo/bar &&\n> +\t\ttouch abc foo/def foo/bar/ghi foo/bar/bar &&\n> +\t\tgit ls-files -o --exclude-standard >../actual &&\n> +\t\tcat >../expected <<-\\EOF &&\n> +\t\t.gitignore\n> +\t\tabc\n> +\t\tfoo/bar/bar\n> +\t\tEOF\n> +\t\ttest_cmp ../expected ../actual\n> +\t)\n> +'\n\nAnd another test here may want to explicitly ensure that we are not\noverly pessimising the ignore processing, so that later changes will\nnot break it, I think.  Or do we already have such a case covered by\nan existing test?\n\nThanks.\n"},{"id":"270210","messageId":"CACsJy8BjyQj0ccmRFSQ5fsYpRtL909828Qv_cauhMpF9nbGU7g@mail.gmail.com","threadId":"40160","inReplyTo":"xmqqd1xkylb4.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH v2 2/2] dir.c: don't exclude whole dir prematurely if neg pattern may match","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2015-09-17T13:21:51Z","receivedAt":"2015-09-17T13:21:51Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Tue, Sep 15, 2015 at 12:15 AM, Junio C Hamano <gitster@pobox.com> wrote:\n> Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:\n>\n>> diff --git a/Documentation/gitignore.txt b/Documentation/gitignore.txt\n>> index 473623d..889a72a 100644\n>> --- a/Documentation/gitignore.txt\n>> +++ b/Documentation/gitignore.txt\n>> @@ -82,12 +82,9 @@ PATTERN FORMAT\n>>\n>>   - An optional prefix \"`!`\" which negates the pattern; any\n>>     matching file excluded by a previous pattern will become\n>> +   included again. It is possible to re-include a file if a parent\n>> +   directory of that file is excluded, with restrictions. See section\n>> +   NOTES for detail.\n>\n> Sounds like a very useful thing.\n>\n>>   - If the pattern ends with a slash, it is removed for the\n>>     purpose of the following description, but it would only find\n>> @@ -141,6 +138,18 @@ not tracked by Git remain untracked.\n>>  To stop tracking a file that is currently tracked, use\n>>  'git rm --cached'.\n>>\n>> +To re-include a file when its parent directory is excluded, the\n>> +following conditions must be met:\n>> +\n>> + - The directory part in the re-include rules must be literal (i.e. no\n>> +   wildcards)\n>> +\n>> + - The rules to exclude the parent directory must not end with a\n>> +   trailing slash.\n>> +\n>> + - The rules to exclude the parent directory must have at least one\n>> +   slash.\n>> +\n>\n> In this bulletted list, don't the readers also need to be told that\n> having \"/abc\" in .gitignore (but not \"!/abc/anything\" in .gitignore)\n> and \"!foo\" in abc/.gitignore would not cause us to descend into\n> \"/abc\" just to examine \"abc/.gitignore\" with pessimistic assumption\n> that there might be some exclusion in there?\n\nYeah I thought it was already mentioned. I guess it's just in my mind.\nWill update.\n\n>> diff --git a/t/t3001-ls-files-others-exclude.sh b/t/t3001-ls-files-others-exclude.sh\n>> index 3fc484e..9de49a6 100755\n>> --- a/t/t3001-ls-files-others-exclude.sh\n>> +++ b/t/t3001-ls-files-others-exclude.sh\n>> @@ -305,4 +305,24 @@ test_expect_success 'ls-files with \"**\" patterns and no slashes' '\n>>       test_cmp expect actual\n>>  '\n>>\n>> +test_expect_success 'negative patterns' '\n>> +     git init reinclude &&\n>> +     (\n>> +             cd reinclude &&\n>> +             cat >.gitignore <<-\\EOF &&\n>> +             /foo\n>> +             !foo/bar/bar\n>> +             EOF\n>> +             mkdir -p foo/bar &&\n>> +             touch abc foo/def foo/bar/ghi foo/bar/bar &&\n>> +             git ls-files -o --exclude-standard >../actual &&\n>> +             cat >../expected <<-\\EOF &&\n>> +             .gitignore\n>> +             abc\n>> +             foo/bar/bar\n>> +             EOF\n>> +             test_cmp ../expected ../actual\n>> +     )\n>> +'\n>\n> And another test here may want to explicitly ensure that we are not\n> overly pessimising the ignore processing, so that later changes will\n> not break it, I think.\n\nYep. Will do.\n-- \nDuy\n"}]}