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