git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org
Cc: "Jeff King" <peff@peff.net>,
	avila.jn@gmail.com, "Junio C Hamano" <gitster@pobox.com>,
	"Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH 4/4] attr.c: fix matching "subdir" without the trailing slash
Date: Mon, 25 Mar 2013 13:05:10 +0700	[thread overview]
Message-ID: <1364191510-8900-5-git-send-email-pclouds@gmail.com> (raw)
In-Reply-To: <1364191510-8900-1-git-send-email-pclouds@gmail.com>

The story goes back to 94bc671 (Add directory pattern matching to
attributes - 2012-12-08). Before this commit, directories are passed
to path_matches without the trailing slash. This is fine for matching
pattern "subdir" with "foo/subdir".

Patterns like "subdir/" (i.e. match _directory_ subdir) won't work
though. So paths are now passed to path_matches with the trailing
slash (i.e. "subdir/"). The trailing slash is used as the directory
indicator (similar to dtype in exclude case). This makes pattern
"subdir/" match directory "subdir/". Pattern "subdir" no longer match
subdir, which is now "subdir/".

As the trailing slash in pathname is the directory indicator, we do
not need to keep it in the pathname for matching. The trailing slash
should be turned to dtype "DT_DIR" and stripped out of pathname. This
keeps the code pattern similar to exclude.

The same applies for the pattern "subdir/". The trailing slash is
converted to flag EXC_FLAG_MUSTBEDIR and should not remain in the
pattern, as noted in parse_exclude_pattern(). prepare_attr_stack()
breaks this and keeps the trailing slash anyway.

To sum up, both patterns and pathnames should never have the trailing
slash when it comes to match_basename.

Reported-and-analyzed-by: Jeff King <peff@peff.net>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 attr.c                          | 11 +++++++++--
 t/t5002-archive-attr-pattern.sh |  6 ++++++
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/attr.c b/attr.c
index 1818ba5..a95c837 100644
--- a/attr.c
+++ b/attr.c
@@ -256,7 +256,7 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
 				      &res->u.pat.flags,
 				      &res->u.pat.nowildcardlen);
 		if (res->u.pat.flags & EXC_FLAG_MUSTBEDIR)
