archive-zip: fix compressed size for stored export-subst files

Currently ZIP archive entries of files with export-subst attribute are broken if they are stored uncompressed. We get the size of a file from sha1_object_info(), but this number is likely wrong for files whose contents are changed due to export-subst placeholder expansion. We use sha1_file_to_archive() to get the expanded file contents and size in that case. We proceed to use that size for the uncompressed size field (good), but the compressed size field is set based on the size from sha1_object_info() (bad). This matters only for uncompressed files because for deflated files we use the correct value after compression is done. And for files without export-subst expansion the sizes from sha1_object_info() and sha1_file_to_archive() are the same, so they are unaffected as well. This patch fixes the issue by setting the compressed size based on the uncompressed size only after we actually know the latter. Also make use of the test file substfile1 to check for the breakage; it was only stored verbatim so far. For that purpose, set the attribute export-subst and replace its contents with the expected expansion after committing. Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Feb 27, 2013 at 11:20 UTC d3c1472fe35e2575508d80415565822ee2211df5
2 files changed +7 -1
archive-zip.c
+1 -1
@@ -240,7 +240,6 @@ static int write_zip_entry(struct archiver_args *args,
240 (mode & 0111) ? ((mode) << 16) : 0;
241 if (S_ISREG(mode) && args->compression_level != 0 && size > 0)
242 method = 8;
243 - compressed_size = (method == 0) ? size : 0;
243
244 if (S_ISREG(mode) && type == OBJ_BLOB && !args->convert &&
245 size > big_file_threshold) {
@@ -259,6 +258,7 @@ static int write_zip_entry(struct archiver_args *args,
258 crc = crc32(crc, buffer, size);
259 out = buffer;
260 }
261 + compressed_size = (method == 0) ? size : 0;
262 } else {
263 return error("unsupported file mode: 0%o (SHA1: %s)", mode,
264 sha1_to_hex(sha1));
t/t5003-archive-zip.sh
+6
@@ -76,6 +76,12 @@ test_expect_success \
76 git update-ref HEAD $(TZ=GMT GIT_COMMITTER_DATE="2005-05-27 22:00:00" \
77 git commit-tree $treeid </dev/null)'
78
79 +test_expect_success 'setup export-subst' '
80 + echo "substfile?" export-subst >>.git/info/attributes &&
81 + git log --max-count=1 "--pretty=format:A${SUBSTFORMAT}O" HEAD \
82 + >a/substfile1
83 +'
84 +
85 test_expect_success \
86 'create bare clone' \
87 'git clone --bare . bare.git &&