Skip to content

Commit a9dc3b6

Browse files
committed
Merge branch 'jk/empty-archive' into maint
"git archive" reports a failure when asked to create an archive out of an empty tree. It would be more intuitive to give an empty archive back in such a case. * jk/empty-archive: archive: handle commits with an empty tree test-lib: factor out $GIT_UNZIP setup
2 parents 9e72a56 + bd54cf1 commit a9dc3b6

File tree

6 files changed

+109
-13
lines changed

6 files changed

+109
-13
lines changed

archive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ static void parse_pathspec_arg(const char **pathspec,
234234
ar_args->pathspec = pathspec = get_pathspec("", pathspec);
235235
if (pathspec) {
236236
while (*pathspec) {
237-
if (!path_exists(ar_args->tree, *pathspec))
237+
if (**pathspec && !path_exists(ar_args->tree, *pathspec))
238238
die("path not found: %s", *pathspec);
239239
pathspec++;
240240
}

t/t0024-crlf-archive.sh

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@
33
test_description='respect crlf in git archive'
44

55
. ./test-lib.sh
6-
GIT_UNZIP=${GIT_UNZIP:-unzip}
7-
8-
test_lazy_prereq UNZIP '
9-
"$GIT_UNZIP" -v
10-
test $? -ne 127
11-
'
126

137
test_expect_success setup '
148

t/t5003-archive-zip.sh

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,9 @@
33
test_description='git archive --format=zip test'
44

55
. ./test-lib.sh
6-
GIT_UNZIP=${GIT_UNZIP:-unzip}
76

87
SUBSTFORMAT=%H%n
98

10-
test_lazy_prereq UNZIP '
11-
"$GIT_UNZIP" -v
12-
test $? -ne 127
13-
'
14-
159
test_lazy_prereq UNZIP_SYMLINKS '
1610
(
1711
mkdir unzip-symlinks &&

t/t5004-archive-corner-cases.sh

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/bin/sh
2+
3+
test_description='test corner cases of git-archive'
4+
. ./test-lib.sh
5+
6+
test_expect_success 'create commit with empty tree' '
7+
git commit --allow-empty -m foo
8+
'
9+
10+
# Make a dir and clean it up afterwards
11+
make_dir() {
12+
mkdir "$1" &&
13+
test_when_finished "rm -rf '$1'"
14+
}
15+
16+
# Check that the dir given in "$1" contains exactly the
17+
# set of paths given as arguments.
18+
check_dir() {
19+
dir=$1; shift
20+
{
21+
echo "$dir" &&
22+
for i in "$@"; do
23+
echo "$dir/$i"
24+
done
25+
} | sort >expect &&
26+
find "$dir" -print | sort >actual &&
27+
test_cmp expect actual
28+
}
29+
30+
test_expect_success 'tar archive of empty tree is empty' '
31+
git archive --format=tar HEAD >empty.tar &&
32+
make_dir extract &&
33+
"$TAR" xf empty.tar -C extract &&
34+
check_dir extract
35+
'
36+
37+
test_expect_success 'tar archive of empty tree with prefix' '
38+
git archive --format=tar --prefix=foo/ HEAD >prefix.tar &&
39+
make_dir extract &&
40+
"$TAR" xf prefix.tar -C extract &&
41+
check_dir extract foo
42+
'
43+
44+
test_expect_success UNZIP 'zip archive of empty tree is empty' '
45+
# Detect the exit code produced when our particular flavor of unzip
46+
# sees an empty archive. Infozip will generate a warning and exit with
47+
# code 1. But in the name of sanity, we do not expect other unzip
48+
# implementations to do the same thing (it would be perfectly
49+
# reasonable to exit 0, for example).
50+
#
51+
# This makes our test less rigorous on some platforms (unzip may not
52+
# handle the empty repo at all, making our later check of its exit code
53+
# a no-op). But we cannot do anything reasonable except skip the test
54+
# on such platforms anyway, and this is the moral equivalent.
55+
"$GIT_UNZIP" "$TEST_DIRECTORY"/t5004/empty.zip
56+
expect_code=$?
57+
58+
git archive --format=zip HEAD >empty.zip &&
59+
make_dir extract &&
60+
(
61+
cd extract &&
62+
test_expect_code $expect_code "$GIT_UNZIP" ../empty.zip
63+
) &&
64+
check_dir extract
65+
'
66+
67+
test_expect_success UNZIP 'zip archive of empty tree with prefix' '
68+
# We do not have to play exit-code tricks here, because our
69+
# result should not be empty; it has a directory in it.
70+
git archive --format=zip --prefix=foo/ HEAD >prefix.zip &&
71+
make_dir extract &&
72+
(
73+
cd extract &&
74+
"$GIT_UNZIP" ../prefix.zip
75+
) &&
76+
check_dir extract foo
77+
'
78+
79+
test_expect_success 'archive complains about pathspec on empty tree' '
80+
test_must_fail git archive --format=tar HEAD -- foo >/dev/null
81+
'
82+
83+
test_expect_success 'create a commit with an empty subtree' '
84+
empty_tree=$(git hash-object -t tree /dev/null) &&
85+
root_tree=$(printf "040000 tree $empty_tree\tsub\n" | git mktree)
86+
'
87+
88+
test_expect_success 'archive empty subtree with no pathspec' '
89+
git archive --format=tar $root_tree >subtree-all.tar &&
90+
make_dir extract &&
91+
"$TAR" xf subtree-all.tar -C extract &&
92+
check_dir extract sub
93+
'
94+
95+
test_expect_success 'archive empty subtree by direct pathspec' '
96+
git archive --format=tar $root_tree -- sub >subtree-path.tar &&
97+
make_dir extract &&
98+
"$TAR" xf subtree-path.tar -C extract &&
99+
check_dir extract sub
100+
'
101+
102+
test_done

t/t5004/empty.zip

62 Bytes
Binary file not shown.

t/test-lib.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,3 +760,9 @@ test_lazy_prereq AUTOIDENT '
760760
# When the tests are run as root, permission tests will report that
761761
# things are writable when they shouldn't be.
762762
test -w / || test_set_prereq SANITY
763+
764+
GIT_UNZIP=${GIT_UNZIP:-unzip}
765+
test_lazy_prereq UNZIP '
766+
"$GIT_UNZIP" -v
767+
test $? -ne 127
768+
'

0 commit comments

Comments
 (0)