-			res->u.pat.patternlen++;
+			p[res->u.pat.patternlen] = '\0';
 		if (res->u.pat.flags & EXC_FLAG_NEGATIVE) {
 			warning(_("Negative patterns are ignored in git attributes\n"
 				  "Use '\\!' for literal leading exclamation."));
@@ -665,9 +665,16 @@ static int path_matches(const char *pathname, int pathlen,
 {
 	const char *pattern = pat->pattern;
 	int prefix = pat->nowildcardlen;
+	int dtype;
+
+	if (pathlen && pathname[pathlen-1] == '/') {
+		dtype = DT_DIR;
+		pathlen--;
+	} else
+		dtype = DT_REG;
 
 	if ((pat->flags & EXC_FLAG_MUSTBEDIR) &&
-	    ((!pathlen) || (pathname[pathlen-1] != '/')))
+	    dtype != DT_DIR)
 		return 0;
 
 	if (pat->flags & EXC_FLAG_NODIR) {
diff --git a/t/t5002-archive-attr-pattern.sh b/t/t5002-archive-attr-pattern.sh
index 0c847fb..98ccc3c 100755
--- a/t/t5002-archive-attr-pattern.sh
+++ b/t/t5002-archive-attr-pattern.sh
@@ -27,6 +27,10 @@ test_expect_success 'setup' '
 	echo ignored-only-if-dir/ export-ignore >>.git/info/attributes &&
 	git add ignored-only-if-dir &&
 
+	mkdir -p ignored-without-slash &&
+	echo ignored without slash >ignored-without-slash/foo &&
+	git add ignored-without-slash/foo &&
+	echo ignored-without-slash export-ignore >>.git/info/attributes &&
 
 	mkdir -p one-level-lower/two-levels-lower/ignored-only-if-dir &&
 	echo ignored by ignored dir >one-level-lower/two-levels-lower/ignored-only-if-dir/ignored-by-ignored-dir &&
@@ -49,6 +53,8 @@ test_expect_exists	archive/not-ignored-dir/ignored-only-if-dir
 test_expect_exists	archive/not-ignored-dir/
 test_expect_missing	archive/ignored-only-if-dir/
 test_expect_missing	archive/ignored-ony-if-dir/ignored-by-ignored-dir
+test_expect_missing	archive/ignored-without-slash/ &&
+test_expect_missing	archive/ignored-without-slash/foo &&
 test_expect_exists	archive/one-level-lower/
 test_expect_missing	archive/one-level-lower/two-levels-lower/ignored-only-if-dir/
 test_expect_missing	archive/one-level-lower/two-levels-lower/ignored-ony-if-dir/ignored-by-ignored-dir
-- 
1.8.2.82.gc24b958

  parent reply	other threads:[~2013-03-25  6:06 UTC|newest]

Thread overview: 66+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-03-19 17:57 [regression?] trailing slash required in .gitattributes Jeff King
2013-03-19 18:10 ` Junio C Hamano
2013-03-19 18:10 ` Jeff King
2013-03-22 22:24   ` Jeff King
2013-03-22 23:08     ` Junio C Hamano
2013-03-23  8:39       ` Jeff King
2013-03-24  5:25         ` Junio C Hamano
2013-03-26 18:39         ` [PATCH 0/4] attribute regression fix for maint-1.8.1 and upward Junio C Hamano
2013-03-26 18:39           ` [PATCH 1/4] attr.c::path_matches(): the basename is part of the pathname Junio C Hamano
2013-03-26 18:49             ` Jeff King
2013-03-27  1:40               ` Duy Nguyen
2013-03-26 18:39           ` [PATCH 2/4] dir.c::match_basename(): pay attention to the length of string parameters Junio C Hamano
2013-03-26 18:55             ` Jeff King
2013-03-26 20:39               ` Jeff King
2013-03-26 20:49                 ` Junio C Hamano
2013-03-26 21:29                   ` Jeff King
2013-03-26 22:33                     ` Junio C Hamano
2013-03-27  1:04                       ` Jeff King
2013-03-26 18:39           ` [PATCH 3/4] attr.c::path_matches(): special case paths that end with a slash Junio C Hamano
2013-03-26 19:05             ` Jeff King
2013-03-26 21:33               ` Jeff King
2013-03-27  1:30                 ` Duy Nguyen
2013-03-28 19:49               ` Jeff King
2013-03-26 18:39           ` [PATCH 4/4] make sure a pattern without trailing slash matches a directory Junio C Hamano
2013-03-26 19:08             ` Jeff King
2013-03-27  1:13           ` [PATCH 0/4] attribute regression fix for maint-1.8.1 and upward Duy Nguyen
2013-03-27  3:57             ` Junio C Hamano
2013-03-27  4:01               ` Duy Nguyen
2013-03-28 21:43           ` [PATCH v2 0/6] " Jeff King
2013-03-28 21:45             ` [PATCH 1/6] attr.c::path_matches(): the basename is part of the pathname Jeff King
2013-03-28 21:47             ` [PATCH 2/6] dir.c::match_basename(): pay attention to the length of string parameters Jeff King
2013-03-28 22:40               ` Jeff King
2013-03-28 22:49                 ` Jeff King
2013-03-28 23:10                   ` Junio C Hamano
2013-03-28 23:40                   ` Duy Nguyen
2013-03-29  1:25               ` Duy Nguyen
2013-03-29  3:02                 ` Jeff King
2013-03-29  5:57                   ` Junio C Hamano
2013-03-28 21:47             ` [PATCH 3/6] dir.c::match_pathname(): adjust patternlen when shifting pattern Jeff King
2013-03-28 21:48             ` [PATCH 4/6] dir.c::match_pathname(): pay attention to the length of string parameters Jeff King
2013-03-28 22:30               ` Junio C Hamano
2013-03-29  8:45               ` Duy Nguyen
2013-03-29 10:03                 ` Duy Nguyen
2013-03-29 11:32                   ` Torsten Bögershausen
2013-03-29 11:37                     ` Duy Nguyen
2013-03-29 12:05                 ` Jeff King
2013-03-29 13:02                   ` Duy Nguyen
2013-03-29 16:44                     ` Junio C Hamano
2013-03-29 17:04                       ` Jeff King
2013-03-29 17:35                         ` Junio C Hamano
2013-03-29 17:44                           ` Jeff King
2013-03-30  1:40                       ` Duy Nguyen
2013-03-28 21:49             ` [PATCH 5/6] attr.c::path_matches(): special case paths that end with a slash Jeff King
2013-03-28 21:50             ` [PATCH 6/6] t: check that a pattern without trailing slash matches a directory Jeff King
2013-03-28 22:21               ` Eric Sunshine
2013-03-28 22:22                 ` Jeff King
2013-03-23  4:18     ` [regression?] trailing slash required in .gitattributes Duy Nguyen
2013-03-23  4:43       ` Duy Nguyen
2013-03-25  6:05 ` [PATCH 0/4] attr directory matching regression Nguyễn Thái Ngọc Duy
2013-03-25  6:05   ` [PATCH 1/4] wildmatch: do not require "text" to be NUL-terminated Nguyễn Thái Ngọc Duy
2013-03-25  6:05   ` [PATCH 2/4] attr.c: fix pattern{,len} inconsistency in struct match_attr Nguyễn Thái Ngọc Duy
2013-03-25  6:05   ` [PATCH 3/4] dir.c: make match_{base,path}name respect {basename,path}len Nguyễn Thái Ngọc Duy
2013-03-25  6:05   ` Nguyễn Thái Ngọc Duy [this message]
2013-03-25  7:20     ` [PATCH 4/4] attr.c: fix matching "subdir" without the trailing slash Duy Nguyen
2013-03-25  9:24       ` Duy Nguyen
2013-03-26 15:10   ` [PATCH 0/4] attr directory matching regression Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1364191510-8900-5-git-send-email-pclouds@gmail.com \
    --to=pclouds@gmail.com \
    --cc=avila.jn@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).