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