]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/read-tree.c
Merge branch 'mk/doc-gitfile-more' into maint-2.43
[thirdparty/git.git] / builtin / read-tree.c
CommitLineData
8bc9a0c7
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
4d3fe0c5 6
07047d68 7#define USE_THE_INDEX_VARIABLE
bc5c5ec0 8#include "builtin.h"
b2141fc1 9#include "config.h"
f394e093 10#include "gettext.h"
41771fa4 11#include "hex.h"
697cc8ef 12#include "lockfile.h"
ee6566e8 13#include "object.h"
dabab1d6 14#include "object-name.h"
ee6566e8 15#include "tree.h"
1ccf5a34 16#include "tree-walk.h"
bad68ec9 17#include "cache-tree.h"
16da134b 18#include "unpack-trees.h"
f8a9d428 19#include "dir.h"
5a56da58 20#include "parse-options.h"
d1cbe1e6 21#include "repository.h"
cfc5789a 22#include "resolve-undo.h"
e38da487 23#include "setup.h"
baf889c2 24#include "sparse-index.h"
25804914
SB
25#include "submodule.h"
26#include "submodule-config.h"
ee6566e8 27
933bf40a 28static int nr_trees;
fb1bb965 29static int read_empty;
ca885a4f 30static struct tree *trees[MAX_UNPACK_TREES];
ee6566e8 31
4939e2c4 32static int list_tree(struct object_id *oid)
ee6566e8 33{
933bf40a
LT
34 struct tree *tree;
35
ca885a4f
JH
36 if (nr_trees >= MAX_UNPACK_TREES)
37 die("I cannot read more than %d trees", MAX_UNPACK_TREES);
a9dbc179 38 tree = parse_tree_indirect(oid);
ee6566e8
DB
39 if (!tree)
40 return -1;
933bf40a 41 trees[nr_trees++] = tree;
ee6566e8
DB
42 return 0;
43}
44
5a56da58 45static const char * const read_tree_usage[] = {
5af8b61c 46 N_("git read-tree [(-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>)\n"
e8eeda1f 47 " [-u | -i]] [--index-output=<file>] [--no-sparse-checkout]\n"
5af8b61c 48 " (--empty | <tree-ish1> [<tree-ish2> [<tree-ish3>]])"),
5a56da58
SB
49 NULL
50};
51
34bf44f2 52static int index_output_cb(const struct option *opt UNUSED, const char *arg,
5a56da58
SB
53 int unset)
54{
517fe807 55 BUG_ON_OPT_NEG(unset);
5a56da58
SB
56 set_alternate_index_output(arg);
57 return 0;
58}
59
60static int exclude_per_directory_cb(const struct option *opt, const char *arg,
61 int unset)
62{
5a56da58
SB
63 struct unpack_trees_options *opts;
64
517fe807
JK
65 BUG_ON_OPT_NEG(unset);
66
5a56da58
SB
67 opts = (struct unpack_trees_options *)opt->value;
68
491a7575
EN
69 if (!opts->update)
70 die("--exclude-per-directory is meaningless unless -u");
71 if (strcmp(arg, ".gitignore"))
72 die("--exclude-per-directory argument must be .gitignore");
5a56da58
SB
73 return 0;
74}
c5bac17a 75
eb9ae4b5 76static void debug_stage(const char *label, const struct cache_entry *ce,
ba655da5
JH
77 struct unpack_trees_options *o)
78{
79 printf("%s ", label);
80 if (!ce)
81 printf("(missing)\n");
82 else if (ce == o->df_conflict_entry)
83 printf("(conflict)\n");
84 else
85 printf("%06o #%d %s %.8s\n",
86 ce->ce_mode, ce_stage(ce), ce->name,
99d1a986 87 oid_to_hex(&ce->oid));
ba655da5
JH
88}
89
5828e835
RS
90static int debug_merge(const struct cache_entry * const *stages,
91 struct unpack_trees_options *o)
ba655da5
JH
92{
93 int i;
94
1ca13dd3 95 printf("* %d-way merge\n", o->internal.merge_size);
ba655da5 96 debug_stage("index", stages[0], o);
1ca13dd3 97 for (i = 1; i <= o->internal.merge_size; i++) {
ba655da5 98 char buf[24];
5096d490 99 xsnprintf(buf, sizeof(buf), "ent#%d", i);
ba655da5
JH
100 debug_stage(buf, stages[i], o);
101 }
102 return 0;
103}
104
a4e7e317
GC
105static int git_read_tree_config(const char *var, const char *value,
106 const struct config_context *ctx, void *cb)
25804914 107{
046b4823
SB
108 if (!strcmp(var, "submodule.recurse"))
109 return git_default_submodule_config(var, value, cb);
25804914 110
a4e7e317 111 return git_default_config(var, value, ctx, cb);
25804914
SB
112}
113
76a7bc09 114int cmd_read_tree(int argc, const char **argv, const char *cmd_prefix)
e83c5163 115{
03b86647 116 int i, stage = 0;
4939e2c4 117 struct object_id oid;
ca885a4f 118 struct tree_desc t[MAX_UNPACK_TREES];
16da134b 119 struct unpack_trees_options opts;
5a56da58 120 int prefix_set = 0;
0fa5a2ed 121 struct lock_file lock_file = LOCK_INIT;
5a56da58 122 const struct option read_tree_options[] = {
4002ec3d 123 OPT__SUPER_PREFIX(&opts.super_prefix),
203c8533 124 OPT_CALLBACK_F(0, "index-output", NULL, N_("file"),
230c6cdb 125 N_("write resulting index to <file>"),
203c8533 126 PARSE_OPT_NONEG, index_output_cb),
84a7f096
SB
127 OPT_BOOL(0, "empty", &read_empty,
128 N_("only empty the index")),
230c6cdb
NTND
129 OPT__VERBOSE(&opts.verbose_update, N_("be verbose")),
130 OPT_GROUP(N_("Merging")),
84a7f096
SB
131 OPT_BOOL('m', NULL, &opts.merge,
132 N_("perform a merge in addition to a read")),
133 OPT_BOOL(0, "trivial", &opts.trivial_merges_only,
134 N_("3-way merge if no file level merging required")),
135 OPT_BOOL(0, "aggressive", &opts.aggressive,
136 N_("3-way merge in presence of adds and removes")),
137 OPT_BOOL(0, "reset", &opts.reset,
138 N_("same as -m, but discard unmerged entries")),
230c6cdb
NTND
139 { OPTION_STRING, 0, "prefix", &opts.prefix, N_("<subdirectory>/"),
140 N_("read the tree into the index under <subdirectory>/"),
5f0df44c 141 PARSE_OPT_NONEG },
84a7f096
SB
142 OPT_BOOL('u', NULL, &opts.update,
143 N_("update working tree with merge result")),
203c8533 144 OPT_CALLBACK_F(0, "exclude-per-directory", &opts,
230c6cdb
NTND
145 N_("gitignore"),
146 N_("allow explicitly ignored files to be overwritten"),
203c8533 147 PARSE_OPT_NONEG, exclude_per_directory_cb),
84a7f096
SB
148 OPT_BOOL('i', NULL, &opts.index_only,
149 N_("don't check the working tree after merging")),
230c6cdb 150 OPT__DRY_RUN(&opts.dry_run, N_("don't update the index or the work tree")),
84a7f096
SB
151 OPT_BOOL(0, "no-sparse-checkout", &opts.skip_sparse_checkout,
152 N_("skip applying sparse checkout filter")),
1ca13dd3 153 OPT_BOOL(0, "debug-unpack", &opts.internal.debug_unpack,
84a7f096 154 N_("debug unpack-trees")),
203c8533 155 OPT_CALLBACK_F(0, "recurse-submodules", NULL,
25804914 156 "checkout", "control recursive updating of submodules",
203c8533 157 PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater),
3e41485d 158 OPT__QUIET(&opts.quiet, N_("suppress feedback messages")),
5a56da58
SB
159 OPT_END()
160 };
bb233d69 161
16da134b
JS
162 memset(&opts, 0, sizeof(opts));
163 opts.head_idx = -1;
34110cd4
LT
164 opts.src_index = &the_index;
165 opts.dst_index = &the_index;
344c52ae 166
046b4823 167 git_config(git_read_tree_config, NULL);
53228a5f 168
76a7bc09 169 argc = parse_options(argc, argv, cmd_prefix, read_tree_options,
5a56da58 170 read_tree_usage, 0);
83adac3c 171
5a56da58
SB
172 prefix_set = opts.prefix ? 1 : 0;
173 if (1 < opts.merge + opts.reset + prefix_set)
174 die("Which one? -m, --reset, or --prefix?");
db137fe9 175
cc89331d
VD
176 /* Prefix should not start with a directory separator */
177 if (opts.prefix && opts.prefix[0] == '/')
178 die("Invalid prefix, prefix cannot start with '/'");
179
480d3d6b
EN
180 if (opts.reset)
181 opts.reset = UNPACK_RESET_OVERWRITE_UNTRACKED;
182
2c66a7c8
VD
183 prepare_repo_settings(the_repository);
184 the_repository->settings.command_requires_full_index = 0;
185
07047d68 186 repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
2c66a7c8 187
5a092ceb
NTND
188 /*
189 * NEEDSWORK
190 *
191 * The old index should be read anyway even if we're going to
192 * destroy all index entries because we still need to preserve
193 * certain information such as index version or split-index
194 * mode.
195 */
196
db137fe9 197 if (opts.reset || opts.merge || opts.prefix) {
fbc1ed62 198 if (repo_read_index_unmerged(the_repository) && (opts.prefix || opts.merge))
e091228e 199 die(_("You need to resolve your current index first"));
db137fe9
AJ
200 stage = opts.merge = 1;
201 }
031b2033 202 resolve_undo_clear_index(&the_index);
5a56da58
SB
203
204 for (i = 0; i < argc; i++) {
205 const char *arg = argv[i];
720d150c 206
d850b7a5 207 if (repo_get_oid(the_repository, arg, &oid))
31fff305 208 die("Not a valid object name %s", arg);
4939e2c4 209 if (list_tree(&oid) < 0)
2de381f9 210 die("failed to unpack tree object %s", arg);
d99082e0 211 stage++;
83adac3c 212 }
b9b10d36 213 if (!nr_trees && !read_empty && !opts.merge)
fb1bb965
JK
214 warning("read-tree: emptying the index with no arguments is deprecated; use --empty");
215 else if (nr_trees > 0 && read_empty)
216 die("passing trees as arguments contradicts --empty");
217
5a56da58
SB
218 if (1 < opts.index_only + opts.update)
219 die("-u and -i at the same time makes no sense");
c01499ef 220 if ((opts.update || opts.index_only) && !opts.merge)
a429d2dd
SB
221 die("%s is meaningless without -m, --reset, or --prefix",
222 opts.update ? "-u" : "-i");
04988c8d
EN
223 if (opts.update && !opts.reset)
224 opts.preserve_ignored = 0;
225 /* otherwise, opts.preserve_ignored is irrelevant */
b6469a81
NTND
226 if (opts.merge && !opts.index_only)
227 setup_work_tree();
7dd43575 228
74970392 229 if (opts.skip_sparse_checkout)
2c66a7c8
VD
230 ensure_full_index(&the_index);
231
16da134b 232 if (opts.merge) {
ee6566e8 233 switch (stage - 1) {
9932242f
JNA
234 case 0:
235 die("you must specify at least one tree to merge");
236 break;
ee6566e8 237 case 1:
16da134b 238 opts.fn = opts.prefix ? bind_merge : oneway_merge;
ee6566e8
DB
239 break;
240 case 2:
16da134b 241 opts.fn = twoway_merge;
fbc1ed62 242 opts.initial_checkout = is_index_unborn(&the_index);
ee6566e8
DB
243 break;
244 case 3:
ee6566e8 245 default:
16da134b 246 opts.fn = threeway_merge;
ee6566e8 247 break;
03efa6d9 248 }
ee6566e8 249
ee6566e8 250 if (stage - 1 >= 3)
16da134b 251 opts.head_idx = stage - 2;
ee6566e8 252 else
16da134b 253 opts.head_idx = 1;
ee6566e8
DB
254 }
255
1ca13dd3 256 if (opts.internal.debug_unpack)
ba655da5
JH
257 opts.fn = debug_merge;
258
dc5d40f5
VD
259 /* If we're going to prime_cache_tree later, skip cache tree update */
260 if (nr_trees == 1 && !opts.prefix)
261 opts.skip_cache_tree_update = 1;
262
dc594180 263 cache_tree_free(&the_index.cache_tree);
933bf40a
LT
264 for (i = 0; i < nr_trees; i++) {
265 struct tree *tree = trees[i];
266 parse_tree(tree);
267 init_tree_desc(t+i, tree->buffer, tree->size);
268 }
203a2fe1
DB
269 if (unpack_trees(nr_trees, t, &opts))
270 return 128;
7927a55d 271
1ca13dd3 272 if (opts.internal.debug_unpack || opts.dry_run)
ba655da5
JH
273 return 0; /* do not write the index out */
274
7927a55d
JH
275 /*
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.
280 */
8cc21ce7 281 if (nr_trees == 1 && !opts.prefix)
c207e9e1
NTND
282 prime_cache_tree(the_repository,
283 the_repository->index,
284 trees[0]);
7927a55d 285
03b86647 286 if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
2de381f9 287 die("unable to write new index file");
9614b8dc 288 return 0;
e83c5163 289}