]>
Commit | Line | Data |
---|---|---|
4df096a5 FBH |
1 | /* |
2 | * Copyright (c) 2006 Franck Bui-Huu | |
3 | * Copyright (c) 2006 Rene Scharfe | |
4 | */ | |
4df096a5 FBH |
5 | #include "cache.h" |
6 | #include "builtin.h" | |
7 | #include "archive.h" | |
8 | #include "commit.h" | |
9 | #include "tree-walk.h" | |
10 | #include "exec_cmd.h" | |
11 | #include "pkt-line.h" | |
23d6d112 | 12 | #include "sideband.h" |
8460b2fc | 13 | #include "attr.h" |
4df096a5 FBH |
14 | |
15 | static const char archive_usage[] = \ | |
e0ffb248 | 16 | "git-archive --format=<fmt> [--prefix=<prefix>/] [--verbose] [<extra>] <tree-ish> [path...]"; |
4df096a5 | 17 | |
6c2f207b SP |
18 | static struct archiver_desc |
19 | { | |
20 | const char *name; | |
21 | write_archive_fn_t write_archive; | |
22 | parse_extra_args_fn_t parse_extra; | |
23 | } archivers[] = { | |
24 | { "tar", write_tar_archive, NULL }, | |
25 | { "zip", write_zip_archive, parse_extra_zip_args }, | |
4df096a5 FBH |
26 | }; |
27 | ||
37f94436 | 28 | static int run_remote_archiver(const char *remote, int argc, |
4df096a5 FBH |
29 | const char **argv) |
30 | { | |
23d6d112 | 31 | char *url, buf[LARGE_PACKET_MAX]; |
4df096a5 FBH |
32 | int fd[2], i, len, rv; |
33 | pid_t pid; | |
fe5ab763 JH |
34 | const char *exec = "git-upload-archive"; |
35 | int exec_at = 0; | |
4df096a5 | 36 | |
fe5ab763 JH |
37 | for (i = 1; i < argc; i++) { |
38 | const char *arg = argv[i]; | |
599065a3 | 39 | if (!prefixcmp(arg, "--exec=")) { |
fe5ab763 JH |
40 | if (exec_at) |
41 | die("multiple --exec specified"); | |
42 | exec = arg + 7; | |
43 | exec_at = i; | |
44 | break; | |
45 | } | |
46 | } | |
4df096a5 | 47 | |
37f94436 | 48 | url = xstrdup(remote); |
7841ce79 | 49 | pid = git_connect(fd, url, exec, 0); |
4df096a5 FBH |
50 | if (pid < 0) |
51 | return pid; | |
52 | ||
fe5ab763 JH |
53 | for (i = 1; i < argc; i++) { |
54 | if (i == exec_at) | |
55 | continue; | |
4df096a5 | 56 | packet_write(fd[1], "argument %s\n", argv[i]); |
fe5ab763 | 57 | } |
4df096a5 FBH |
58 | packet_flush(fd[1]); |
59 | ||
60 | len = packet_read_line(fd[0], buf, sizeof(buf)); | |
61 | if (!len) | |
62 | die("git-archive: expected ACK/NAK, got EOF"); | |
63 | if (buf[len-1] == '\n') | |
64 | buf[--len] = 0; | |
65 | if (strcmp(buf, "ACK")) { | |
cc44c765 | 66 | if (len > 5 && !prefixcmp(buf, "NACK ")) |
4df096a5 FBH |
67 | die("git-archive: NACK %s", buf + 5); |
68 | die("git-archive: protocol error"); | |
69 | } | |
70 | ||
71 | len = packet_read_line(fd[0], buf, sizeof(buf)); | |
72 | if (len) | |
73 | die("git-archive: expected a flush"); | |
74 | ||
75 | /* Now, start reading from fd[0] and spit it out to stdout */ | |
9ac13ec9 | 76 | rv = recv_sideband("archive", fd[0], 1, 2); |
4df096a5 | 77 | close(fd[0]); |
ec587fde | 78 | close(fd[1]); |
4df096a5 FBH |
79 | rv |= finish_connect(pid); |
80 | ||
81 | return !!rv; | |
82 | } | |
83 | ||
38c9c9b7 RS |
84 | static void *format_subst(const struct commit *commit, const char *format, |
85 | unsigned long *sizep) | |
df4a394f RS |
86 | { |
87 | unsigned long len = *sizep, result_len = 0; | |
88 | const char *a = format; | |
89 | char *result = NULL; | |
90 | ||
91 | for (;;) { | |
92 | const char *b, *c; | |
93 | char *fmt, *formatted = NULL; | |
94 | unsigned long a_len, fmt_len, formatted_len, allocated = 0; | |
95 | ||
96 | b = memmem(a, len, "$Format:", 8); | |
97 | if (!b || a + len < b + 9) | |
98 | break; | |
99 | c = memchr(b + 8, '$', len - 8); | |
100 | if (!c) | |
101 | break; | |
102 | ||
103 | a_len = b - a; | |
104 | fmt_len = c - b - 8; | |
105 | fmt = xmalloc(fmt_len + 1); | |
106 | memcpy(fmt, b + 8, fmt_len); | |
107 | fmt[fmt_len] = '\0'; | |
108 | ||
109 | formatted_len = format_commit_message(commit, fmt, &formatted, | |
110 | &allocated); | |
20fbfd86 | 111 | free(fmt); |
df4a394f RS |
112 | result = xrealloc(result, result_len + a_len + formatted_len); |
113 | memcpy(result + result_len, a, a_len); | |
114 | memcpy(result + result_len + a_len, formatted, formatted_len); | |
115 | result_len += a_len + formatted_len; | |
116 | len -= c + 1 - a; | |
117 | a = c + 1; | |
118 | } | |
119 | ||
120 | if (result && len) { | |
121 | result = xrealloc(result, result_len + len); | |
122 | memcpy(result + result_len, a, len); | |
123 | result_len += len; | |
124 | } | |
125 | ||
126 | *sizep = result_len; | |
127 | ||
128 | return result; | |
129 | } | |
130 | ||
8460b2fc RS |
131 | static void *convert_to_archive(const char *path, |
132 | const void *src, unsigned long *sizep, | |
133 | const struct commit *commit) | |
134 | { | |
38c9c9b7 | 135 | static struct git_attr *attr_export_subst; |
8460b2fc | 136 | struct git_attr_check check[1]; |
8460b2fc RS |
137 | |
138 | if (!commit) | |
139 | return NULL; | |
140 | ||
38c9c9b7 RS |
141 | if (!attr_export_subst) |
142 | attr_export_subst = git_attr("export-subst", 12); | |
8460b2fc | 143 | |
38c9c9b7 | 144 | check[0].attr = attr_export_subst; |
8460b2fc RS |
145 | if (git_checkattr(path, ARRAY_SIZE(check), check)) |
146 | return NULL; | |
147 | if (!ATTR_TRUE(check[0].value)) | |
148 | return NULL; | |
149 | ||
38c9c9b7 | 150 | return format_subst(commit, src, sizep); |
8460b2fc RS |
151 | } |
152 | ||
153 | void *sha1_file_to_archive(const char *path, const unsigned char *sha1, | |
154 | unsigned int mode, enum object_type *type, | |
155 | unsigned long *size, | |
156 | const struct commit *commit) | |
157 | { | |
158 | void *buffer, *converted; | |
159 | ||
160 | buffer = read_sha1_file(sha1, type, size); | |
161 | if (buffer && S_ISREG(mode)) { | |
162 | converted = convert_to_working_tree(path, buffer, size); | |
163 | if (converted) { | |
164 | free(buffer); | |
165 | buffer = converted; | |
166 | } | |
167 | ||
168 | converted = convert_to_archive(path, buffer, size, commit); | |
169 | if (converted) { | |
170 | free(buffer); | |
171 | buffer = converted; | |
172 | } | |
173 | } | |
174 | ||
175 | return buffer; | |
176 | } | |
177 | ||
4df096a5 FBH |
178 | static int init_archiver(const char *name, struct archiver *ar) |
179 | { | |
180 | int rv = -1, i; | |
181 | ||
182 | for (i = 0; i < ARRAY_SIZE(archivers); i++) { | |
183 | if (!strcmp(name, archivers[i].name)) { | |
6c2f207b SP |
184 | memset(ar, 0, sizeof(*ar)); |
185 | ar->name = archivers[i].name; | |
186 | ar->write_archive = archivers[i].write_archive; | |
187 | ar->parse_extra = archivers[i].parse_extra; | |
4df096a5 FBH |
188 | rv = 0; |
189 | break; | |
190 | } | |
191 | } | |
192 | return rv; | |
193 | } | |
194 | ||
195 | void parse_pathspec_arg(const char **pathspec, struct archiver_args *ar_args) | |
196 | { | |
197 | ar_args->pathspec = get_pathspec(ar_args->base, pathspec); | |
198 | } | |
199 | ||
200 | void parse_treeish_arg(const char **argv, struct archiver_args *ar_args, | |
201 | const char *prefix) | |
202 | { | |
203 | const char *name = argv[0]; | |
204 | const unsigned char *commit_sha1; | |
205 | time_t archive_time; | |
206 | struct tree *tree; | |
8460b2fc | 207 | const struct commit *commit; |
4df096a5 FBH |
208 | unsigned char sha1[20]; |
209 | ||
210 | if (get_sha1(name, sha1)) | |
211 | die("Not a valid object name"); | |
212 | ||
213 | commit = lookup_commit_reference_gently(sha1, 1); | |
214 | if (commit) { | |
215 | commit_sha1 = commit->object.sha1; | |
216 | archive_time = commit->date; | |
217 | } else { | |
218 | commit_sha1 = NULL; | |
219 | archive_time = time(NULL); | |
220 | } | |
221 | ||
222 | tree = parse_tree_indirect(sha1); | |
223 | if (tree == NULL) | |
224 | die("not a tree object"); | |
225 | ||
226 | if (prefix) { | |
227 | unsigned char tree_sha1[20]; | |
228 | unsigned int mode; | |
229 | int err; | |
230 | ||
231 | err = get_tree_entry(tree->object.sha1, prefix, | |
232 | tree_sha1, &mode); | |
233 | if (err || !S_ISDIR(mode)) | |
234 | die("current working directory is untracked"); | |
235 | ||
4df096a5 FBH |
236 | tree = parse_tree_indirect(tree_sha1); |
237 | } | |
238 | ar_args->tree = tree; | |
239 | ar_args->commit_sha1 = commit_sha1; | |
8460b2fc | 240 | ar_args->commit = commit; |
4df096a5 FBH |
241 | ar_args->time = archive_time; |
242 | } | |
243 | ||
4df096a5 FBH |
244 | int parse_archive_args(int argc, const char **argv, struct archiver *ar) |
245 | { | |
246 | const char *extra_argv[MAX_EXTRA_ARGS]; | |
247 | int extra_argc = 0; | |
8ff21b1a | 248 | const char *format = "tar"; |
4df096a5 | 249 | const char *base = ""; |
e0ffb248 | 250 | int verbose = 0; |
4df096a5 FBH |
251 | int i; |
252 | ||
253 | for (i = 1; i < argc; i++) { | |
254 | const char *arg = argv[i]; | |
255 | ||
256 | if (!strcmp(arg, "--list") || !strcmp(arg, "-l")) { | |
37f94436 JH |
257 | for (i = 0; i < ARRAY_SIZE(archivers); i++) |
258 | printf("%s\n", archivers[i].name); | |
259 | exit(0); | |
4df096a5 | 260 | } |
e0ffb248 JH |
261 | if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) { |
262 | verbose = 1; | |
263 | continue; | |
264 | } | |
cc44c765 | 265 | if (!prefixcmp(arg, "--format=")) { |
4df096a5 FBH |
266 | format = arg + 9; |
267 | continue; | |
268 | } | |
cc44c765 | 269 | if (!prefixcmp(arg, "--prefix=")) { |
4df096a5 FBH |
270 | base = arg + 9; |
271 | continue; | |
272 | } | |
4df096a5 FBH |
273 | if (!strcmp(arg, "--")) { |
274 | i++; | |
275 | break; | |
276 | } | |
277 | if (arg[0] == '-') { | |
278 | if (extra_argc > MAX_EXTRA_ARGS - 1) | |
279 | die("Too many extra options"); | |
280 | extra_argv[extra_argc++] = arg; | |
281 | continue; | |
282 | } | |
283 | break; | |
284 | } | |
285 | ||
37f94436 JH |
286 | /* We need at least one parameter -- tree-ish */ |
287 | if (argc - 1 < i) | |
4df096a5 | 288 | usage(archive_usage); |
4df096a5 FBH |
289 | if (init_archiver(format, ar) < 0) |
290 | die("Unknown archive format '%s'", format); | |
291 | ||
37f94436 JH |
292 | if (extra_argc) { |
293 | if (!ar->parse_extra) | |
2232c0c6 RS |
294 | die("'%s' format does not handle %s", |
295 | ar->name, extra_argv[0]); | |
4df096a5 FBH |
296 | ar->args.extra = ar->parse_extra(extra_argc, extra_argv); |
297 | } | |
e0ffb248 | 298 | ar->args.verbose = verbose; |
4df096a5 FBH |
299 | ar->args.base = base; |
300 | ||
301 | return i; | |
302 | } | |
303 | ||
d751864c | 304 | static const char *extract_remote_arg(int *ac, const char **av) |
37f94436 JH |
305 | { |
306 | int ix, iy, cnt = *ac; | |
307 | int no_more_options = 0; | |
308 | const char *remote = NULL; | |
309 | ||
310 | for (ix = iy = 1; ix < cnt; ix++) { | |
311 | const char *arg = av[ix]; | |
312 | if (!strcmp(arg, "--")) | |
313 | no_more_options = 1; | |
314 | if (!no_more_options) { | |
cc44c765 | 315 | if (!prefixcmp(arg, "--remote=")) { |
37f94436 JH |
316 | if (remote) |
317 | die("Multiple --remote specified"); | |
318 | remote = arg + 9; | |
319 | continue; | |
320 | } | |
321 | if (arg[0] != '-') | |
322 | no_more_options = 1; | |
323 | } | |
324 | if (ix != iy) | |
325 | av[iy] = arg; | |
326 | iy++; | |
327 | } | |
328 | if (remote) { | |
329 | av[--cnt] = NULL; | |
330 | *ac = cnt; | |
331 | } | |
332 | return remote; | |
333 | } | |
334 | ||
4df096a5 FBH |
335 | int cmd_archive(int argc, const char **argv, const char *prefix) |
336 | { | |
337 | struct archiver ar; | |
338 | int tree_idx; | |
37f94436 | 339 | const char *remote = NULL; |
4df096a5 | 340 | |
d751864c | 341 | remote = extract_remote_arg(&argc, argv); |
37f94436 JH |
342 | if (remote) |
343 | return run_remote_archiver(remote, argc, argv); | |
4df096a5 | 344 | |
aa909861 | 345 | setvbuf(stderr, NULL, _IOLBF, BUFSIZ); |
8142f603 | 346 | |
37f94436 JH |
347 | memset(&ar, 0, sizeof(ar)); |
348 | tree_idx = parse_archive_args(argc, argv, &ar); | |
265d5280 RS |
349 | if (prefix == NULL) |
350 | prefix = setup_git_directory(); | |
4df096a5 FBH |
351 | |
352 | argv += tree_idx; | |
353 | parse_treeish_arg(argv, &ar.args, prefix); | |
354 | parse_pathspec_arg(argv + 1, &ar.args); | |
355 | ||
356 | return ar.write_archive(&ar.args); | |
357 | } |