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