]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/read-tree.c
cache.h: remove dependence on hex.h; make other files include it explicitly
[thirdparty/git.git] / builtin / read-tree.c
1 /*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6
7 #define USE_THE_INDEX_VARIABLE
8 #include "cache.h"
9 #include "config.h"
10 #include "hex.h"
11 #include "lockfile.h"
12 #include "object.h"
13 #include "tree.h"
14 #include "tree-walk.h"
15 #include "cache-tree.h"
16 #include "unpack-trees.h"
17 #include "dir.h"
18 #include "builtin.h"
19 #include "parse-options.h"
20 #include "resolve-undo.h"
21 #include "submodule.h"
22 #include "submodule-config.h"
23
24 static int nr_trees;
25 static int read_empty;
26 static struct tree *trees[MAX_UNPACK_TREES];
27
28 static int list_tree(struct object_id *oid)
29 {
30 struct tree *tree;
31
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;
39 }
40
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
46 };
47
48 static int index_output_cb(const struct option *opt, const char *arg,
49 int unset)
50 {
51 BUG_ON_OPT_NEG(unset);
52 set_alternate_index_output(arg);
53 return 0;
54 }
55
56 static int exclude_per_directory_cb(const struct option *opt, const char *arg,
57 int unset)
58 {
59 struct unpack_trees_options *opts;
60
61 BUG_ON_OPT_NEG(unset);
62
63 opts = (struct unpack_trees_options *)opt->value;
64
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;
70 }
71
72 static void debug_stage(const char *label, const struct cache_entry *ce,
73 struct unpack_trees_options *o)
74 {
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));
84 }
85
86 static int debug_merge(const struct cache_entry * const *stages,
87 struct unpack_trees_options *o)
88 {
89 int i;
90
91 printf("* %d-way merge\n", o->merge_size);
92 debug_stage("index", stages[0], o);
93 for (i = 1; i <= o->merge_size; i++) {
94 char buf[24];
95 xsnprintf(buf, sizeof(buf), "ent#%d", i);
96 debug_stage(buf, stages[i], o);
97 }
98 return 0;
99 }
100
101 static int git_read_tree_config(const char *var, const char *value, void *cb)
102 {
103 if (!strcmp(var, "submodule.recurse"))
104 return git_default_submodule_config(var, value, cb);
105
106 return git_default_config(var, value, cb);
107 }
108
109 int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix)
110 {
111 int i, stage = 0;
112 struct object_id oid;
113 struct tree_desc t[MAX_UNPACK_TREES];
114 struct unpack_trees_options opts;
115 int prefix_set = 0;
116 struct lock_file lock_file = LOCK_INIT;
117 const struct option read_tree_options[] = {
118 OPT__SUPER_PREFIX(&opts.super_prefix),
119 OPT_CALLBACK_F(0, "index-output", NULL, N_("file"),
120 N_("write resulting index to <file>"),
121 PARSE_OPT_NONEG, index_output_cb),
122 OPT_BOOL(0, "empty", &read_empty,
123 N_("only empty the index")),
124 OPT__VERBOSE(&opts.verbose_update, N_("be verbose")),
125 OPT_GROUP(N_("Merging")),
126 OPT_BOOL('m', NULL, &opts.merge,
127 N_("perform a merge in addition to a read")),
128 OPT_BOOL(0, "trivial", &opts.trivial_merges_only,
129 N_("3-way merge if no file level merging required")),
130 OPT_BOOL(0, "aggressive", &opts.aggressive,
131 N_("3-way merge in presence of adds and removes")),
132 OPT_BOOL(0, "reset", &opts.reset,
133 N_("same as -m, but discard unmerged entries")),
134 { OPTION_STRING, 0, "prefix", &opts.prefix, N_("<subdirectory>/"),
135 N_("read the tree into the index under <subdirectory>/"),
136 PARSE_OPT_NONEG },
137 OPT_BOOL('u', NULL, &opts.update,
138 N_("update working tree with merge result")),
139 OPT_CALLBACK_F(0, "exclude-per-directory", &opts,
140 N_("gitignore"),
141 N_("allow explicitly ignored files to be overwritten"),
142 PARSE_OPT_NONEG, exclude_per_directory_cb),
143 OPT_BOOL('i', NULL, &opts.index_only,
144 N_("don't check the working tree after merging")),
145 OPT__DRY_RUN(&opts.dry_run, N_("don't update the index or the work tree")),
146 OPT_BOOL(0, "no-sparse-checkout", &opts.skip_sparse_checkout,
147 N_("skip applying sparse checkout filter")),
148 OPT_BOOL(0, "debug-unpack", &opts.debug_unpack,
149 N_("debug unpack-trees")),
150 OPT_CALLBACK_F(0, "recurse-submodules", NULL,
151 "checkout", "control recursive updating of submodules",
152 PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater),
153 OPT__QUIET(&opts.quiet, N_("suppress feedback messages")),
154 OPT_END()
155 };
156
157 memset(&opts, 0, sizeof(opts));
158 opts.head_idx = -1;
159 opts.src_index = &the_index;
160 opts.dst_index = &the_index;
161
162 git_config(git_read_tree_config, NULL);
163
164 argc = parse_options(argc, argv, cmd_prefix, read_tree_options,
165 read_tree_usage, 0);
166
167 prefix_set = opts.prefix ? 1 : 0;
168 if (1 < opts.merge + opts.reset + prefix_set)
169 die("Which one? -m, --reset, or --prefix?");
170
171 /* Prefix should not start with a directory separator */
172 if (opts.prefix && opts.prefix[0] == '/')
173 die("Invalid prefix, prefix cannot start with '/'");
174
175 if (opts.reset)
176 opts.reset = UNPACK_RESET_OVERWRITE_UNTRACKED;
177
178 prepare_repo_settings(the_repository);
179 the_repository->settings.command_requires_full_index = 0;
180
181 repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
182
183 /*
184 * NEEDSWORK
185 *
186 * The old index should be read anyway even if we're going to
187 * destroy all index entries because we still need to preserve
188 * certain information such as index version or split-index
189 * mode.
190 */
191
192 if (opts.reset || opts.merge || opts.prefix) {
193 if (repo_read_index_unmerged(the_repository) && (opts.prefix || opts.merge))
194 die(_("You need to resolve your current index first"));
195 stage = opts.merge = 1;
196 }
197 resolve_undo_clear_index(&the_index);
198
199 for (i = 0; i < argc; i++) {
200 const char *arg = argv[i];
201
202 if (get_oid(arg, &oid))
203 die("Not a valid object name %s", arg);
204 if (list_tree(&oid) < 0)
205 die("failed to unpack tree object %s", arg);
206 stage++;
207 }
208 if (!nr_trees && !read_empty && !opts.merge)
209 warning("read-tree: emptying the index with no arguments is deprecated; use --empty");
210 else if (nr_trees > 0 && read_empty)
211 die("passing trees as arguments contradicts --empty");
212
213 if (1 < opts.index_only + opts.update)
214 die("-u and -i at the same time makes no sense");
215 if ((opts.update || opts.index_only) && !opts.merge)
216 die("%s is meaningless without -m, --reset, or --prefix",
217 opts.update ? "-u" : "-i");
218 if (opts.update && !opts.reset)
219 opts.preserve_ignored = 0;
220 /* otherwise, opts.preserve_ignored is irrelevant */
221 if (opts.merge && !opts.index_only)
222 setup_work_tree();
223
224 if (opts.skip_sparse_checkout)
225 ensure_full_index(&the_index);
226
227 if (opts.merge) {
228 switch (stage - 1) {
229 case 0:
230 die("you must specify at least one tree to merge");
231 break;
232 case 1:
233 opts.fn = opts.prefix ? bind_merge : oneway_merge;
234 break;
235 case 2:
236 opts.fn = twoway_merge;
237 opts.initial_checkout = is_index_unborn(&the_index);
238 break;
239 case 3:
240 default:
241 opts.fn = threeway_merge;
242 break;
243 }
244
245 if (stage - 1 >= 3)
246 opts.head_idx = stage - 2;
247 else
248 opts.head_idx = 1;
249 }
250
251 if (opts.debug_unpack)
252 opts.fn = debug_merge;
253
254 /* If we're going to prime_cache_tree later, skip cache tree update */
255 if (nr_trees == 1 && !opts.prefix)
256 opts.skip_cache_tree_update = 1;
257
258 cache_tree_free(&the_index.cache_tree);
259 for (i = 0; i < nr_trees; i++) {
260 struct tree *tree = trees[i];
261 parse_tree(tree);
262 init_tree_desc(t+i, tree->buffer, tree->size);
263 }
264 if (unpack_trees(nr_trees, t, &opts))
265 return 128;
266
267 if (opts.debug_unpack || opts.dry_run)
268 return 0; /* do not write the index out */
269
270 /*
271 * When reading only one tree (either the most basic form,
272 * "-m ent" or "--reset ent" form), we can obtain a fully
273 * valid cache-tree because the index must match exactly
274 * what came from the tree.
275 */
276 if (nr_trees == 1 && !opts.prefix)
277 prime_cache_tree(the_repository,
278 the_repository->index,
279 trees[0]);
280
281 if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
282 die("unable to write new index file");
283 return 0;
284 }