t0410: make test description clearer
[git/gitster.git] / builtin / read-tree.c
blobd2a807a828b6aba726d325ec49cf01f8e84858d7
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #define USE_THE_REPOSITORY_VARIABLE
7 #include "builtin.h"
8 #include "config.h"
9 #include "gettext.h"
10 #include "hex.h"
11 #include "lockfile.h"
12 #include "object.h"
13 #include "object-name.h"
14 #include "tree.h"
15 #include "tree-walk.h"
16 #include "cache-tree.h"
17 #include "unpack-trees.h"
18 #include "parse-options.h"
19 #include "resolve-undo.h"
20 #include "setup.h"
21 #include "sparse-index.h"
22 #include "submodule.h"
24 static int nr_trees;
25 static int read_empty;
26 static struct tree *trees[MAX_UNPACK_TREES];
28 static int list_tree(struct object_id *oid)
30 struct tree *tree;
32 if (nr_trees >= MAX_UNPACK_TREES)
33 die("I cannot read more than %d trees", MAX_UNPACK_TREES);
34 tree = parse_tree_indirect(oid);
35 if (!tree)
36 return -1;
37 trees[nr_trees++] = tree;
38 return 0;
41 static const char * const read_tree_usage[] = {
42 N_("git read-tree [(-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>)\n"
43 " [-u | -i]] [--index-output=<file>] [--no-sparse-checkout]\n"
44 " (--empty | <tree-ish1> [<tree-ish2> [<tree-ish3>]])"),
45 NULL
48 static int index_output_cb(const struct option *opt UNUSED, const char *arg,
49 int unset)
51 BUG_ON_OPT_NEG(unset);
52 set_alternate_index_output(arg);
53 return 0;
56 static int exclude_per_directory_cb(const struct option *opt, const char *arg,
57 int unset)
59 struct unpack_trees_options *opts;
61 BUG_ON_OPT_NEG(unset);
63 opts = (struct unpack_trees_options *)opt->value;
65 if (!opts->update)
66 die("--exclude-per-directory is meaningless unless -u");
67 if (strcmp(arg, ".gitignore"))
68 die("--exclude-per-directory argument must be .gitignore");
69 return 0;
72 static void debug_stage(const char *label, const struct cache_entry *ce,
73 struct unpack_trees_options *o)
75 printf("%s ", label);
76 if (!ce)
77 printf("(missing)\n");
78 else if (ce == o->df_conflict_entry)
79 printf("(conflict)\n");
80 else
81 printf("%06o #%d %s %.8s\n",
82 ce->ce_mode, ce_stage(ce), ce->name,
83 oid_to_hex(&ce->oid));
86 static int debug_merge(const struct cache_entry * const *stages,
87 struct unpack_trees_options *o)
89 int i;
91 printf("* %d-way merge\n", o->internal.merge_size);
92 debug_stage("index", stages[0], o);
93 for (i = 1; i <= o->internal.merge_size; i++) {
94 char buf[24];
95 xsnprintf(buf, sizeof(buf), "ent#%d", i);
96 debug_stage(buf, stages[i], o);
98 return 0;
101 static int git_read_tree_config(const char *var, const char *value,
102 const struct config_context *ctx, void *cb)
104 if (!strcmp(var, "submodule.recurse"))
105 return git_default_submodule_config(var, value, cb);
107 return git_default_config(var, value, ctx, cb);
110 int cmd_read_tree(int argc,
111 const char **argv,
112 const char *cmd_prefix,
113 struct repository *repo UNUSED)
115 int i, stage = 0;
116 struct object_id oid;
117 struct tree_desc t[MAX_UNPACK_TREES];
118 struct unpack_trees_options opts;
119 int prefix_set = 0;
120 struct lock_file lock_file = LOCK_INIT;
121 const struct option read_tree_options[] = {
122 OPT__SUPER_PREFIX(&opts.super_prefix),
123 OPT_CALLBACK_F(0, "index-output", NULL, N_("file"),
124 N_("write resulting index to <file>"),
125 PARSE_OPT_NONEG, index_output_cb),
126 OPT_BOOL(0, "empty", &read_empty,
127 N_("only empty the index")),
128 OPT__VERBOSE(&opts.verbose_update, N_("be verbose")),
129 OPT_GROUP(N_("Merging")),
130 OPT_BOOL('m', NULL, &opts.merge,
131 N_("perform a merge in addition to a read")),
132 OPT_BOOL(0, "trivial", &opts.trivial_merges_only,
133 N_("3-way merge if no file level merging required")),
134 OPT_BOOL(0, "aggressive", &opts.aggressive,
135 N_("3-way merge in presence of adds and removes")),
136 OPT_BOOL(0, "reset", &opts.reset,
137 N_("same as -m, but discard unmerged entries")),
138 { OPTION_STRING, 0, "prefix", &opts.prefix, N_("<subdirectory>/"),
139 N_("read the tree into the index under <subdirectory>/"),
140 PARSE_OPT_NONEG },
141 OPT_BOOL('u', NULL, &opts.update,
142 N_("update working tree with merge result")),
143 OPT_CALLBACK_F(0, "exclude-per-directory", &opts,
144 N_("gitignore"),
145 N_("allow explicitly ignored files to be overwritten"),
146 PARSE_OPT_NONEG, exclude_per_directory_cb),
147 OPT_BOOL('i', NULL, &opts.index_only,
148 N_("don't check the working tree after merging")),
149 OPT__DRY_RUN(&opts.dry_run, N_("don't update the index or the work tree")),
150 OPT_BOOL(0, "no-sparse-checkout", &opts.skip_sparse_checkout,
151 N_("skip applying sparse checkout filter")),
152 OPT_BOOL(0, "debug-unpack", &opts.internal.debug_unpack,
153 N_("debug unpack-trees")),
154 OPT_CALLBACK_F(0, "recurse-submodules", NULL,
155 "checkout", "control recursive updating of submodules",
156 PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater),
157 OPT__QUIET(&opts.quiet, N_("suppress feedback messages")),
158 OPT_END()
161 memset(&opts, 0, sizeof(opts));
162 opts.head_idx = -1;
163 opts.src_index = the_repository->index;
164 opts.dst_index = the_repository->index;
166 git_config(git_read_tree_config, NULL);
168 argc = parse_options(argc, argv, cmd_prefix, read_tree_options,
169 read_tree_usage, 0);
171 prefix_set = opts.prefix ? 1 : 0;
172 if (1 < opts.merge + opts.reset + prefix_set)
173 die("Which one? -m, --reset, or --prefix?");
175 /* Prefix should not start with a directory separator */
176 if (opts.prefix && opts.prefix[0] == '/')
177 die("Invalid prefix, prefix cannot start with '/'");
179 if (opts.reset)
180 opts.reset = UNPACK_RESET_OVERWRITE_UNTRACKED;
182 prepare_repo_settings(the_repository);
183 the_repository->settings.command_requires_full_index = 0;
185 repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
188 * NEEDSWORK
190 * The old index should be read anyway even if we're going to
191 * destroy all index entries because we still need to preserve
192 * certain information such as index version or split-index
193 * mode.
196 if (opts.reset || opts.merge || opts.prefix) {
197 if (repo_read_index_unmerged(the_repository) && (opts.prefix || opts.merge))
198 die(_("You need to resolve your current index first"));
199 stage = opts.merge = 1;
201 resolve_undo_clear_index(the_repository->index);
203 for (i = 0; i < argc; i++) {
204 const char *arg = argv[i];
206 if (repo_get_oid(the_repository, arg, &oid))
207 die("Not a valid object name %s", arg);
208 if (list_tree(&oid) < 0)
209 die("failed to unpack tree object %s", arg);
210 stage++;
212 if (!nr_trees && !read_empty && !opts.merge)
213 warning("read-tree: emptying the index with no arguments is deprecated; use --empty");
214 else if (nr_trees > 0 && read_empty)
215 die("passing trees as arguments contradicts --empty");
217 if (1 < opts.index_only + opts.update)
218 die("-u and -i at the same time makes no sense");
219 if ((opts.update || opts.index_only) && !opts.merge)
220 die("%s is meaningless without -m, --reset, or --prefix",
221 opts.update ? "-u" : "-i");
222 if (opts.update && !opts.reset)
223 opts.preserve_ignored = 0;
224 /* otherwise, opts.preserve_ignored is irrelevant */
225 if (opts.merge && !opts.index_only)
226 setup_work_tree();
228 if (opts.skip_sparse_checkout)
229 ensure_full_index(the_repository->index);
231 if (opts.merge) {
232 switch (stage - 1) {
233 case 0:
234 die("you must specify at least one tree to merge");
235 break;
236 case 1:
237 opts.fn = opts.prefix ? bind_merge : oneway_merge;
238 break;
239 case 2:
240 opts.fn = twoway_merge;
241 opts.initial_checkout = is_index_unborn(the_repository->index);
242 break;
243 case 3:
244 default:
245 opts.fn = threeway_merge;
246 break;
249 if (stage - 1 >= 3)
250 opts.head_idx = stage - 2;
251 else
252 opts.head_idx = 1;
255 if (opts.internal.debug_unpack)
256 opts.fn = debug_merge;
258 /* If we're going to prime_cache_tree later, skip cache tree update */
259 if (nr_trees == 1 && !opts.prefix)
260 opts.skip_cache_tree_update = 1;
262 cache_tree_free(&the_repository->index->cache_tree);
263 for (i = 0; i < nr_trees; i++) {
264 struct tree *tree = trees[i];
265 if (parse_tree(tree) < 0)
266 return 128;
267 init_tree_desc(t+i, &tree->object.oid, tree->buffer, tree->size);
269 if (unpack_trees(nr_trees, t, &opts))
270 return 128;
272 if (opts.internal.debug_unpack || opts.dry_run)
273 return 0; /* do not write the index out */
276 * When reading only one tree (either the most basic form,
277 * "-m ent" or "--reset ent" form), we can obtain a fully
278 * valid cache-tree because the index must match exactly
279 * what came from the tree.
281 if (nr_trees == 1 && !opts.prefix)
282 prime_cache_tree(the_repository,
283 the_repository->index,
284 trees[0]);
286 if (write_locked_index(the_repository->index, &lock_file, COMMIT_LOCK))
287 die("unable to write new index file");
288 return 0;