git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Jean-Noël AVILA" <avila.jn@gmail.com>
To: git@vger.kernel.org
Subject: [PATCHv2] Add directory pattern matching to attributes
Date: Sat, 8 Dec 2012 21:04:39 +0100	[thread overview]
Message-ID: <201212082104.39411.avila.jn@gmail.com> (raw)

The manpage of gitattributes says: "The rules how the pattern
matches paths are the same as in .gitignore files" and the gitignore
pattern matching has a pattern ending with / for directory matching.

This rule is specifically relevant for the 'export-ignore' rule used
for git archive.

Signed-off-by: Jean-Noel Avila <jn.avila@free.fr>
---
 archive.c                       |    3 ++-
 attr.c                          |   32 ++++++++++++++++------
 t/t5002-archive-attr-pattern.sh |   57 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 83 insertions(+), 9 deletions(-)
 create mode 100644 t/t5002-archive-attr-pattern.sh

diff --git a/archive.c b/archive.c
index 4666404..93e00bb 100644
--- a/archive.c
+++ b/archive.c
@@ -120,6 +120,8 @@ 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, '/');
 	path_without_prefix = path.buf + args->baselen;
 
 	setup_archive_check(check);
@@ -130,7 +132,6 @@ static int write_archive_entry(const unsigned char *sha1, const char *base,
 	}
 
 	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);
diff --git a/attr.c b/attr.c
index 097ae87..cdba88a 100644
--- a/attr.c
+++ b/attr.c
@@ -564,17 +564,31 @@ static void bootstrap_attr_stack(void)
 	attr_stack = elem;
 }
 
+static const char *find_basename(const char *path)
+{
+	char pathbuf[PATH_MAX];
+	int pathlen;
+	const char *cp;
+
+	pathlen =strlen(path);
+	if (path[pathlen-1] != '/') {
+		cp =strrchr(path, '/');
+		return cp ? cp + 1: path;
+	} else {
+		strncpy(pathbuf, path, pathlen);
+		pathbuf[pathlen-1] = '\0';
+		cp =strrchr(pathbuf, '/');
+		return cp ? path + (cp - pathbuf) + 1 : path;
+	}
+}
+
 static void prepare_attr_stack(const char *path)
 {
 	struct attr_stack *elem, *info;
 	int dirlen, len;
 	const char *cp;
 
-	cp = strrchr(path, '/');
-	if (!cp)
-		dirlen = 0;
-	else
-		dirlen = cp - path;
+	dirlen = find_basename(path) - path;
 
 	/*
 	 * At the bottom of the attribute stack is the built-in
@@ -668,6 +682,10 @@ static int path_matches(const char *pathname, int pathlen,
 	const char *pattern = pat->pattern;
 	int prefix = pat->nowildcardlen;
 
+	if ((pat->flags & EXC_FLAG_MUSTBEDIR) &&
+	    ((!pathlen) || (pathname[pathlen-1] != '/')))
+		return 0;
+
 	if (pat->flags & EXC_FLAG_NODIR) {
 		return match_basename(basename,
 				      pathlen - (basename - pathname),
@@ -758,9 +776,7 @@ static void collect_all_attrs(const char *path)
 	for (i = 0; i < attr_nr; i++)
 		check_all_attr[i].value = ATTR__UNKNOWN;
 
-	basename = strrchr(path, '/');
-	basename = basename ? basename + 1 : path;
-
+	basename = find_basename(path);
 	pathlen = strlen(path);
 	rem = attr_nr;
 	for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
diff --git a/t/t5002-archive-attr-pattern.sh b/t/t5002-archive-attr-pattern.sh
new file mode 100644
index 0000000..0c847fb
--- /dev/null
+++ b/t/t5002-archive-attr-pattern.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+
+test_description='git archive attribute pattern tests'
+
+. ./test-lib.sh
+
+test_expect_exists() {
+	test_expect_success " $1 exists" "test -e $1"
+}
+
+test_expect_missing() {
+	test_expect_success " $1 does not exist" "test ! -e $1"
+}
+
+test_expect_success 'setup' '
+	echo ignored >ignored &&
+	echo ignored export-ignore >>.git/info/attributes &&
+	git add ignored &&
+
+	mkdir not-ignored-dir &&
+	echo ignored-in-tree >not-ignored-dir/ignored &&
+	echo not-ignored-in-tree >not-ignored-dir/ignored-only-if-dir &&
+	git add not-ignored-dir &&
+
+	mkdir ignored-only-if-dir &&
+	echo ignored by ignored dir >ignored-only-if-dir/ignored-by-ignored-dir &&
+	echo ignored-only-if-dir/ export-ignore >>.git/info/attributes &&
+	git add ignored-only-if-dir &&
+
+
+	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 &&
+
+	git commit -m. &&
+
+	git clone --bare . bare &&
+	cp .git/info/attributes bare/info/attributes
+'
+
+test_expect_success 'git archive' '
+	git archive HEAD >archive.tar &&
+	(mkdir archive && cd archive && "$TAR" xf -) <archive.tar
+'
+
+test_expect_missing	archive/ignored
+test_expect_missing	archive/not-ignored-dir/ignored
+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_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
+
+
+test_done
-- 
1.7.10.4

             reply	other threads:[~2012-12-08 20:05 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-12-08 20:04 Jean-Noël AVILA [this message]
2012-12-13 20:08 ` [PATCHv2] Add directory pattern matching to attributes Junio C Hamano

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=201212082104.39411.avila.jn@gmail.com \
    --to=avila.jn@gmail.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

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

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