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