Skip to content

Commit a7d7f12

Browse files
committed
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
2 parents 8b36f0b + 5ff247a commit a7d7f12

File tree

2 files changed

+85
-11
lines changed

2 files changed

+85
-11
lines changed

archive.c

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,39 @@ struct archiver_context {
103103
struct directory *bottom;
104104
};
105105

106+
static const struct attr_check *get_archive_attrs(const char *path)
107+
{
108+
static struct attr_check *check;
109+
if (!check)
110+
check = attr_check_initl("export-ignore", "export-subst", NULL);
111+
return git_check_attr(path, check) ? NULL : check;
112+
}
113+
114+
static int check_attr_export_ignore(const struct attr_check *check)
115+
{
116+
return check && ATTR_TRUE(check->items[0].value);
117+
}
118+
119+
static int check_attr_export_subst(const struct attr_check *check)
120+
{
121+
return check && ATTR_TRUE(check->items[1].value);
122+
}
123+
124+
static int should_queue_directories(const struct archiver_args *args)
125+
{
126+
return args->pathspec.has_wildcard;
127+
}
128+
106129
static int write_archive_entry(const unsigned char *sha1, const char *base,
107130
int baselen, const char *filename, unsigned mode, int stage,
108131
void *context)
109132
{
110133
static struct strbuf path = STRBUF_INIT;
111-
static struct attr_check *check;
112134
struct archiver_context *c = context;
113135
struct archiver_args *args = c->args;
114136
write_archive_entry_fn_t write_entry = c->write_entry;
115-
const char *path_without_prefix;
116137
int err;
138+
const char *path_without_prefix;
117139

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

128-
if (!check)
129-
check = attr_check_initl("export-ignore", "export-subst", NULL);
130-
if (!git_check_attr(path_without_prefix, check)) {
131-
if (ATTR_TRUE(check->items[0].value))
150+
if (!S_ISDIR(mode) || !should_queue_directories(args)) {
151+
const struct attr_check *check;
152+
check = get_archive_attrs(path_without_prefix);
153+
if (check_attr_export_ignore(check))
132154
return 0;
133-
args->convert = ATTR_TRUE(check->items[1].value);
155+
args->convert = check_attr_export_subst(check);
134156
}
135157

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

206228
if (S_ISDIR(mode)) {
229+
size_t baselen = base->len;
230+
const struct attr_check *check;
231+
232+
/* Borrow base, but restore its original value when done. */
233+
strbuf_addstr(base, filename);
234+
strbuf_addch(base, '/');
235+
check = get_archive_attrs(base->buf);
236+
strbuf_setlen(base, baselen);
237+
238+
if (check_attr_export_ignore(check))
239+
return 0;
207240
queue_directory(sha1, base, filename,
208241
mode, stage, c);
209242
return READ_TREE_RECURSIVE;
@@ -257,7 +290,7 @@ int write_archive_entries(struct archiver_args *args,
257290
}
258291

259292
err = read_tree_recursive(args->tree, "", 0, 0, &args->pathspec,
260-
args->pathspec.has_wildcard ?
293+
should_queue_directories(args) ?
261294
queue_or_write_archive_entry :
262295
write_archive_entry_buf,
263296
&context);

t/t5001-archive-attr.sh

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ test_description='git archive attribute tests'
77
SUBSTFORMAT='%H (%h)%n'
88

99
test_expect_exists() {
10-
test_expect_success " $1 exists" "test -e $1"
10+
test_expect_${2:-success} " $1 exists" "test -e $1"
1111
}
1212

1313
test_expect_missing() {
14-
test_expect_success " $1 does not exist" "test ! -e $1"
14+
test_expect_${2:-success} " $1 does not exist" "test ! -e $1"
15+
}
16+
17+
extract_tar_to_dir () {
18+
(mkdir "$1" && cd "$1" && "$TAR" xf -) <"$1.tar"
1519
}
1620

1721
test_expect_success 'setup' '
@@ -21,12 +25,19 @@ test_expect_success 'setup' '
2125
2226
echo ignored by tree >ignored-by-tree &&
2327
echo ignored-by-tree export-ignore >.gitattributes &&
24-
git add ignored-by-tree .gitattributes &&
28+
mkdir ignored-by-tree.d &&
29+
>ignored-by-tree.d/file &&
30+
echo ignored-by-tree.d export-ignore >>.gitattributes &&
31+
git add ignored-by-tree ignored-by-tree.d .gitattributes &&
2532
2633
echo ignored by worktree >ignored-by-worktree &&
2734
echo ignored-by-worktree export-ignore >.gitattributes &&
2835
git add ignored-by-worktree &&
2936
37+
mkdir excluded-by-pathspec.d &&
38+
>excluded-by-pathspec.d/file &&
39+
git add excluded-by-pathspec.d &&
40+
3041
printf "A\$Format:%s\$O" "$SUBSTFORMAT" >nosubstfile &&
3142
printf "A\$Format:%s\$O" "$SUBSTFORMAT" >substfile1 &&
3243
printf "A not substituted O" >substfile2 &&
@@ -46,7 +57,37 @@ test_expect_success 'git archive' '
4657

4758
test_expect_missing archive/ignored
4859
test_expect_missing archive/ignored-by-tree
60+
test_expect_missing archive/ignored-by-tree.d
61+
test_expect_missing archive/ignored-by-tree.d/file
4962
test_expect_exists archive/ignored-by-worktree
63+
test_expect_exists archive/excluded-by-pathspec.d
64+
test_expect_exists archive/excluded-by-pathspec.d/file
65+
66+
test_expect_success 'git archive with pathspec' '
67+
git archive HEAD ":!excluded-by-pathspec.d" >archive-pathspec.tar &&
68+
extract_tar_to_dir archive-pathspec
69+
'
70+
71+
test_expect_missing archive-pathspec/ignored
72+
test_expect_missing archive-pathspec/ignored-by-tree
73+
test_expect_missing archive-pathspec/ignored-by-tree.d
74+
test_expect_missing archive-pathspec/ignored-by-tree.d/file
75+
test_expect_exists archive-pathspec/ignored-by-worktree
76+
test_expect_missing archive-pathspec/excluded-by-pathspec.d failure
77+
test_expect_missing archive-pathspec/excluded-by-pathspec.d/file
78+
79+
test_expect_success 'git archive with wildcard pathspec' '
80+
git archive HEAD ":!excluded-by-p*" >archive-pathspec-wildcard.tar &&
81+
extract_tar_to_dir archive-pathspec-wildcard
82+
'
83+
84+
test_expect_missing archive-pathspec-wildcard/ignored
85+
test_expect_missing archive-pathspec-wildcard/ignored-by-tree
86+
test_expect_missing archive-pathspec-wildcard/ignored-by-tree.d
87+
test_expect_missing archive-pathspec-wildcard/ignored-by-tree.d/file
88+
test_expect_exists archive-pathspec-wildcard/ignored-by-worktree
89+
test_expect_missing archive-pathspec-wildcard/excluded-by-pathspec.d
90+
test_expect_missing archive-pathspec-wildcard/excluded-by-pathspec.d/file
5091

5192
test_expect_success 'git archive with worktree attributes' '
5293
git archive --worktree-attributes HEAD >worktree.tar &&

0 commit comments

Comments
 (0)