THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

584 lines
15KB

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