@matheusbsbs / matheus-bagliano / commits / 5e6cfc80e2
Fork 0

git-archive: convert archive entries like checkouts do

As noted by Johan Herland, git-archive is a kind of checkout and needs to apply any checkout filters that might be configured. This patch adds the convenience function convert_sha1_file which returns a buffer containing the object's contents, after converting, if necessary (i.e. it's a combination of read_sha1_file and convert_to_working_tree). Direct calls to read_sha1_file in git-archive are then replaced by calls to convert_sha1_file. Since convert_sha1_file expects its path argument to be NUL-terminated -- a convention it inherits from convert_to_working_tree -- the patch also changes the path handling in archive-tar.c to always NUL-terminate the string. It used to solely rely on the len field of struct strbuf before. archive-zip.c already NUL-terminates the path and thus needs no such change. Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <junkio@cox.net>

René Scharfe committed May 19, 2007 at 00:09 UTC 5e6cfc80e26a4d0ebac38cff74c2cdebbe66cd27
4 files changed +26 -8
archive-tar.c
+9 -7
@@ -82,12 +82,13 @@ static void strbuf_append_string(struct strbuf *sb, const char *s)
82 {
83 int slen = strlen(s);
84 int total = sb->len + slen;
85 - if (total > sb->alloc) {
86 - sb->buf = xrealloc(sb->buf, total);
87 - sb->alloc = total;
85 + if (total + 1 > sb->alloc) {
86 + sb->buf = xrealloc(sb->buf, total + 1);
87 + sb->alloc = total + 1;
88 }
89 memcpy(sb->buf + sb->len, s, slen);
90 sb->len = total;
91 + sb->buf[total] = '\0';
92 }
93
94 /*
@@ -270,20 +271,21 @@ static int write_tar_entry(const unsigned char *sha1,
271 path.alloc = PATH_MAX;
272 path.len = path.eof = 0;
273 }
273 - if (path.alloc < baselen + filenamelen) {
274 + if (path.alloc < baselen + filenamelen + 1) {
275 free(path.buf);
275 - path.buf = xmalloc(baselen + filenamelen);
276 - path.alloc = baselen + filenamelen;
276 + path.buf = xmalloc(baselen + filenamelen + 1);
277 + path.alloc = baselen + filenamelen + 1;
278 }
279 memcpy(path.buf, base, baselen);
280 memcpy(path.buf + baselen, filename, filenamelen);
281 path.len = baselen + filenamelen;
282 + path.buf[path.len] = '\0';
283 if (S_ISDIR(mode) || S_ISDIRLNK(mode)) {
284 strbuf_append_string(&path, "/");
285 buffer = NULL;
286 size = 0;
287 } else {
286 - buffer = read_sha1_file(sha1, &type, &size);
288 + buffer = convert_sha1_file(path.buf, sha1, mode, &type, &size);
289 if (!buffer)
290 die("cannot read %s", sha1_to_hex(sha1));
291 }
archive-zip.c
+1 -1
@@ -195,7 +195,7 @@ static int write_zip_entry(const unsigned char *sha1,
195 if (S_ISREG(mode) && zlib_compression_level != 0)
196 method = 8;
197 result = 0;
198 - buffer = read_sha1_file(sha1, &type, &size);
198 + buffer = convert_sha1_file(path, sha1, mode, &type, &size);
199 if (!buffer)
200 die("cannot read %s", sha1_to_hex(sha1));
201 crc = crc32(crc, buffer, size);
cache.h
+1
@@ -548,6 +548,7 @@ extern void trace_argv_printf(const char **argv, int count, const char *format,
548 /* convert.c */
549 extern char *convert_to_git(const char *path, const char *src, unsigned long *sizep);
550 extern char *convert_to_working_tree(const char *path, const char *src, unsigned long *sizep);
551 +extern void *convert_sha1_file(const char *path, const unsigned char *sha1, unsigned int mode, enum object_type *type, unsigned long *size);
552
553 /* match-trees.c */
554 void shift_tree(const unsigned char *, const unsigned char *, unsigned char *, int);
convert.c
+15
@@ -652,3 +652,18 @@ char *convert_to_working_tree(const char *path, const char *src, unsigned long *
652
653 return buf;
654 }
655 +
656 +void *convert_sha1_file(const char *path, const unsigned char *sha1,
657 + unsigned int mode, enum object_type *type,
658 + unsigned long *size)
659 +{
660 + void *buffer = read_sha1_file(sha1, type, size);
661 + if (S_ISREG(mode) && buffer) {
662 + void *converted = convert_to_working_tree(path, buffer, size);
663 + if (converted) {
664 + free(buffer);
665 + buffer = converted;
666 + }
667 + }
668 + return buffer;
669 +}