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[] = {
20 .write_archive = write_tar_archive,
24 .write_archive = write_zip_archive,
25 .parse_extra = parse_extra_zip_args,
29 static int run_remote_archiver(struct archiver *ar, int argc,
33 int fd[2], i, len, rv;
36 sprintf(buf, "git-upload-archive");
38 url = xstrdup(ar->remote);
39 pid = git_connect(fd, url, buf);
43 for (i = 1; i < argc; i++) {
44 if (!strncmp(argv[i], "--remote=", 9))
46 packet_write(fd[1], "argument %s\n", argv[i]);
50 len = packet_read_line(fd[0], buf, sizeof(buf));
52 die("git-archive: expected ACK/NAK, got EOF");
53 if (buf[len-1] == '\n')
55 if (strcmp(buf, "ACK")) {
56 if (len > 5 && !strncmp(buf, "NACK ", 5))
57 die("git-archive: NACK %s", buf + 5);
58 die("git-archive: protocol error");
61 len = packet_read_line(fd[0], buf, sizeof(buf));
63 die("git-archive: expected a flush");
65 /* Now, start reading from fd[0] and spit it out to stdout */
66 rv = copy_fd(fd[0], 1);
69 rv |= finish_connect(pid);
74 static int init_archiver(const char *name, struct archiver *ar)
78 for (i = 0; i < ARRAY_SIZE(archivers); i++) {
79 if (!strcmp(name, archivers[i].name)) {
80 memcpy(ar, &archivers[i], sizeof(struct archiver));
88 void parse_pathspec_arg(const char **pathspec, struct archiver_args *ar_args)
90 ar_args->pathspec = get_pathspec(ar_args->base, pathspec);
93 void parse_treeish_arg(const char **argv, struct archiver_args *ar_args,
96 const char *name = argv[0];
97 const unsigned char *commit_sha1;
100 struct commit *commit;
101 unsigned char sha1[20];
103 if (get_sha1(name, sha1))
104 die("Not a valid object name");
106 commit = lookup_commit_reference_gently(sha1, 1);
108 commit_sha1 = commit->object.sha1;
109 archive_time = commit->date;
112 archive_time = time(NULL);
115 tree = parse_tree_indirect(sha1);
117 die("not a tree object");
120 unsigned char tree_sha1[20];
124 err = get_tree_entry(tree->object.sha1, prefix,
126 if (err || !S_ISDIR(mode))
127 die("current working directory is untracked");
130 tree = parse_tree_indirect(tree_sha1);
132 ar_args->tree = tree;
133 ar_args->commit_sha1 = commit_sha1;
134 ar_args->time = archive_time;
137 static const char *default_parse_extra(struct archiver *ar,
142 snprintf(msg, sizeof(msg) - 4, "'%s' format does not handle %s",
145 return strcat(msg, "...");
148 int parse_archive_args(int argc, const char **argv, struct archiver *ar)
150 const char *extra_argv[MAX_EXTRA_ARGS];
152 const char *format = NULL; /* might want to default to "tar" */
153 const char *remote = NULL;
154 const char *base = "";
158 for (i = 1; i < argc; i++) {
159 const char *arg = argv[i];
161 if (!strcmp(arg, "--list") || !strcmp(arg, "-l")) {
165 if (!strncmp(arg, "--format=", 9)) {
169 if (!strncmp(arg, "--prefix=", 9)) {
173 if (!strncmp(arg, "--remote=", 9)) {
177 if (!strcmp(arg, "--")) {
182 if (extra_argc > MAX_EXTRA_ARGS - 1)
183 die("Too many extra options");
184 extra_argv[extra_argc++] = arg;
192 for (i = 0; i < ARRAY_SIZE(archivers); i++)
193 printf("%s\n", archivers[i].name);
196 die("--list and --remote are mutually exclusive");
200 usage(archive_usage);
202 die("You must specify an archive format");
203 if (init_archiver(format, ar) < 0)
204 die("Unknown archive format '%s'", format);
206 if (extra_argc && !remote) {
207 if (!ar->parse_extra) {
208 die("%s", default_parse_extra(ar, extra_argv));
210 ar->args.extra = ar->parse_extra(extra_argc, extra_argv);
213 ar->args.base = base;
218 int cmd_archive(int argc, const char **argv, const char *prefix)
223 tree_idx = parse_archive_args(argc, argv, &ar);
226 return run_remote_archiver(&ar, argc, argv);
229 prefix = setup_git_directory();
232 parse_treeish_arg(argv, &ar.args, prefix);
233 parse_pathspec_arg(argv + 1, &ar.args);
235 return ar.write_archive(&ar.args);