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