2 * Copyright (c) 2006 Franck Bui-Huu
3 * Copyright (c) 2006 Rene Scharfe
15 static const char archive_usage[] = \
16 "git archive --format=<fmt> [--prefix=<prefix>/] [--verbose] [<extra>] <tree-ish> [path...]";
18 #define USES_ZLIB_COMPRESSION 1
20 const struct archiver archivers[] = {
21 { "tar", write_tar_archive },
22 { "zip", write_zip_archive, USES_ZLIB_COMPRESSION },
25 static int run_remote_archiver(const char *remote, int argc,
28 char *url, buf[LARGE_PACKET_MAX];
29 int fd[2], i, len, rv;
30 struct child_process *conn;
31 const char *exec = "git-upload-archive";
34 for (i = 1; i < argc; i++) {
35 const char *arg = argv[i];
36 if (!prefixcmp(arg, "--exec=")) {
38 die("multiple --exec specified");
45 url = xstrdup(remote);
46 conn = git_connect(fd, url, exec, 0);
48 for (i = 1; i < argc; i++) {
51 packet_write(fd[1], "argument %s\n", argv[i]);
55 len = packet_read_line(fd[0], buf, sizeof(buf));
57 die("git-archive: expected ACK/NAK, got EOF");
58 if (buf[len-1] == '\n')
60 if (strcmp(buf, "ACK")) {
61 if (len > 5 && !prefixcmp(buf, "NACK "))
62 die("git-archive: NACK %s", buf + 5);
63 die("git-archive: protocol error");
66 len = packet_read_line(fd[0], buf, sizeof(buf));
68 die("git-archive: expected a flush");
70 /* Now, start reading from fd[0] and spit it out to stdout */
71 rv = recv_sideband("archive", fd[0], 1, 2);
74 rv |= finish_connect(conn);
79 static const struct archiver *lookup_archiver(const char *name)
83 for (i = 0; i < ARRAY_SIZE(archivers); i++) {
84 if (!strcmp(name, archivers[i].name))
90 void parse_pathspec_arg(const char **pathspec, struct archiver_args *ar_args)
92 ar_args->pathspec = get_pathspec(ar_args->base, pathspec);
95 void parse_treeish_arg(const char **argv, struct archiver_args *ar_args,
98 const char *name = argv[0];
99 const unsigned char *commit_sha1;
102 const struct commit *commit;
103 unsigned char sha1[20];
105 if (get_sha1(name, sha1))
106 die("Not a valid object name");
108 commit = lookup_commit_reference_gently(sha1, 1);
110 commit_sha1 = commit->object.sha1;
111 archive_time = commit->date;
114 archive_time = time(NULL);
117 tree = parse_tree_indirect(sha1);
119 die("not a tree object");
122 unsigned char tree_sha1[20];
126 err = get_tree_entry(tree->object.sha1, prefix,
128 if (err || !S_ISDIR(mode))
129 die("current working directory is untracked");
131 tree = parse_tree_indirect(tree_sha1);
133 ar_args->tree = tree;
134 ar_args->commit_sha1 = commit_sha1;
135 ar_args->commit = commit;
136 ar_args->time = archive_time;
139 int parse_archive_args(int argc, const char **argv, const struct archiver **ar,
140 struct archiver_args *args)
142 const char *format = "tar";
143 const char *base = "";
144 int compression_level = -1;
148 for (i = 1; i < argc; i++) {
149 const char *arg = argv[i];
151 if (!strcmp(arg, "--list") || !strcmp(arg, "-l")) {
152 for (i = 0; i < ARRAY_SIZE(archivers); i++)
153 printf("%s\n", archivers[i].name);
156 if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) {
160 if (!prefixcmp(arg, "--format=")) {
164 if (!prefixcmp(arg, "--prefix=")) {
168 if (!strcmp(arg, "--")) {
172 if (arg[0] == '-' && isdigit(arg[1]) && arg[2] == '\0') {
173 compression_level = arg[1] - '0';
177 die("Unknown argument: %s", arg);
181 /* We need at least one parameter -- tree-ish */
183 usage(archive_usage);
184 *ar = lookup_archiver(format);
186 die("Unknown archive format '%s'", format);
188 args->compression_level = Z_DEFAULT_COMPRESSION;
189 if (compression_level != -1) {
190 if ((*ar)->flags & USES_ZLIB_COMPRESSION)
191 args->compression_level = compression_level;
193 die("Argument not supported for format '%s': -%d",
194 format, compression_level);
197 args->verbose = verbose;
199 args->baselen = strlen(base);
204 static const char *extract_remote_arg(int *ac, const char **av)
206 int ix, iy, cnt = *ac;
207 int no_more_options = 0;
208 const char *remote = NULL;
210 for (ix = iy = 1; ix < cnt; ix++) {
211 const char *arg = av[ix];
212 if (!strcmp(arg, "--"))
214 if (!no_more_options) {
215 if (!prefixcmp(arg, "--remote=")) {
217 die("Multiple --remote specified");
235 int cmd_archive(int argc, const char **argv, const char *prefix)
237 const struct archiver *ar = NULL;
238 struct archiver_args args;
240 const char *remote = NULL;
242 remote = extract_remote_arg(&argc, argv);
244 return run_remote_archiver(remote, argc, argv);
246 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
248 tree_idx = parse_archive_args(argc, argv, &ar, &args);
250 prefix = setup_git_directory();
253 parse_treeish_arg(argv, &args, prefix);
254 parse_pathspec_arg(argv + 1, &args);
256 return ar->write_archive(&args);