archive.c 18 KB

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