]>
Commit | Line | Data |
---|---|---|
1 | #include "cache.h" | |
2 | #include "config.h" | |
3 | #include "refs.h" | |
4 | #include "object-store.h" | |
5 | #include "commit.h" | |
6 | #include "tree-walk.h" | |
7 | #include "attr.h" | |
8 | #include "archive.h" | |
9 | #include "parse-options.h" | |
10 | #include "unpack-trees.h" | |
11 | #include "dir.h" | |
12 | ||
13 | static char const * const archive_usage[] = { | |
14 | N_("git archive [<options>] <tree-ish> [<path>...]"), | |
15 | N_("git archive --list"), | |
16 | N_("git archive --remote <repo> [--exec <cmd>] [<options>] <tree-ish> [<path>...]"), | |
17 | N_("git archive --remote <repo> [--exec <cmd>] --list"), | |
18 | NULL | |
19 | }; | |
20 | ||
21 | static const struct archiver **archivers; | |
22 | static int nr_archivers; | |
23 | static int alloc_archivers; | |
24 | static int remote_allow_unreachable; | |
25 | ||
26 | void register_archiver(struct archiver *ar) | |
27 | { | |
28 | ALLOC_GROW(archivers, nr_archivers + 1, alloc_archivers); | |
29 | archivers[nr_archivers++] = ar; | |
30 | } | |
31 | ||
32 | void init_archivers(void) | |
33 | { | |
34 | init_tar_archiver(); | |
35 | init_zip_archiver(); | |
36 | } | |
37 | ||
38 | static void format_subst(const struct commit *commit, | |
39 | const char *src, size_t len, | |
40 | struct strbuf *buf) | |
41 | { | |
42 | char *to_free = NULL; | |
43 | struct strbuf fmt = STRBUF_INIT; | |
44 | struct pretty_print_context ctx = {0}; | |
45 | ctx.date_mode.type = DATE_NORMAL; | |
46 | ctx.abbrev = DEFAULT_ABBREV; | |
47 | ||
48 | if (src == buf->buf) | |
49 | to_free = strbuf_detach(buf, NULL); | |
50 | for (;;) { | |
51 | const char *b, *c; | |
52 | ||
53 | b = memmem(src, len, "$Format:", 8); | |
54 | if (!b) | |
55 | break; | |
56 | c = memchr(b + 8, '$', (src + len) - b - 8); | |
57 | if (!c) | |
58 | break; | |
59 | ||
60 | strbuf_reset(&fmt); | |
61 | strbuf_add(&fmt, b + 8, c - b - 8); | |
62 | ||
63 | strbuf_add(buf, src, b - src); | |
64 | format_commit_message(commit, fmt.buf, buf, &ctx); | |
65 | len -= c + 1 - src; | |
66 | src = c + 1; | |
67 | } | |
68 | strbuf_add(buf, src, len); | |
69 | strbuf_release(&fmt); | |
70 | free(to_free); | |
71 | } | |
72 | ||
73 | void *object_file_to_archive(const struct archiver_args *args, | |
74 | const char *path, const struct object_id *oid, | |
75 | unsigned int mode, enum object_type *type, | |
76 | unsigned long *sizep) | |
77 | { | |
78 | void *buffer; | |
79 | const struct commit *commit = args->convert ? args->commit : NULL; | |
80 | struct checkout_metadata meta; | |
81 | ||
82 | init_checkout_metadata(&meta, args->refname, | |
83 | args->commit_oid ? args->commit_oid : | |
84 | (args->tree ? &args->tree->object.oid : NULL), oid); | |
85 | ||
86 | path += args->baselen; | |
87 | buffer = read_object_file(oid, type, sizep); | |
88 | if (buffer && S_ISREG(mode)) { | |
89 | struct strbuf buf = STRBUF_INIT; | |
90 | size_t size = 0; | |
91 | ||
92 | strbuf_attach(&buf, buffer, *sizep, *sizep + 1); | |
93 | convert_to_working_tree(args->repo->index, path, buf.buf, buf.len, &buf, &meta); | |
94 | if (commit) | |
95 | format_subst(commit, buf.buf, buf.len, &buf); | |
96 | buffer = strbuf_detach(&buf, &size); | |
97 | *sizep = size; | |
98 | } | |
99 | ||
100 | return buffer; | |
101 | } | |
102 | ||
103 | struct directory { | |
104 | struct directory *up; | |
105 | struct object_id oid; | |
106 | int baselen, len; | |
107 | unsigned mode; | |
108 | int stage; | |
109 | char path[FLEX_ARRAY]; | |
110 | }; | |
111 | ||
112 | struct archiver_context { | |
113 | struct archiver_args *args; | |
114 | write_archive_entry_fn_t write_entry; | |
115 | struct directory *bottom; | |
116 | }; | |
117 | ||
118 | static const struct attr_check *get_archive_attrs(struct index_state *istate, | |
119 | const char *path) | |
120 | { | |
121 | static struct attr_check *check; | |
122 | if (!check) | |
123 | check = attr_check_initl("export-ignore", "export-subst", NULL); | |
124 | git_check_attr(istate, path, check); | |
125 | return check; | |
126 | } | |
127 | ||
128 | static int check_attr_export_ignore(const struct attr_check *check) | |
129 | { | |
130 | return check && ATTR_TRUE(check->items[0].value); | |
131 | } | |
132 | ||
133 | static int check_attr_export_subst(const struct attr_check *check) | |
134 | { | |
135 | return check && ATTR_TRUE(check->items[1].value); | |
136 | } | |
137 | ||
138 | static int write_archive_entry(const struct object_id *oid, const char *base, | |
139 | int baselen, const char *filename, unsigned mode, int stage, | |
140 | void *context) | |
141 | { | |
142 | static struct strbuf path = STRBUF_INIT; | |
143 | struct archiver_context *c = context; | |
144 | struct archiver_args *args = c->args; | |
145 | write_archive_entry_fn_t write_entry = c->write_entry; | |
146 | int err; | |
147 | const char *path_without_prefix; | |
148 | ||
149 | args->convert = 0; | |
150 | strbuf_reset(&path); | |
151 | strbuf_grow(&path, PATH_MAX); | |
152 | strbuf_add(&path, args->base, args->baselen); | |
153 | strbuf_add(&path, base, baselen); | |
154 | strbuf_addstr(&path, filename); | |
155 | if (S_ISDIR(mode) || S_ISGITLINK(mode)) | |
156 | strbuf_addch(&path, '/'); | |
157 | path_without_prefix = path.buf + args->baselen; | |
158 | ||
159 | if (!S_ISDIR(mode)) { | |
160 | const struct attr_check *check; | |
161 | check = get_archive_attrs(args->repo->index, path_without_prefix); | |
162 | if (check_attr_export_ignore(check)) | |
163 | return 0; | |
164 | args->convert = check_attr_export_subst(check); | |
165 | } | |
166 | ||
167 | if (S_ISDIR(mode) || S_ISGITLINK(mode)) { | |
168 | if (args->verbose) | |
169 | fprintf(stderr, "%.*s\n", (int)path.len, path.buf); | |
170 | err = write_entry(args, oid, path.buf, path.len, mode); | |
171 | if (err) | |
172 | return err; | |
173 | return (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0); | |
174 | } | |
175 | ||
176 | if (args->verbose) | |
177 | fprintf(stderr, "%.*s\n", (int)path.len, path.buf); | |
178 | return write_entry(args, oid, path.buf, path.len, mode); | |
179 | } | |
180 | ||
181 | static void queue_directory(const unsigned char *sha1, | |
182 | struct strbuf *base, const char *filename, | |
183 | unsigned mode, int stage, struct archiver_context *c) | |
184 | { | |
185 | struct directory *d; | |
186 | size_t len = st_add4(base->len, 1, strlen(filename), 1); | |
187 | d = xmalloc(st_add(sizeof(*d), len)); | |
188 | d->up = c->bottom; | |
189 | d->baselen = base->len; | |
190 | d->mode = mode; | |
191 | d->stage = stage; | |
192 | c->bottom = d; | |
193 | d->len = xsnprintf(d->path, len, "%.*s%s/", (int)base->len, base->buf, filename); | |
194 | hashcpy(d->oid.hash, sha1); | |
195 | } | |
196 | ||
197 | static int write_directory(struct archiver_context *c) | |
198 | { | |
199 | struct directory *d = c->bottom; | |
200 | int ret; | |
201 | ||
202 | if (!d) | |
203 | return 0; | |
204 | c->bottom = d->up; | |
205 | d->path[d->len - 1] = '\0'; /* no trailing slash */ | |
206 | ret = | |
207 | write_directory(c) || | |
208 | write_archive_entry(&d->oid, d->path, d->baselen, | |
209 | d->path + d->baselen, d->mode, | |
210 | d->stage, c) != READ_TREE_RECURSIVE; | |
211 | free(d); | |
212 | return ret ? -1 : 0; | |
213 | } | |
214 | ||
215 | static int queue_or_write_archive_entry(const struct object_id *oid, | |
216 | struct strbuf *base, const char *filename, | |
217 | unsigned mode, int stage, void *context) | |
218 | { | |
219 | struct archiver_context *c = context; | |
220 | ||
221 | while (c->bottom && | |
222 | !(base->len >= c->bottom->len && | |
223 | !strncmp(base->buf, c->bottom->path, c->bottom->len))) { | |
224 | struct directory *next = c->bottom->up; | |
225 | free(c->bottom); | |
226 | c->bottom = next; | |
227 | } | |
228 | ||
229 | if (S_ISDIR(mode)) { | |
230 | size_t baselen = base->len; | |
231 | const struct attr_check *check; | |
232 | ||
233 | /* Borrow base, but restore its original value when done. */ | |
234 | strbuf_addstr(base, filename); | |
235 | strbuf_addch(base, '/'); | |
236 | check = get_archive_attrs(c->args->repo->index, base->buf); | |
237 | strbuf_setlen(base, baselen); | |
238 | ||
239 | if (check_attr_export_ignore(check)) | |
240 | return 0; | |
241 | queue_directory(oid->hash, base, filename, | |
242 | mode, stage, c); | |
243 | return READ_TREE_RECURSIVE; | |
244 | } | |
245 | ||
246 | if (write_directory(c)) | |
247 | return -1; | |
248 | return write_archive_entry(oid, base->buf, base->len, filename, mode, | |
249 | stage, context); | |
250 | } | |
251 | ||
252 | int write_archive_entries(struct archiver_args *args, | |
253 | write_archive_entry_fn_t write_entry) | |
254 | { | |
255 | struct archiver_context context; | |
256 | struct unpack_trees_options opts; | |
257 | struct tree_desc t; | |
258 | int err; | |
259 | ||
260 | if (args->baselen > 0 && args->base[args->baselen - 1] == '/') { | |
261 | size_t len = args->baselen; | |
262 | ||
263 | while (len > 1 && args->base[len - 2] == '/') | |
264 | len--; | |
265 | if (args->verbose) | |
266 | fprintf(stderr, "%.*s\n", (int)len, args->base); | |
267 | err = write_entry(args, &args->tree->object.oid, args->base, | |
268 | len, 040777); | |
269 | if (err) | |
270 | return err; | |
271 | } | |
272 | ||
273 | memset(&context, 0, sizeof(context)); | |
274 | context.args = args; | |
275 | context.write_entry = write_entry; | |
276 | ||
277 | /* | |
278 | * Setup index and instruct attr to read index only | |
279 | */ | |
280 | if (!args->worktree_attributes) { | |
281 | memset(&opts, 0, sizeof(opts)); | |
282 | opts.index_only = 1; | |
283 | opts.head_idx = -1; | |
284 | opts.src_index = args->repo->index; | |
285 | opts.dst_index = args->repo->index; | |
286 | opts.fn = oneway_merge; | |
287 | init_tree_desc(&t, args->tree->buffer, args->tree->size); | |
288 | if (unpack_trees(1, &t, &opts)) | |
289 | return -1; | |
290 | git_attr_set_direction(GIT_ATTR_INDEX); | |
291 | } | |
292 | ||
293 | err = read_tree_recursive(args->repo, args->tree, "", | |
294 | 0, 0, &args->pathspec, | |
295 | queue_or_write_archive_entry, | |
296 | &context); | |
297 | if (err == READ_TREE_RECURSIVE) | |
298 | err = 0; | |
299 | while (context.bottom) { | |
300 | struct directory *next = context.bottom->up; | |
301 | free(context.bottom); | |
302 | context.bottom = next; | |
303 | } | |
304 | return err; | |
305 | } | |
306 | ||
307 | static const struct archiver *lookup_archiver(const char *name) | |
308 | { | |
309 | int i; | |
310 | ||
311 | if (!name) | |
312 | return NULL; | |
313 | ||
314 | for (i = 0; i < nr_archivers; i++) { | |
315 | if (!strcmp(name, archivers[i]->name)) | |
316 | return archivers[i]; | |
317 | } | |
318 | return NULL; | |
319 | } | |
320 | ||
321 | struct path_exists_context { | |
322 | struct pathspec pathspec; | |
323 | struct archiver_args *args; | |
324 | }; | |
325 | ||
326 | static int reject_entry(const struct object_id *oid, struct strbuf *base, | |
327 | const char *filename, unsigned mode, | |
328 | int stage, void *context) | |
329 | { | |
330 | int ret = -1; | |
331 | struct path_exists_context *ctx = context; | |
332 | ||
333 | if (S_ISDIR(mode)) { | |
334 | struct strbuf sb = STRBUF_INIT; | |
335 | strbuf_addbuf(&sb, base); | |
336 | strbuf_addstr(&sb, filename); | |
337 | if (!match_pathspec(ctx->args->repo->index, | |
338 | &ctx->pathspec, | |
339 | sb.buf, sb.len, 0, NULL, 1)) | |
340 | ret = READ_TREE_RECURSIVE; | |
341 | strbuf_release(&sb); | |
342 | } | |
343 | return ret; | |
344 | } | |
345 | ||
346 | static int path_exists(struct archiver_args *args, const char *path) | |
347 | { | |
348 | const char *paths[] = { path, NULL }; | |
349 | struct path_exists_context ctx; | |
350 | int ret; | |
351 | ||
352 | ctx.args = args; | |
353 | parse_pathspec(&ctx.pathspec, 0, 0, "", paths); | |
354 | ctx.pathspec.recursive = 1; | |
355 | ret = read_tree_recursive(args->repo, args->tree, "", | |
356 | 0, 0, &ctx.pathspec, | |
357 | reject_entry, &ctx); | |
358 | clear_pathspec(&ctx.pathspec); | |
359 | return ret != 0; | |
360 | } | |
361 | ||
362 | static void parse_pathspec_arg(const char **pathspec, | |
363 | struct archiver_args *ar_args) | |
364 | { | |
365 | /* | |
366 | * must be consistent with parse_pathspec in path_exists() | |
367 | * Also if pathspec patterns are dependent, we're in big | |
368 | * trouble as we test each one separately | |
369 | */ | |
370 | parse_pathspec(&ar_args->pathspec, 0, | |
371 | PATHSPEC_PREFER_FULL, | |
372 | "", pathspec); | |
373 | ar_args->pathspec.recursive = 1; | |
374 | if (pathspec) { | |
375 | while (*pathspec) { | |
376 | if (**pathspec && !path_exists(ar_args, *pathspec)) | |
377 | die(_("pathspec '%s' did not match any files"), *pathspec); | |
378 | pathspec++; | |
379 | } | |
380 | } | |
381 | } | |
382 | ||
383 | static void parse_treeish_arg(const char **argv, | |
384 | struct archiver_args *ar_args, const char *prefix, | |
385 | int remote) | |
386 | { | |
387 | const char *name = argv[0]; | |
388 | const struct object_id *commit_oid; | |
389 | time_t archive_time; | |
390 | struct tree *tree; | |
391 | const struct commit *commit; | |
392 | struct object_id oid; | |
393 | char *ref = NULL; | |
394 | ||
395 | /* Remotes are only allowed to fetch actual refs */ | |
396 | if (remote && !remote_allow_unreachable) { | |
397 | const char *colon = strchrnul(name, ':'); | |
398 | int refnamelen = colon - name; | |
399 | ||
400 | if (!dwim_ref(name, refnamelen, &oid, &ref)) | |
401 | die(_("no such ref: %.*s"), refnamelen, name); | |
402 | } else { | |
403 | dwim_ref(name, strlen(name), &oid, &ref); | |
404 | } | |
405 | ||
406 | if (get_oid(name, &oid)) | |
407 | die(_("not a valid object name: %s"), name); | |
408 | ||
409 | commit = lookup_commit_reference_gently(ar_args->repo, &oid, 1); | |
410 | if (commit) { | |
411 | commit_oid = &commit->object.oid; | |
412 | archive_time = commit->date; | |
413 | } else { | |
414 | commit_oid = NULL; | |
415 | archive_time = time(NULL); | |
416 | } | |
417 | ||
418 | tree = parse_tree_indirect(&oid); | |
419 | if (tree == NULL) | |
420 | die(_("not a tree object: %s"), oid_to_hex(&oid)); | |
421 | ||
422 | if (prefix) { | |
423 | struct object_id tree_oid; | |
424 | unsigned short mode; | |
425 | int err; | |
426 | ||
427 | err = get_tree_entry(ar_args->repo, | |
428 | &tree->object.oid, | |
429 | prefix, &tree_oid, | |
430 | &mode); | |
431 | if (err || !S_ISDIR(mode)) | |
432 | die(_("current working directory is untracked")); | |
433 | ||
434 | tree = parse_tree_indirect(&tree_oid); | |
435 | } | |
436 | ar_args->refname = ref; | |
437 | ar_args->tree = tree; | |
438 | ar_args->commit_oid = commit_oid; | |
439 | ar_args->commit = commit; | |
440 | ar_args->time = archive_time; | |
441 | } | |
442 | ||
443 | #define OPT__COMPR(s, v, h, p) \ | |
444 | OPT_SET_INT_F(s, NULL, v, h, p, PARSE_OPT_NONEG) | |
445 | #define OPT__COMPR_HIDDEN(s, v, p) \ | |
446 | OPT_SET_INT_F(s, NULL, v, "", p, PARSE_OPT_NONEG | PARSE_OPT_HIDDEN) | |
447 | ||
448 | static int parse_archive_args(int argc, const char **argv, | |
449 | const struct archiver **ar, struct archiver_args *args, | |
450 | const char *name_hint, int is_remote) | |
451 | { | |
452 | const char *format = NULL; | |
453 | const char *base = NULL; | |
454 | const char *remote = NULL; | |
455 | const char *exec = NULL; | |
456 | const char *output = NULL; | |
457 | int compression_level = -1; | |
458 | int verbose = 0; | |
459 | int i; | |
460 | int list = 0; | |
461 | int worktree_attributes = 0; | |
462 | struct option opts[] = { | |
463 | OPT_GROUP(""), | |
464 | OPT_STRING(0, "format", &format, N_("fmt"), N_("archive format")), | |
465 | OPT_STRING(0, "prefix", &base, N_("prefix"), | |
466 | N_("prepend prefix to each pathname in the archive")), | |
467 | OPT_STRING('o', "output", &output, N_("file"), | |
468 | N_("write the archive to this file")), | |
469 | OPT_BOOL(0, "worktree-attributes", &worktree_attributes, | |
470 | N_("read .gitattributes in working directory")), | |
471 | OPT__VERBOSE(&verbose, N_("report archived files on stderr")), | |
472 | OPT__COMPR('0', &compression_level, N_("store only"), 0), | |
473 | OPT__COMPR('1', &compression_level, N_("compress faster"), 1), | |
474 | OPT__COMPR_HIDDEN('2', &compression_level, 2), | |
475 | OPT__COMPR_HIDDEN('3', &compression_level, 3), | |
476 | OPT__COMPR_HIDDEN('4', &compression_level, 4), | |
477 | OPT__COMPR_HIDDEN('5', &compression_level, 5), | |
478 | OPT__COMPR_HIDDEN('6', &compression_level, 6), | |
479 | OPT__COMPR_HIDDEN('7', &compression_level, 7), | |
480 | OPT__COMPR_HIDDEN('8', &compression_level, 8), | |
481 | OPT__COMPR('9', &compression_level, N_("compress better"), 9), | |
482 | OPT_GROUP(""), | |
483 | OPT_BOOL('l', "list", &list, | |
484 | N_("list supported archive formats")), | |
485 | OPT_GROUP(""), | |
486 | OPT_STRING(0, "remote", &remote, N_("repo"), | |
487 | N_("retrieve the archive from remote repository <repo>")), | |
488 | OPT_STRING(0, "exec", &exec, N_("command"), | |
489 | N_("path to the remote git-upload-archive command")), | |
490 | OPT_END() | |
491 | }; | |
492 | ||
493 | argc = parse_options(argc, argv, NULL, opts, archive_usage, 0); | |
494 | ||
495 | if (remote) | |
496 | die(_("Unexpected option --remote")); | |
497 | if (exec) | |
498 | die(_("Option --exec can only be used together with --remote")); | |
499 | if (output) | |
500 | die(_("Unexpected option --output")); | |
501 | ||
502 | if (!base) | |
503 | base = ""; | |
504 | ||
505 | if (list) { | |
506 | for (i = 0; i < nr_archivers; i++) | |
507 | if (!is_remote || archivers[i]->flags & ARCHIVER_REMOTE) | |
508 | printf("%s\n", archivers[i]->name); | |
509 | exit(0); | |
510 | } | |
511 | ||
512 | if (!format && name_hint) | |
513 | format = archive_format_from_filename(name_hint); | |
514 | if (!format) | |
515 | format = "tar"; | |
516 | ||
517 | /* We need at least one parameter -- tree-ish */ | |
518 | if (argc < 1) | |
519 | usage_with_options(archive_usage, opts); | |
520 | *ar = lookup_archiver(format); | |
521 | if (!*ar || (is_remote && !((*ar)->flags & ARCHIVER_REMOTE))) | |
522 | die(_("Unknown archive format '%s'"), format); | |
523 | ||
524 | args->compression_level = Z_DEFAULT_COMPRESSION; | |
525 | if (compression_level != -1) { | |
526 | if ((*ar)->flags & ARCHIVER_WANT_COMPRESSION_LEVELS) | |
527 | args->compression_level = compression_level; | |
528 | else { | |
529 | die(_("Argument not supported for format '%s': -%d"), | |
530 | format, compression_level); | |
531 | } | |
532 | } | |
533 | args->verbose = verbose; | |
534 | args->base = base; | |
535 | args->baselen = strlen(base); | |
536 | args->worktree_attributes = worktree_attributes; | |
537 | ||
538 | return argc; | |
539 | } | |
540 | ||
541 | int write_archive(int argc, const char **argv, const char *prefix, | |
542 | struct repository *repo, | |
543 | const char *name_hint, int remote) | |
544 | { | |
545 | const struct archiver *ar = NULL; | |
546 | struct archiver_args args; | |
547 | ||
548 | git_config_get_bool("uploadarchive.allowunreachable", &remote_allow_unreachable); | |
549 | git_config(git_default_config, NULL); | |
550 | ||
551 | args.repo = repo; | |
552 | argc = parse_archive_args(argc, argv, &ar, &args, name_hint, remote); | |
553 | if (!startup_info->have_repository) { | |
554 | /* | |
555 | * We know this will die() with an error, so we could just | |
556 | * die ourselves; but its error message will be more specific | |
557 | * than what we could write here. | |
558 | */ | |
559 | setup_git_directory(); | |
560 | } | |
561 | ||
562 | parse_treeish_arg(argv, &args, prefix, remote); | |
563 | parse_pathspec_arg(argv + 1, &args); | |
564 | ||
565 | return ar->write_archive(ar, &args); | |
566 | } | |
567 | ||
568 | static int match_extension(const char *filename, const char *ext) | |
569 | { | |
570 | int prefixlen = strlen(filename) - strlen(ext); | |
571 | ||
572 | /* | |
573 | * We need 1 character for the '.', and 1 character to ensure that the | |
574 | * prefix is non-empty (k.e., we don't match .tar.gz with no actual | |
575 | * filename). | |
576 | */ | |
577 | if (prefixlen < 2 || filename[prefixlen - 1] != '.') | |
578 | return 0; | |
579 | return !strcmp(filename + prefixlen, ext); | |
580 | } | |
581 | ||
582 | const char *archive_format_from_filename(const char *filename) | |
583 | { | |
584 | int i; | |
585 | ||
586 | for (i = 0; i < nr_archivers; i++) | |
587 | if (match_extension(filename, archivers[i]->name)) | |
588 | return archivers[i]->name; | |
589 | return NULL; | |
590 | } |