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