From bed69a6e82bbef99159f9051fd4581cd37c9acf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sat, 19 Aug 2017 07:28:54 +0200 Subject: [PATCH 1/3] t5001: add tests for export-ignore attributes and exclude pathspecs Demonstrate mishandling of the attribute export-ignore by git archive when used together with pathspecs. Wildcard pathspecs can even cause it to abort. And a directory excluded without a wildcard is still included as an empty folder in the archive. Test-case-by: David Adam Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- t/t5001-archive-attr.sh | 47 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/t/t5001-archive-attr.sh b/t/t5001-archive-attr.sh index b04d955bfa..063622bc71 100755 --- a/t/t5001-archive-attr.sh +++ b/t/t5001-archive-attr.sh @@ -7,11 +7,15 @@ test_description='git archive attribute tests' SUBSTFORMAT='%H (%h)%n' test_expect_exists() { - test_expect_success " $1 exists" "test -e $1" + test_expect_${2:-success} " $1 exists" "test -e $1" } test_expect_missing() { - test_expect_success " $1 does not exist" "test ! -e $1" + test_expect_${2:-success} " $1 does not exist" "test ! -e $1" +} + +extract_tar_to_dir () { + (mkdir "$1" && cd "$1" && "$TAR" xf -) <"$1.tar" } test_expect_success 'setup' ' @@ -21,12 +25,19 @@ test_expect_success 'setup' ' echo ignored by tree >ignored-by-tree && echo ignored-by-tree export-ignore >.gitattributes && - git add ignored-by-tree .gitattributes && + mkdir ignored-by-tree.d && + >ignored-by-tree.d/file && + echo ignored-by-tree.d export-ignore >>.gitattributes && + git add ignored-by-tree ignored-by-tree.d .gitattributes && echo ignored by worktree >ignored-by-worktree && echo ignored-by-worktree export-ignore >.gitattributes && git add ignored-by-worktree && + mkdir excluded-by-pathspec.d && + >excluded-by-pathspec.d/file && + git add excluded-by-pathspec.d && + printf "A\$Format:%s\$O" "$SUBSTFORMAT" >nosubstfile && printf "A\$Format:%s\$O" "$SUBSTFORMAT" >substfile1 && printf "A not substituted O" >substfile2 && @@ -46,7 +57,37 @@ test_expect_success 'git archive' ' test_expect_missing archive/ignored test_expect_missing archive/ignored-by-tree +test_expect_missing archive/ignored-by-tree.d +test_expect_missing archive/ignored-by-tree.d/file test_expect_exists archive/ignored-by-worktree +test_expect_exists archive/excluded-by-pathspec.d +test_expect_exists archive/excluded-by-pathspec.d/file + +test_expect_success 'git archive with pathspec' ' + git archive HEAD ":!excluded-by-pathspec.d" >archive-pathspec.tar && + extract_tar_to_dir archive-pathspec +' + +test_expect_missing archive-pathspec/ignored +test_expect_missing archive-pathspec/ignored-by-tree +test_expect_missing archive-pathspec/ignored-by-tree.d +test_expect_missing archive-pathspec/ignored-by-tree.d/file +test_expect_exists archive-pathspec/ignored-by-worktree +test_expect_missing archive-pathspec/excluded-by-pathspec.d failure +test_expect_missing archive-pathspec/excluded-by-pathspec.d/file + +test_expect_failure 'git archive with wildcard pathspec' ' + git archive HEAD ":!excluded-by-p*" >archive-pathspec-wildcard.tar && + extract_tar_to_dir archive-pathspec-wildcard +' + +test_expect_missing archive-pathspec-wildcard/ignored +test_expect_missing archive-pathspec-wildcard/ignored-by-tree +test_expect_missing archive-pathspec-wildcard/ignored-by-tree.d +test_expect_missing archive-pathspec-wildcard/ignored-by-tree.d/file +test_expect_exists archive-pathspec-wildcard/ignored-by-worktree failure +test_expect_missing archive-pathspec-wildcard/excluded-by-pathspec.d +test_expect_missing archive-pathspec-wildcard/excluded-by-pathspec.d/file test_expect_success 'git archive with worktree attributes' ' git archive --worktree-attributes HEAD >worktree.tar && From c6c08f7e9a94d062fb2f90687156798e78cf8991 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sat, 19 Aug 2017 07:29:43 +0200 Subject: [PATCH 2/3] archive: factor out helper functions for handling attributes Add helpers for accessing attributes that encapsulate the details of how to retrieve their values. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- archive.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/archive.c b/archive.c index 60b3035a7a..66f1abe735 100644 --- a/archive.c +++ b/archive.c @@ -103,12 +103,30 @@ struct archiver_context { struct directory *bottom; }; +static const struct attr_check *get_archive_attrs(const char *path) +{ + static struct attr_check *check; + if (!check) + check = attr_check_initl("export-ignore", "export-subst", NULL); + return git_check_attr(path, check) ? NULL : check; +} + +static int check_attr_export_ignore(const struct attr_check *check) +{ + return check && ATTR_TRUE(check->items[0].value); +} + +static int check_attr_export_subst(const struct attr_check *check) +{ + return check && ATTR_TRUE(check->items[1].value); +} + static int write_archive_entry(const unsigned char *sha1, const char *base, int baselen, const char *filename, unsigned mode, int stage, void *context) { static struct strbuf path = STRBUF_INIT; - static struct attr_check *check; + const struct attr_check *check; struct archiver_context *c = context; struct archiver_args *args = c->args; write_archive_entry_fn_t write_entry = c->write_entry; @@ -125,13 +143,10 @@ static int write_archive_entry(const unsigned char *sha1, const char *base, strbuf_addch(&path, '/'); path_without_prefix = path.buf + args->baselen; - if (!check) - check = attr_check_initl("export-ignore", "export-subst", NULL); - if (!git_check_attr(path_without_prefix, check)) { - if (ATTR_TRUE(check->items[0].value)) - return 0; - args->convert = ATTR_TRUE(check->items[1].value); - } + check = get_archive_attrs(path_without_prefix); + if (check_attr_export_ignore(check)) + return 0; + args->convert = check_attr_export_subst(check); if (S_ISDIR(mode) || S_ISGITLINK(mode)) { if (args->verbose) From 5ff247ac0cc9b2d09c8c24bd0c8df12eed94aebc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sat, 19 Aug 2017 07:32:37 +0200 Subject: [PATCH 3/3] archive: don't queue excluded directories Reject directories with the attribute export-ignore already while queuing them. This prevents read_tree_recursive() from descending into them and this avoids write_archive_entry() rejecting them later on, which queue_or_write_archive_entry() is not prepared for. Borrow the existing strbuf to build the full path to avoid string copies and extra allocations; just make sure we restore the original value before moving on. Keep checking any other attributes in write_archive_entry() as before, but avoid checking them twice. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- archive.c | 32 +++++++++++++++++++++++++------- t/t5001-archive-attr.sh | 4 ++-- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/archive.c b/archive.c index 66f1abe735..2ad7e6cb90 100644 --- a/archive.c +++ b/archive.c @@ -121,17 +121,21 @@ static int check_attr_export_subst(const struct attr_check *check) return check && ATTR_TRUE(check->items[1].value); } +static int should_queue_directories(const struct archiver_args *args) +{ + return args->pathspec.has_wildcard; +} + static int write_archive_entry(const unsigned char *sha1, const char *base, int baselen, const char *filename, unsigned mode, int stage, void *context) { static struct strbuf path = STRBUF_INIT; - const struct attr_check *check; struct archiver_context *c = context; struct archiver_args *args = c->args; write_archive_entry_fn_t write_entry = c->write_entry; - const char *path_without_prefix; int err; + const char *path_without_prefix; args->convert = 0; strbuf_reset(&path); @@ -143,10 +147,13 @@ static int write_archive_entry(const unsigned char *sha1, const char *base, strbuf_addch(&path, '/'); path_without_prefix = path.buf + args->baselen; - check = get_archive_attrs(path_without_prefix); - if (check_attr_export_ignore(check)) - return 0; - args->convert = check_attr_export_subst(check); + if (!S_ISDIR(mode) || !should_queue_directories(args)) { + const struct attr_check *check; + check = get_archive_attrs(path_without_prefix); + if (check_attr_export_ignore(check)) + return 0; + args->convert = check_attr_export_subst(check); + } if (S_ISDIR(mode) || S_ISGITLINK(mode)) { if (args->verbose) @@ -219,6 +226,17 @@ static int queue_or_write_archive_entry(const unsigned char *sha1, } if (S_ISDIR(mode)) { + size_t baselen = base->len; + const struct attr_check *check; + + /* Borrow base, but restore its original value when done. */ + strbuf_addstr(base, filename); + strbuf_addch(base, '/'); + check = get_archive_attrs(base->buf); + strbuf_setlen(base, baselen); + + if (check_attr_export_ignore(check)) + return 0; queue_directory(sha1, base, filename, mode, stage, c); return READ_TREE_RECURSIVE; @@ -272,7 +290,7 @@ int write_archive_entries(struct archiver_args *args, } err = read_tree_recursive(args->tree, "", 0, 0, &args->pathspec, - args->pathspec.has_wildcard ? + should_queue_directories(args) ? queue_or_write_archive_entry : write_archive_entry_buf, &context); diff --git a/t/t5001-archive-attr.sh b/t/t5001-archive-attr.sh index 063622bc71..897f6f06d5 100755 --- a/t/t5001-archive-attr.sh +++ b/t/t5001-archive-attr.sh @@ -76,7 +76,7 @@ test_expect_exists archive-pathspec/ignored-by-worktree test_expect_missing archive-pathspec/excluded-by-pathspec.d failure test_expect_missing archive-pathspec/excluded-by-pathspec.d/file -test_expect_failure 'git archive with wildcard pathspec' ' +test_expect_success 'git archive with wildcard pathspec' ' git archive HEAD ":!excluded-by-p*" >archive-pathspec-wildcard.tar && extract_tar_to_dir archive-pathspec-wildcard ' @@ -85,7 +85,7 @@ test_expect_missing archive-pathspec-wildcard/ignored test_expect_missing archive-pathspec-wildcard/ignored-by-tree test_expect_missing archive-pathspec-wildcard/ignored-by-tree.d test_expect_missing archive-pathspec-wildcard/ignored-by-tree.d/file -test_expect_exists archive-pathspec-wildcard/ignored-by-worktree failure +test_expect_exists archive-pathspec-wildcard/ignored-by-worktree test_expect_missing archive-pathspec-wildcard/excluded-by-pathspec.d test_expect_missing archive-pathspec-wildcard/excluded-by-pathspec.d/file