Skip to contentSkip to conversation

trailing slash required in .gitattributes

regression? In review Jeff King 5 participants 66 messages Jun 15, 2016 Kbuild

lore
Share & tools
lore

Jeff King

author
#0 lore raw

Prior to v1.8.1.1, if I did this:

git init
echo content >foo &&
mkdir subdir &&
echo content >subdir/bar &&
echo "subdir export-ignore" >.gitattributes
git add . &&
git commit -m one &&
git archive HEAD | tar tf -

my archive would contain only "foo" and ".gitattributes", not subdir. As of v1.8.1.1, the attribute on subdir is ignored unless it is written with a trailing slash, like:

subdir/ export-ignore

The issue bisects to 94bc671 (Add directory pattern matching to attributes, 2012-12-08). That commit actually tests not only that "subdir/" matches, but also that just "subdir" does not match.

The commit message there is vague about the reasoning, but my understanding is that it was meant to harmonize gitignore and gitattributes, the former of which can take "dir/". I don't have a problem with offering "dir/" to match only directories, but what is the point in disallowing just "dir" to match a directory?

It seems like a pointless regression to me, but I'm not clear whether it was intentional or not (and if it was intentional, I think we would need to handle it with a proper transition period, not in a maint release).

-Peff

author
#1 lore raw
Quoted 15 lines · Jeff King

Prior to v1.8.1.1, if I did this:

git init
echo content >foo &&
mkdir subdir &&
echo content >subdir/bar &&
echo "subdir export-ignore" >.gitattributes
git add . &&
git commit -m one &&
git archive HEAD | tar tf -

my archive would contain only "foo" and ".gitattributes", not subdir. As of v1.8.1.1, the attribute on subdir is ignored unless it is written with a trailing slash, like:

Quoted 5 lines

subdir/ export-ignore

The issue bisects to 94bc671 (Add directory pattern matching to attributes, 2012-12-08). That commit actually tests not only that "subdir/" matches, but also that just "subdir" does not match.

Sorry, I mis-read the tests. They are not testing that "subdir" does not work, only that "subdir/" will match only a directory, not a regular file. Which does make sense.

So I think the regression is accidental. And we would want tests like this on top (which currently fail):

t/t5002-archive-attr-pattern.sh +6 -0
index 0c847fb..3be809c 100755
@@ -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_missing	archive/ignored-ony-if-dir/ignored-by-ignored-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
author
#2 lore raw
Quoted 7 lines · Jeff King
Quoted 3 lines

The issue bisects to 94bc671 (Add directory pattern matching to attributes, 2012-12-08). That commit actually tests not only that "subdir/" matches, but also that just "subdir" does not match.

[...]
So I think the regression is accidental. And we would want tests like this on top (which currently fail):
[...]

I'm having trouble figuring out the right solution for this.

The problem is in path_matches, which used to receive just the unadorned pathname, and now receives "path/" for directories. It now looks like this:

Quoted 11 lines

static int path_matches(const char *pathname, int pathlen,
const char *basename, const struct pattern *pat, const char *base, int baselen)

{
	const char *pattern = pat->pattern;
	int prefix = pat->nowildcardlen;

	if ((pat->flags & EXC_FLAG_MUSTBEDIR) &&
	    ((!pathlen) || (pathname[pathlen-1] != '/')))
		return 0;

This first stanza checks that a pattern like "foo/" must be matched by a real directory. Which is fine; that's the point of adding the "/" to the pattern.

Quoted 10 lines
	if (pat->flags & EXC_FLAG_NODIR) {
		return match_basename(basename,
				      pathlen - (basename - pathname),
				      pattern, prefix,
				      pat->patternlen, pat->flags);
	}
	return match_pathname(pathname, pathlen,
			      base, baselen,
			      pattern, prefix, pat->patternlen, pat->flags);
}

But then here we'll end up feeding "foo/" to be compared with "foo", which we don't want. For a pattern "foo", we want to match _either_ "foo/" or "foo". So you'd think something like:

  if (pathlen && pathname[pathlen-1] == '/')
          pathlen--;

would work. But it seems that match_basename, despite taking the length of all of the strings we pass it, will happily use NUL-terminated functions like strcmp or fnmatch. Converting the former to check lengths should be pretty straightforward. But there is no version of fnmatch that does what we want. I wonder if we using wildmatch can get around this limitation.

-Peff

#3 lore raw
Quoted 1 line · Jeff King

I'm having trouble figuring out the right solution for this.

Thanks for looking into this. It was on my todo list, but you beat me to it :)

Quoted 7 lines

But then here we'll end up feeding "foo/" to be compared with "foo", which we don't want. For a pattern "foo", we want to match _either_ "foo/" or "foo". So you'd think something like:

  if (pathlen && pathname[pathlen-1] == '/')
          pathlen--;
Quoted 6 lines

would work. But it seems that match_basename, despite taking the length of all of the strings we pass it, will happily use NUL-terminated functions like strcmp or fnmatch. Converting the former to check lengths should be pretty straightforward. But there is no version of fnmatch that does what we want. I wonder if we using wildmatch can get around this limitation.

You can use nwildmatch() from this patch. I tested it lightly with t3070-wildmatch.sh, feeding the strings with no terminating NUL. It seems to work ok.

-- 8< --
Subject: [PATCH] wildmatch: do not require "text" to be NUL-terminated

This may be helpful when we just want to match a part of "text". nwildmatch can be used for this purpose.

---
 wildmatch.c | 25 +++++++++++++------------
 wildmatch.h | 11 +++++++++--
 2 files changed, 22 insertions(+), 14 deletions(-)
wildmatch.c +13 -12
index 7192bdc..f97ae2a 100644
@@ -52,7 +52,8 @@ typedef unsigned char uchar;
 #define ISXDIGIT(c) (ISASCII(c) && isxdigit(c))
 
 /* Match pattern "p" against "text" */
-static int dowild(const uchar *p, const uchar *text, unsigned int flags)
+static int dowild(const uchar *p, const uchar *text,
+		  const uchar *textend, unsigned int flags)
 {
 	uchar p_ch;
 	const uchar *pattern = p;
@@ -60,8 +61,9 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
 	for ( ; (p_ch = *p) != '\0'; text++, p++) {
 		int matched, match_slash, negated;
 		uchar t_ch, prev_ch;
-		if ((t_ch = *text) == '\0' && p_ch != '*')
+		if (text >= textend && p_ch != '*')
 			return WM_ABORT_ALL;
+		t_ch = *text;
 		if ((flags & WM_CASEFOLD) && ISUPPER(t_ch))
 			t_ch = tolower(t_ch);
 		if ((flags & WM_CASEFOLD) && ISUPPER(p_ch))
@@ -101,7 +103,7 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
 					 * both foo/bar and foo/a/bar.
 					 */
 					if (p[0] == '/' &&
-					    dowild(p + 1, text, flags) == WM_MATCH)
+					    dowild(p + 1, text, textend, flags) == WM_MATCH)
 						return WM_MATCH;
 					match_slash = 1;
 				} else
@@ -130,9 +132,7 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
 				/* the slash is consumed by the top-level for loop */
 				break;
 			}
