]>
Commit | Line | Data |
---|---|---|
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" |
4fac1d3a RS |
12 | |
13 | static char const * const archive_usage[] = { | |
9c9b4f2f | 14 | N_("git archive [<options>] <tree-ish> [<path>...]"), |
0012a387 | 15 | N_("git archive --list"), |
9c9b4f2f | 16 | N_("git archive --remote <repo> [--exec <cmd>] [<options>] <tree-ish> [<path>...]"), |
0012a387 | 17 | N_("git archive --remote <repo> [--exec <cmd>] --list"), |
4fac1d3a RS |
18 | NULL |
19 | }; | |
c0885435 | 20 | |
13e0f88d JK |
21 | static const struct archiver **archivers; |
22 | static int nr_archivers; | |
23 | static int alloc_archivers; | |
7671b632 | 24 | static int remote_allow_unreachable; |
13e0f88d JK |
25 | |
26 | void register_archiver(struct archiver *ar) | |
27 | { | |
28 | ALLOC_GROW(archivers, nr_archivers + 1, alloc_archivers); | |
29 | archivers[nr_archivers++] = ar; | |
30 | } | |
c0885435 | 31 | |
00436bf1 JS |
32 | void init_archivers(void) |
33 | { | |
34 | init_tar_archiver(); | |
35 | init_zip_archiver(); | |
36 | } | |
37 | ||
18125644 | 38 | static void format_subst(const struct commit *commit, |
ec36c42a NTND |
39 | const char *src, size_t len, |
40 | struct strbuf *buf) | |
18125644 LH |
41 | { |
42 | char *to_free = NULL; | |
f285a2d7 | 43 | struct strbuf fmt = STRBUF_INIT; |
dd2e794a | 44 | struct pretty_print_context ctx = {0}; |
a5481a6c | 45 | ctx.date_mode.type = DATE_NORMAL; |
35039ced | 46 | ctx.abbrev = DEFAULT_ABBREV; |
18125644 LH |
47 | |
48 | if (src == buf->buf) | |
49 | to_free = strbuf_detach(buf, NULL); | |
18125644 LH |
50 | for (;;) { |
51 | const char *b, *c; | |
52 | ||
53 | b = memmem(src, len, "$Format:", 8); | |
75b7dfbd | 54 | if (!b) |
18125644 | 55 | break; |
75b7dfbd | 56 | c = memchr(b + 8, '$', (src + len) - b - 8); |
18125644 LH |
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); | |
dd2e794a | 64 | format_commit_message(commit, fmt.buf, buf, &ctx); |
18125644 LH |
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 | ||
e5ec981a | 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) | |
18125644 LH |
77 | { |
78 | void *buffer; | |
9cb513b7 | 79 | const struct commit *commit = args->convert ? args->commit : NULL; |
c397aac0 | 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); | |
18125644 | 85 | |
9cb513b7 | 86 | path += args->baselen; |
b4f5aca4 | 87 | buffer = read_object_file(oid, type, sizep); |
18125644 | 88 | if (buffer && S_ISREG(mode)) { |
f285a2d7 | 89 | struct strbuf buf = STRBUF_INIT; |
18125644 LH |
90 | size_t size = 0; |
91 | ||
18125644 | 92 | strbuf_attach(&buf, buffer, *sizep, *sizep + 1); |
c397aac0 | 93 | convert_to_working_tree(args->repo->index, path, buf.buf, buf.len, &buf, &meta); |
1d11d5bb RS |
94 | if (commit) |
95 | format_subst(commit, buf.buf, buf.len, &buf); | |
18125644 LH |
96 | buffer = strbuf_detach(&buf, &size); |
97 | *sizep = size; | |
98 | } | |
99 | ||
100 | return buffer; | |
101 | } | |
102 | ||
ed22b417 NTND |
103 | struct directory { |
104 | struct directory *up; | |
13609673 | 105 | struct object_id oid; |
ed22b417 NTND |
106 | int baselen, len; |
107 | unsigned mode; | |
108 | int stage; | |
109 | char path[FLEX_ARRAY]; | |
110 | }; | |
111 | ||
562e25ab RS |
112 | struct 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 |
118 | static 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); | |
d64324cb TB |
124 | git_check_attr(istate, path, check); |
125 | return check; | |
c6c08f7e RS |
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 | ||
015ff4f8 | 138 | static int write_archive_entry(const struct object_id *oid, const char *base, |
562e25ab RS |
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; | |
5ff247ac | 147 | const char *path_without_prefix; |
562e25ab | 148 | |
9cb513b7 | 149 | args->convert = 0; |
562e25ab RS |
150 | strbuf_reset(&path); |
151 | strbuf_grow(&path, PATH_MAX); | |
ebfbdb34 | 152 | strbuf_add(&path, args->base, args->baselen); |
562e25ab RS |
153 | strbuf_add(&path, base, baselen); |
154 | strbuf_addstr(&path, filename); | |
94bc671a JNA |
155 | if (S_ISDIR(mode) || S_ISGITLINK(mode)) |
156 | strbuf_addch(&path, '/'); | |
1d11d5bb | 157 | path_without_prefix = path.buf + args->baselen; |
562e25ab | 158 | |
43180940 | 159 | if (!S_ISDIR(mode)) { |
5ff247ac | 160 | const struct attr_check *check; |
b612ee20 | 161 | check = get_archive_attrs(args->repo->index, path_without_prefix); |
5ff247ac | 162 | if (check_attr_export_ignore(check)) |
1d11d5bb | 163 | return 0; |
5ff247ac | 164 | args->convert = check_attr_export_subst(check); |
1d11d5bb | 165 | } |
562e25ab RS |
166 | |
167 | if (S_ISDIR(mode) || S_ISGITLINK(mode)) { | |
562e25ab RS |
168 | if (args->verbose) |
169 | fprintf(stderr, "%.*s\n", (int)path.len, path.buf); | |
015ff4f8 | 170 | err = write_entry(args, oid, path.buf, path.len, mode); |
562e25ab RS |
171 | if (err) |
172 | return err; | |
d3bee161 | 173 | return (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0); |
562e25ab RS |
174 | } |
175 | ||
562e25ab RS |
176 | if (args->verbose) |
177 | fprintf(stderr, "%.*s\n", (int)path.len, path.buf); | |
015ff4f8 | 178 | return write_entry(args, oid, path.buf, path.len, mode); |
562e25ab RS |
179 | } |
180 | ||
ed22b417 | 181 | static void queue_directory(const unsigned char *sha1, |
6a0b0b6d | 182 | struct strbuf *base, const char *filename, |
ed22b417 NTND |
183 | unsigned mode, int stage, struct archiver_context *c) |
184 | { | |
185 | struct directory *d; | |
50a6c8ef JK |
186 | size_t len = st_add4(base->len, 1, strlen(filename), 1); |
187 | d = xmalloc(st_add(sizeof(*d), len)); | |
ed22b417 | 188 | d->up = c->bottom; |
6a0b0b6d | 189 | d->baselen = base->len; |
ed22b417 NTND |
190 | d->mode = mode; |
191 | d->stage = stage; | |
192 | c->bottom = d; | |
c7ab0ba3 | 193 | d->len = xsnprintf(d->path, len, "%.*s%s/", (int)base->len, base->buf, filename); |
13609673 | 194 | hashcpy(d->oid.hash, sha1); |
ed22b417 NTND |
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) || | |
015ff4f8 | 208 | write_archive_entry(&d->oid, d->path, d->baselen, |
ed22b417 NTND |
209 | d->path + d->baselen, d->mode, |
210 | d->stage, c) != READ_TREE_RECURSIVE; | |
211 | free(d); | |
212 | return ret ? -1 : 0; | |
213 | } | |
214 | ||
df46d77e | 215 | static int queue_or_write_archive_entry(const struct object_id *oid, |
6a0b0b6d | 216 | struct strbuf *base, const char *filename, |
ed22b417 NTND |
217 | unsigned mode, int stage, void *context) |
218 | { | |
219 | struct archiver_context *c = context; | |
220 | ||
221 | while (c->bottom && | |
6a0b0b6d NTND |
222 | !(base->len >= c->bottom->len && |
223 | !strncmp(base->buf, c->bottom->path, c->bottom->len))) { | |
ed22b417 NTND |
224 | struct directory *next = c->bottom->up; |
225 | free(c->bottom); | |
226 | c->bottom = next; | |
227 | } | |
228 | ||
229 | if (S_ISDIR(mode)) { | |
5ff247ac RS |
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, '/'); | |
b612ee20 | 236 | check = get_archive_attrs(c->args->repo->index, base->buf); |
5ff247ac RS |
237 | strbuf_setlen(base, baselen); |
238 | ||
239 | if (check_attr_export_ignore(check)) | |
240 | return 0; | |
df46d77e | 241 | queue_directory(oid->hash, base, filename, |
ed22b417 NTND |
242 | mode, stage, c); |
243 | return READ_TREE_RECURSIVE; | |
244 | } | |
245 | ||
246 | if (write_directory(c)) | |
247 | return -1; | |
015ff4f8 | 248 | return write_archive_entry(oid, base->buf, base->len, filename, mode, |
ed22b417 NTND |
249 | stage, context); |
250 | } | |
251 | ||
562e25ab RS |
252 | int write_archive_entries(struct archiver_args *args, |
253 | write_archive_entry_fn_t write_entry) | |
254 | { | |
255 | struct archiver_context context; | |
ba053ea9 NTND |
256 | struct unpack_trees_options opts; |
257 | struct tree_desc t; | |
562e25ab RS |
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); | |
015ff4f8 | 267 | err = write_entry(args, &args->tree->object.oid, args->base, |
9cb513b7 | 268 | len, 040777); |
562e25ab RS |
269 | if (err) |
270 | return err; | |
271 | } | |
272 | ||
ed22b417 | 273 | memset(&context, 0, sizeof(context)); |
562e25ab RS |
274 | context.args = args; |
275 | context.write_entry = write_entry; | |
276 | ||
ba053ea9 NTND |
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; | |
b612ee20 NTND |
284 | opts.src_index = args->repo->index; |
285 | opts.dst_index = args->repo->index; | |
ba053ea9 NTND |
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; | |
c4500e25 | 290 | git_attr_set_direction(GIT_ATTR_INDEX); |
ba053ea9 NTND |
291 | } |
292 | ||
e092073d NTND |
293 | err = read_tree_recursive(args->repo, args->tree, "", |
294 | 0, 0, &args->pathspec, | |
43180940 | 295 | queue_or_write_archive_entry, |
ed22b417 | 296 | &context); |
562e25ab RS |
297 | if (err == READ_TREE_RECURSIVE) |
298 | err = 0; | |
ed22b417 NTND |
299 | while (context.bottom) { |
300 | struct directory *next = context.bottom->up; | |
301 | free(context.bottom); | |
302 | context.bottom = next; | |
303 | } | |
562e25ab RS |
304 | return err; |
305 | } | |
6e94e683 | 306 | |
c0885435 RS |
307 | static const struct archiver *lookup_archiver(const char *name) |
308 | { | |
309 | int i; | |
310 | ||
4fac1d3a RS |
311 | if (!name) |
312 | return NULL; | |
313 | ||
13e0f88d JK |
314 | for (i = 0; i < nr_archivers; i++) { |
315 | if (!strcmp(name, archivers[i]->name)) | |
316 | return archivers[i]; | |
c0885435 RS |
317 | } |
318 | return NULL; | |
319 | } | |
320 | ||
b612ee20 NTND |
321 | struct path_exists_context { |
322 | struct pathspec pathspec; | |
323 | struct archiver_args *args; | |
324 | }; | |
325 | ||
df46d77e | 326 | static int reject_entry(const struct object_id *oid, struct strbuf *base, |
6a0b0b6d | 327 | const char *filename, unsigned mode, |
d5f53d6d RS |
328 | int stage, void *context) |
329 | { | |
ed22b417 | 330 | int ret = -1; |
b612ee20 NTND |
331 | struct path_exists_context *ctx = context; |
332 | ||
ed22b417 NTND |
333 | if (S_ISDIR(mode)) { |
334 | struct strbuf sb = STRBUF_INIT; | |
6a0b0b6d | 335 | strbuf_addbuf(&sb, base); |
ed22b417 | 336 | strbuf_addstr(&sb, filename); |
b612ee20 NTND |
337 | if (!match_pathspec(ctx->args->repo->index, |
338 | &ctx->pathspec, | |
339 | sb.buf, sb.len, 0, NULL, 1)) | |
ed22b417 NTND |
340 | ret = READ_TREE_RECURSIVE; |
341 | strbuf_release(&sb); | |
342 | } | |
343 | return ret; | |
d5f53d6d RS |
344 | } |
345 | ||
b612ee20 | 346 | static int path_exists(struct archiver_args *args, const char *path) |
d5f53d6d | 347 | { |
f0096c06 | 348 | const char *paths[] = { path, NULL }; |
b612ee20 | 349 | struct path_exists_context ctx; |
f0096c06 NTND |
350 | int ret; |
351 | ||
b612ee20 NTND |
352 | ctx.args = args; |
353 | parse_pathspec(&ctx.pathspec, 0, 0, "", paths); | |
354 | ctx.pathspec.recursive = 1; | |
e092073d NTND |
355 | ret = read_tree_recursive(args->repo, args->tree, "", |
356 | 0, 0, &ctx.pathspec, | |
b612ee20 NTND |
357 | reject_entry, &ctx); |
358 | clear_pathspec(&ctx.pathspec); | |
f0096c06 | 359 | return ret != 0; |
d5f53d6d RS |
360 | } |
361 | ||
c0885435 RS |
362 | static void parse_pathspec_arg(const char **pathspec, |
363 | struct archiver_args *ar_args) | |
364 | { | |
f3e743a0 NTND |
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); | |
ed22b417 | 373 | ar_args->pathspec.recursive = 1; |
d5f53d6d RS |
374 | if (pathspec) { |
375 | while (*pathspec) { | |
b612ee20 | 376 | if (**pathspec && !path_exists(ar_args, *pathspec)) |
f3e743a0 | 377 | die(_("pathspec '%s' did not match any files"), *pathspec); |
d5f53d6d RS |
378 | pathspec++; |
379 | } | |
380 | } | |
c0885435 RS |
381 | } |
382 | ||
383 | static void parse_treeish_arg(const char **argv, | |
ee27ca4a JK |
384 | struct archiver_args *ar_args, const char *prefix, |
385 | int remote) | |
c0885435 RS |
386 | { |
387 | const char *name = argv[0]; | |
bbf05cf7 | 388 | const struct object_id *commit_oid; |
c0885435 RS |
389 | time_t archive_time; |
390 | struct tree *tree; | |
391 | const struct commit *commit; | |
13609673 | 392 | struct object_id oid; |
c397aac0 | 393 | char *ref = NULL; |
c0885435 | 394 | |
ee27ca4a | 395 | /* Remotes are only allowed to fetch actual refs */ |
7671b632 | 396 | if (remote && !remote_allow_unreachable) { |
2c5495f7 RM |
397 | const char *colon = strchrnul(name, ':'); |
398 | int refnamelen = colon - name; | |
c51a351a | 399 | |
cca5fa64 | 400 | if (!dwim_ref(name, refnamelen, &oid, &ref)) |
c6e7965d | 401 | die(_("no such ref: %.*s"), refnamelen, name); |
c397aac0 | 402 | } else { |
403 | dwim_ref(name, strlen(name), &oid, &ref); | |
ee27ca4a | 404 | } |
0f544ee8 | 405 | |
e82caf38 | 406 | if (get_oid(name, &oid)) |
c6e7965d | 407 | die(_("not a valid object name: %s"), name); |
c0885435 | 408 | |
ce3a7ec8 | 409 | commit = lookup_commit_reference_gently(ar_args->repo, &oid, 1); |
c0885435 | 410 | if (commit) { |
bbf05cf7 | 411 | commit_oid = &commit->object.oid; |
c0885435 RS |
412 | archive_time = commit->date; |
413 | } else { | |
bbf05cf7 | 414 | commit_oid = NULL; |
c0885435 RS |
415 | archive_time = time(NULL); |
416 | } | |
417 | ||
a9dbc179 | 418 | tree = parse_tree_indirect(&oid); |
c0885435 | 419 | if (tree == NULL) |
c6e7965d | 420 | die(_("not a tree object: %s"), oid_to_hex(&oid)); |
c0885435 RS |
421 | |
422 | if (prefix) { | |
13609673 | 423 | struct object_id tree_oid; |
5ec1e728 | 424 | unsigned short mode; |
c0885435 RS |
425 | int err; |
426 | ||
50ddb089 NTND |
427 | err = get_tree_entry(ar_args->repo, |
428 | &tree->object.oid, | |
429 | prefix, &tree_oid, | |
916bc35b | 430 | &mode); |
c0885435 | 431 | if (err || !S_ISDIR(mode)) |
c6e7965d | 432 | die(_("current working directory is untracked")); |
c0885435 | 433 | |
a9dbc179 | 434 | tree = parse_tree_indirect(&tree_oid); |
c0885435 | 435 | } |
c397aac0 | 436 | ar_args->refname = ref; |
c0885435 | 437 | ar_args->tree = tree; |
bbf05cf7 | 438 | ar_args->commit_oid = commit_oid; |
c0885435 RS |
439 | ar_args->commit = commit; |
440 | ar_args->time = archive_time; | |
441 | } | |
442 | ||
4fac1d3a | 443 | #define OPT__COMPR(s, v, h, p) \ |
3e4a67b4 | 444 | OPT_SET_INT_F(s, NULL, v, h, p, PARSE_OPT_NONEG) |
4fac1d3a | 445 | #define OPT__COMPR_HIDDEN(s, v, p) \ |
3e4a67b4 | 446 | OPT_SET_INT_F(s, NULL, v, "", p, PARSE_OPT_NONEG | PARSE_OPT_HIDDEN) |
4fac1d3a | 447 | |
c0885435 | 448 | static int parse_archive_args(int argc, const char **argv, |
56baa61d | 449 | const struct archiver **ar, struct archiver_args *args, |
7b97730b | 450 | const char *name_hint, int is_remote) |
c0885435 | 451 | { |
56baa61d | 452 | const char *format = NULL; |
4fac1d3a RS |
453 | const char *base = NULL; |
454 | const char *remote = NULL; | |
455 | const char *exec = NULL; | |
aec0c1bb | 456 | const char *output = NULL; |
c0885435 RS |
457 | int compression_level = -1; |
458 | int verbose = 0; | |
459 | int i; | |
4fac1d3a | 460 | int list = 0; |
ba053ea9 | 461 | int worktree_attributes = 0; |
4fac1d3a RS |
462 | struct option opts[] = { |
463 | OPT_GROUP(""), | |
0012a387 NTND |
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")), | |
f858c646 | 469 | OPT_BOOL(0, "worktree-attributes", &worktree_attributes, |
0012a387 NTND |
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), | |
4fac1d3a RS |
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), | |
0012a387 | 481 | OPT__COMPR('9', &compression_level, N_("compress better"), 9), |
4fac1d3a | 482 | OPT_GROUP(""), |
f858c646 | 483 | OPT_BOOL('l', "list", &list, |
0012a387 | 484 | N_("list supported archive formats")), |
4fac1d3a | 485 | OPT_GROUP(""), |
0012a387 NTND |
486 | OPT_STRING(0, "remote", &remote, N_("repo"), |
487 | N_("retrieve the archive from remote repository <repo>")), | |
b0ff9654 | 488 | OPT_STRING(0, "exec", &exec, N_("command"), |
0012a387 | 489 | N_("path to the remote git-upload-archive command")), |
4fac1d3a RS |
490 | OPT_END() |
491 | }; | |
492 | ||
37782920 | 493 | argc = parse_options(argc, argv, NULL, opts, archive_usage, 0); |
4fac1d3a RS |
494 | |
495 | if (remote) | |
5a36d00c | 496 | die(_("Unexpected option --remote")); |
4fac1d3a | 497 | if (exec) |
5a36d00c | 498 | die(_("Option --exec can only be used together with --remote")); |
52e77876 | 499 | if (output) |
5a36d00c | 500 | die(_("Unexpected option --output")); |
4fac1d3a RS |
501 | |
502 | if (!base) | |
503 | base = ""; | |
504 | ||
505 | if (list) { | |
13e0f88d | 506 | for (i = 0; i < nr_archivers; i++) |
7b97730b JK |
507 | if (!is_remote || archivers[i]->flags & ARCHIVER_REMOTE) |
508 | printf("%s\n", archivers[i]->name); | |
4fac1d3a | 509 | exit(0); |
c0885435 RS |
510 | } |
511 | ||
56baa61d JK |
512 | if (!format && name_hint) |
513 | format = archive_format_from_filename(name_hint); | |
514 | if (!format) | |
515 | format = "tar"; | |
516 | ||
c0885435 | 517 | /* We need at least one parameter -- tree-ish */ |
4fac1d3a RS |
518 | if (argc < 1) |
519 | usage_with_options(archive_usage, opts); | |
c0885435 | 520 | *ar = lookup_archiver(format); |
7b97730b | 521 | if (!*ar || (is_remote && !((*ar)->flags & ARCHIVER_REMOTE))) |
5a36d00c | 522 | die(_("Unknown archive format '%s'"), format); |
c0885435 RS |
523 | |
524 | args->compression_level = Z_DEFAULT_COMPRESSION; | |
525 | if (compression_level != -1) { | |
13e0f88d | 526 | if ((*ar)->flags & ARCHIVER_WANT_COMPRESSION_LEVELS) |
c0885435 RS |
527 | args->compression_level = compression_level; |
528 | else { | |
5a36d00c | 529 | die(_("Argument not supported for format '%s': -%d"), |
c0885435 RS |
530 | format, compression_level); |
531 | } | |
532 | } | |
533 | args->verbose = verbose; | |
534 | args->base = base; | |
535 | args->baselen = strlen(base); | |
ba053ea9 | 536 | args->worktree_attributes = worktree_attributes; |
c0885435 | 537 | |
4fac1d3a | 538 | return argc; |
c0885435 RS |
539 | } |
540 | ||
6e94e683 | 541 | int write_archive(int argc, const char **argv, const char *prefix, |
b612ee20 | 542 | struct repository *repo, |
eb0224c6 | 543 | const char *name_hint, int remote) |
6e94e683 RS |
544 | { |
545 | const struct archiver *ar = NULL; | |
546 | struct archiver_args args; | |
6e94e683 | 547 | |
95790ff6 TA |
548 | git_config_get_bool("uploadarchive.allowunreachable", &remote_allow_unreachable); |
549 | git_config(git_default_config, NULL); | |
550 | ||
b612ee20 | 551 | args.repo = repo; |
7b97730b | 552 | argc = parse_archive_args(argc, argv, &ar, &args, name_hint, remote); |
eb0224c6 | 553 | if (!startup_info->have_repository) { |
23212862 JK |
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 | } | |
6e94e683 | 561 | |
ee27ca4a | 562 | parse_treeish_arg(argv, &args, prefix, remote); |
6e94e683 RS |
563 | parse_pathspec_arg(argv + 1, &args); |
564 | ||
4d7c9898 | 565 | return ar->write_archive(ar, &args); |
6e94e683 | 566 | } |
56baa61d | 567 | |
08716b3c JK |
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 | */ | |
b1cdfb54 | 577 | if (prefixlen < 2 || filename[prefixlen - 1] != '.') |
08716b3c JK |
578 | return 0; |
579 | return !strcmp(filename + prefixlen, ext); | |
580 | } | |
581 | ||
56baa61d JK |
582 | const char *archive_format_from_filename(const char *filename) |
583 | { | |
08716b3c JK |
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; | |
56baa61d JK |
589 | return NULL; |
590 | } |