1 /*
2 * Copyright (c) 2006 Franck Bui-Huu
3 * Copyright (c) 2006 Rene Scharfe
4 */
5 #include <time.h>
6 #include "cache.h"
7 #include "builtin.h"
8 #include "archive.h"
9 #include "commit.h"
10 #include "tree-walk.h"
11 #include "exec_cmd.h"
12 #include "pkt-line.h"
14 static const char archive_usage[] = \
15 "git-archive --format=<fmt> [--prefix=<prefix>/] [<extra>] <tree-ish> [path...]";
17 struct archiver archivers[] = {
18 {
19 .name = "tar",
20 .write_archive = write_tar_archive,
21 },
22 {
23 .name = "zip",
24 .write_archive = write_zip_archive,
25 .parse_extra = parse_extra_zip_args,
26 },
27 };
29 static int run_remote_archiver(const char *remote, int argc,
30 const char **argv)
31 {
32 char *url, buf[1024];
33 int fd[2], i, len, rv;
34 pid_t pid;
36 sprintf(buf, "git-upload-archive");
38 url = xstrdup(remote);
39 pid = git_connect(fd, url, buf);
40 if (pid < 0)
41 return pid;
43 for (i = 1; i < argc; i++)
44 packet_write(fd[1], "argument %s\n", argv[i]);
45 packet_flush(fd[1]);
47 len = packet_read_line(fd[0], buf, sizeof(buf));
48 if (!len)
49 die("git-archive: expected ACK/NAK, got EOF");
50 if (buf[len-1] == '\n')
51 buf[--len] = 0;
52 if (strcmp(buf, "ACK")) {
53 if (len > 5 && !strncmp(buf, "NACK ", 5))
54 die("git-archive: NACK %s", buf + 5);
55 die("git-archive: protocol error");
56 }
58 len = packet_read_line(fd[0], buf, sizeof(buf));
59 if (len)
60 die("git-archive: expected a flush");
62 /* Now, start reading from fd[0] and spit it out to stdout */
63 rv = copy_fd(fd[0], 1);
65 close(fd[0]);
66 rv |= finish_connect(pid);
68 return !!rv;
69 }
71 static int init_archiver(const char *name, struct archiver *ar)
72 {
73 int rv = -1, i;
75 for (i = 0; i < ARRAY_SIZE(archivers); i++) {
76 if (!strcmp(name, archivers[i].name)) {
77 memcpy(ar, &archivers[i], sizeof(struct archiver));
78 rv = 0;
79 break;
80 }
81 }
82 return rv;
83 }
85 void parse_pathspec_arg(const char **pathspec, struct archiver_args *ar_args)
86 {
87 ar_args->pathspec = get_pathspec(ar_args->base, pathspec);
88 }
90 void parse_treeish_arg(const char **argv, struct archiver_args *ar_args,
91 const char *prefix)
92 {
93 const char *name = argv[0];
94 const unsigned char *commit_sha1;
95 time_t archive_time;
96 struct tree *tree;
97 struct commit *commit;
98 unsigned char sha1[20];
100 if (get_sha1(name, sha1))
101 die("Not a valid object name");
103 commit = lookup_commit_reference_gently(sha1, 1);
104 if (commit) {
105 commit_sha1 = commit->object.sha1;
106 archive_time = commit->date;
107 } else {
108 commit_sha1 = NULL;
109 archive_time = time(NULL);
110 }
112 tree = parse_tree_indirect(sha1);
113 if (tree == NULL)
114 die("not a tree object");
116 if (prefix) {
117 unsigned char tree_sha1[20];
118 unsigned int mode;
119 int err;
121 err = get_tree_entry(tree->object.sha1, prefix,
122 tree_sha1, &mode);
123 if (err || !S_ISDIR(mode))
124 die("current working directory is untracked");
126 free(tree);
127 tree = parse_tree_indirect(tree_sha1);
128 }
129 ar_args->tree = tree;
130 ar_args->commit_sha1 = commit_sha1;
131 ar_args->time = archive_time;
132 }
134 static const char *default_parse_extra(struct archiver *ar,
135 const char **argv)
136 {
137 static char msg[64];
139 snprintf(msg, sizeof(msg) - 4, "'%s' format does not handle %s",
140 ar->name, *argv);
142 return strcat(msg, "...");
143 }
145 int parse_archive_args(int argc, const char **argv, struct archiver *ar)
146 {
147 const char *extra_argv[MAX_EXTRA_ARGS];
148 int extra_argc = 0;
149 const char *format = NULL; /* might want to default to "tar" */
150 const char *base = "";
151 int i;
153 for (i = 1; i < argc; i++) {
154 const char *arg = argv[i];
156 if (!strcmp(arg, "--list") || !strcmp(arg, "-l")) {
157 for (i = 0; i < ARRAY_SIZE(archivers); i++)
158 printf("%s\n", archivers[i].name);
159 exit(0);
160 }
161 if (!strncmp(arg, "--format=", 9)) {
162 format = arg + 9;
163 continue;
164 }
165 if (!strncmp(arg, "--prefix=", 9)) {
166 base = arg + 9;
167 continue;
168 }
169 if (!strcmp(arg, "--")) {
170 i++;
171 break;
172 }
173 if (arg[0] == '-') {
174 if (extra_argc > MAX_EXTRA_ARGS - 1)
175 die("Too many extra options");
176 extra_argv[extra_argc++] = arg;
177 continue;
178 }
179 break;
180 }
182 /* We need at least one parameter -- tree-ish */
183 if (argc - 1 < i)
184 usage(archive_usage);
185 if (!format)
186 die("You must specify an archive format");
187 if (init_archiver(format, ar) < 0)
188 die("Unknown archive format '%s'", format);
190 if (extra_argc) {
191 if (!ar->parse_extra)
192 die("%s", default_parse_extra(ar, extra_argv));
193 ar->args.extra = ar->parse_extra(extra_argc, extra_argv);
194 }
195 ar->args.base = base;
197 return i;
198 }
200 static const char *remote_request(int *ac, const char **av)
201 {
202 int ix, iy, cnt = *ac;
203 int no_more_options = 0;
204 const char *remote = NULL;
206 for (ix = iy = 1; ix < cnt; ix++) {
207 const char *arg = av[ix];
208 if (!strcmp(arg, "--"))
209 no_more_options = 1;
210 if (!no_more_options) {
211 if (!strncmp(arg, "--remote=", 9)) {
212 if (remote)
213 die("Multiple --remote specified");
214 remote = arg + 9;
215 continue;
216 }
217 if (arg[0] != '-')
218 no_more_options = 1;
219 }
220 if (ix != iy)
221 av[iy] = arg;
222 iy++;
223 }
224 if (remote) {
225 av[--cnt] = NULL;
226 *ac = cnt;
227 }
228 return remote;
229 }
231 int cmd_archive(int argc, const char **argv, const char *prefix)
232 {
233 struct archiver ar;
234 int tree_idx;
235 const char *remote = NULL;
237 remote = remote_request(&argc, argv);
238 if (remote)
239 return run_remote_archiver(remote, argc, argv);
241 memset(&ar, 0, sizeof(ar));
242 tree_idx = parse_archive_args(argc, argv, &ar);
243 if (prefix == NULL)
244 prefix = setup_git_directory();
246 argv += tree_idx;
247 parse_treeish_arg(argv, &ar.args, prefix);
248 parse_pathspec_arg(argv + 1, &ar.args);
250 return ar.write_archive(&ar.args);
251 }