Merge branch 'rs/archive-excluded-directory'

"git archive" did not work well with pathspecs and the
export-ignore attribute.

* rs/archive-excluded-directory:
  archive: don't queue excluded directories
  archive: factor out helper functions for handling attributes
  t5001: add tests for export-ignore attributes and exclude pathspecs
maint
Junio C Hamano 2017-09-06 13:11:25 +09:00
commit a7d7f125e3
2 changed files with 85 additions and 11 deletions

View File

@ -103,17 +103,39 @@ struct archiver_context {
struct directory *bottom; 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 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, static int write_archive_entry(const unsigned char *sha1, const char *base,
int baselen, const char *filename, unsigned mode, int stage, int baselen, const char *filename, unsigned mode, int stage,
void *context) void *context)
{ {
static struct strbuf path = STRBUF_INIT; static struct strbuf path = STRBUF_INIT;
static struct attr_check *check;
struct archiver_context *c = context; struct archiver_context *c = context;
struct archiver_args *args = c->args; struct archiver_args *args = c->args;
write_archive_entry_fn_t write_entry = c->write_entry; write_archive_entry_fn_t write_entry = c->write_entry;
const char *path_without_prefix;
int err; int err;
const char *path_without_prefix;


args->convert = 0; args->convert = 0;
strbuf_reset(&path); strbuf_reset(&path);
@ -125,12 +147,12 @@ static int write_archive_entry(const unsigned char *sha1, const char *base,
strbuf_addch(&path, '/'); strbuf_addch(&path, '/');
path_without_prefix = path.buf + args->baselen; path_without_prefix = path.buf + args->baselen;


if (!check) if (!S_ISDIR(mode) || !should_queue_directories(args)) {
check = attr_check_initl("export-ignore", "export-subst", NULL); const struct attr_check *check;
if (!git_check_attr(path_without_prefix, check)) { check = get_archive_attrs(path_without_prefix);
if (ATTR_TRUE(check->items[0].value)) if (check_attr_export_ignore(check))
return 0; return 0;
args->convert = ATTR_TRUE(check->items[1].value); args->convert = check_attr_export_subst(check);
} }


if (S_ISDIR(mode) || S_ISGITLINK(mode)) { if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
@ -204,6 +226,17 @@ static int queue_or_write_archive_entry(const unsigned char *sha1,
} }


if (S_ISDIR(mode)) { 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, queue_directory(sha1, base, filename,
mode, stage, c); mode, stage, c);
return READ_TREE_RECURSIVE; return READ_TREE_RECURSIVE;
@ -257,7 +290,7 @@ int write_archive_entries(struct archiver_args *args,
} }


err = read_tree_recursive(args->tree, "", 0, 0, &args->pathspec, err = read_tree_recursive(args->tree, "", 0, 0, &args->pathspec,
args->pathspec.has_wildcard ? should_queue_directories(args) ?
queue_or_write_archive_entry : queue_or_write_archive_entry :
write_archive_entry_buf, write_archive_entry_buf,
&context); &context);

View File

@ -7,11 +7,15 @@ test_description='git archive attribute tests'
SUBSTFORMAT='%H (%h)%n' SUBSTFORMAT='%H (%h)%n'


test_expect_exists() { 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_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' ' test_expect_success 'setup' '
@ -21,12 +25,19 @@ test_expect_success 'setup' '


echo ignored by tree >ignored-by-tree && echo ignored by tree >ignored-by-tree &&
echo ignored-by-tree export-ignore >.gitattributes && 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 >ignored-by-worktree &&
echo ignored-by-worktree export-ignore >.gitattributes && echo ignored-by-worktree export-ignore >.gitattributes &&
git add ignored-by-worktree && 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" >nosubstfile &&
printf "A\$Format:%s\$O" "$SUBSTFORMAT" >substfile1 && printf "A\$Format:%s\$O" "$SUBSTFORMAT" >substfile1 &&
printf "A not substituted O" >substfile2 && 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
test_expect_missing archive/ignored-by-tree 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/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_success '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
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' ' test_expect_success 'git archive with worktree attributes' '
git archive --worktree-attributes HEAD >worktree.tar && git archive --worktree-attributes HEAD >worktree.tar &&