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