git-archive: wire up TAR format.
This is based on Rene Scharfe's earlier patch, but uses the archiver support introduced by the previous patch. Signed-off-by: Franck Bui-Huu <vagabon.xyz@gmail.com> Acked-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <junkio@cox.net>
Franck Bui-Huu committed
Sep 7, 2006 at 15:12 UTC
efd8696cd7ccfa5042ef710e89fe0d10efdcd085
3 files changed
+72
-1
archive.h
+4
@@ -37,5 +37,9 @@ extern void parse_treeish_arg(const char **treeish,
37
38
extern void parse_pathspec_arg(const char **pathspec,
39
struct archiver_args *args);
40
+/*
41
+ * Archive-format specific backends.
42
+ */
43
+extern int write_tar_archive(struct archiver_args *);
44
45
#endif /* ARCHIVE_H */
builtin-archive.c
+1
-1
@@ -15,7 +15,7 @@ static const char archive_usage[] = \
15
"git-archive --format=<fmt> [--prefix=<prefix>/] [<extra>] <tree-ish> [path...]";
16
17
struct archiver archivers[] = {
18
- { "" /* dummy */ },
18
+ { .name = "tar", .write_archive = write_tar_archive },
19
};
20
21
static int run_remote_archiver(struct archiver *ar, int argc,
builtin-tar-tree.c
+67
@@ -9,6 +9,7 @@
9
#include "tar.h"
10
#include "builtin.h"
11
#include "pkt-line.h"
12
+#include "archive.h"
13
14
#define RECORDSIZE (512)
15
#define BLOCKSIZE (RECORDSIZE * 20)
@@ -338,6 +339,72 @@ static int generate_tar(int argc, const char **argv, const char *prefix)
339
return 0;
340
}
341
342
+static int write_tar_entry(const unsigned char *sha1,
343
+ const char *base, int baselen,
344
+ const char *filename, unsigned mode, int stage)
345
+{
346
+ static struct strbuf path;
347
+ int filenamelen = strlen(filename);
348
+ void *buffer;
349
+ char type[20];
350
+ unsigned long size;
351
+
352
+ if (!path.alloc) {
353
+ path.buf = xmalloc(PATH_MAX);
354
+ path.alloc = PATH_MAX;
355
+ path.len = path.eof = 0;
356
+ }
357
+ if (path.alloc < baselen + filenamelen) {
358
+ free(path.buf);
359
+ path.buf = xmalloc(baselen + filenamelen);
360
+ path.alloc = baselen + filenamelen;
361
+ }
362
+ memcpy(path.buf, base, baselen);
363
+ memcpy(path.buf + baselen, filename, filenamelen);
364
+ path.len = baselen + filenamelen;
365
+ if (S_ISDIR(mode)) {
366
+ strbuf_append_string(&path, "/");
367
+ buffer = NULL;
368
+ size = 0;
369
+ } else {
370
+ buffer = read_sha1_file(sha1, type, &size);
371
+ if (!buffer)
372
+ die("cannot read %s", sha1_to_hex(sha1));
373
+ }
374
+
375
+ write_entry(sha1, &path, mode, buffer, size);
376
+ free(buffer);
377
+
378
+ return READ_TREE_RECURSIVE;
379
+}
380
+
381
+int write_tar_archive(struct archiver_args *args)
382
+{
383
+ int plen = strlen(args->base);
384
+
385
+ git_config(git_tar_config);
386
+
387
+ archive_time = args->time;
388
+
389
+ if (args->commit_sha1)
390
+ write_global_extended_header(args->commit_sha1);
391
+
392
+ if (args->base && plen > 0 && args->base[plen - 1] == '/') {
393
+ char *base = strdup(args->base);
394
+ int baselen = strlen(base);
395
+
396
+ while (baselen > 0 && base[baselen - 1] == '/')
397
+ base[--baselen] = '\0';
398
+ write_tar_entry(args->tree->object.sha1, "", 0, base, 040777, 0);
399
+ free(base);
400
+ }
401
+ read_tree_recursive(args->tree, args->base, plen, 0,
402
+ args->pathspec, write_tar_entry);
403
+ write_trailer();
404
+
405
+ return 0;
406
+}
407
+
408
static const char *exec = "git-upload-tar";
409
410
static int remote_tar(int argc, const char **argv)