read-tree.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * GIT - The information manager from hell
  3. *
  4. * Copyright (C) Linus Torvalds, 2005
  5. */
  6. #define USE_THE_INDEX_COMPATIBILITY_MACROS
  7. #include "cache.h"
  8. #include "config.h"
  9. #include "lockfile.h"
  10. #include "object.h"
  11. #include "tree.h"
  12. #include "tree-walk.h"
  13. #include "cache-tree.h"
  14. #include "unpack-trees.h"
  15. #include "dir.h"
  16. #include "builtin.h"
  17. #include "parse-options.h"
  18. #include "resolve-undo.h"
  19. #include "submodule.h"
  20. #include "submodule-config.h"
  21. static int nr_trees;
  22. static int read_empty;
  23. static struct tree *trees[MAX_UNPACK_TREES];
  24. static int list_tree(struct object_id *oid)
  25. {
  26. struct tree *tree;
  27. if (nr_trees >= MAX_UNPACK_TREES)
  28. die("I cannot read more than %d trees", MAX_UNPACK_TREES);
  29. tree = parse_tree_indirect(oid);
  30. if (!tree)
  31. return -1;
  32. trees[nr_trees++] = tree;
  33. return 0;
  34. }
  35. static const char * const read_tree_usage[] = {
  36. N_("git read-tree [(-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>) [-u [--exclude-per-directory=<gitignore>] | -i]] [--no-sparse-checkout] [--index-output=<file>] (--empty | <tree-ish1> [<tree-ish2> [<tree-ish3>]])"),
  37. NULL
  38. };
  39. static int index_output_cb(const struct option *opt, const char *arg,
  40. int unset)
  41. {
  42. BUG_ON_OPT_NEG(unset);
  43. set_alternate_index_output(arg);
  44. return 0;
  45. }
  46. static int exclude_per_directory_cb(const struct option *opt, const char *arg,
  47. int unset)
  48. {
  49. struct dir_struct *dir;
  50. struct unpack_trees_options *opts;
  51. BUG_ON_OPT_NEG(unset);
  52. opts = (struct unpack_trees_options *)opt->value;
  53. if (opts->dir)
  54. die("more than one --exclude-per-directory given.");
  55. dir = xcalloc(1, sizeof(*opts->dir));
  56. dir->flags |= DIR_SHOW_IGNORED;
  57. dir->exclude_per_dir = arg;
  58. opts->dir = dir;
  59. /* We do not need to nor want to do read-directory
  60. * here; we are merely interested in reusing the
  61. * per directory ignore stack mechanism.
  62. */
  63. return 0;
  64. }
  65. static void debug_stage(const char *label, const struct cache_entry *ce,
  66. struct unpack_trees_options *o)
  67. {
  68. printf("%s ", label);
  69. if (!ce)
  70. printf("(missing)\n");
  71. else if (ce == o->df_conflict_entry)
  72. printf("(conflict)\n");
  73. else
  74. printf("%06o #%d %s %.8s\n",
  75. ce->ce_mode, ce_stage(ce), ce->name,
  76. oid_to_hex(&ce->oid));
  77. }
  78. static int debug_merge(const struct cache_entry * const *stages,
  79. struct unpack_trees_options *o)
  80. {
  81. int i;
  82. printf("* %d-way merge\n", o->merge_size);
  83. debug_stage("index", stages[0], o);
  84. for (i = 1; i <= o->merge_size; i++) {
  85. char buf[24];
  86. xsnprintf(buf, sizeof(buf), "ent#%d", i);
  87. debug_stage(buf, stages[i], o);
  88. }
  89. return 0;
  90. }
  91. static int git_read_tree_config(const char *var, const char *value, void *cb)
  92. {
  93. if (!strcmp(var, "submodule.recurse"))
  94. return git_default_submodule_config(var, value, cb);
  95. return git_default_config(var, value, cb);
  96. }
  97. int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix)
  98. {
  99. int i, stage = 0;
  100. struct object_id oid;
  101. struct tree_desc t[MAX_UNPACK_TREES];
  102. struct unpack_trees_options opts;
  103. int prefix_set = 0;
  104. struct lock_file lock_file = LOCK_INIT;
  105. const struct option read_tree_options[] = {
  106. OPT_CALLBACK_F(0, "index-output", NULL, N_("file"),
  107. N_("write resulting index to <file>"),
  108. PARSE_OPT_NONEG, index_output_cb),
  109. OPT_BOOL(0, "empty", &read_empty,
  110. N_("only empty the index")),
  111. OPT__VERBOSE(&opts.verbose_update, N_("be verbose")),
  112. OPT_GROUP(N_("Merging")),
  113. OPT_BOOL('m', NULL, &opts.merge,
  114. N_("perform a merge in addition to a read")),
  115. OPT_BOOL(0, "trivial", &opts.trivial_merges_only,
  116. N_("3-way merge if no file level merging required")),
  117. OPT_BOOL(0, "aggressive", &opts.aggressive,
  118. N_("3-way merge in presence of adds and removes")),
  119. OPT_BOOL(0, "reset", &opts.reset,
  120. N_("same as -m, but discard unmerged entries")),
  121. { OPTION_STRING, 0, "prefix", &opts.prefix, N_("<subdirectory>/"),
  122. N_("read the tree into the index under <subdirectory>/"),
  123. PARSE_OPT_NONEG },
  124. OPT_BOOL('u', NULL, &opts.update,
  125. N_("update working tree with merge result")),
  126. OPT_CALLBACK_F(0, "exclude-per-directory", &opts,
  127. N_("gitignore"),
  128. N_("allow explicitly ignored files to be overwritten"),
  129. PARSE_OPT_NONEG, exclude_per_directory_cb),
  130. OPT_BOOL('i', NULL, &opts.index_only,
  131. N_("don't check the working tree after merging")),
  132. OPT__DRY_RUN(&opts.dry_run, N_("don't update the index or the work tree")),
  133. OPT_BOOL(0, "no-sparse-checkout", &opts.skip_sparse_checkout,
  134. N_("skip applying sparse checkout filter")),
  135. OPT_BOOL(0, "debug-unpack", &opts.debug_unpack,
  136. N_("debug unpack-trees")),
  137. OPT_CALLBACK_F(0, "recurse-submodules", NULL,
  138. "checkout", "control recursive updating of submodules",
  139. PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater),
  140. OPT__QUIET(&opts.quiet, N_("suppress feedback messages")),
  141. OPT_END()
  142. };
  143. memset(&opts, 0, sizeof(opts));
  144. opts.head_idx = -1;
  145. opts.src_index = &the_index;
  146. opts.dst_index = &the_index;
  147. git_config(git_read_tree_config, NULL);
  148. argc = parse_options(argc, argv, cmd_prefix, read_tree_options,
  149. read_tree_usage, 0);
  150. hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
  151. prefix_set = opts.prefix ? 1 : 0;
  152. if (1 < opts.merge + opts.reset + prefix_set)
  153. die("Which one? -m, --reset, or --prefix?");
  154. /*
  155. * NEEDSWORK
  156. *
  157. * The old index should be read anyway even if we're going to
  158. * destroy all index entries because we still need to preserve
  159. * certain information such as index version or split-index
  160. * mode.
  161. */
  162. if (opts.reset || opts.merge || opts.prefix) {
  163. if (read_cache_unmerged() && (opts.prefix || opts.merge))
  164. die(_("You need to resolve your current index first"));
  165. stage = opts.merge = 1;
  166. }
  167. resolve_undo_clear();
  168. for (i = 0; i < argc; i++) {
  169. const char *arg = argv[i];
  170. if (get_oid(arg, &oid))
  171. die("Not a valid object name %s", arg);
  172. if (list_tree(&oid) < 0)
  173. die("failed to unpack tree object %s", arg);
  174. stage++;
  175. }
  176. if (!nr_trees && !read_empty && !opts.merge)
  177. warning("read-tree: emptying the index with no arguments is deprecated; use --empty");
  178. else if (nr_trees > 0 && read_empty)
  179. die("passing trees as arguments contradicts --empty");
  180. if (1 < opts.index_only + opts.update)
  181. die("-u and -i at the same time makes no sense");
  182. if ((opts.update || opts.index_only) && !opts.merge)
  183. die("%s is meaningless without -m, --reset, or --prefix",
  184. opts.update ? "-u" : "-i");
  185. if ((opts.dir && !opts.update))
  186. die("--exclude-per-directory is meaningless unless -u");
  187. if (opts.merge && !opts.index_only)
  188. setup_work_tree();
  189. if (opts.merge) {
  190. switch (stage - 1) {
  191. case 0:
  192. die("you must specify at least one tree to merge");
  193. break;
  194. case 1:
  195. opts.fn = opts.prefix ? bind_merge : oneway_merge;
  196. break;
  197. case 2:
  198. opts.fn = twoway_merge;
  199. opts.initial_checkout = is_cache_unborn();
  200. break;
  201. case 3:
  202. default:
  203. opts.fn = threeway_merge;
  204. break;
  205. }
  206. if (stage - 1 >= 3)
  207. opts.head_idx = stage - 2;
  208. else
  209. opts.head_idx = 1;
  210. }
  211. if (opts.debug_unpack)
  212. opts.fn = debug_merge;
  213. cache_tree_free(&active_cache_tree);
  214. for (i = 0; i < nr_trees; i++) {
  215. struct tree *tree = trees[i];
  216. parse_tree(tree);
  217. init_tree_desc(t+i, tree->buffer, tree->size);
  218. }
  219. if (unpack_trees(nr_trees, t, &opts))
  220. return 128;
  221. if (opts.debug_unpack || opts.dry_run)
  222. return 0; /* do not write the index out */
  223. /*
  224. * When reading only one tree (either the most basic form,
  225. * "-m ent" or "--reset ent" form), we can obtain a fully
  226. * valid cache-tree because the index must match exactly
  227. * what came from the tree.
  228. */
  229. if (nr_trees == 1 && !opts.prefix)
  230. prime_cache_tree(the_repository,
  231. the_repository->index,
  232. trees[0]);
  233. if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
  234. die("unable to write new index file");
  235. return 0;
  236. }