]> git.ipfire.org Git - thirdparty/git.git/blame - archive.c
cocci: apply the "pretty.h" part of "the_repository.pending"
[thirdparty/git.git] / archive.c
CommitLineData
18125644 1#include "cache.h"
b2141fc1 2#include "config.h"
fb58c8d5 3#include "refs.h"
cbd53a21 4#include "object-store.h"
18125644 5#include "commit.h"
c0885435 6#include "tree-walk.h"
18125644 7#include "attr.h"
562e25ab 8#include "archive.h"
4fac1d3a 9#include "parse-options.h"
ba053ea9 10#include "unpack-trees.h"
ed22b417 11#include "dir.h"
de1f68a9 12#include "quote.h"
4fac1d3a
RS
13
14static char const * const archive_usage[] = {
9c9b4f2f 15 N_("git archive [<options>] <tree-ish> [<path>...]"),
959d670d 16 "git archive --list",
9c9b4f2f 17 N_("git archive --remote <repo> [--exec <cmd>] [<options>] <tree-ish> [<path>...]"),
0012a387 18 N_("git archive --remote <repo> [--exec <cmd>] --list"),
4fac1d3a
RS
19 NULL
20};
c0885435 21
13e0f88d
JK
22static const struct archiver **archivers;
23static int nr_archivers;
24static int alloc_archivers;
7671b632 25static int remote_allow_unreachable;
13e0f88d
JK
26
27void register_archiver(struct archiver *ar)
28{
29 ALLOC_GROW(archivers, nr_archivers + 1, alloc_archivers);
30 archivers[nr_archivers++] = ar;
31}
c0885435 32
00436bf1
JS
33void init_archivers(void)
34{
35 init_tar_archiver();
36 init_zip_archiver();
37}
38
18125644 39static void format_subst(const struct commit *commit,
ec36c42a 40 const char *src, size_t len,
96099726 41 struct strbuf *buf, struct pretty_print_context *ctx)
18125644
LH
42{
43 char *to_free = NULL;
f285a2d7 44 struct strbuf fmt = STRBUF_INIT;
18125644
LH
45
46 if (src == buf->buf)
47 to_free = strbuf_detach(buf, NULL);
18125644
LH
48 for (;;) {
49 const char *b, *c;
50
51 b = memmem(src, len, "$Format:", 8);
75b7dfbd 52 if (!b)
18125644 53 break;
75b7dfbd 54 c = memchr(b + 8, '$', (src + len) - b - 8);
18125644
LH
55 if (!c)
56 break;
57
58 strbuf_reset(&fmt);
59 strbuf_add(&fmt, b + 8, c - b - 8);
60
61 strbuf_add(buf, src, b - src);
bab82164
ÆAB
62 repo_format_commit_message(the_repository, commit, fmt.buf,
63 buf, ctx);
18125644
LH
64 len -= c + 1 - src;
65 src = c + 1;
66 }
67 strbuf_add(buf, src, len);
68 strbuf_release(&fmt);
69 free(to_free);
70}
71
200589ab
RS
72static void *object_file_to_archive(const struct archiver_args *args,
73 const char *path,
74 const struct object_id *oid,
75 unsigned int mode,
76 enum object_type *type,
77 unsigned long *sizep)
18125644
LH
78{
79 void *buffer;
9cb513b7 80 const struct commit *commit = args->convert ? args->commit : NULL;
c397aac0 81 struct checkout_metadata meta;
82
83 init_checkout_metadata(&meta, args->refname,
84 args->commit_oid ? args->commit_oid :
85 (args->tree ? &args->tree->object.oid : NULL), oid);
18125644 86
9cb513b7 87 path += args->baselen;
bc726bd0 88 buffer = repo_read_object_file(the_repository, oid, type, sizep);
18125644 89 if (buffer && S_ISREG(mode)) {
f285a2d7 90 struct strbuf buf = STRBUF_INIT;
18125644
LH
91 size_t size = 0;
92
18125644 93 strbuf_attach(&buf, buffer, *sizep, *sizep + 1);
c397aac0 94 convert_to_working_tree(args->repo->index, path, buf.buf, buf.len, &buf, &meta);
1d11d5bb 95 if (commit)
96099726 96 format_subst(commit, buf.buf, buf.len, &buf, args->pretty_ctx);
18125644
LH
97 buffer = strbuf_detach(&buf, &size);
98 *sizep = size;
99 }
100
101 return buffer;
102}
103
ed22b417
NTND
104struct directory {
105 struct directory *up;
13609673 106 struct object_id oid;
ed22b417
NTND
107 int baselen, len;
108 unsigned mode;
ed22b417
NTND
109 char path[FLEX_ARRAY];
110};
111
562e25ab
RS
112struct archiver_context {
113 struct archiver_args *args;
114 write_archive_entry_fn_t write_entry;
ed22b417 115 struct directory *bottom;
562e25ab
RS
116};
117
b612ee20
NTND
118static const struct attr_check *get_archive_attrs(struct index_state *istate,
119 const char *path)
c6c08f7e
RS
120{
121 static struct attr_check *check;
122 if (!check)
123 check = attr_check_initl("export-ignore", "export-subst", NULL);
47cfc9bd 124 git_check_attr(istate, NULL, path, check);
d64324cb 125 return check;
c6c08f7e
RS
126}
127
128static int check_attr_export_ignore(const struct attr_check *check)
129{
130 return check && ATTR_TRUE(check->items[0].value);
131}
132
133static int check_attr_export_subst(const struct attr_check *check)
134{
135 return check && ATTR_TRUE(check->items[1].value);
136}
137
015ff4f8 138static int write_archive_entry(const struct object_id *oid, const char *base,
7367d882 139 int baselen, const char *filename, unsigned mode,
562e25ab
RS
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;
5ff247ac 147 const char *path_without_prefix;
200589ab
RS
148 unsigned long size;
149 void *buffer;
150 enum object_type type;
562e25ab 151
9cb513b7 152 args->convert = 0;
562e25ab
RS
153 strbuf_reset(&path);
154 strbuf_grow(&path, PATH_MAX);
ebfbdb34 155 strbuf_add(&path, args->base, args->baselen);
562e25ab
RS
156 strbuf_add(&path, base, baselen);
157 strbuf_addstr(&path, filename);
94bc671a
JNA
158 if (S_ISDIR(mode) || S_ISGITLINK(mode))
159 strbuf_addch(&path, '/');
1d11d5bb 160 path_without_prefix = path.buf + args->baselen;
562e25ab 161
43180940 162 if (!S_ISDIR(mode)) {
5ff247ac 163 const struct attr_check *check;
b612ee20 164 check = get_archive_attrs(args->repo->index, path_without_prefix);
5ff247ac 165 if (check_attr_export_ignore(check))
1d11d5bb 166 return 0;
5ff247ac 167 args->convert = check_attr_export_subst(check);
1d11d5bb 168 }
562e25ab 169
e3733b64
RS
170 if (args->verbose)
171 fprintf(stderr, "%.*s\n", (int)path.len, path.buf);
172
562e25ab 173 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
200589ab 174 err = write_entry(args, oid, path.buf, path.len, mode, NULL, 0);
562e25ab
RS
175 if (err)
176 return err;
d3bee161 177 return (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0);
562e25ab
RS
178 }
179
200589ab
RS
180 /* Stream it? */
181 if (S_ISREG(mode) && !args->convert &&
182 oid_object_info(args->repo, oid, &size) == OBJ_BLOB &&
183 size > big_file_threshold)
184 return write_entry(args, oid, path.buf, path.len, mode, NULL, size);
185
186 buffer = object_file_to_archive(args, path.buf, oid, mode, &type, &size);
187 if (!buffer)
c4904377 188 return error(_("cannot read '%s'"), oid_to_hex(oid));
200589ab
RS
189 err = write_entry(args, oid, path.buf, path.len, mode, buffer, size);
190 free(buffer);
191 return err;
562e25ab
RS
192}
193
e124ecf7 194static void queue_directory(const struct object_id *oid,
6a0b0b6d 195 struct strbuf *base, const char *filename,
7367d882 196 unsigned mode, struct archiver_context *c)
ed22b417
NTND
197{
198 struct directory *d;
50a6c8ef
JK
199 size_t len = st_add4(base->len, 1, strlen(filename), 1);
200 d = xmalloc(st_add(sizeof(*d), len));
ed22b417 201 d->up = c->bottom;
6a0b0b6d 202 d->baselen = base->len;
ed22b417 203 d->mode = mode;
ed22b417 204 c->bottom = d;
c7ab0ba3 205 d->len = xsnprintf(d->path, len, "%.*s%s/", (int)base->len, base->buf, filename);
e124ecf7 206 oidcpy(&d->oid, oid);
ed22b417
NTND
207}
208
209static int write_directory(struct archiver_context *c)
210{
211 struct directory *d = c->bottom;
212 int ret;
213
214 if (!d)
215 return 0;
216 c->bottom = d->up;
217 d->path[d->len - 1] = '\0'; /* no trailing slash */
218 ret =
219 write_directory(c) ||
015ff4f8 220 write_archive_entry(&d->oid, d->path, d->baselen,
ed22b417 221 d->path + d->baselen, d->mode,
7367d882 222 c) != READ_TREE_RECURSIVE;
ed22b417
NTND
223 free(d);
224 return ret ? -1 : 0;
225}
226
df46d77e 227static int queue_or_write_archive_entry(const struct object_id *oid,
6a0b0b6d 228 struct strbuf *base, const char *filename,
47957485 229 unsigned mode, void *context)
ed22b417
NTND
230{
231 struct archiver_context *c = context;
232
233 while (c->bottom &&
6a0b0b6d
NTND
234 !(base->len >= c->bottom->len &&
235 !strncmp(base->buf, c->bottom->path, c->bottom->len))) {
ed22b417
NTND
236 struct directory *next = c->bottom->up;
237 free(c->bottom);
238 c->bottom = next;
239 }
240
241 if (S_ISDIR(mode)) {
5ff247ac
RS
242 size_t baselen = base->len;
243 const struct attr_check *check;
244
245 /* Borrow base, but restore its original value when done. */
246 strbuf_addstr(base, filename);
247 strbuf_addch(base, '/');
b612ee20 248 check = get_archive_attrs(c->args->repo->index, base->buf);
5ff247ac
RS
249 strbuf_setlen(base, baselen);
250
251 if (check_attr_export_ignore(check))
252 return 0;
e124ecf7 253 queue_directory(oid, base, filename, mode, c);
ed22b417
NTND
254 return READ_TREE_RECURSIVE;
255 }
256
257 if (write_directory(c))
258 return -1;
015ff4f8 259 return write_archive_entry(oid, base->buf, base->len, filename, mode,
7367d882 260 context);
ed22b417
NTND
261}
262
2947a793
RS
263struct extra_file_info {
264 char *base;
265 struct stat stat;
237a1d13 266 void *content;
2947a793
RS
267};
268
562e25ab
RS
269int write_archive_entries(struct archiver_args *args,
270 write_archive_entry_fn_t write_entry)
271{
272 struct archiver_context context;
ba053ea9
NTND
273 struct unpack_trees_options opts;
274 struct tree_desc t;
562e25ab 275 int err;
2947a793
RS
276 struct strbuf path_in_archive = STRBUF_INIT;
277 struct strbuf content = STRBUF_INIT;
14228447 278 struct object_id fake_oid;
2947a793 279 int i;
562e25ab 280
14228447 281 oidcpy(&fake_oid, null_oid());
282
562e25ab
RS
283 if (args->baselen > 0 && args->base[args->baselen - 1] == '/') {
284 size_t len = args->baselen;
285
286 while (len > 1 && args->base[len - 2] == '/')
287 len--;
288 if (args->verbose)
289 fprintf(stderr, "%.*s\n", (int)len, args->base);
015ff4f8 290 err = write_entry(args, &args->tree->object.oid, args->base,
200589ab 291 len, 040777, NULL, 0);
562e25ab
RS
292 if (err)
293 return err;
294 }
295
ed22b417 296 memset(&context, 0, sizeof(context));
562e25ab
RS
297 context.args = args;
298 context.write_entry = write_entry;
299
ba053ea9
NTND
300 /*
301 * Setup index and instruct attr to read index only
302 */
303 if (!args->worktree_attributes) {
304 memset(&opts, 0, sizeof(opts));
305 opts.index_only = 1;
306 opts.head_idx = -1;
b612ee20
NTND
307 opts.src_index = args->repo->index;
308 opts.dst_index = args->repo->index;
ba053ea9
NTND
309 opts.fn = oneway_merge;
310 init_tree_desc(&t, args->tree->buffer, args->tree->size);
311 if (unpack_trees(1, &t, &opts))
312 return -1;
c4500e25 313 git_attr_set_direction(GIT_ATTR_INDEX);
ba053ea9
NTND
314 }
315
47957485
ÆAB
316 err = read_tree(args->repo, args->tree,
317 &args->pathspec,
318 queue_or_write_archive_entry,
319 &context);
562e25ab
RS
320 if (err == READ_TREE_RECURSIVE)
321 err = 0;
ed22b417
NTND
322 while (context.bottom) {
323 struct directory *next = context.bottom->up;
324 free(context.bottom);
325 context.bottom = next;
326 }
2947a793
RS
327
328 for (i = 0; i < args->extra_files.nr; i++) {
329 struct string_list_item *item = args->extra_files.items + i;
330 char *path = item->string;
331 struct extra_file_info *info = item->util;
332
333 put_be64(fake_oid.hash, i + 1);
334
237a1d13
JS
335 if (!info->content) {
336 strbuf_reset(&path_in_archive);
337 if (info->base)
338 strbuf_addstr(&path_in_archive, info->base);
339 strbuf_addstr(&path_in_archive, basename(path));
340
341 strbuf_reset(&content);
342 if (strbuf_read_file(&content, path, info->stat.st_size) < 0)
343 err = error_errno(_("cannot read '%s'"), path);
344 else
345 err = write_entry(args, &fake_oid, path_in_archive.buf,
346 path_in_archive.len,
347 canon_mode(info->stat.st_mode),
348 content.buf, content.len);
349 } else {
350 err = write_entry(args, &fake_oid,
351 path, strlen(path),
6a616619 352 canon_mode(info->stat.st_mode),
237a1d13
JS
353 info->content, info->stat.st_size);
354 }
355
2947a793
RS
356 if (err)
357 break;
358 }
359 strbuf_release(&path_in_archive);
360 strbuf_release(&content);
361
562e25ab
RS
362 return err;
363}
6e94e683 364
c0885435
RS
365static const struct archiver *lookup_archiver(const char *name)
366{
367 int i;
368
4fac1d3a
RS
369 if (!name)
370 return NULL;
371
13e0f88d
JK
372 for (i = 0; i < nr_archivers; i++) {
373 if (!strcmp(name, archivers[i]->name))
374 return archivers[i];
c0885435
RS
375 }
376 return NULL;
377}
378
b612ee20
NTND
379struct path_exists_context {
380 struct pathspec pathspec;
381 struct archiver_args *args;
382};
383
5cf88fd8 384static int reject_entry(const struct object_id *oid UNUSED,
555ff1c8 385 struct strbuf *base,
6a0b0b6d 386 const char *filename, unsigned mode,
7367d882 387 void *context)
d5f53d6d 388{
ed22b417 389 int ret = -1;
b612ee20
NTND
390 struct path_exists_context *ctx = context;
391
ed22b417
NTND
392 if (S_ISDIR(mode)) {
393 struct strbuf sb = STRBUF_INIT;
6a0b0b6d 394 strbuf_addbuf(&sb, base);
ed22b417 395 strbuf_addstr(&sb, filename);
b612ee20
NTND
396 if (!match_pathspec(ctx->args->repo->index,
397 &ctx->pathspec,
398 sb.buf, sb.len, 0, NULL, 1))
ed22b417
NTND
399 ret = READ_TREE_RECURSIVE;
400 strbuf_release(&sb);
401 }
402 return ret;
d5f53d6d
RS
403}
404
b612ee20 405static int path_exists(struct archiver_args *args, const char *path)
d5f53d6d 406{
f0096c06 407 const char *paths[] = { path, NULL };
b612ee20 408 struct path_exists_context ctx;
f0096c06
NTND
409 int ret;
410
b612ee20
NTND
411 ctx.args = args;
412 parse_pathspec(&ctx.pathspec, 0, 0, "", paths);
413 ctx.pathspec.recursive = 1;
47957485
ÆAB
414 ret = read_tree(args->repo, args->tree,
415 &ctx.pathspec,
416 reject_entry, &ctx);
b612ee20 417 clear_pathspec(&ctx.pathspec);
f0096c06 418 return ret != 0;
d5f53d6d
RS
419}
420
c0885435
RS
421static void parse_pathspec_arg(const char **pathspec,
422 struct archiver_args *ar_args)
423{
f3e743a0
NTND
424 /*
425 * must be consistent with parse_pathspec in path_exists()
426 * Also if pathspec patterns are dependent, we're in big
427 * trouble as we test each one separately
428 */
429 parse_pathspec(&ar_args->pathspec, 0,
430 PATHSPEC_PREFER_FULL,
431 "", pathspec);
ed22b417 432 ar_args->pathspec.recursive = 1;
d5f53d6d
RS
433 if (pathspec) {
434 while (*pathspec) {
b612ee20 435 if (**pathspec && !path_exists(ar_args, *pathspec))
f3e743a0 436 die(_("pathspec '%s' did not match any files"), *pathspec);
d5f53d6d
RS
437 pathspec++;
438 }
439 }
c0885435
RS
440}
441
442static void parse_treeish_arg(const char **argv,
ee27ca4a
JK
443 struct archiver_args *ar_args, const char *prefix,
444 int remote)
c0885435
RS
445{
446 const char *name = argv[0];
bbf05cf7 447 const struct object_id *commit_oid;
c0885435
RS
448 time_t archive_time;
449 struct tree *tree;
450 const struct commit *commit;
13609673 451 struct object_id oid;
c397aac0 452 char *ref = NULL;
c0885435 453
ee27ca4a 454 /* Remotes are only allowed to fetch actual refs */
7671b632 455 if (remote && !remote_allow_unreachable) {
2c5495f7
RM
456 const char *colon = strchrnul(name, ':');
457 int refnamelen = colon - name;
c51a351a 458
f24c30e0 459 if (!dwim_ref(name, refnamelen, &oid, &ref, 0))
c6e7965d 460 die(_("no such ref: %.*s"), refnamelen, name);
c397aac0 461 } else {
f24c30e0 462 dwim_ref(name, strlen(name), &oid, &ref, 0);
ee27ca4a 463 }
0f544ee8 464
d850b7a5 465 if (repo_get_oid(the_repository, name, &oid))
c6e7965d 466 die(_("not a valid object name: %s"), name);
c0885435 467
ce3a7ec8 468 commit = lookup_commit_reference_gently(ar_args->repo, &oid, 1);
c0885435 469 if (commit) {
bbf05cf7 470 commit_oid = &commit->object.oid;
c0885435
RS
471 archive_time = commit->date;
472 } else {
bbf05cf7 473 commit_oid = NULL;
c0885435
RS
474 archive_time = time(NULL);
475 }
fd2da4b1
RS
476 if (ar_args->mtime_option)
477 archive_time = approxidate(ar_args->mtime_option);
c0885435 478
a9dbc179 479 tree = parse_tree_indirect(&oid);
afe8a907 480 if (!tree)
c6e7965d 481 die(_("not a tree object: %s"), oid_to_hex(&oid));
c0885435
RS
482
483 if (prefix) {
13609673 484 struct object_id tree_oid;
5ec1e728 485 unsigned short mode;
c0885435
RS
486 int err;
487
50ddb089
NTND
488 err = get_tree_entry(ar_args->repo,
489 &tree->object.oid,
490 prefix, &tree_oid,
916bc35b 491 &mode);
c0885435 492 if (err || !S_ISDIR(mode))
c6e7965d 493 die(_("current working directory is untracked"));
c0885435 494
a9dbc179 495 tree = parse_tree_indirect(&tree_oid);
c0885435 496 }
c397aac0 497 ar_args->refname = ref;
c0885435 498 ar_args->tree = tree;
bbf05cf7 499 ar_args->commit_oid = commit_oid;
c0885435
RS
500 ar_args->commit = commit;
501 ar_args->time = archive_time;
502}
503
1ee34710 504static void extra_file_info_clear(void *util, const char *str UNUSED)
2947a793
RS
505{
506 struct extra_file_info *info = util;
507 free(info->base);
237a1d13 508 free(info->content);
2947a793
RS
509 free(info);
510}
511
512static int add_file_cb(const struct option *opt, const char *arg, int unset)
513{
514 struct archiver_args *args = opt->value;
515 const char **basep = (const char **)opt->defval;
516 const char *base = *basep;
517 char *path;
518 struct string_list_item *item;
519 struct extra_file_info *info;
520
521 if (unset) {
522 string_list_clear_func(&args->extra_files,
523 extra_file_info_clear);
524 return 0;
525 }
526
527 if (!arg)
528 return -1;
529
237a1d13 530 info = xmalloc(sizeof(*info));
2947a793 531 info->base = xstrdup_or_null(base);
237a1d13
JS
532
533 if (!strcmp(opt->long_name, "add-file")) {
534 path = prefix_filename(args->prefix, arg);
535 if (stat(path, &info->stat))
536 die(_("File not found: %s"), path);
537 if (!S_ISREG(info->stat.st_mode))
538 die(_("Not a regular file: %s"), path);
539 info->content = NULL; /* read the file later */
540 } else if (!strcmp(opt->long_name, "add-virtual-file")) {
de1f68a9
JS
541 struct strbuf buf = STRBUF_INIT;
542 const char *p = arg;
543
544 if (*p != '"')
545 p = strchr(p, ':');
546 else if (unquote_c_style(&buf, p, &p) < 0)
547 die(_("unclosed quote: '%s'"), arg);
237a1d13 548
de1f68a9 549 if (!p || *p != ':')
237a1d13
JS
550 die(_("missing colon: '%s'"), arg);
551
de1f68a9
JS
552 if (p == arg)
553 die(_("empty file name: '%s'"), arg);
554
555 path = buf.len ?
556 strbuf_detach(&buf, NULL) : xstrndup(arg, p - arg);
557
558 if (args->prefix) {
559 char *save = path;
560 path = prefix_filename(args->prefix, path);
561 free(save);
237a1d13
JS
562 }
563 memset(&info->stat, 0, sizeof(info->stat));
564 info->stat.st_mode = S_IFREG | 0644;
de1f68a9 565 info->content = xstrdup(p + 1);
237a1d13
JS
566 info->stat.st_size = strlen(info->content);
567 } else {
568 BUG("add_file_cb() called for %s", opt->long_name);
569 }
570 item = string_list_append_nodup(&args->extra_files, path);
571 item->util = info;
572
2947a793
RS
573 return 0;
574}
575
cde8ea9c
RS
576static int number_callback(const struct option *opt, const char *arg, int unset)
577{
578 BUG_ON_OPT_NEG(unset);
579 *(int *)opt->value = strtol(arg, NULL, 10);
580 return 0;
581}
4fac1d3a 582
c0885435 583static int parse_archive_args(int argc, const char **argv,
56baa61d 584 const struct archiver **ar, struct archiver_args *args,
7b97730b 585 const char *name_hint, int is_remote)
c0885435 586{
56baa61d 587 const char *format = NULL;
4fac1d3a
RS
588 const char *base = NULL;
589 const char *remote = NULL;
590 const char *exec = NULL;
aec0c1bb 591 const char *output = NULL;
fd2da4b1 592 const char *mtime_option = NULL;
c0885435
RS
593 int compression_level = -1;
594 int verbose = 0;
595 int i;
4fac1d3a 596 int list = 0;
ba053ea9 597 int worktree_attributes = 0;
4fac1d3a
RS
598 struct option opts[] = {
599 OPT_GROUP(""),
0012a387
NTND
600 OPT_STRING(0, "format", &format, N_("fmt"), N_("archive format")),
601 OPT_STRING(0, "prefix", &base, N_("prefix"),
602 N_("prepend prefix to each pathname in the archive")),
2947a793
RS
603 { OPTION_CALLBACK, 0, "add-file", args, N_("file"),
604 N_("add untracked file to archive"), 0, add_file_cb,
605 (intptr_t)&base },
237a1d13
JS
606 { OPTION_CALLBACK, 0, "add-virtual-file", args,
607 N_("path:content"), N_("add untracked file to archive"), 0,
608 add_file_cb, (intptr_t)&base },
0012a387
NTND
609 OPT_STRING('o', "output", &output, N_("file"),
610 N_("write the archive to this file")),
f858c646 611 OPT_BOOL(0, "worktree-attributes", &worktree_attributes,
0012a387
NTND
612 N_("read .gitattributes in working directory")),
613 OPT__VERBOSE(&verbose, N_("report archived files on stderr")),
fd2da4b1
RS
614 { OPTION_STRING, 0, "mtime", &mtime_option, N_("time"),
615 N_("set modification time of archive entries"),
616 PARSE_OPT_NONEG },
cde8ea9c
RS
617 OPT_NUMBER_CALLBACK(&compression_level,
618 N_("set compression level"), number_callback),
4fac1d3a 619 OPT_GROUP(""),
f858c646 620 OPT_BOOL('l', "list", &list,
0012a387 621 N_("list supported archive formats")),
4fac1d3a 622 OPT_GROUP(""),
0012a387
NTND
623 OPT_STRING(0, "remote", &remote, N_("repo"),
624 N_("retrieve the archive from remote repository <repo>")),
b0ff9654 625 OPT_STRING(0, "exec", &exec, N_("command"),
0012a387 626 N_("path to the remote git-upload-archive command")),
4fac1d3a
RS
627 OPT_END()
628 };
629
37782920 630 argc = parse_options(argc, argv, NULL, opts, archive_usage, 0);
4fac1d3a
RS
631
632 if (remote)
5a36d00c 633 die(_("Unexpected option --remote"));
4fac1d3a 634 if (exec)
6fa00ee8 635 die(_("the option '%s' requires '%s'"), "--exec", "--remote");
52e77876 636 if (output)
5a36d00c 637 die(_("Unexpected option --output"));
2947a793 638 if (is_remote && args->extra_files.nr)
12909b6b 639 die(_("options '%s' and '%s' cannot be used together"), "--add-file", "--remote");
4fac1d3a
RS
640
641 if (!base)
642 base = "";
643
644 if (list) {
13e0f88d 645 for (i = 0; i < nr_archivers; i++)
7b97730b
JK
646 if (!is_remote || archivers[i]->flags & ARCHIVER_REMOTE)
647 printf("%s\n", archivers[i]->name);
4fac1d3a 648 exit(0);
c0885435
RS
649 }
650
56baa61d
JK
651 if (!format && name_hint)
652 format = archive_format_from_filename(name_hint);
653 if (!format)
654 format = "tar";
655
c0885435 656 /* We need at least one parameter -- tree-ish */
4fac1d3a
RS
657 if (argc < 1)
658 usage_with_options(archive_usage, opts);
c0885435 659 *ar = lookup_archiver(format);
7b97730b 660 if (!*ar || (is_remote && !((*ar)->flags & ARCHIVER_REMOTE)))
5a36d00c 661 die(_("Unknown archive format '%s'"), format);
c0885435
RS
662
663 args->compression_level = Z_DEFAULT_COMPRESSION;
664 if (compression_level != -1) {
cde8ea9c
RS
665 int levels_ok = (*ar)->flags & ARCHIVER_WANT_COMPRESSION_LEVELS;
666 int high_ok = (*ar)->flags & ARCHIVER_HIGH_COMPRESSION_LEVELS;
667 if (levels_ok && (compression_level <= 9 || high_ok))
c0885435
RS
668 args->compression_level = compression_level;
669 else {
5a36d00c 670 die(_("Argument not supported for format '%s': -%d"),
c0885435
RS
671 format, compression_level);
672 }
673 }
674 args->verbose = verbose;
675 args->base = base;
676 args->baselen = strlen(base);
ba053ea9 677 args->worktree_attributes = worktree_attributes;
fd2da4b1 678 args->mtime_option = mtime_option;
c0885435 679
4fac1d3a 680 return argc;
c0885435
RS
681}
682
6e94e683 683int write_archive(int argc, const char **argv, const char *prefix,
b612ee20 684 struct repository *repo,
eb0224c6 685 const char *name_hint, int remote)
6e94e683
RS
686{
687 const struct archiver *ar = NULL;
96099726
RS
688 struct pretty_print_describe_status describe_status = {0};
689 struct pretty_print_context ctx = {0};
6e94e683 690 struct archiver_args args;
2947a793 691 int rc;
6e94e683 692
95790ff6
TA
693 git_config_get_bool("uploadarchive.allowunreachable", &remote_allow_unreachable);
694 git_config(git_default_config, NULL);
695
96099726
RS
696 describe_status.max_invocations = 1;
697 ctx.date_mode.type = DATE_NORMAL;
698 ctx.abbrev = DEFAULT_ABBREV;
699 ctx.describe_status = &describe_status;
700 args.pretty_ctx = &ctx;
b612ee20 701 args.repo = repo;
2947a793 702 args.prefix = prefix;
bc40dfb1 703 string_list_init_dup(&args.extra_files);
7b97730b 704 argc = parse_archive_args(argc, argv, &ar, &args, name_hint, remote);
eb0224c6 705 if (!startup_info->have_repository) {
23212862
JK
706 /*
707 * We know this will die() with an error, so we could just
708 * die ourselves; but its error message will be more specific
709 * than what we could write here.
710 */
711 setup_git_directory();
712 }
6e94e683 713
ee27ca4a 714 parse_treeish_arg(argv, &args, prefix, remote);
6e94e683
RS
715 parse_pathspec_arg(argv + 1, &args);
716
2947a793
RS
717 rc = ar->write_archive(ar, &args);
718
719 string_list_clear_func(&args.extra_files, extra_file_info_clear);
1c3e4129 720 free(args.refname);
7615cf94 721 clear_pathspec(&args.pathspec);
2947a793
RS
722
723 return rc;
6e94e683 724}
56baa61d 725
08716b3c
JK
726static int match_extension(const char *filename, const char *ext)
727{
728 int prefixlen = strlen(filename) - strlen(ext);
729
730 /*
731 * We need 1 character for the '.', and 1 character to ensure that the
732 * prefix is non-empty (k.e., we don't match .tar.gz with no actual
733 * filename).
734 */
b1cdfb54 735 if (prefixlen < 2 || filename[prefixlen - 1] != '.')
08716b3c
JK
736 return 0;
737 return !strcmp(filename + prefixlen, ext);
738}
739
56baa61d
JK
740const char *archive_format_from_filename(const char *filename)
741{
08716b3c
JK
742 int i;
743
744 for (i = 0; i < nr_archivers; i++)
745 if (match_extension(filename, archivers[i]->name))
746 return archivers[i]->name;
56baa61d
JK
747 return NULL;
748}