-			while (1) {
-				if (t_ch == '\0')
-					break;
+			while (text < textend) {
 				/*
 				 * Try to advance faster when an asterisk is
 				 * followed by a literal. We know in this case
@@ -145,18 +145,18 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
 					p_ch = *p;
 					if ((flags & WM_CASEFOLD) && ISUPPER(p_ch))
 						p_ch = tolower(p_ch);
-					while ((t_ch = *text) != '\0' &&
+					while (text < textend &&
 					       (match_slash || t_ch != '/')) {
 						if ((flags & WM_CASEFOLD) && ISUPPER(t_ch))
 							t_ch = tolower(t_ch);
 						if (t_ch == p_ch)
 							break;
-						text++;
+						t_ch = *++text;
 					}
 					if (t_ch != p_ch)
 						return WM_NOMATCH;
 				}
-				if ((matched = dowild(p, text, flags)) != WM_NOMATCH) {
+				if ((matched = dowild(p, text, textend, flags)) != WM_NOMATCH) {
 					if (!match_slash || matched != WM_ABORT_TO_STARSTAR)
 						return matched;
 				} else if (!match_slash && t_ch == '/')
@@ -261,12 +261,13 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
 		}
 	}
 
-	return *text ? WM_NOMATCH : WM_MATCH;
+	return text < textend ? WM_NOMATCH : WM_MATCH;
 }
 
 /* Match the "pattern" against the "text" string. */
-int wildmatch(const char *pattern, const char *text,
+int nwildmatch(const char *pattern, const char *text, int textlen,
 	      unsigned int flags, struct wildopts *wo)
 {
-	return dowild((const uchar*)pattern, (const uchar*)text, flags);
+	return dowild((const uchar*)pattern, (const uchar*)text,
+		      (const uchar*)text + textlen, flags);
 }
wildmatch.h +9 -2
index 4090c8f..cdd7544 100644
@@ -12,7 +12,14 @@
 
 struct wildopts;
 
-int wildmatch(const char *pattern, const char *text,
-	      unsigned int flags,
+int nwildmatch(const char *pattern, const char *text,
+	      int len, unsigned int flags,
 	      struct wildopts *wo);
+
+/* Match the "pattern" against the "text" string. */
+static inline int wildmatch(const char *pattern, const char *text,
+	      unsigned int flags, struct wildopts *wo)
+{
+	return nwildmatch(pattern, text, strlen(text), flags, wo);
+}
 #endif
Signature · 3 lines
1.8.2.83.gc99314b

-- 8< --
#4 lore raw
Quoted 3 lines · Duy Nguyen

You can use nwildmatch() from this patch. I tested it lightly with t3070-wildmatch.sh, feeding the strings with no terminating NUL. It seems to work ok.

And valgrind spotted my faults, especially for using strchr. You would need this on top:

-- 8< --

wildmatch.c +15 -9
index f97ae2a..939bac8 100644
@@ -61,9 +61,13 @@ static int dowild(const uchar *p, const uchar *text,
 	for ( ; (p_ch = *p) != '\0'; text++, p++) {
 		int matched, match_slash, negated;
 		uchar t_ch, prev_ch;
-		if (text >= textend && p_ch != '*')
-			return WM_ABORT_ALL;
-		t_ch = *text;
+		if (text >= textend) {
+			if (p_ch != '*')
+				return WM_ABORT_ALL;
+			else
+				t_ch = '\0';
+		} else
+			t_ch = *text;
 		if ((flags & WM_CASEFOLD) && ISUPPER(t_ch))
 			t_ch = tolower(t_ch);
 		if ((flags & WM_CASEFOLD) && ISUPPER(p_ch))
@@ -115,8 +119,9 @@ static int dowild(const uchar *p, const uchar *text,
 				/* Trailing "**" matches everything.  Trailing "*" matches
 				 * only if there are no more slash characters. */
 				if (!match_slash) {
-					if (strchr((char*)text, '/') != NULL)
-						return WM_NOMATCH;
+					for (;text < textend; text++)
+						if (*text == '/')
+							return WM_NOMATCH;
 				}
 				return WM_MATCH;
 			} else if (!match_slash && *p == '/') {
@@ -125,10 +130,11 @@ static int dowild(const uchar *p, const uchar *text,
 				 * with WM_PATHNAME matches the next
 				 * directory
 				 */
-				const char *slash = strchr((char*)text, '/');
-				if (!slash)
+				for (;text < textend; text++)
+					if (*text == '/')
+						break;
+				if (text == textend)
 					return WM_NOMATCH;
-				text = (const uchar*)slash;
 				/* the slash is consumed by the top-level for loop */
 				break;
 			}
@@ -151,7 +157,7 @@ static int dowild(const uchar *p, const uchar *text,
 							t_ch = tolower(t_ch);
 						if (t_ch == p_ch)
 							break;
-						t_ch = *++text;
+						t_ch = ++text < textend ? *text : '\0';
 					}
 					if (t_ch != p_ch)
 						return WM_NOMATCH;

-- 8< --

#5 lore raw
In reply to Jeff King
Quoted 9 lines · Jeff King
  if (pathlen && pathname[pathlen-1] == '/')
          pathlen--;

would work. But it seems that match_basename, despite taking the length of all of the strings we pass it, will happily use NUL-terminated functions like strcmp or fnmatch. Converting the former to check lengths should be pretty straightforward. But there is no version of fnmatch that does what we want. I wonder if we using wildmatch can get around this limitation.

Or save away pathname[pathlen], temporarily NUL terminate and call these functions?

author
#6 lore raw
Quoted 12 lines · Junio C Hamano
Quoted 9 lines · Jeff King
  if (pathlen && pathname[pathlen-1] == '/')
          pathlen--;

would work. But it seems that match_basename, despite taking the length of all of the strings we pass it, will happily use NUL-terminated functions like strcmp or fnmatch. Converting the former to check lengths should be pretty straightforward. But there is no version of fnmatch that does what we want. I wonder if we using wildmatch can get around this limitation.

Quoted 2 lines

Or save away pathname[pathlen], temporarily NUL terminate and call these functions?

Yeah, that is a possibility, though it involves casting away some constness. Patch is below, which seems to work.

It still feels really ugly to me, and like match_basename is misdesigned and should respect the lengths we pass it. Also, if it does respect the lengths, it should be able to go much faster (e.g., in the common case, we can drop a ton of strcmp_icase calls if we just check the lengths beforehand). I feel like Duy was working on something like this recently, but I don't see anything in pu.

---

attr.c +46 -8
index e2f9377..bd00a78 100644
@@ -663,20 +663,58 @@ static int path_matches(const char *pathname, int pathlen,
 {
 	const char *pattern = pat->pattern;
 	int prefix = pat->nowildcardlen;
+	char path_munge = 0;
+	char pattern_munge = 0;
+	int ret;
 
 	if ((pat->flags & EXC_FLAG_MUSTBEDIR) &&
 	    ((!pathlen) || (pathname[pathlen-1] != '/')))
 		return 0;
 
+	/*
+	 * Drop trailing slash from path, as we would want
+	 * an unadorned pattern like "foo" to match both the
+	 * file "foo" and the directory "foo/".
+	 */
+	if (pathlen && pathname[pathlen-1] == '/') {
+		pathlen--;
+
+		/*
+		 * The match_* functions, despite taking a string length, will
+		 * happily read all the way up to the NUL-terminating character.
+		 * So we must not only shrink pathlen, but munge the buffer
+		 * to NUL-terminate it.
+		 */
+		path_munge = pathname[pathlen];
+		((char *)pathname)[pathlen] = '\0';
+	}
+
+	/*
+	 * The pattern up to patternlen will not include a
+	 * trailing slash, but it may still be present in the string.
+	 * And since the match_* functions will read up to the NUL,
+	 * we need to terminate the buffer.
+	 */
+	pattern_munge = pattern[pat->patternlen];
+	((char *)pattern)[pat->patternlen] = '\0';
+
 	if (pat->flags & EXC_FLAG_NODIR) {
-		return match_basename(basename,
-				      pathlen - (basename - pathname),
-				      pattern, prefix,
-				      pat->patternlen, pat->flags);
-	}
-	return match_pathname(pathname, pathlen,
-			      base, baselen,
-			      pattern, prefix, pat->patternlen, pat->flags);
+		ret = match_basename(basename,
+				     pathlen - (basename - pathname),
+				     pattern, prefix,
+				     pat->patternlen, pat->flags);
+	}
+	else {
+		ret = match_pathname(pathname, pathlen,
+				     base, baselen,
+				     pattern, prefix,
+				     pat->patternlen, pat->flags);
+	}
+
+	if (path_munge)
+		((char *)pathname)[pathlen] = path_munge;
+	((char *)pattern)[pat->patternlen] = pattern_munge;
+	return ret;
 }
 
 static int macroexpand_one(int attr_nr, int rem);
t/t5002-archive-attr-pattern.sh +6 -0
index 0c847fb..3be809c 100755
@@ -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_missing	archive/ignored-ony-if-dir/ignored-by-ignored-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
#7 lore raw
Quoted 2 lines · Jeff King

Yeah, that is a possibility, though it involves casting away some constness. Patch is below, which seems to work.

Hmm, because this was after I read this part:

Quoted 3 lines

... match_basename, despite taking the length
of all of the strings we pass it, will happily use NUL-terminated functions like strcmp or fnmatch.

I actually meant to do that inside match_basename(), not in one particular caller of it. Otherwise, new callers to match_basename() will also suffer from this broken API that pretends as if it takes counted strings but uses strings as NUL terminated, no?

Quoted 2 lines

It still feels really ugly to me, and like match_basename is misdesigned and should respect the lengths we pass it.

Exactly.

#8 lore raw

cover attribute regression fix for maint-1.8.1 and upward

In reply to Jeff King

So here is an attempt to fix the unintended regression, on top of 9db9eecfe5c2 (attr: avoid calling find_basename() twice per path, 2013-01-16). It consists of four patches.

The first patch is not essential to the fix, but I think it clarifies what is going on in this codepath.

The second patch addresses the issue Jeff noticed; it appears as if match_basename() takes counted strings, but one of the strings was not a counted string at all. Its length was given to the function because the caller already had one, so that we do not have to do strlen() ourselves. And the other one was meant to be a counted string, but the callee was not using it as such. The patch makes them both counted strings and treat them as such.

The third patch is the main fix. As I said in the log message, I didn't look at it very carefully, so extra sets of eyeballs are very much appreciated.

The last one is a test stolen from Jeff to seal the series. It needs sign-off from Jeff.

Jeff King (1):
make sure a pattern without trailing slash matches a directory

Junio C Hamano (3):
attr.c::path_matches(): the basename is part of the pathname dir.c::match_basename(): pay attention to the length of string
parameters
attr.c::path_matches(): special case paths that end with a slash

 attr.c                          | 23 ++++++++++++-----------
 dir.c                           | 31 +++++++++++++++++++++++++++----
 t/t5002-archive-attr-pattern.sh |  6 ++++++
 3 files changed, 45 insertions(+), 15 deletions(-)
Signature · 1 line
1.8.2-350-g3df87a1
#9 lore raw

PATCH 1/4 attr.c::path_matches(): the basename is part of the pathname

The function takes two strings (pathname and basename) as if they are independent strings, but in reality, the latter is always pointing into a substring in the former.

Clarify this relationship by expressing the latter as an offset into the former.

---
 attr.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
attr.c +10 -9
index ab2aab2..4cfe0ee 100644
@@ -655,7 +655,7 @@ static void prepare_attr_stack(const char *path, int dirlen)
 }
 
 static int path_matches(const char *pathname, int pathlen,
-			const char *basename,
+			int basename_offset,
 			const struct pattern *pat,
 			const char *base, int baselen)
 {
@@ -667,8 +667,8 @@ static int path_matches(const char *pathname, int pathlen,
 		return 0;
 
 	if (pat->flags & EXC_FLAG_NODIR) {
-		return match_basename(basename,
-				      pathlen - (basename - pathname),
+		return match_basename(pathname + basename_offset,
+				      pathlen - basename_offset,
 				      pattern, prefix,
 				      pat->patternlen, pat->flags);
 	}
@@ -701,7 +701,7 @@ static int fill_one(const char *what, struct match_attr *a, int rem)
 	return rem;
 }
 
-static int fill(const char *path, int pathlen, const char *basename,
+static int fill(const char *path, int pathlen, int basename_offset,
 		struct attr_stack *stk, int rem)
 {
 	int i;
@@ -711,7 +711,7 @@ static int fill(const char *path, int pathlen, const char *basename,
 		struct match_attr *a = stk->attrs[i];
 		if (a->is_macro)
 			continue;
-		if (path_matches(path, pathlen, basename,
+		if (path_matches(path, pathlen, basename_offset,
 				 &a->u.pat, base, stk->originlen))
 			rem = fill_one("fill", a, rem);
 	}
@@ -750,7 +750,8 @@ static void collect_all_attrs(const char *path)
 {
 	struct attr_stack *stk;
 	int i, pathlen, rem, dirlen;
-	const char *basename, *cp, *last_slash = NULL;
+	const char *cp, *last_slash = NULL;
+	int basename_offset;
 
 	for (cp = path; *cp; cp++) {
 		if (*cp == '/' && cp[1])
@@ -758,10 +759,10 @@ static void collect_all_attrs(const char *path)
 	}
 	pathlen = cp - path;
 	if (last_slash) {
-		basename = last_slash + 1;
+		basename_offset = last_slash + 1 - path;
 		dirlen = last_slash - path;
 	} else {
-		basename = path;
+		basename_offset = 0;
 		dirlen = 0;
 	}
 
@@ -771,7 +772,7 @@ static void collect_all_attrs(const char *path)
 
 	rem = attr_nr;
 	for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
-		rem = fill(path, pathlen, basename, stk, rem);
+		rem = fill(path, pathlen, basename_offset, stk, rem);
 }
 
 int git_check_attr(const char *path, int num, struct git_attr_check *check)
Signature · 1 line
1.8.2-350-g3df87a1
author
#10 lore raw

Re: [PATCH 1/4] attr.c::path_matches(): the basename is part of the pathname

Quoted 8 lines · Junio C Hamano

The function takes two strings (pathname and basename) as if they are independent strings, but in reality, the latter is always pointing into a substring in the former.

Clarify this relationship by expressing the latter as an offset into the former.

This is a huge improvement in maintainability. My initial fix attempt was to just xstrdup() the strings (knowing that the performance would be horrible, but I was still investigating correctness issues at that point). And of course I ran into this same issue as I tried to make a copy of pathname.

So even without the rest of the fix, this is definitely a good idea. :)

-Peff

#11 lore raw

Re: [PATCH 1/4] attr.c::path_matches(): the basename is part of the pathname

Quoted 11 lines · Jeff King
Quoted 8 lines · Junio C Hamano

The function takes two strings (pathname and basename) as if they are independent strings, but in reality, the latter is always pointing into a substring in the former.

Clarify this relationship by expressing the latter as an offset into the former.

Quoted 7 lines

This is a huge improvement in maintainability. My initial fix attempt was to just xstrdup() the strings (knowing that the performance would be horrible, but I was still investigating correctness issues at that point). And of course I ran into this same issue as I tried to make a copy of pathname.

So even without the rest of the fix, this is definitely a good idea. :)

match_{base,path}name and their exclude callers do the same thing. I guess I'm used to it and did not see the maintainability issue. Maybe we should do the same there too.

Signature · 1 line
Duy
#12 lore raw

PATCH 2/4 dir.c::match_basename(): pay attention to the length of string parameters

In reply to Junio C Hamano

The function takes two counted strings (<basename, basenamelen> and <pattern, patternlen>) as parameters, together with prefix (the length of the prefix in pattern that is to be matched literally without globbing against the basename) and EXC_* flags that tells it how to match the pattern against the basename.

However, it did not pay attention to the length of these counted strings. Update them to do the following:

* When the entire pattern is to be matched literally, the pattern matches the basename only when the lengths of them are the same, and they match up to that length.

* When the pattern is "*" followed by a string to be matched literally, make sure that the basenamelen is equal or longer than the "literal" part of the pattern, and the tail of the basename string matches that literal part.

* Otherwise, make sure we use only the counted part of the strings when calling fnmatch_icase(). Because these counted strings are full strings most of the time, avoid unnecessary allocation.

---
 dir.c | 31 +++++++++++++++++++++++++++----
 1 file changed, 27 insertions(+), 4 deletions(-)
dir.c +27 -4
index 5a83aa7..aa16303 100644
@@ -537,15 +537,38 @@ int match_basename(const char *basename, int basenamelen,
 		   int flags)
 {
 	if (prefix == patternlen) {
-		if (!strcmp_icase(pattern, basename))
+		if (patternlen == basenamelen &&
+		    !strncmp_icase(pattern, basename, basenamelen))
 			return 1;
 	} else if (flags & EXC_FLAG_ENDSWITH) {
+		/* "*literal" matching against "fooliteral" */
 		if (patternlen - 1 <= basenamelen &&
-		    !strcmp_icase(pattern + 1,
-				  basename + basenamelen - patternlen + 1))
+		    !strncmp_icase(pattern + 1,
+				   basename + basenamelen - (patternlen - 1),
+				   patternlen - 1))
 			return 1;
 	} else {
-		if (fnmatch_icase(pattern, basename, 0) == 0)
+		int match_status;
+		struct strbuf pat = STRBUF_INIT;
+		struct strbuf base = STRBUF_INIT;
+		const char *use_pat = pattern;
+		const char *use_base = basename;
+
+		if (pattern[patternlen]) {
+			strbuf_add(&pat, pattern, patternlen);
+			use_pat = pat.buf;
+		}
+		if (basename[basenamelen]) {
+			strbuf_add(&base, basename, basenamelen);
+			use_base = base.buf;
+		}
+		match_status = fnmatch_icase(use_pat, use_base, 0);
+		if (use_pat)
+			strbuf_release(&pat);
+		if (use_base)
+			strbuf_release(&base);
+
+		if (match_status == 0)
 			return 1;
 	}
 	return 0;
Signature · 1 line
1.8.2-350-g3df87a1
author
#13 lore raw

Re: [PATCH 2/4] dir.c::match_basename(): pay attention to the length of string parameters

Quoted 13 lines · Junio C Hamano

The function takes two counted strings (<basename, basenamelen> and <pattern, patternlen>) as parameters, together with prefix (the length of the prefix in pattern that is to be matched literally without globbing against the basename) and EXC_* flags that tells it how to match the pattern against the basename.

However, it did not pay attention to the length of these counted strings. Update them to do the following:

* When the entire pattern is to be matched literally, the pattern matches the basename only when the lengths of them are the same, and they match up to that length.

Quoted 8 lines

* When the pattern is "*" followed by a string to be matched literally, make sure that the basenamelen is equal or longer than the "literal" part of the pattern, and the tail of the basename string matches that literal part.

* Otherwise, make sure we use only the counted part of the strings when calling fnmatch_icase(). Because these counted strings are full strings most of the time, avoid unnecessary allocation.

I think this is OK, with the intention that we would eventually drop the allocations from your third bullet point in favor of using a byte-counted version of fnmatch (i.e., nwildmatch). But until then we're going to see a performance drop.

The pattern is usually going to be NUL-terminated at the length counter, but every time we feed a directory, it's going to run into this allocation. And we do it once for _every_ directory against _every_ wildcard gitignore pattern. So I think it is probably going to be measurable. I guess we can try measuring it on something like WebKit, which has plenty of both directories and gitattributes.

-Peff

author
#14 lore raw

Re: [PATCH 2/4] dir.c::match_basename(): pay attention to the length of string parameters

Quoted 9 lines · Jeff King
Quoted 3 lines

* Otherwise, make sure we use only the counted part of the strings when calling fnmatch_icase(). Because these counted strings are full strings most of the time, avoid unnecessary allocation.

I think this is OK, with the intention that we would eventually drop the allocations from your third bullet point in favor of using a byte-counted version of fnmatch (i.e., nwildmatch). But until then we're going to see a performance drop.

Quoted 6 lines

The pattern is usually going to be NUL-terminated at the length counter, but every time we feed a directory, it's going to run into this allocation. And we do it once for _every_ directory against _every_ wildcard gitignore pattern. So I think it is probably going to be measurable. I guess we can try measuring it on something like WebKit, which has plenty of both directories and gitattributes.

I timed this doing "git archive HEAD" on webkit.git before and after. It actually ended up not mattering much (I think because it is only the directories which are affected, not each individually path, so it's a much smaller number than you'd think). The best-of-five timing was slightly slower, but was within the noise.

So I do still think it would make sense to go to a byte-limited version of fnmatch eventually, just for code cleanliness and predictability of performance, but this is really not a bad solution in the interim.

-Peff

#15 lore raw

Re: [PATCH 2/4] dir.c::match_basename(): pay attention to the length of string parameters

Quoted 5 lines · Jeff King

I timed this doing "git archive HEAD" on webkit.git before and after. It actually ended up not mattering much (I think because it is only the directories which are affected, not each individually path, so it's a much smaller number than you'd think). The best-of-five timing was slightly slower, but was within the noise.

Interesting. Because "archive" has to incur a large I/O cost anyway, I expected extra allocation for correctness for only the directory paths would be dwarfed in the noise.

I actually care more about cases other than "archive", though. Do we even feed directory paths to the machinery?

Quoted 3 lines

So I do still think it would make sense to go to a byte-limited version of fnmatch eventually, just for code cleanliness and predictability of performance, but this is really not a bad solution in the interim.

Yes, what we do with wildmatch is a separate issue for 'master' and upwards.

author
#16 lore raw

Re: [PATCH 2/4] dir.c::match_basename(): pay attention to the length of string parameters

Quoted 8 lines · Junio C Hamano
Quoted 5 lines · Jeff King

I timed this doing "git archive HEAD" on webkit.git before and after. It actually ended up not mattering much (I think because it is only the directories which are affected, not each individually path, so it's a much smaller number than you'd think). The best-of-five timing was slightly slower, but was within the noise.

Quoted 6 lines

Interesting. Because "archive" has to incur a large I/O cost anyway, I expected extra allocation for correctness for only the directory paths would be dwarfed in the noise.

I actually care more about cases other than "archive", though. Do we even feed directory paths to the machinery?

In general, no, I don't think so. That's why I tested "archive", since I knew it did. In the normal case, we should just feed file paths, meaning we only run into this code path when somebody has "foo/" in their pattern. Testing like:

  git ls-files -z >files
  time git check-attr --stdin -z -a <files >/dev/null

showed a difference well within the noise.

Quoted 6 lines
Quoted 3 lines

So I do still think it would make sense to go to a byte-limited version of fnmatch eventually, just for code cleanliness and predictability of performance, but this is really not a bad solution in the interim.

Yes, what we do with wildmatch is a separate issue for 'master' and upwards.

Oh, agreed. I just wanted to see how much performance would be impacted for the interim. But it seems that it's not.

So I think your series is the right direction, but we would want to factor out the allocation code and use it from match_pathname, as well.

-Peff

#17 lore raw

Re: [PATCH 2/4] dir.c::match_basename(): pay attention to the length of string parameters

Quoted 2 lines · Jeff King

So I think your series is the right direction, but we would want to factor out the allocation code and use it from match_pathname, as well.

I am deep into today's integration cycle, so perhaps in the meantime you can help with a follow-up patch ;-)?

author
#18 lore raw

Re: [PATCH 2/4] dir.c::match_basename(): pay attention to the length of string parameters

Quoted 7 lines · Junio C Hamano
Quoted 2 lines · Jeff King

So I think your series is the right direction, but we would want to factor out the allocation code and use it from match_pathname, as well.

I am deep into today's integration cycle, so perhaps in the meantime you can help with a follow-up patch ;-)?

Your afternoon integration is my dinner-time. :)

I'll try to look at it tomorrow.

-Peff

#19 lore raw

PATCH 3/4 attr.c::path_matches(): special case paths that end with a slash

In reply to Junio C Hamano

The function is given a string that ends with a slash to signal that the path is a directory to make sure that a pattern that ends with a slash (i.e. MUSTBEDIR) can tell directories and non-directories apart. However, the pattern itself (pat->pattern and pat->patternlen) that came from such a MUSTBEDIR pattern is represented as a string that ends with a slash, but patternlen does not count that trailing slash. A MUSTBEDIR pattern "element/" is represented as a counted string <"element/", 7> and this must match match pathname "element/".

Because match_basename() wants to see pathname "element" to match against the pattern <"element/", 7>, reduce the length of the path to exclude the trailing slash when calling match_basename().

A similar adjustment for match_pathname() might be needed, but I didn't look into it.

---
 attr.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
attr.c +3 -3
index 4cfe0ee..00a0016 100644
@@ -661,14 +661,14 @@ static int path_matches(const char *pathname, int pathlen,
 {
 	const char *pattern = pat->pattern;
 	int prefix = pat->nowildcardlen;
+	int isdir = (pathlen && pathname[pathlen - 1] == '/');
 
-	if ((pat->flags & EXC_FLAG_MUSTBEDIR) &&
-	    ((!pathlen) || (pathname[pathlen-1] != '/')))
+	if ((pat->flags & EXC_FLAG_MUSTBEDIR) && !isdir)
 		return 0;
 
 	if (pat->flags & EXC_FLAG_NODIR) {
 		return match_basename(pathname + basename_offset,
-				      pathlen - basename_offset,
+				      pathlen - basename_offset - isdir,
 				      pattern, prefix,
 				      pat->patternlen, pat->flags);
 	}
Signature · 1 line
1.8.2-350-g3df87a1
author
#20 lore raw

Re: [PATCH 3/4] attr.c::path_matches(): special case paths that end with a slash

Quoted 2 lines · Junio C Hamano

A similar adjustment for match_pathname() might be needed, but I didn't look into it.

I notice that match_pathname takes _two_ lengths for the pattern: the nowildcardlen (called "prefix", and the full patternlen). But the first thing it does is:

  if (*pattern == '/') {
          pattern++;
          prefix--;
  }

which seems obviously wrong, as patternlen should be dropped, too. But we do not seem to look at patternlen at all! I think we can drop the parameter totally.

We do seem to use strncmp_icase through the rest of the function, though, which should be OK. The one exception is that we call fnmatch at the end. Should the allocation hack from the previous patch make its way into an "fnmatch_icase_bytes()" function, so we can use it here, too? And then when we have a more efficient solution, we can just plug it in there.

-Peff

author
#21 lore raw

Re: [PATCH 3/4] attr.c::path_matches(): special case paths that end with a slash

Quoted 18 lines · Jeff King
Quoted 2 lines · Junio C Hamano

A similar adjustment for match_pathname() might be needed, but I didn't look into it.

I notice that match_pathname takes _two_ lengths for the pattern: the nowildcardlen (called "prefix", and the full patternlen). But the first thing it does is:

  if (*pattern == '/') {
          pattern++;
          prefix--;
  }

which seems obviously wrong, as patternlen should be dropped, too. But we do not seem to look at patternlen at all! I think we can drop the parameter totally.

Quoted 6 lines

We do seem to use strncmp_icase through the rest of the function, though, which should be OK. The one exception is that we call fnmatch at the end. Should the allocation hack from the previous patch make its way into an "fnmatch_icase_bytes()" function, so we can use it here, too? And then when we have a more efficient solution, we can just plug it in there.

Hmm. match_pathname does have this:

        /*
         * baselen does not count the trailing slash. base[] may or
         * may not end with a trailing slash though.
         */
        if (pathlen < baselen + 1 ||
            (baselen && pathname[baselen] != '/') ||
            strncmp_icase(pathname, base, baselen))
                return 0;

which seems to imply that the trailing slash is important here, and that we should not drop it when passing the path to match_pathname. I'm still trying to figure out exactly what it is that the extra slash check is for, and whether it might not have the same problem.

-Peff

#22 lore raw

Re: [PATCH 3/4] attr.c::path_matches(): special case paths that end with a slash

Quoted 11 lines · Jeff King

Hmm. match_pathname does have this:

        /*
         * baselen does not count the trailing slash. base[] may or
         * may not end with a trailing slash though.
         */
        if (pathlen < baselen + 1 ||
            (baselen && pathname[baselen] != '/') ||
            strncmp_icase(pathname, base, baselen))
                return 0;
Quoted 4 lines

which seems to imply that the trailing slash is important here, and that we should not drop it when passing the path to match_pathname. I'm still trying to figure out exactly what it is that the extra slash check is for, and whether it might not have the same problem.

The "may not end with a trailing slash" can only happen when baselen == 0. And that rule is documented in code, at this line in last_exclude_matching_from_list:

assert(x->baselen == 0 || x->base[x->baselen - 1] == '/');

Signature · 1 line
Duy
author
#23 lore raw

Re: [PATCH 3/4] attr.c::path_matches(): special case paths that end with a slash

In reply to Jeff King
Quoted 11 lines · Jeff King
Quoted 2 lines · Junio C Hamano

A similar adjustment for match_pathname() might be needed, but I didn't look into it.

[...]
We do seem to use strncmp_icase through the rest of the function, though, which should be OK. The one exception is that we call fnmatch at the end. Should the allocation hack from the previous patch make its way into an "fnmatch_icase_bytes()" function, so we can use it here, too? And then when we have a more efficient solution, we can just plug it in there.

Hmm, yeah, there is more going on here than just that. If I add the tests below, the first one (a wildcard) passes, because you fixed the fnmatch code path. But the deep/ ones do not, as they should be going through match_pathname. I expected the deep/with/wildcard one to fail (because of the fnmatch problem I mentioned above), but not the deep/and/slashless one, which should be using strncmp. I'll see if I can track down the cause.

-Peff

---

t/t5002-archive-attr-pattern.sh +21 -0
index 3be809c..234a615 100755
@@ -32,6 +32,21 @@ test_expect_success 'setup' '
 	git add ignored-without-slash/foo &&
 	echo ignored-without-slash export-ignore >>.git/info/attributes &&
 
+	mkdir -p wildcard-without-slash &&
+	echo "ignored without slash" >wildcard-without-slash/foo &&
+	git add wildcard-without-slash/foo &&
+	echo "wild*-without-slash export-ignore" >>.git/info/attributes &&
+
+	mkdir -p deep/and/slashless &&
+	echo "ignored without slash" >deep/and/slashless/foo &&
+	git add deep/and/slashless/foo &&
+	echo deep/and/slashless export-ignore >>.git/info/attributes &&
+
+	mkdir -p deep/with/wildcard &&
+	echo "ignored without slash" >deep/with/wildcard/foo &&
+	git add deep/with/wildcard/foo &&
+	echo "deep/*t*/wildcard 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 &&
 	git add one-level-lower &&
@@ -55,6 +70,12 @@ test_expect_missing     archive/ignored-without-slash/foo &&
 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_missing     archive/wildcard-without-slash/
+test_expect_missing     archive/wildcard-without-slash/foo &&
+test_expect_missing     archive/deep/and/slashless/ &&
+test_expect_missing     archive/deep/and/slashless/foo &&
+test_expect_missing     archive/deep/with/wildcard/ &&
+test_expect_missing     archive/deep/with/wildcard/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
#24 lore raw

PATCH 4/4 make sure a pattern without trailing slash matches a directory

In reply to Junio C Hamano

From: Jeff King <[email protected]>

Prior to v1.8.1.1, with:

git init
echo content >foo &&
mkdir subdir &&
echo content >subdir/bar &&
echo "subdir export-ignore" >.gitattributes
git add . &&
git commit -m one &&
git archive HEAD | tar tf -

the resulting archive would contain only "foo" and ".gitattributes", not subdir. This was broken with a recent change that intended to allow "subdir/ export-ignore" to also exclude the directory, but instead ended up _requiring_ the trailing slash by mistake.

A pattern "subdir" should match any path "subdir", whether it is a directory or a non-diretory. A pattern "subdir/" insists that a path "subdir" must be a directory for it to match.

---
 t/t5002-archive-attr-pattern.sh | 6 ++++++
 1 file changed, 6 insertions(+)
t/t5002-archive-attr-pattern.sh +6 -0
index 0c847fb..3be809c 100755
@@ -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
Signature · 1 line
1.8.2-350-g3df87a1
author
#25 lore raw

Re: [PATCH 4/4] make sure a pattern without trailing slash matches a directory

Quoted 13 lines · Junio C Hamano

From: Jeff King <[email protected]>

Prior to v1.8.1.1, with:

git init
echo content >foo &&
mkdir subdir &&
echo content >subdir/bar &&
echo "subdir export-ignore" >.gitattributes
git add . &&
git commit -m one &&
git archive HEAD | tar tf -

Quoted 4 lines

the resulting archive would contain only "foo" and ".gitattributes", not subdir. This was broken with a recent change that intended to allow "subdir/ export-ignore" to also exclude the directory, but instead ended up _requiring_ the trailing slash by mistake.

Yeah, I think that is fine. I'd just squash this test and description into the previous patch, though (I do not care about dropping my commit count by 1).

And of course,

Quoted 3 lines

A pattern "subdir" should match any path "subdir", whether it is a directory or a non-diretory. A pattern "subdir/" insists that a path "subdir" must be a directory for it to match.

s/diretory/directory/

-Peff

#26 lore raw

Re: [PATCH 0/4] attribute regression fix for maint-1.8.1 and upward

In reply to Junio C Hamano
Quoted 3 lines · Junio C Hamano

So here is an attempt to fix the unintended regression, on top of 9db9eecfe5c2 (attr: avoid calling find_basename() twice per path, 2013-01-16). It consists of four patches.

Not that I disagree with this. Just wanted to see how far the "dtype" idea went. How about this? git_check_attr() now takes dtype as an argument and the caller must not add the trailing slash. This could be split into two patches, one for git_check_attr prototype change, and the other the real meat.

-- 8< --

archive.c +4 -4
index 93e00bb..ab811b3 100644
@@ -112,7 +112,7 @@ static int write_archive_entry(const unsigned char *sha1, const char *base,
 	write_archive_entry_fn_t write_entry = c->write_entry;
 	struct git_attr_check check[2];
 	const char *path_without_prefix;
-	int err;
+	int err, dtype;
 
 	args->convert = 0;
 	strbuf_reset(&path);
@@ -120,18 +120,18 @@ static int write_archive_entry(const unsigned char *sha1, const char *base,
 	strbuf_add(&path, args->base, args->baselen);
 	strbuf_add(&path, base, baselen);
 	strbuf_addstr(&path, filename);
-	if (S_ISDIR(mode) || S_ISGITLINK(mode))
-		strbuf_addch(&path, '/');
+	dtype = S_ISDIR(mode) || S_ISGITLINK(mode) ? DT_DIR : DT_REG;
 	path_without_prefix = path.buf + args->baselen;
 
 	setup_archive_check(check);
-	if (!git_check_attr(path_without_prefix, ARRAY_SIZE(check), check)) {
+	if (!git_check_attr(path_without_prefix, dtype, ARRAY_SIZE(check), check)) {
 		if (ATTR_TRUE(check[0].value))
 			return 0;
 		args->convert = ATTR_TRUE(check[1].value);
 	}
 
 	if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
+		strbuf_addch(&path, '/');
 		if (args->verbose)
 			fprintf(stderr, "%.*s\n", (int)path.len, path.buf);
 		err = write_entry(args, sha1, path.buf, path.len, mode);
attr.c +17 -19
index e2f9377..ab6422c 100644
@@ -255,6 +255,8 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
 				      &res->u.pat.patternlen,
 				      &res->u.pat.flags,
 				      &res->u.pat.nowildcardlen);
+		if (res->u.pat.flags & EXC_FLAG_MUSTBEDIR)
+			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."));
@@ -657,15 +659,14 @@ static void prepare_attr_stack(const char *path, int dirlen)
 }
 
 static int path_matches(const char *pathname, int pathlen,
-			const char *basename,
+			const char *basename, int dtype,
 			const struct pattern *pat,
 			const char *base, int baselen)
 {
 	const char *pattern = pat->pattern;
 	int prefix = pat->nowildcardlen;
 
-	if ((pat->flags & EXC_FLAG_MUSTBEDIR) &&
-	    ((!pathlen) || (pathname[pathlen-1] != '/')))
+	if ((pat->flags & EXC_FLAG_MUSTBEDIR) && dtype != DT_DIR)
 		return 0;
 
 	if (pat->flags & EXC_FLAG_NODIR) {
@@ -704,7 +705,7 @@ static int fill_one(const char *what, struct match_attr *a, int rem)
 }
 
 static int fill(const char *path, int pathlen, const char *basename,
-		struct attr_stack *stk, int rem)
+		int dtype, struct attr_stack *stk, int rem)
 {
 	int i;
 	const char *base = stk->origin ? stk->origin : "";
@@ -713,7 +714,7 @@ static int fill(const char *path, int pathlen, const char *basename,
 		struct match_attr *a = stk->attrs[i];
 		if (a->is_macro)
 			continue;
-		if (path_matches(path, pathlen, basename,
+		if (path_matches(path, pathlen, basename, dtype,
 				 &a->u.pat, base, stk->originlen))
 			rem = fill_one("fill", a, rem);
 	}
@@ -748,20 +749,17 @@ static int macroexpand_one(int attr_nr, int rem)
  * Collect all attributes for path into the array pointed to by
  * check_all_attr.
  */
-static void collect_all_attrs(const char *path)
+static void collect_all_attrs(const char *path, int dtype)
 {
 	struct attr_stack *stk;
 	int i, pathlen, rem, dirlen;
-	const char *basename, *cp, *last_slash = NULL;
+	const char *basename, *cp;
 
-	for (cp = path; *cp; cp++) {
-		if (*cp == '/' && cp[1])
-			last_slash = cp;
-	}
-	pathlen = cp - path;
-	if (last_slash) {
-		basename = last_slash + 1;
-		dirlen = last_slash - path;
+	cp = strrchr(path, '/');
+	pathlen = strlen(path);
+	if (cp) {
+		basename = cp + 1;
+		dirlen = cp - path;
 	} else {
 		basename = path;
 		dirlen = 0;
@@ -773,14 +771,14 @@ static void collect_all_attrs(const char *path)
 
 	rem = attr_nr;
 	for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
-		rem = fill(path, pathlen, basename, stk, rem);
+		rem = fill(path, pathlen, basename, dtype, stk, rem);
 }
 
-int git_check_attr(const char *path, int num, struct git_attr_check *check)
+int git_check_attr(const char *path, int dtype, int num, struct git_attr_check *check)
 {
 	int i;
 
-	collect_all_attrs(path);
+	collect_all_attrs(path, dtype);
 
 	for (i = 0; i < num; i++) {
 		const char *value = check_all_attr[check[i].attr->attr_nr].value;
@@ -796,7 +794,7 @@ int git_all_attrs(const char *path, int *num, struct git_attr_check **check)
 {
 	int i, count, j;
 
-	collect_all_attrs(path);
+	collect_all_attrs(path, DT_REG);
 
 	/* Count the number of attributes that are set. */
 	count = 0;
attr.h +1 -1
index 8b08d33..ce39c9c 100644
@@ -36,7 +36,7 @@ struct git_attr_check {
  */
 char *git_attr_name(struct git_attr *);
 
-int git_check_attr(const char *path, int, struct git_attr_check *);
+int git_check_attr(const char *path, int, int, struct git_attr_check *);
 
 /*
  * Retrieve all attributes that apply to the specified path.  *num
builtin/check-attr.c +1 -1
index 075d01d..261e57d 100644
@@ -49,7 +49,7 @@ static void check_attr(const char *prefix, int cnt,
 	char *full_path =
 		prefix_path(prefix, prefix ? strlen(prefix) : 0, file);
 	if (check != NULL) {
-		if (git_check_attr(full_path, cnt, check))
+		if (git_check_attr(full_path, DT_REG, cnt, check))
 			die("git_check_attr died");
 		output_attr(cnt, check, file);
 	} else {
builtin/pack-objects.c +1 -1
index f069462..7a77288 100644
@@ -894,7 +894,7 @@ static int no_try_delta(const char *path)
 	struct git_attr_check check[1];
 
 	setup_delta_attr_check(check);
-	if (git_check_attr(path, ARRAY_SIZE(check), check))
+	if (git_check_attr(path, DT_REG, ARRAY_SIZE(check), check))
 		return 0;
 	if (ATTR_FALSE(check->value))
 		return 1;
convert.c +1 -1
index 3520252..3f09cbb 100644
@@ -755,7 +755,7 @@ static void convert_attrs(struct conv_attrs *ca, const char *path)
 		git_config(read_convert_config, NULL);
 	}
 
-	if (!git_check_attr(path, NUM_CONV_ATTRS, ccheck)) {
+	if (!git_check_attr(path, DT_REG, NUM_CONV_ATTRS, ccheck)) {
 		ca->crlf_action = git_path_check_crlf(path, ccheck + 4);
 		if (ca->crlf_action == CRLF_GUESS)
 			ca->crlf_action = git_path_check_crlf(path, ccheck + 0);
ll-merge.c +2 -2
index fb61ea6..1944392 100644
@@ -342,7 +342,7 @@ static int git_path_check_merge(const char *path, struct git_attr_check check[2]
 		check[0].attr = git_attr("merge");
 		check[1].attr = git_attr("conflict-marker-size");
 	}
-	return git_check_attr(path, 2, check);
+	return git_check_attr(path, DT_REG, 2, check);
 }
 
 static void normalize_file(mmfile_t *mm, const char *path)
@@ -399,7 +399,7 @@ int ll_merge_marker_size(const char *path)
 
 	if (!check.attr)
 		check.attr = git_attr("conflict-marker-size");
-	if (!git_check_attr(path, 1, &check) && check.value) {
+	if (!git_check_attr(path, DT_REG, 1, &check) && check.value) {
 		marker_size = atoi(check.value);
 		if (marker_size <= 0)
 			marker_size = DEFAULT_CONFLICT_MARKER_SIZE;
t/t5002-archive-attr-pattern.sh +6 -0
index 0c847fb..98ccc3c 100755
@@ -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
userdiff.c +1 -1
index ea43a03..fd4f576 100644
@@ -260,7 +260,7 @@ struct userdiff_driver *userdiff_find_by_path(const char *path)
 
 	if (!path)
 		return NULL;
-	if (git_check_attr(path, 1, &check))
+	if (git_check_attr(path, DT_REG, 1, &check))
 		return NULL;
 
 	if (ATTR_TRUE(check.value))
ws.c +1 -1
index b498d75..34c2145 100644
@@ -88,7 +88,7 @@ unsigned whitespace_rule(const char *pathname)
 	struct git_attr_check attr_whitespace_rule;
 
 	setup_whitespace_attr_check(&attr_whitespace_rule);
-	if (!git_check_attr(pathname, 1, &attr_whitespace_rule)) {
+	if (!git_check_attr(pathname, DT_REG, 1, &attr_whitespace_rule)) {
 		const char *value;
 
 		value = attr_whitespace_rule.value;

-- 8< --

#27 lore raw

Re: [PATCH 0/4] attribute regression fix for maint-1.8.1 and upward

Quoted 4 lines · Duy Nguyen

How about this? git_check_attr() now takes dtype as an argument and the caller must not add the trailing slash. This could be split into two patches, one for git_check_attr prototype change, and the other the real meat.

"git check-attr" fundamentally cannot know, but aside from that do all the callsites know if the path in question is a directory or not? My impression was that there are some cases you do not necessarily know.

"Add slash when you _know_ it is a directory, but otherwise pass the path without trailing slash." is easier to understand than "Pass 040000 if you know it is a directory, but otherwise pass 100644", exactly because "otherwise" in both of these instructions include the case where the path in question _is_ a directory (you just do not know what it is).

I do not particularly like the "trailing slash on the basename" approach, but it feels less bad than passing dtype down.

#28 lore raw

Re: [PATCH 0/4] attribute regression fix for maint-1.8.1 and upward

Quoted 19 lines · Junio C Hamano
Quoted 4 lines · Duy Nguyen

How about this? git_check_attr() now takes dtype as an argument and the caller must not add the trailing slash. This could be split into two patches, one for git_check_attr prototype change, and the other the real meat.

"git check-attr" fundamentally cannot know, but aside from that do all the callsites know if the path in question is a directory or not? My impression was that there are some cases you do not necessarily know.

"Add slash when you _know_ it is a directory, but otherwise pass the path without trailing slash." is easier to understand than "Pass 040000 if you know it is a directory, but otherwise pass 100644", exactly because "otherwise" in both of these instructions include the case where the path in question _is_ a directory (you just do not know what it is).

Quoted 2 lines

I do not particularly like the "trailing slash on the basename" approach, but it feels less bad than passing dtype down.

Fair enough. I'll rebase my changes on top of yours as long term cleanup. Maybe I can make nwildmatch take patternlen too.

Signature · 1 line
Duy
author
#29 lore raw

v2 cover attribute regression fix for maint-1.8.1 and upward

In reply to Junio C Hamano
Quoted 3 lines · Junio C Hamano

So here is an attempt to fix the unintended regression, on top of 9db9eecfe5c2 (attr: avoid calling find_basename() twice per path, 2013-01-16). It consists of four patches.

Here's my update to the series. I think this should fix all of the issues. And it should be very easy to drop in Duy's nwildmatch later on; it can just replace the fnmatch_icase_mem function added in patch 2 below.

The main fix in this iteration is that match_pathname receives the same treatment as match_basename, which is done in patches 3 and 4 (the issues were subtly different enough that I didn't want to squash it all together; plus, gotta keep that commit count up).

[1/6]: attr.c::path_matches(): the basename is part of the pathname [2/6]: dir.c::match_basename(): pay attention to the length of string parameters [3/6]: dir.c::match_pathname(): adjust patternlen when shifting pattern [4/6]: dir.c::match_pathname(): pay attention to the length of string parameters [5/6]: attr.c::path_matches(): special case paths that end with a slash [6/6]: t: check that a pattern without trailing slash matches a directory

-Peff

PS I followed your subject-naming convention since I was adding into
your series, but it seems quite long to me. I would have just said:
"match_basename: pay attention...".

author
#30 lore raw

PATCH v2 1/6 attr.c::path_matches(): the basename is part of the pathname

From: Junio C Hamano <[email protected]>

The function takes two strings (pathname and basename) as if they are independent strings, but in reality, the latter is always pointing into a substring in the former.

Clarify this relationship by expressing the latter as an offset into the former.

---
This is identical to the original 1/4.

 attr.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
attr.c +10 -9
index ab2aab2..4cfe0ee 100644
@@ -655,7 +655,7 @@ static int path_matches(const char *pathname, int pathlen,
 }
 
 static int path_matches(const char *pathname, int pathlen,
-			const char *basename,
+			int basename_offset,
 			const struct pattern *pat,
 			const char *base, int baselen)
 {
@@ -667,8 +667,8 @@ static int path_matches(const char *pathname, int pathlen,
 		return 0;
 
 	if (pat->flags & EXC_FLAG_NODIR) {
-		return match_basename(basename,
-				      pathlen - (basename - pathname),
+		return match_basename(pathname + basename_offset,
+				      pathlen - basename_offset,
 				      pattern, prefix,
 				      pat->patternlen, pat->flags);
 	}
@@ -701,7 +701,7 @@ static int fill_one(const char *what, struct match_attr *a, int rem)
 	return rem;
 }
 
-static int fill(const char *path, int pathlen, const char *basename,
+static int fill(const char *path, int pathlen, int basename_offset,
 		struct attr_stack *stk, int rem)
 {
 	int i;
@@ -711,7 +711,7 @@ static int fill(const char *path, int pathlen, const char *basename,
 		struct match_attr *a = stk->attrs[i];
 		if (a->is_macro)
 			continue;
-		if (path_matches(path, pathlen, basename,
+		if (path_matches(path, pathlen, basename_offset,
 				 &a->u.pat, base, stk->originlen))
 			rem = fill_one("fill", a, rem);
 	}
@@ -750,7 +750,8 @@ static void collect_all_attrs(const char *path)
 {
 	struct attr_stack *stk;
 	int i, pathlen, rem, dirlen;
-	const char *basename, *cp, *last_slash = NULL;
+	const char *cp, *last_slash = NULL;
+	int basename_offset;
 
 	for (cp = path; *cp; cp++) {
 		if (*cp == '/' && cp[1])
@@ -758,10 +759,10 @@ static void collect_all_attrs(const char *path)
 	}
 	pathlen = cp - path;
 	if (last_slash) {
-		basename = last_slash + 1;
+		basename_offset = last_slash + 1 - path;
 		dirlen = last_slash - path;
 	} else {
-		basename = path;
+		basename_offset = 0;
 		dirlen = 0;
 	}
 
@@ -771,7 +772,7 @@ static void collect_all_attrs(const char *path)
 
 	rem = attr_nr;
 	for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
-		rem = fill(path, pathlen, basename, stk, rem);
+		rem = fill(path, pathlen, basename_offset, stk, rem);
 }
 
 int git_check_attr(const char *path, int num, struct git_attr_check *check)
Signature · 1 line
1.8.2.13.g0f18d3c
author
#31 lore raw

PATCH v2 2/6 dir.c::match_basename(): pay attention to the length of string parameters

In reply to Jeff King

From: Junio C Hamano <[email protected]>

The function takes two counted strings (<basename, basenamelen> and <pattern, patternlen>) as parameters, together with prefix (the length of the prefix in pattern that is to be matched literally without globbing against the basename) and EXC_* flags that tells it how to match the pattern against the basename.

However, it did not pay attention to the length of these counted strings. Update them to do the following:

* When the entire pattern is to be matched literally, the pattern matches the basename only when the lengths of them are the same, and they match up to that length.

* When the pattern is "*" followed by a string to be matched literally, make sure that the basenamelen is equal or longer than the "literal" part of the pattern, and the tail of the basename string matches that literal part.

* Otherwise, use the new fnmatch_icase_mem helper to make sure we only lookmake sure we use only look at the counted part of the strings. Because these counted strings are full strings most of the time, we check for termination to avoid unnecessary allocation.

---
Compared to v1:

- This factors the fnmatch bits into a helper function so we can reuse it later. As a result, the variable names are changed a bit.

- The original did:

if (use_pat)
strbuf_release(&pat);

but AFAICT that was a useless conditional; use_pat always points to either the incoming buffer or the strbuf. But strbuf_release will handle both cases for us.

 dir.c | 40 ++++++++++++++++++++++++++++++++++++----
 1 file changed, 36 insertions(+), 4 deletions(-)
dir.c +36 -4
index 5a83aa7..fac82c1 100644
@@ -34,6 +34,33 @@ int fnmatch_icase(const char *pattern, const char *string, int flags)
 	return fnmatch(pattern, string, flags | (ignore_case ? FNM_CASEFOLD : 0));
 }
 
+static int fnmatch_icase_mem(const char *pattern, int patternlen,
+			     const char *string, int stringlen,
+			     int flags)
+{
+	int match_status;
+	struct strbuf pat_buf = STRBUF_INIT;
+	struct strbuf str_buf = STRBUF_INIT;
+	const char *use_pat = pattern;
+	const char *use_str = string;
+
+	if (pattern[patternlen]) {
+		strbuf_add(&pat_buf, pattern, patternlen);
+		use_pat = pat_buf.buf;
+	}
+	if (string[stringlen]) {
+		strbuf_add(&str_buf, string, stringlen);
+		use_str = str_buf.buf;
+	}
+
+	match_status = fnmatch_icase(use_pat, use_str, 0);
+
+	strbuf_release(&pat_buf);
+	strbuf_release(&str_buf);
+
+	return match_status;
+}
+
 static size_t common_prefix_len(const char **pathspec)
 {
 	const char *n, *first;
@@ -537,15 +564,20 @@ int match_basename(const char *basename, int basenamelen,
 		   int flags)
 {
 	if (prefix == patternlen) {
-		if (!strcmp_icase(pattern, basename))
+		if (patternlen == basenamelen &&
+		    !strncmp_icase(pattern, basename, basenamelen))
 			return 1;
 	} else if (flags & EXC_FLAG_ENDSWITH) {
+		/* "*literal" matching against "fooliteral" */
 		if (patternlen - 1 <= basenamelen &&
-		    !strcmp_icase(pattern + 1,
-				  basename + basenamelen - patternlen + 1))
+		    !strncmp_icase(pattern + 1,
+				   basename + basenamelen - (patternlen - 1),
+				   patternlen - 1))
 			return 1;
 	} else {
-		if (fnmatch_icase(pattern, basename, 0) == 0)
+		if (fnmatch_icase_mem(pattern, patternlen,
+				      basename, basenamelen,
+				      0) == 0)
 			return 1;
 	}
 	return 0;
Signature · 1 line
1.8.2.13.g0f18d3c
author
#32 lore raw

Re: [PATCH 2/6] dir.c::match_basename(): pay attention to the length of string parameters

Quoted 8 lines · Jeff King

From: Junio C Hamano <[email protected]>

The function takes two counted strings (<basename, basenamelen> and <pattern, patternlen>) as parameters, together with prefix (the length of the prefix in pattern that is to be matched literally without globbing against the basename) and EXC_* flags that tells it how to match the pattern against the basename.

Quoted 6 lines

However, it did not pay attention to the length of these counted strings. Update them to do the following:

* When the entire pattern is to be matched literally, the pattern matches the basename only when the lengths of them are the same, and they match up to that length.

Hrm. Though the tip of this series passes all tests, this one actually breaks bisectability. What happens is that the existing code passes:

path=foo/
pathlen=4

pattern=foo/
patternlen=3

match_basename is happy to compare "foo/" to "foo/" and realize they're equal. With this change, we compare "foo" to "foo/" and do not match. It isn't until the later patch where you start passing pathlen=3 that it works again.

I wonder if it is worth reordering the series to put the path_matches fix first.

-Peff

author
#33 lore raw

Re: [PATCH 2/6] dir.c::match_basename(): pay attention to the length of string parameters

Quoted 13 lines · Jeff King
Quoted 11 lines · Jeff King

From: Junio C Hamano <[email protected]>

The function takes two counted strings (<basename, basenamelen> and <pattern, patternlen>) as parameters, together with prefix (the length of the prefix in pattern that is to be matched literally without globbing against the basename) and EXC_* flags that tells it how to match the pattern against the basename.

However, it did not pay attention to the length of these counted strings. Update them to do the following:

Quoted 6 lines
Quoted 3 lines

* When the entire pattern is to be matched literally, the pattern matches the basename only when the lengths of them are the same, and they match up to that length.

Hrm. Though the tip of this series passes all tests, this one actually breaks bisectability. What happens is that the existing code passes:

Ugh. That is a problem, but this series does _not_ pass all tests. I think I failed to run the complete test suite on the correct tip.

My match_pathspec fix breaks at least t1011.

-Peff

#34 lore raw

Re: [PATCH 2/6] dir.c::match_basename(): pay attention to the length of string parameters

Quoted 19 lines · Jeff King
Quoted 17 lines · Jeff King
Quoted 14 lines · Jeff King

From: Junio C Hamano <[email protected]>

The function takes two counted strings (<basename, basenamelen> and <pattern, patternlen>) as parameters, together with prefix (the length of the prefix in pattern that is to be matched literally without globbing against the basename) and EXC_* flags that tells it how to match the pattern against the basename.

However, it did not pay attention to the length of these counted strings. Update them to do the following:

* When the entire pattern is to be matched literally, the pattern matches the basename only when the lengths of them are the same, and they match up to that length.

Quoted 7 lines
Quoted 2 lines

Hrm. Though the tip of this series passes all tests, this one actually breaks bisectability. What happens is that the existing code passes:

Ugh. That is a problem, but this series does _not_ pass all tests. I think I failed to run the complete test suite on the correct tip.

My match_pathspec fix breaks at least t1011.

Yeah, the tip of 'jch' (slightly ahead of 'next' that I use myself) has 0003, 1011 and 3001 broken X-<.

#36 lore raw

Re: [PATCH 2/6] dir.c::match_basename(): pay attention to the length of string parameters

In reply to Jeff King
Quoted 10 lines · Jeff King
dir.c +10 -0
+static int fnmatch_icase_mem(const char *pattern, int patternlen,
+                            const char *string, int stringlen,
+                            int flags)
+{
+       int match_status;
+       struct strbuf pat_buf = STRBUF_INIT;
+       struct strbuf str_buf = STRBUF_INIT;
+       const char *use_pat = pattern;
+       const char *use_str = string;
+

dir.c int fnmatch_icase(const char *pattern, const char *string, int flags) · line 56

Quoted 10 lines
dir.c +10 -0
+       if (pattern[patternlen]) {
+               strbuf_add(&pat_buf, pattern, patternlen);
+               use_pat = pat_buf.buf;
+       }
+       if (string[stringlen]) {
+               strbuf_add(&str_buf, string, stringlen);
+               use_str = str_buf.buf;
+       }
+
+       match_status = fnmatch_icase(use_pat, use_str, 0);

You should pass flags in here instead of 0.

Signature · 1 line
Duy
author
#37 lore raw

Re: [PATCH 2/6] dir.c::match_basename(): pay attention to the length of string parameters

Quoted 15 lines · Nguyen Thai Ngoc Duy
Quoted 14 lines · Jeff King
dir.c +14 -0
+static int fnmatch_icase_mem(const char *pattern, int patternlen,
+                            const char *string, int stringlen,
+                            int flags)
+{
+       int match_status;
+       struct strbuf pat_buf = STRBUF_INIT;
+       struct strbuf str_buf = STRBUF_INIT;
+       const char *use_pat = pattern;
+       const char *use_str = string;
+
+       if (pattern[patternlen]) {
+               strbuf_add(&pat_buf, pattern, patternlen);
+               use_pat = pat_buf.buf;
+       }
Quoted 8 lines

dir.c int fnmatch_icase(const char *pattern, const char *string, int flags) · line 56

Quoted 6 lines
dir.c +6 -0
+       if (string[stringlen]) {
+               strbuf_add(&str_buf, string, stringlen);
+               use_str = str_buf.buf;
+       }
+
+       match_status = fnmatch_icase(use_pat, use_str, 0);

You should pass flags in here instead of 0.

Eek, yeah, that's obviously wrong. Thanks for catching it. Fixing that clears up all of the test failures outside of t5002.

And if you move patch 5 ("special case paths that end with a slash") into position 2, it cleans up the mid-series failures of t5002, making the series clean for later bisecting.

Thanks for looking it over.

-Peff

#38 lore raw

Re: [PATCH 2/6] dir.c::match_basename(): pay attention to the length of string parameters

Quoted 6 lines · Jeff King

Eek, yeah, that's obviously wrong. Thanks for catching it. Fixing that clears up all of the test failures outside of t5002.

And if you move patch 5 ("special case paths that end with a slash") into position 2, it cleans up the mid-series failures of t5002, making the series clean for later bisecting.

Yeah, the tip becomes test-clean with that "flags" change, but it appears that there seems to be some mismerge (there is another topic or two in flight that want to touch dir.c for pathspecs) that breaks 0003 and 3001 when merged to 'pu'.

Duy, can you take another look?

author
#39 lore raw

PATCH v2 3/6 dir.c::match_pathname(): adjust patternlen when shifting pattern

In reply to Jeff King

If we receive a pattern that starts with "/", we shift it forward to avoid looking at the "/" part. Since the prefix and patternlen parameters are counts of what is in the pattern, we must decrement them as we increment the pointer.

We remembered to handle prefix, but not patternlen. This didn't cause any bugs, though, because the patternlen parameter is not actually used. Since it will be used in future patches, let's correct this oversight.

---
New in this iteration.

 dir.c | 1 +
 1 file changed, 1 insertion(+)
dir.c +1 -0
index fac82c1..cc4ce8b 100644
@@ -597,6 +597,7 @@ int match_pathname(const char *pathname, int pathlen,
 	 */
 	if (*pattern == '/') {
 		pattern++;
+		patternlen--;
 		prefix--;
 	}
 
Signature · 1 line
1.8.2.13.g0f18d3c
author
#40 lore raw

PATCH v2 4/6 dir.c::match_pathname(): pay attention to the length of string parameters

In reply to Jeff King

This function takes two counted strings: a <pattern, patternlen> pair and a <pathname, pathlen> pair. But we end up feeding the result to fnmatch, which expects NUL-terminated strings.

We can fix this by calling the fnmatch_icase_mem function, which handles re-allocating into a NUL-terminated string if necessary.

While we're at it, we can avoid even calling fnmatch in some cases. In addition to patternlen, we get "prefix", the size of the pattern that contains no wildcard characters. We do a straight match of the prefix part first, and then use fnmatch to cover the rest. But if there are no wildcards in the pattern at all, we do not even need to call fnmatch; we would simply be comparing two empty strings.

---
New in this iteration.

 dir.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
dir.c +12 -1
index cc4ce8b..3ad44c3 100644
@@ -624,11 +624,22 @@ int match_pathname(const char *pathname, int pathlen,
 		if (strncmp_icase(pattern, name, prefix))
 			return 0;
 		pattern += prefix;
+		patternlen -= prefix;
 		name    += prefix;
 		namelen -= prefix;
+
+		/*
+		 * If the whole pattern did not have a wildcard,
+		 * then our prefix match is all we need; we
+		 * do not need to call fnmatch at all.
+		 */
+		if (!patternlen && !namelen)
+			return 1;
 	}
 
-	return fnmatch_icase(pattern, name, FNM_PATHNAME) == 0;
+	return fnmatch_icase_mem(pattern, patternlen,
+				 name, namelen,
+				 FNM_PATHNAME) == 0;
 }
 
 /* Scan the list and let the last match determine the fate.
Signature · 1 line
1.8.2.13.g0f18d3c
#41 lore raw

Re: [PATCH 4/6] dir.c::match_pathname(): pay attention to the length of string parameters

Quoted 7 lines · Jeff King

This function takes two counted strings: a <pattern, patternlen> pair and a <pathname, pathlen> pair. But we end up feeding the result to fnmatch, which expects NUL-terminated strings.

We can fix this by calling the fnmatch_icase_mem function, which handles re-allocating into a NUL-terminated string if necessary.

Quoted 6 lines

While we're at it, we can avoid even calling fnmatch in some cases. In addition to patternlen, we get "prefix", the size of the pattern that contains no wildcard characters. We do a straight match of the prefix part first, and then use fnmatch to cover the rest. But if there are no wildcards in the pattern at all, we do not even need to call fnmatch; we would simply be comparing two empty strings.

It is not a new issue, but it would be nicer to have a comment to warn people that this part needs to be adjusted when they want to add support for using FNM_PERIOD in our codebase.

Other than that, looks sane to me. Thanks.

Quoted 36 lines

---
New in this iteration.

 dir.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
dir.c +12 -1
index cc4ce8b..3ad44c3 100644
@@ -624,11 +624,22 @@ int match_pathname(const char *pathname, int pathlen,
 		if (strncmp_icase(pattern, name, prefix))
 			return 0;
 		pattern += prefix;
+		patternlen -= prefix;
 		name    += prefix;
 		namelen -= prefix;
+
+		/*
+		 * If the whole pattern did not have a wildcard,
+		 * then our prefix match is all we need; we
+		 * do not need to call fnmatch at all.
+		 */
+		if (!patternlen && !namelen)
+			return 1;
 	}
 
-	return fnmatch_icase(pattern, name, FNM_PATHNAME) == 0;
+	return fnmatch_icase_mem(pattern, patternlen,
+				 name, namelen,
+				 FNM_PATHNAME) == 0;
 }
 
 /* Scan the list and let the last match determine the fate.
#42 lore raw

Re: [PATCH 4/6] dir.c::match_pathname(): pay attention to the length of string parameters

In reply to Jeff King

dir.c int match_pathname(const char *pathname, int pathlen, · line 643

Quoted 5 lines · Jeff King
dir.c +3 -1
-       return fnmatch_icase(pattern, name, FNM_PATHNAME) == 0;
+       return fnmatch_icase_mem(pattern, patternlen,
+                                name, namelen,
+                                FNM_PATHNAME) == 0;
 }

I think you (or Junio) should rebase this on maint. Since c41244e (included in maint), this call is turned to wildmatch(WM_PATHNAME) and WM_PATHNAME is _not_ the same as FNM_PATHNAME for patterns like "foo/**/bar". A diff between next and pu shows me that WM_PATHNAME is incorrectly converted to FNM_PATHNAME. I hope that is the cause of all breakages Junio found out on pu.

Signature · 1 line
Duy
author
#43 lore raw

Re: [PATCH 4/6] dir.c::match_pathname(): pay attention to the length of string parameters

Quoted 7 lines · Nguyen Thai Ngoc Duy

dir.c int match_pathname(const char *pathname, int pathlen, · line 643

Quoted 5 lines · Jeff King
dir.c +3 -1
-       return fnmatch_icase(pattern, name, FNM_PATHNAME) == 0;
+       return fnmatch_icase_mem(pattern, patternlen,
+                                name, namelen,
+                                FNM_PATHNAME) == 0;
 }
Quoted 6 lines

I think you (or Junio) should rebase this on maint. Since c41244e (included in maint), this call is turned to wildmatch(WM_PATHNAME) and WM_PATHNAME is _not_ the same as FNM_PATHNAME for patterns like "foo/**/bar". A diff between next and pu shows me that WM_PATHNAME is incorrectly converted to FNM_PATHNAME. I hope that is the cause of all breakages Junio found out on pu.

I don't think we want to rebase; the regression is in the v1.8.1 series, and I suspected that Junio was planning to ship a v1.8.1.6 with the fix. The wildmatch code comes in v1.8.2.

So we would want to do any adjustment to the fix when we merge up to maint.

-Peff

#44 lore raw

Re: [PATCH 4/6] dir.c::match_pathname(): pay attention to the length of string parameters

Quoted 16 lines · Jeff King
Quoted 7 lines · Nguyen Thai Ngoc Duy

Review

Quoted 5 lines · Jeff King
(quoted diff) +3 -1
-       return fnmatch_icase(pattern, name, FNM_PATHNAME) == 0;
+       return fnmatch_icase_mem(pattern, patternlen,
+                                name, namelen,
+                                FNM_PATHNAME) == 0;
 }
Quoted 6 lines

I think you (or Junio) should rebase this on maint. Since c41244e (included in maint), this call is turned to wildmatch(WM_PATHNAME) and WM_PATHNAME is _not_ the same as FNM_PATHNAME for patterns like "foo/**/bar". A diff between next and pu shows me that WM_PATHNAME is incorrectly converted to FNM_PATHNAME. I hope that is the cause of all breakages Junio found out on pu.

Quoted 6 lines

I don't think we want to rebase; the regression is in the v1.8.1 series, and I suspected that Junio was planning to ship a v1.8.1.6 with the fix. The wildmatch code comes in v1.8.2.

So we would want to do any adjustment to the fix when we merge up to maint.

OK. Then Junio, you may need to resolve the conflict with something like this. Originally match_basename uses fnmatch, not wildmatch. But using wildmatch there too should be fine, now that both match_{base,path}name share fnmatch_icase_mem().

-- 8< --

dir.c +4 -2
index 73a08af..84744df 100644
@@ -81,7 +81,9 @@ static int fnmatch_icase_mem(const char *pattern, int patternlen,
 		use_str = str_buf.buf;
 	}
 
-	match_status = fnmatch_icase(use_pat, use_str, flags);
+	if (ignore_case)
+		flags |= WM_CASEFOLD;
+	match_status = wildmatch(use_pat, use_str, flags, NULL);
 
 	strbuf_release(&pat_buf);
 	strbuf_release(&str_buf);
@@ -564,7 +566,7 @@ int match_pathname(const char *pathname, int pathlen,
 
 	return fnmatch_icase_mem(pattern, patternlen,
 				 name, namelen,
-				 FNM_PATHNAME) == 0;
+				 WM_PATHNAME) == 0;
 }
 
 /*

-- 8< --

#45 lore raw

Re: [PATCH 4/6] dir.c::match_pathname(): pay attention to the length of string parameters

Quoted 7 lines · Duy Nguyen
Quoted 2 lines

So we would want to do any adjustment to the fix when we merge up to maint.

OK. Then Junio, you may need to resolve the conflict with something like this. Originally match_basename uses fnmatch, not wildmatch. But using wildmatch there too should be fine, now that both match_{base,path}name share fnmatch_icase_mem().

Thanks.

The result still smells somewhat funny, though.

fnmatch_icase_mem() is meant to be a wrapper of fnmatch_icase() for counted strings and its matching semantics should be the same as fnmatch_icase().

With the merge-fix, fnmatch_icase_mem() calls into wildmatch(), but fnmatch_icase() still calls into fnmatch().

The latter's flags are meant to be taken from FNM_* family, but the former takes flags from WM_* family of bits, no?

I think you are running with USE_WILDMATCH which may make the differences harder to notice, but the name fnmatch_icase_mem() that is not in the same family as fnmatch but is from the wildmatch() family smells like an accident waiting to happen.

I tend to think in the longer term it may be a good idea to build with USE_WILDMATCH unconditionally (we can lose compat/fnmatch), so in the end this may not matter that much, but before that happens, soon after we merge the regression fix with this merge-fix, we may want to update the codebase as if we applied a series that were based on 'maint' as you suggested, i.e. using raw wildmatch() consistently in the match_{base,path}name() codepath.

Opinions?

Quoted 27 lines

-- 8< --

dir.c +4 -3
index 73a08af..84744df 100644
@@ -81,7 +81,9 @@ static int fnmatch_icase_mem(const char *pattern, int patternlen,
 		use_str = str_buf.buf;
 	}
 
-	match_status = fnmatch_icase(use_pat, use_str, flags);
+	if (ignore_case)
+		flags |= WM_CASEFOLD;
+	match_status = wildmatch(use_pat, use_str, flags, NULL);
 
 	strbuf_release(&pat_buf);
 	strbuf_release(&str_buf);
@@ -564,7 +566,7 @@ int match_pathname(const char *pathname, int pathlen,
 
 	return fnmatch_icase_mem(pattern, patternlen,
 				 name, namelen,
-				 FNM_PATHNAME) == 0;
+				 WM_PATHNAME) == 0;
 }
 
 /*
-- 8< --
author
#46 lore raw

Re: [PATCH 4/6] dir.c::match_pathname(): pay attention to the length of string parameters

Quoted 18 lines · Junio C Hamano
Quoted 7 lines · Duy Nguyen
Quoted 2 lines

So we would want to do any adjustment to the fix when we merge up to maint.

OK. Then Junio, you may need to resolve the conflict with something like this. Originally match_basename uses fnmatch, not wildmatch. But using wildmatch there too should be fine, now that both match_{base,path}name share fnmatch_icase_mem().

Thanks.

The result still smells somewhat funny, though.

fnmatch_icase_mem() is meant to be a wrapper of fnmatch_icase() for counted strings and its matching semantics should be the same as fnmatch_icase().

Quoted 5 lines

With the merge-fix, fnmatch_icase_mem() calls into wildmatch(), but fnmatch_icase() still calls into fnmatch().

The latter's flags are meant to be taken from FNM_* family, but the former takes flags from WM_* family of bits, no?

Yeah, that does not seem right. If match_pathname has learned to call into wildmatch instead of fnmatch_icase in the interim, then the right resolution is to convert its call to fnmatch_icase_mem to a new wildmatch_mem. Presumably that can be done by either tweaking fnmatch_icase_mem, or, if wildmatch is ready to take counted strings, calling into it with the right options.

Quoted 4 lines

I think you are running with USE_WILDMATCH which may make the differences harder to notice, but the name fnmatch_icase_mem() that is not in the same family as fnmatch but is from the wildmatch() family smells like an accident waiting to happen.

Agreed.

Quoted 3 lines

I tend to think in the longer term it may be a good idea to build with USE_WILDMATCH unconditionally (we can lose compat/fnmatch), so in the end this may not matter that much

Yeah, I think that is a sane long-term goal.

-Peff

#47 lore raw

Re: [PATCH 4/6] dir.c::match_pathname(): pay attention to the length of string parameters

Quoted 15 lines · Jeff King

On Fri, Mar 29, 2013 at 09:44:32AM -0700, Junio C Hamano wrote:
...

Quoted 5 lines

With the merge-fix, fnmatch_icase_mem() calls into wildmatch(), but fnmatch_icase() still calls into fnmatch().

The latter's flags are meant to be taken from FNM_* family, but the former takes flags from WM_* family of bits, no?

Yeah, that does not seem right. If match_pathname has learned to call into wildmatch instead of fnmatch_icase in the interim, then the right resolution is to convert its call to fnmatch_icase_mem to a new wildmatch_mem. Presumably that can be done by either tweaking fnmatch_icase_mem, or, if wildmatch is ready to take counted strings, calling into it with the right options.

Quoted 6 lines
Quoted 4 lines

I think you are running with USE_WILDMATCH which may make the differences harder to notice, but the name fnmatch_icase_mem() that is not in the same family as fnmatch but is from the wildmatch() family smells like an accident waiting to happen.

Agreed.

This may be just the matter of naming.

It smelled wrong to me only because the "fnmatch" in the helper fnmatch_icase_mem() told me that it should forever use fnmatch semantics. After reading its (only) two callsites, they are both "the caller has transformed the inputs to this lowest level pathname vs pattern matching function in order to reduce the cost of matching, and now it is time to exercise the matcher". The only thing they care about is that they are calling "the lowest level pathname vs pattern matching function."

If we pronounce "fnmatch_icase_mem()" as "match_path_with_pattern()" or something in the original series, the problem may go away ;-)

Does any caller pass FNM_* bits to a callchain that reach the new *_mem() function?

author
#48 lore raw

Re: [PATCH 4/6] dir.c::match_pathname(): pay attention to the length of string parameters

Quoted 11 lines · Junio C Hamano

This may be just the matter of naming.

It smelled wrong to me only because the "fnmatch" in the helper fnmatch_icase_mem() told me that it should forever use fnmatch semantics. After reading its (only) two callsites, they are both "the caller has transformed the inputs to this lowest level pathname vs pattern matching function in order to reduce the cost of matching, and now it is time to exercise the matcher". The only thing they care about is that they are calling "the lowest level pathname vs pattern matching function."

Quoted 2 lines

If we pronounce "fnmatch_icase_mem()" as "match_path_with_pattern()" or something in the original series, the problem may go away ;-)

Yes, since there are only the two new added callers, if they both want to switch to wildmatch, then it is fine for the helper function to switch. The danger is if some other caller wants to start using it; I added it with the name I did figuring that other spots might want to use it eventually. But if all of fnmatch is going to go away in favor of wildmatch eventually, then that helper is not all that useful (or it would be even more useful as "wildmatch_mem" or similar).

Quoted 2 lines

Does any caller pass FNM_* bits to a callchain that reach the new *_mem() function?

The series only adds two callers, and they provide constant flags; match_basename passes no flags, and should be OK. match_pathname uses FNM_PATHNAME, and would need to be converted to use WM_PATHNAME as part of the conflict resolution.

-Peff

#49 lore raw

Re: [PATCH 4/6] dir.c::match_pathname(): pay attention to the length of string parameters

In reply to Junio C Hamano
Quoted 3 lines · Junio C Hamano

I tend to think in the longer term it may be a good idea to build with USE_WILDMATCH unconditionally (we can lose compat/fnmatch), so in the end this may not matter that much

I was thinking about that yesterday. After all, it's the purpose of USE_WILDMATCH and nd/retire-fnmatch series. So how about this patch to enable USE_WILDMATCH for a wider audience? I think it could get merged down to master. If wildmatch's bug history is messy by the time we enter rc cycles, we could revert the patch and cut new releases with fnmatch. If not, after 1.9 is released, I'll submit a patch to remove fnmatch compatiblity later and we will be on wildmatch only.

-- 8< --

Makefile +2 -2
index 598d631..8a740f8 100644
@@ -106,7 +106,7 @@ all::
 # Define NO_FNMATCH_CASEFOLD if your fnmatch function doesn't have the
 # FNM_CASEFOLD GNU extension.
 #
-# Define USE_WILDMATCH if you want to use Git's wildmatch
+# Define NO_USE_WILDMATCH if you do not want to use Git's wildmatch
 # implementation as fnmatch
 #
 # Define NO_GECOS_IN_PWENT if you don't have pw_gecos in struct passwd
@@ -1255,7 +1255,7 @@ ifdef NO_FNMATCH_CASEFOLD
 	COMPAT_OBJS += compat/fnmatch/fnmatch.o
 endif
 endif
-ifdef USE_WILDMATCH
+ifndef NO_USE_WILDMATCH
 	COMPAT_CFLAGS += -DUSE_WILDMATCH
 endif
 ifdef NO_SETENV

-- 8< --

#50 lore raw

Re: [PATCH 4/6] dir.c::match_pathname(): pay attention to the length of string parameters

In reply to Duy Nguyen
Quoted 7 lines · Duy Nguyen

dir.c int match_pathname(const char *pathname, int pathlen, · line 643

Quoted 5 lines · Jeff King
dir.c +3 -1
-       return fnmatch_icase(pattern, name, FNM_PATHNAME) == 0;
+       return fnmatch_icase_mem(pattern, patternlen,
+                                name, namelen,
+                                FNM_PATHNAME) == 0;
 }
Quoted 6 lines

I think you (or Junio) should rebase this on maint. Since c41244e (included in maint), this call is turned to wildmatch(WM_PATHNAME) and WM_PATHNAME is _not_ the same as FNM_PATHNAME for patterns like "foo/**/bar". A diff between next and pu shows me that WM_PATHNAME is incorrectly converted to FNM_PATHNAME. I hope that is the cause of all breakages Junio found out on pu.

Just tested. t0003 and t3001 on 'pu' work for me because I have USE_WILDMATCH on (which turns FNM_PATHNAME to WM_PATHNAME). Both break without USE_WILDMATCH.

Signature · 1 line
Duy
#51 lore raw

Re: [PATCH 4/6] dir.c::match_pathname(): pay attention to the length of string parameters

Quoted 15 lines · Duy Nguyen
Quoted 7 lines · Duy Nguyen

Review

Quoted 5 lines · Jeff King
(quoted diff) +3 -1
-       return fnmatch_icase(pattern, name, FNM_PATHNAME) == 0;
+       return fnmatch_icase_mem(pattern, patternlen,
+                                name, namelen,
+                                FNM_PATHNAME) == 0;
 }
Quoted 6 lines

I think you (or Junio) should rebase this on maint. Since c41244e (included in maint), this call is turned to wildmatch(WM_PATHNAME) and WM_PATHNAME is _not_ the same as FNM_PATHNAME for patterns like "foo/**/bar". A diff between next and pu shows me that WM_PATHNAME is incorrectly converted to FNM_PATHNAME. I hope that is the cause of all breakages Junio found out on pu.

Quoted 4 lines

Just tested. t0003 and t3001 on 'pu' work for me because I have USE_WILDMATCH on (which turns FNM_PATHNAME to WM_PATHNAME). Both break without USE_WILDMATCH.

Hm, tested what?

dir.c +1 -1
index 73a08af..0b63167 100644
@@ -564,7 +564,7 @@ int match_pathname(const char *pathname, int pathlen,
 
        return fnmatch_icase_mem(pattern, patternlen,
                                 name, namelen,
-                                FNM_PATHNAME) == 0;
+                                WM_PATHNAME) == 0;
 }
 

Gives only one breakage, so we are coming closer.

*** t3001-ls-files-others-exclude.sh ***
[snip]
not ok 17 - ls-files with "**" patterns

#52 lore raw

Re: [PATCH 4/6] dir.c::match_pathname(): pay attention to the length of string parameters

Quoted 5 lines · Torsten Bögershausen
Quoted 4 lines

Just tested. t0003 and t3001 on 'pu' work for me because I have USE_WILDMATCH on (which turns FNM_PATHNAME to WM_PATHNAME). Both break without USE_WILDMATCH.

Hm, tested what?

Tested t0003 and t3001 with and without USE_WILDMATCH, which is pretty much like you patch, except that wildmatch is used instead of fnmatch.

Quoted 21 lines
dir.c +1 -1
index 73a08af..0b63167 100644
@@ -564,7 +564,7 @@ int match_pathname(const char *pathname, int pathlen,
 
        return fnmatch_icase_mem(pattern, patternlen,
                                 name, namelen,
-                                FNM_PATHNAME) == 0;
+                                WM_PATHNAME) == 0;
 }
 

Gives only one breakage, so we are coming closer.

*** t3001-ls-files-others-exclude.sh ***
[snip]
not ok 17 - ls-files with "**" patterns

Signature · 1 line
Duy
author
#53 lore raw

PATCH v2 5/6 attr.c::path_matches(): special case paths that end with a slash

In reply to Jeff King

From: Junio C Hamano <[email protected]>

The function is given a string that ends with a slash to signal that the path is a directory to make sure that a pattern that ends with a slash (i.e. MUSTBEDIR) can tell directories and non-directories apart. However, the pattern itself (pat->pattern and pat->patternlen) that came from such a MUSTBEDIR pattern is represented as a string that ends with a slash, but patternlen does not count that trailing slash. A MUSTBEDIR pattern "element/" is represented as a counted string <"element/", 7> and this must match match pathname "element/".

Because match_basename() and match_pathname() want to see pathname "element" to match against the pattern <"element/", 7>, reduce the length of the path to exclude the trailing slash when calling these functions.

---
Tweaked since v1 to also drop the trailing slash when we pass the path to match_pathname.

 attr.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
attr.c +4 -4
index 4cfe0ee..4d620bc 100644
@@ -661,18 +661,18 @@ static int path_matches(const char *pathname, int pathlen,
 {
 	const char *pattern = pat->pattern;
 	int prefix = pat->nowildcardlen;
+	int isdir = (pathlen && pathname[pathlen - 1] == '/');
 
-	if ((pat->flags & EXC_FLAG_MUSTBEDIR) &&
-	    ((!pathlen) || (pathname[pathlen-1] != '/')))
+	if ((pat->flags & EXC_FLAG_MUSTBEDIR) && !isdir)
 		return 0;
 
 	if (pat->flags & EXC_FLAG_NODIR) {
 		return match_basename(pathname + basename_offset,
-				      pathlen - basename_offset,
+				      pathlen - basename_offset - isdir,
 				      pattern, prefix,
 				      pat->patternlen, pat->flags);
 	}
-	return match_pathname(pathname, pathlen,
+	return match_pathname(pathname, pathlen - isdir,
 			      base, baselen,
 			      pattern, prefix, pat->patternlen, pat->flags);
 }
Signature · 1 line
1.8.2.13.g0f18d3c
author
#54 lore raw

PATCH v2 6/6 t: check that a pattern without trailing slash matches a directory

In reply to Jeff King

Prior to v1.8.1.1, with:

git init
echo content >foo &&
mkdir subdir &&
echo content >subdir/bar &&
echo "subdir export-ignore" >.gitattributes
git add . &&
git commit -m one &&
git archive HEAD | tar tf -

the resulting archive would contain only "foo" and ".gitattributes", not subdir. This was broken with a recent change that intended to allow "subdir/ export-ignore" to also exclude the directory, but instead ended up _requiring_ the trailing slash by mistake.

A pattern "subdir" should match any path "subdir", whether it is a directory or a non-diretory. A pattern "subdir/" insists that a path "subdir" must be a directory for it to match.

This patch adds test not just for this simple case, but also for deeper cross-directory cases, as well as cases with wildcards.

---
Added new tests since v1 that handle the match_pathname code path.

 t/t5002-archive-attr-pattern.sh | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
t/t5002-archive-attr-pattern.sh +27 -0
index 0c847fb..6667d15 100755
@@ -27,6 +27,25 @@ 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 wildcard-without-slash &&
+	echo "ignored without slash" >wildcard-without-slash/foo &&
+	git add wildcard-without-slash/foo &&
+	echo "wild*-without-slash export-ignore" >>.git/info/attributes &&
+
+	mkdir -p deep/and/slashless &&
+	echo "ignored without slash" >deep/and/slashless/foo &&
+	git add deep/and/slashless/foo &&
+	echo "deep/and/slashless export-ignore" >>.git/info/attributes &&
+
+	mkdir -p deep/with/wildcard &&
+	echo "ignored without slash" >deep/with/wildcard/foo &&
+	git add deep/with/wildcard/foo &&
+	echo "deep/*t*/wildcard 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 +68,14 @@ test_expect_missing	archive/ignored-ony-if-dir/ignored-by-ignored-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_missing	archive/wildcard-without-slash/
+test_expect_missing	archive/wildcard-without-slash/foo &&
+test_expect_missing	archive/deep/and/slashless/ &&
+test_expect_missing	archive/deep/and/slashless/foo &&
+test_expect_missing	archive/deep/with/wildcard/ &&
+test_expect_missing	archive/deep/with/wildcard/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
Signature · 1 line
1.8.2.13.g0f18d3c
#55 lore raw

Re: [PATCH 6/6] t: check that a pattern without trailing slash matches a directory

Quoted 2 lines · Jeff King

A pattern "subdir" should match any path "subdir", whether it is a directory or a non-diretory. A pattern "subdir/" insists that a

s/diretory/directory/ [1]

Quoted 1 line

path "subdir" must be a directory for it to match.

[1]: http://article.gmane.org/gmane.comp.version-control.git/219185/

author
#56 lore raw

Re: [PATCH 6/6] t: check that a pattern without trailing slash matches a directory

Quoted 5 lines · Eric Sunshine
Quoted 2 lines · Jeff King

A pattern "subdir" should match any path "subdir", whether it is a directory or a non-diretory. A pattern "subdir/" insists that a

s/diretory/directory/ [1]

I think I've taken proofreading to a new level by missing the error I already corrected somebody else for. Thanks for noticing. :)

-Peff

#57 lore raw
In reply to Jeff King
Quoted 27 lines · Jeff King

Prior to v1.8.1.1, if I did this:

git init
echo content >foo &&
mkdir subdir &&
echo content >subdir/bar &&
echo "subdir export-ignore" >.gitattributes
git add . &&
git commit -m one &&
git archive HEAD | tar tf -

my archive would contain only "foo" and ".gitattributes", not subdir. As of v1.8.1.1, the attribute on subdir is ignored unless it is written with a trailing slash, like:

subdir/ export-ignore

The issue bisects to 94bc671 (Add directory pattern matching to attributes, 2012-12-08). That commit actually tests not only that "subdir/" matches, but also that just "subdir" does not match.

The commit message there is vague about the reasoning, but my understanding is that it was meant to harmonize gitignore and gitattributes, the former of which can take "dir/". I don't have a problem with offering "dir/" to match only directories, but what is the point in disallowing just "dir" to match a directory?

Quoted 3 lines

It seems like a pointless regression to me, but I'm not clear whether it was intentional or not (and if it was intentional, I think we would need to handle it with a proper transition period, not in a maint release).

I agree that is a pointless regression.

#58 lore raw

cover attr directory matching regression

In reply to Jeff King

I think the fix is something like this. There is still one thing I'd like to do: make this code not rely on NUL for terminating the patterns. That should remove the ugly "p[len] = '\0'" in prepare_attr_stack() 4/4 and and the reallocation in add_exclude() (in current code). But let's deal with the regression first.

Nguyễn Thái Ngọc Duy (4):
wildmatch: do not require "text" to be NUL-terminated attr.c: fix pattern{,len} inconsistency in struct match_attr dir.c: make match_{base,path}name respect {basename,path}len attr.c: fix matching "subdir" without the trailing slash

 attr.c                          | 11 ++++++++++-
 dir.c                           | 13 ++++++++-----
 dir.h                           |  2 +-
 t/t5002-archive-attr-pattern.sh |  6 ++++++
 wildmatch.c                     | 43 ++++++++++++++++++++++++-----------------
 wildmatch.h                     | 11 +++++++++--
 6 files changed, 59 insertions(+), 27 deletions(-)
Signature · 1 line
1.8.2.82.gc24b958
#59 lore raw

PATCH 1/4 wildmatch: do not require "text" to be NUL-terminated

This may be helpful when we just want to match a part of "text". nwildmatch can be used for this purpose.

---
 wildmatch.c | 43 +++++++++++++++++++++++++------------------
 wildmatch.h | 11 +++++++++--
 2 files changed, 34 insertions(+), 20 deletions(-)
wildmatch.c +25 -18
index 7192bdc..939bac8 100644
@@ -52,7 +52,8 @@ typedef unsigned char uchar;
 #define ISXDIGIT(c) (ISASCII(c) && isxdigit(c))
 
 /* Match pattern "p" against "text" */
-static int dowild(const uchar *p, const uchar *text, unsigned int flags)
+static int dowild(const uchar *p, const uchar *text,
+		  const uchar *textend, unsigned int flags)
 {
 	uchar p_ch;
 	const uchar *pattern = p;
@@ -60,8 +61,13 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
 	for ( ; (p_ch = *p) != '\0'; text++, p++) {
 		int matched, match_slash, negated;
 		uchar t_ch, prev_ch;
-		if ((t_ch = *text) == '\0' && p_ch != '*')
-			return WM_ABORT_ALL;
+		if (text >= textend) {
+			if (p_ch != '*')
+				return WM_ABORT_ALL;
+			else
+				t_ch = '\0';
+		} else
+			t_ch = *text;
 		if ((flags & WM_CASEFOLD) && ISUPPER(t_ch))
 			t_ch = tolower(t_ch);
 		if ((flags & WM_CASEFOLD) && ISUPPER(p_ch))
@@ -101,7 +107,7 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
 					 * both foo/bar and foo/a/bar.
 					 */
 					if (p[0] == '/' &&
-					    dowild(p + 1, text, flags) == WM_MATCH)
+					    dowild(p + 1, text, textend, flags) == WM_MATCH)
 						return WM_MATCH;
 					match_slash = 1;
 				} else
@@ -113,8 +119,9 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
 				/* Trailing "**" matches everything.  Trailing "*" matches
 				 * only if there are no more slash characters. */
 				if (!match_slash) {
-					if (strchr((char*)text, '/') != NULL)
-						return WM_NOMATCH;
+					for (;text < textend; text++)
+						if (*text == '/')
+							return WM_NOMATCH;
 				}
 				return WM_MATCH;
 			} else if (!match_slash && *p == '/') {
@@ -123,16 +130,15 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
 				 * with WM_PATHNAME matches the next
 				 * directory
 				 */
-				const char *slash = strchr((char*)text, '/');
-				if (!slash)
+				for (;text < textend; text++)
+					if (*text == '/')
+						break;
+				if (text == textend)
 					return WM_NOMATCH;
-				text = (const uchar*)slash;
 				/* the slash is consumed by the top-level for loop */
 				break;
 			}
-			while (1) {
-				if (t_ch == '\0')
-					break;
+			while (text < textend) {
 				/*
 				 * Try to advance faster when an asterisk is
 				 * followed by a literal. We know in this case
@@ -145,18 +151,18 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
 					p_ch = *p;
 					if ((flags & WM_CASEFOLD) && ISUPPER(p_ch))
 						p_ch = tolower(p_ch);
-					while ((t_ch = *text) != '\0' &&
+					while (text < textend &&
 					       (match_slash || t_ch != '/')) {
 						if ((flags & WM_CASEFOLD) && ISUPPER(t_ch))
 							t_ch = tolower(t_ch);
 						if (t_ch == p_ch)
 							break;
-						text++;
+						t_ch = ++text < textend ? *text : '\0';
 					}
 					if (t_ch != p_ch)
 						return WM_NOMATCH;
 				}
-				if ((matched = dowild(p, text, flags)) != WM_NOMATCH) {
+				if ((matched = dowild(p, text, textend, flags)) != WM_NOMATCH) {
 					if (!match_slash || matched != WM_ABORT_TO_STARSTAR)
 						return matched;
 				} else if (!match_slash && t_ch == '/')
@@ -261,12 +267,13 @@ static int dowild(const uchar *p, const uchar *text, unsigned int flags)
 		}
 	}
 
-	return *text ? WM_NOMATCH : WM_MATCH;
+	return text < textend ? WM_NOMATCH : WM_MATCH;
 }
 
 /* Match the "pattern" against the "text" string. */
-int wildmatch(const char *pattern, const char *text,
+int nwildmatch(const char *pattern, const char *text, int textlen,
 	      unsigned int flags, struct wildopts *wo)
 {
-	return dowild((const uchar*)pattern, (const uchar*)text, flags);
+	return dowild((const uchar*)pattern, (const uchar*)text,
+		      (const uchar*)text + textlen, flags);
 }
wildmatch.h +9 -2
index 4090c8f..cdd7544 100644
@@ -12,7 +12,14 @@
 
 struct wildopts;
 
-int wildmatch(const char *pattern, const char *text,
-	      unsigned int flags,
+int nwildmatch(const char *pattern, const char *text,
+	      int len, unsigned int flags,
 	      struct wildopts *wo);
+
+/* Match the "pattern" against the "text" string. */
+static inline int wildmatch(const char *pattern, const char *text,
+	      unsigned int flags, struct wildopts *wo)
+{
+	return nwildmatch(pattern, text, strlen(text), flags, wo);
+}
 #endif
Signature · 1 line
1.8.2.82.gc24b958
#60 lore raw

PATCH 2/4 attr.c: fix pattern{,len} inconsistency in struct match_attr

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

When parse_exclude_pattern detects EXC_FLAG_MUSTBEDIR, it sets patternlen _not_ to include the trailing slash and expects the caller to trim it. Of the two callers, add_exclude() does, parse_attr_line() does not.

Because of that, after parse_attr_line() returns, we may have pattern "foo/" but its length is reported 3. Some functions do not care about patternlen and will see the pattern as "foo/" while others may see it as "foo". This patch makes patternlen reflect the true length of pattern.

This is a bandage patch that's required for the next patch to pass the test suite as that patch will rely on patternlen's correctness. The true fix comes in the patch after the next one.

---
 attr.c | 2 ++
 dir.h  | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
attr.c +2 -0
index e2f9377..1818ba5 100644
@@ -255,6 +255,8 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
 				      &res->u.pat.patternlen,
 				      &res->u.pat.flags,
 				      &res->u.pat.nowildcardlen);
+		if (res->u.pat.flags & EXC_FLAG_MUSTBEDIR)
+			res->u.pat.patternlen++;
 		if (res->u.pat.flags & EXC_FLAG_NEGATIVE) {
 			warning(_("Negative patterns are ignored in git attributes\n"
 				  "Use '\\!' for literal leading exclamation."));
dir.h +1 -1
index c3eb4b5..dc63fc8 100644
@@ -40,7 +40,7 @@ struct exclude_list {
 		struct exclude_list *el;
 
 		const char *pattern;
-		int patternlen;
+		int patternlen;	/* must equal strlen(pattern) */
 		int nowildcardlen;
 		const char *base;
 		int baselen;
Signature · 1 line
1.8.2.82.gc24b958
#61 lore raw

PATCH 3/4 dir.c: make match_{base,path}name respect {basename,path}len

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

When match_basename was split out of excluded_from_list in 593cb88 (exclude: split basename matching code into a separate function - 2012-10-15), it took basenamelen only as a hint. basename was required to be NUL-terminated at the given length.

This was fine until match_basename had a new caller (from attr.c) and therefore was no longer excluded_from_list's internal business. Make match_basename stop relying on the NUL assumption above.

Do the same for match_pathname. From now on, only pattern is required to be NUL-terminated at the specified patternlen for both match_{base,path}name.

---
 dir.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
dir.c +8 -5
index 57394e4..30f5241 100644
@@ -626,15 +626,18 @@ int match_basename(const char *basename, int basenamelen,
 		   int flags)
 {
 	if (prefix == patternlen) {
-		if (!strcmp_icase(pattern, basename))
+		if (patternlen == basenamelen &&
+		    !strncmp_icase(pattern, basename, basenamelen))
 			return 1;
 	} else if (flags & EXC_FLAG_ENDSWITH) {
 		if (patternlen - 1 <= basenamelen &&
-		    !strcmp_icase(pattern + 1,
-				  basename + basenamelen - patternlen + 1))
+		    !strncmp_icase(pattern + 1,
+				   basename + basenamelen - patternlen + 1,
+				   patternlen - 1))
 			return 1;
 	} else {
-		if (fnmatch_icase(pattern, basename, 0) == 0)
+		if (nwildmatch(pattern, basename, basenamelen,
+			       ignore_case ? WM_CASEFOLD : 0, NULL) == 0)
 			return 1;
 	}
 	return 0;
@@ -684,7 +687,7 @@ int match_pathname(const char *pathname, int pathlen,
 		namelen -= prefix;
 	}
 
-	return wildmatch(pattern, name,
+	return nwildmatch(pattern, name, namelen,
 			 WM_PATHNAME | (ignore_case ? WM_CASEFOLD : 0),
 			 NULL) == 0;
 }
Signature · 1 line
1.8.2.82.gc24b958
#62 lore raw

PATCH 4/4 attr.c: fix matching "subdir" without the trailing slash

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

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.

---
 attr.c                          | 11 +++++++++--
 t/t5002-archive-attr-pattern.sh |  6 ++++++
 2 files changed, 15 insertions(+), 2 deletions(-)
attr.c +9 -2
index 1818ba5..a95c837 100644
@@ -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) {
t/t5002-archive-attr-pattern.sh +6 -0
index 0c847fb..98ccc3c 100755
@@ -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
Signature · 1 line
1.8.2.82.gc24b958
#63 lore raw

Re: [PATCH 4/4] attr.c: fix matching "subdir" without the trailing slash

Quoted 12 lines · Nguyễn Thái Ngọc Duy

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/".

Quoted 4 lines

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.

On second thought, maybe we should not pass path "subdir/" at all. Instead we create a fake dtype based on the trailing slash and pass it down to attr.c:fill() -> path_matches(), just like how last_exclude_matching_from_list() is called.

Signature · 1 line
Duy
#64 lore raw

Re: [PATCH 4/4] attr.c: fix matching "subdir" without the trailing slash

Quoted 4 lines · Duy Nguyen

On second thought, maybe we should not pass path "subdir/" at all. Instead we create a fake dtype based on the trailing slash and pass it down to attr.c:fill() -> path_matches(), just like how last_exclude_matching_from_list() is called.

I was hoping to make a small patch, but as it turns out, collect_all_attrs() takes a const path that contains the trailing slash, we still need to ignore it in match_{base,path}name so the whole series is still required. The only difference is in the final patch, which is a bit longer:

-- 8< --

attr.c +12 -7
index 1818ba5..0b2b716 100644
@@ -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."));
@@ -659,15 +659,14 @@ static void prepare_attr_stack(const char *path, int dirlen)
 }
 
 static int path_matches(const char *pathname, int pathlen,
-			const char *basename,
+			const char *basename, int dtype,
 			const struct pattern *pat,
 			const char *base, int baselen)
 {
 	const char *pattern = pat->pattern;
 	int prefix = pat->nowildcardlen;
 
-	if ((pat->flags & EXC_FLAG_MUSTBEDIR) &&
-	    ((!pathlen) || (pathname[pathlen-1] != '/')))
+	if ((pat->flags & EXC_FLAG_MUSTBEDIR) && dtype != DT_DIR)
 		return 0;
 
 	if (pat->flags & EXC_FLAG_NODIR) {
@@ -706,7 +705,7 @@ static int fill_one(const char *what, struct match_attr *a, int rem)
 }
 
 static int fill(const char *path, int pathlen, const char *basename,
-		struct attr_stack *stk, int rem)
+		int dtype, struct attr_stack *stk, int rem)
 {
 	int i;
 	const char *base = stk->origin ? stk->origin : "";
@@ -715,7 +714,7 @@ static int fill(const char *path, int pathlen, const char *basename,
 		struct match_attr *a = stk->attrs[i];
 		if (a->is_macro)
 			continue;
-		if (path_matches(path, pathlen, basename,
+		if (path_matches(path, pathlen, basename, dtype,
 				 &a->u.pat, base, stk->originlen))
 			rem = fill_one("fill", a, rem);
 	}
@@ -755,11 +754,17 @@ static void collect_all_attrs(const char *path)
 	struct attr_stack *stk;
 	int i, pathlen, rem, dirlen;
 	const char *basename, *cp, *last_slash = NULL;
+	int dtype;
 
 	for (cp = path; *cp; cp++) {
 		if (*cp == '/' && cp[1])
 			last_slash = cp;
 	}
+	if (cp > path && cp[-1] == '/') {
+		dtype = DT_DIR;
+		cp--;
+	} else
+		dtype = DT_REG;
 	pathlen = cp - path;
 	if (last_slash) {
 		basename = last_slash + 1;
@@ -775,7 +780,7 @@ static void collect_all_attrs(const char *path)
 
 	rem = attr_nr;
 	for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
-		rem = fill(path, pathlen, basename, stk, rem);
+		rem = fill(path, pathlen, basename, dtype, stk, rem);
 }
 
 int git_check_attr(const char *path, int num, struct git_attr_check *check)
t/t5002-archive-attr-pattern.sh +6 -0
index 0c847fb..98ccc3c 100755
@@ -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

-- 8<--

#65 lore raw

Re: [PATCH 0/4] attr directory matching regression

In reply to Nguyễn Thái Ngọc Duy
Quoted 5 lines · Nguyễn Thái Ngọc Duy

I think the fix is something like this. There is still one thing I'd like to do: make this code not rely on NUL for terminating the patterns. That should remove the ugly "p[len] = '\0'" in prepare_attr_stack() 4/4 and and the reallocation in add_exclude() (in current code). But let's deal with the regression first.

As a regression fix, we would need a fix that applies to maint-1.8.1, not 'next'.

Quoted 14 lines

Nguyễn Thái Ngọc Duy (4):
wildmatch: do not require "text" to be NUL-terminated attr.c: fix pattern{,len} inconsistency in struct match_attr dir.c: make match_{base,path}name respect {basename,path}len attr.c: fix matching "subdir" without the trailing slash

 attr.c                          | 11 ++++++++++-
 dir.c                           | 13 ++++++++-----
 dir.h                           |  2 +-
 t/t5002-archive-attr-pattern.sh |  6 ++++++
 wildmatch.c                     | 43 ++++++++++++++++++++++++-----------------
 wildmatch.h                     | 11 +++++++++--
 6 files changed, 59 insertions(+), 27 deletions(-)