2 * Copyright (c) 2006 Franck Bui-Huu
3 * Copyright (c) 2006 Rene Scharfe
10 #include "tree-walk.h"
14 static const char archive_usage[] = \
15 "git-archive --format=<fmt> [--prefix=<prefix>/] [<extra>] <tree-ish> [path...]";
17 struct archiver archivers[] = {
18 { .name = "tar", .write_archive = write_tar_archive },
19 { .name = "zip", .write_archive = write_zip_archive },
22 static int run_remote_archiver(struct archiver *ar, int argc,
26 int fd[2], i, len, rv;
29 sprintf(buf, "git-upload-archive");
31 url = xstrdup(ar->remote);
32 pid = git_connect(fd, url, buf);
36 for (i = 1; i < argc; i++) {
37 if (!strncmp(argv[i], "--remote=", 9))
39 packet_write(fd[1], "argument %s\n", argv[i]);
43 len = packet_read_line(fd[0], buf, sizeof(buf));
45 die("git-archive: expected ACK/NAK, got EOF");
46 if (buf[len-1] == '\n')
48 if (strcmp(buf, "ACK")) {
49 if (len > 5 && !strncmp(buf, "NACK ", 5))
50 die("git-archive: NACK %s", buf + 5);
51 die("git-archive: protocol error");
54 len = packet_read_line(fd[0], buf, sizeof(buf));
56 die("git-archive: expected a flush");
58 /* Now, start reading from fd[0] and spit it out to stdout */
59 rv = copy_fd(fd[0], 1);
62 rv |= finish_connect(pid);
67 static int init_archiver(const char *name, struct archiver *ar)
71 for (i = 0; i < ARRAY_SIZE(archivers); i++) {
72 if (!strcmp(name, archivers[i].name)) {
73 memcpy(ar, &archivers[i], sizeof(struct archiver));
81 void parse_pathspec_arg(const char **pathspec, struct archiver_args *ar_args)
83 ar_args->pathspec = get_pathspec(ar_args->base, pathspec);
86 void parse_treeish_arg(const char **argv, struct archiver_args *ar_args,
89 const char *name = argv[0];
90 const unsigned char *commit_sha1;
93 struct commit *commit;
94 unsigned char sha1[20];
96 if (get_sha1(name, sha1))
97 die("Not a valid object name");
99 commit = lookup_commit_reference_gently(sha1, 1);
101 commit_sha1 = commit->object.sha1;
102 archive_time = commit->date;
105 archive_time = time(NULL);
108 tree = parse_tree_indirect(sha1);
110 die("not a tree object");
113 unsigned char tree_sha1[20];
117 err = get_tree_entry(tree->object.sha1, prefix,
119 if (err || !S_ISDIR(mode))
120 die("current working directory is untracked");
123 tree = parse_tree_indirect(tree_sha1);
125 ar_args->tree = tree;
126 ar_args->commit_sha1 = commit_sha1;
127 ar_args->time = archive_time;
130 static const char *default_parse_extra(struct archiver *ar,
135 snprintf(msg, sizeof(msg) - 4, "'%s' format does not handle %s",
138 return strcat(msg, "...");
141 int parse_archive_args(int argc, const char **argv, struct archiver *ar)
143 const char *extra_argv[MAX_EXTRA_ARGS];
145 const char *format = NULL; /* might want to default to "tar" */
146 const char *remote = NULL;
147 const char *base = "";
151 for (i = 1; i < argc; i++) {
152 const char *arg = argv[i];
154 if (!strcmp(arg, "--list") || !strcmp(arg, "-l")) {
158 if (!strncmp(arg, "--format=", 9)) {
162 if (!strncmp(arg, "--prefix=", 9)) {
166 if (!strncmp(arg, "--remote=", 9)) {
170 if (!strcmp(arg, "--")) {
175 if (extra_argc > MAX_EXTRA_ARGS - 1)
176 die("Too many extra options");
177 extra_argv[extra_argc++] = arg;
185 for (i = 0; i < ARRAY_SIZE(archivers); i++)
186 printf("%s\n", archivers[i].name);
189 die("--list and --remote are mutually exclusive");
193 usage(archive_usage);
195 die("You must specify an archive format");
196 if (init_archiver(format, ar) < 0)
197 die("Unknown archive format '%s'", format);
199 if (extra_argc && !remote) {
200 if (!ar->parse_extra) {
201 die("%s", default_parse_extra(ar, extra_argv));
203 ar->args.extra = ar->parse_extra(extra_argc, extra_argv);
206 ar->args.base = base;
211 int cmd_archive(int argc, const char **argv, const char *prefix)
216 tree_idx = parse_archive_args(argc, argv, &ar);
219 return run_remote_archiver(&ar, argc, argv);
222 prefix = setup_git_directory();
225 parse_treeish_arg(argv, &ar.args, prefix);
226 parse_pathspec_arg(argv + 1, &ar.args);
228 return ar.write_archive(&ar.args);