git-archive: ignore prefix when checking file attribute
Ulrik Sverdrup noticed that git-archive doesn't correctly apply the attribute export-subst when the option --prefix is given, too. When it checked if a file has the attribute turned on, git-archive would try to look up the full path -- including the prefix -- in .gitattributes. That's wrong, as the prefix doesn't need to have any relation to any existing directories, tracked or not. This patch makes git-archive ignore the prefix when looking up if value of the attribute export-subst for a file. Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Apr 9, 2008 at 23:14 UTC
ac7fa2776c4a839a5a96c13886714774b52844de
3 files changed
+22
-5
archive-tar.c
+4
-2
@@ -17,6 +17,7 @@ static time_t archive_time;
17
static int tar_umask = 002;
18
static int verbose;
19
static const struct commit *commit;
20
+static size_t base_len;
21
22
/* writes out the whole block, but only if it is full */
23
static void write_if_needed(void)
@@ -251,8 +252,8 @@ static int write_tar_entry(const unsigned char *sha1,
252
buffer = NULL;
253
size = 0;
254
} else {
254
- buffer = sha1_file_to_archive(path.buf, sha1, mode, &type,
255
- &size, commit);
255
+ buffer = sha1_file_to_archive(path.buf + base_len, sha1, mode,
256
+ &type, &size, commit);
257
if (!buffer)
258
die("cannot read %s", sha1_to_hex(sha1));
259
}
@@ -272,6 +273,7 @@ int write_tar_archive(struct archiver_args *args)
273
archive_time = args->time;
274
verbose = args->verbose;
275
commit = args->commit;
276
+ base_len = args->base ? strlen(args->base) : 0;
277
278
if (args->commit_sha1)
279
write_global_extended_header(args->commit_sha1);
archive-zip.c
+4
-2
@@ -13,6 +13,7 @@ static int verbose;
13
static int zip_date;
14
static int zip_time;
15
static const struct commit *commit;
16
+static size_t base_len;
17
18
static unsigned char *zip_dir;
19
static unsigned int zip_dir_size;
@@ -197,8 +198,8 @@ static int write_zip_entry(const unsigned char *sha1,
198
if (S_ISREG(mode) && zlib_compression_level != 0)
199
method = 8;
200
result = 0;
200
- buffer = sha1_file_to_archive(path, sha1, mode, &type, &size,
201
- commit);
201
+ buffer = sha1_file_to_archive(path + base_len, sha1, mode,
202
+ &type, &size, commit);
203
if (!buffer)
204
die("cannot read %s", sha1_to_hex(sha1));
205
crc = crc32(crc, buffer, size);
@@ -321,6 +322,7 @@ int write_zip_archive(struct archiver_args *args)
322
zip_dir_size = ZIP_DIRECTORY_MIN_SIZE;
323
verbose = args->verbose;
324
commit = args->commit;
325
+ base_len = args->base ? strlen(args->base) : 0;
326
327
if (args->base && plen > 0 && args->base[plen - 1] == '/') {
328
char *base = xstrdup(args->base);
t/t5000-tar-tree.sh
+14
-1
@@ -109,9 +109,10 @@ test_expect_success \
109
'diff -r a c/prefix/a'
110
111
test_expect_success \
112
- 'create an archive with a substfiles' \
112
+ 'create archives with substfiles' \
113
'echo "substfile?" export-subst >a/.gitattributes &&
114
git archive HEAD >f.tar &&
115
+ git archive --prefix=prefix/ HEAD >g.tar &&
116
rm a/.gitattributes'
117
118
test_expect_success \
@@ -126,6 +127,18 @@ test_expect_success \
127
diff a/substfile2 f/a/substfile2
128
'
129
130
+test_expect_success \
131
+ 'extract substfiles from archive with prefix' \
132
+ '(mkdir g && cd g && $TAR xf -) <g.tar'
133
+
134
+test_expect_success \
135
+ 'validate substfile contents from archive with prefix' \
136
+ 'git log --max-count=1 "--pretty=format:A${SUBSTFORMAT}O" HEAD \
137
+ >g/prefix/a/substfile1.expected &&
138
+ diff g/prefix/a/substfile1.expected g/prefix/a/substfile1 &&
139
+ diff a/substfile2 g/prefix/a/substfile2
140
+'
141
+
142
test_expect_success \
143
'git archive --format=zip' \
144
'git archive --format=zip HEAD >d.zip'