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