@matheusbsbs / matheus-bagliano / commits / 87af29f09f
Fork 0

git-tar-tree: devolve git-tar-tree into a wrapper for git-archive

This patch removes the custom tree walker tree_traverse(), and makes generate_tar() use write_tar_archive() and the infrastructure provided by git-archive instead. As a kind of side effect, make write_tar_archive() able to handle NULL as base directory, as this is what the new and simple generate_tar() uses to indicate the absence of a base directory. This was simpler and cleaner than playing tricks with empty strings. The behaviour of git-tar-tree should be unchanged (quick tests didn't indicate otherwise) except for the text of some error messages. Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <junkio@cox.net>

Rene Scharfe committed Sep 16, 2006 at 21:20 UTC 87af29f09f119c4a4fa7fdf308483ae1abb74b49
1 file changed +18 -72
builtin-tar-tree.c
+18 -72
@@ -3,7 +3,6 @@
3 */
4 #include <time.h>
5 #include "cache.h"
6 -#include "tree-walk.h"
6 #include "commit.h"
7 #include "strbuf.h"
8 #include "tar.h"
@@ -248,37 +247,6 @@ static void write_global_extended_header(const unsigned char *sha1)
247 free(ext_header.buf);
248 }
249
251 -static void traverse_tree(struct tree_desc *tree, struct strbuf *path)
252 -{
253 - int pathlen = path->len;
254 - struct name_entry entry;
255 -
256 - while (tree_entry(tree, &entry)) {
257 - void *eltbuf;
258 - char elttype[20];
259 - unsigned long eltsize;
260 -
261 - eltbuf = read_sha1_file(entry.sha1, elttype, &eltsize);
262 - if (!eltbuf)
263 - die("cannot read %s", sha1_to_hex(entry.sha1));
264 -
265 - path->len = pathlen;
266 - strbuf_append_string(path, entry.path);
267 - if (S_ISDIR(entry.mode))
268 - strbuf_append_string(path, "/");
269 -
270 - write_entry(entry.sha1, path, entry.mode, eltbuf, eltsize);
271 -
272 - if (S_ISDIR(entry.mode)) {
273 - struct tree_desc subtree;
274 - subtree.buf = eltbuf;
275 - subtree.size = eltsize;
276 - traverse_tree(&subtree, path);
277 - }
278 - free(eltbuf);
279 - }
280 -}
281 -
250 static int git_tar_config(const char *var, const char *value)
251 {
252 if (!strcmp(var, "tar.umask")) {
@@ -295,51 +263,29 @@ static int git_tar_config(const char *var, const char *value)
263
264 static int generate_tar(int argc, const char **argv, const char *prefix)
265 {
298 - unsigned char sha1[20], tree_sha1[20];
299 - struct commit *commit;
300 - struct tree_desc tree;
301 - struct strbuf current_path;
302 - void *buffer;
303 -
304 - current_path.buf = xmalloc(PATH_MAX);
305 - current_path.alloc = PATH_MAX;
306 - current_path.len = current_path.eof = 0;
266 + struct archiver_args args;
267 + int result;
268 + char *base = NULL;
269
270 git_config(git_tar_config);
271
310 - switch (argc) {
311 - case 3:
312 - strbuf_append_string(&current_path, argv[2]);
313 - strbuf_append_string(&current_path, "/");
314 - /* FALLTHROUGH */
315 - case 2:
316 - if (get_sha1(argv[1], sha1))
317 - die("Not a valid object name %s", argv[1]);
318 - break;
319 - default:
272 + memset(&args, 0, sizeof(args));
273 + if (argc != 2 && argc != 3)
274 usage(tar_tree_usage);
275 + if (argc == 3) {
276 + int baselen = strlen(argv[2]);
277 + base = xmalloc(baselen + 2);
278 + memcpy(base, argv[2], baselen);
279 + base[baselen] = '/';
280 + base[baselen + 1] = '\0';
281 }
282 + args.base = base;
283 + parse_treeish_arg(argv + 1, &args, NULL);
284
323 - commit = lookup_commit_reference_gently(sha1, 1);
324 - if (commit) {
325 - write_global_extended_header(commit->object.sha1);
326 - archive_time = commit->date;
327 - } else
328 - archive_time = time(NULL);
329 -
330 - tree.buf = buffer = read_object_with_reference(sha1, tree_type,
331 - &tree.size, tree_sha1);
332 - if (!tree.buf)
333 - die("not a reference to a tag, commit or tree object: %s",
334 - sha1_to_hex(sha1));
335 -
336 - if (current_path.len > 0)
337 - write_entry(tree_sha1, &current_path, 040777, NULL, 0);
338 - traverse_tree(&tree, &current_path);
339 - write_trailer();
340 - free(buffer);
341 - free(current_path.buf);
342 - return 0;
285 + result = write_tar_archive(&args);
286 + free(base);
287 +
288 + return result;
289 }
290
291 static int write_tar_entry(const unsigned char *sha1,
@@ -383,7 +329,7 @@ static int write_tar_entry(const unsigned char *sha1,
329
330 int write_tar_archive(struct archiver_args *args)
331 {
386 - int plen = strlen(args->base);
332 + int plen = args->base ? strlen(args->base) : 0;
333
334 git_config(git_tar_config);
335