All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>,
	"Michael Haggerty" <mhagger@alum.mit.edu>,
	"Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH 10/10] gitignore: forbid "abc**def"
Date: Fri,  5 Oct 2012 11:41:09 +0700	[thread overview]
Message-ID: <1349412069-627-11-git-send-email-pclouds@gmail.com> (raw)
In-Reply-To: <1349412069-627-1-git-send-email-pclouds@gmail.com>

Deep down wildmatch() sees "**" as "*" that can also match slashes. On
the surface, it may be confusing to users as the above pattern can
match "abcdef", "abcxyzdef", "abc/def", "abc/x/def",
"abc/x/y/def"... For now we just forbid it. Users can only do
"**/def", "abc/**" or "abc/**/def". The syntax may be re-enabled in
future.

There's a minor problem with this particular approach. "**" inside
square brackets are mistaken as the wildcard while they are not. Git
shows a confusing message when users do that.

Note that this patch hides a potential problem that if "abc**def" is
ever supported, EXC_FLAG_NODIR flag should be turned off or only the
base name is matched against the pattern, which makes no sense.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Documentation/gitignore.txt        |  6 ++++--
 dir.c                              | 12 +++++++++---
 t/t0003-attributes.sh              |  5 +++++
 t/t3001-ls-files-others-exclude.sh |  5 +++++
 4 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/Documentation/gitignore.txt b/Documentation/gitignore.txt
index eb81d31..ad9fc2f 100644
--- a/Documentation/gitignore.txt
+++ b/Documentation/gitignore.txt
@@ -94,8 +94,10 @@ PATTERN FORMAT
    "Documentation/git.html" but not "Documentation/ppc/ppc.html"
    or "tools/perf/Documentation/perf.html".
 +
-Contrary to fnmatch(3), git matches "**" to anything including
-slashes, similar to rsync(1).
+In addition to fnmatch(3) syntax, "**" can be used to match one or
+more directories. For example, "abc/**/def" matches "abc/x/def",
+"abc/x/y/def", "abc/x/y/z/def" and so on. "**" must be wrapped by
+slashes.
 
  - A leading slash matches the beginning of the pathname.
    For example, "/{asterisk}.c" matches "cat-file.c" but not
diff --git a/dir.c b/dir.c
index cb78273..f30117f 100644
--- a/dir.c
+++ b/dir.c
@@ -327,12 +327,18 @@ void parse_exclude_pattern(const char **pattern,
 		len--;
 		*flags |= EXC_FLAG_MUSTBEDIR;
 	}
+	*flags |= EXC_FLAG_NODIR;
 	for (i = 0; i < len; i++) {
 		if (p[i] == '/')
-			break;
+			*flags &= ~EXC_FLAG_NODIR;
+		if ((p[i] == '*' && p[i + 1] == '*' &&
+		     (i == 0 || p[i - 1] != '\\')) &&
+		    !((i == 0 || p[i - 1] == '/') &&
+		      (p[i + 2] == '\0' ||
+		       p[i + 2] == '/' ||
+		       (p[i + 2] == '\\' && p[i + 3] == '/'))))
+			die(_("** in .gitignore or .gitattributes must be wrapped by slashes"));
 	}
-	if (i == len)
-		*flags |= EXC_FLAG_NODIR;
 	*nowildcardlen = simple_length(p);
 	/*
 	 * we should have excluded the trailing slash from 'p' too,
diff --git a/t/t0003-attributes.sh b/t/t0003-attributes.sh
index 6c3c554..ddeb321 100755
--- a/t/t0003-attributes.sh
+++ b/t/t0003-attributes.sh
@@ -249,4 +249,9 @@ EOF
 	test_line_count = 0 err
 '
 
+test_expect_success '"**" with no slashes test' '
+	echo "a**f foo=bar" >.gitattributes &&
+	test_must_fail git check-attr foo -- "f"
+'
+
 test_done
diff --git a/t/t3001-ls-files-others-exclude.sh b/t/t3001-ls-files-others-exclude.sh
index 67c8bcf..f5c62d0 100755
--- a/t/t3001-ls-files-others-exclude.sh
+++ b/t/t3001-ls-files-others-exclude.sh
@@ -225,4 +225,9 @@ EOF
 	test_cmp expect actual
 '
 
+
+test_expect_success 'ls-files with "**" patterns and no slashes' '
+	test_must_fail git ls-files -o -i --exclude "one**a.1"
+'
+
 test_done
-- 
1.7.12.1.405.gb727dc9

      parent reply	other threads:[~2012-10-05  7:28 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-05  4:40 [PATCH 00/10] nd/wildmatch take 2 Nguyễn Thái Ngọc Duy
2012-10-05  4:41 ` [PATCH 01/10] gitignore: make pattern parsing code a separate function Nguyễn Thái Ngọc Duy
2012-10-05  4:41 ` [PATCH 02/10] attr: avoid strlen() on every match Nguyễn Thái Ngọc Duy
2012-10-05  4:41 ` [PATCH 03/10] attr: avoid searching for basename " Nguyễn Thái Ngọc Duy
2012-10-05  4:41 ` [PATCH 04/10] attr: more matching optimizations from .gitignore Nguyễn Thái Ngọc Duy
2012-10-05 18:48   ` Junio C Hamano
2012-10-06  5:02     ` Nguyen Thai Ngoc Duy
2012-10-06  5:36       ` Junio C Hamano
2012-10-06  6:43         ` Nguyen Thai Ngoc Duy
2012-10-06  6:59           ` Junio C Hamano
2012-10-08  3:26     ` Nguyen Thai Ngoc Duy
2012-10-08 15:50       ` Junio C Hamano
2012-10-05  4:41 ` [PATCH 05/10] Import wildmatch from rsync Nguyễn Thái Ngọc Duy
2012-10-05 10:30   ` Peter Krefting
2012-10-05 11:18     ` Nguyen Thai Ngoc Duy
2012-10-05  4:41 ` [PATCH 06/10] wildmatch: remove static variable force_lower_case Nguyễn Thái Ngọc Duy
2012-10-05  4:41 ` [PATCH 07/10] wildmatch: fix case-insensitive matching Nguyễn Thái Ngọc Duy
2012-10-05  4:41 ` [PATCH 08/10] Integrate wildmatch to git Nguyễn Thái Ngọc Duy
2012-10-05 21:20   ` Thiago Farina
2012-10-06  9:25     ` Joachim Schmitz
2012-10-05  4:41 ` [PATCH 09/10] Support "**" in .gitignore and .gitattributes patterns using wildmatch() Nguyễn Thái Ngọc Duy
2012-10-05  4:41 ` Nguyễn Thái Ngọc Duy [this message]

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=1349412069-627-11-git-send-email-pclouds@gmail.com \
    --to=pclouds@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=mhagger@alum.mit.edu \
    /path/to/YOUR_REPLY

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

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.