Git fork
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6#define USE_THE_REPOSITORY_VARIABLE
7#include "builtin.h"
8#include "config.h"
9#include "environment.h"
10#include "gettext.h"
11#include "hex.h"
12#include "lockfile.h"
13#include "object.h"
14#include "object-name.h"
15#include "tree.h"
16#include "tree-walk.h"
17#include "cache-tree.h"
18#include "unpack-trees.h"
19#include "parse-options.h"
20#include "resolve-undo.h"
21#include "setup.h"
22#include "sparse-index.h"
23#include "submodule.h"
24
25static int nr_trees;
26static int read_empty;
27static struct tree *trees[MAX_UNPACK_TREES];
28
29static int list_tree(struct object_id *oid)
30{
31 struct tree *tree;
32
33 if (nr_trees >= MAX_UNPACK_TREES)
34 die("I cannot read more than %d trees", MAX_UNPACK_TREES);
35 tree = parse_tree_indirect(oid);
36 if (!tree)
37 return -1;
38 trees[nr_trees++] = tree;
39 return 0;
40}
41
42static const char * const read_tree_usage[] = {
43 N_("git read-tree [(-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>)\n"
44 " [-u | -i]] [--index-output=<file>] [--no-sparse-checkout]\n"
45 " (--empty | <tree-ish1> [<tree-ish2> [<tree-ish3>]])"),
46 NULL
47};
48
49static int index_output_cb(const struct option *opt UNUSED, const char *arg,
50 int unset)
51{
52 BUG_ON_OPT_NEG(unset);
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{
60 struct unpack_trees_options *opts;
61
62 BUG_ON_OPT_NEG(unset);
63
64 opts = (struct unpack_trees_options *)opt->value;
65
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");
70 return 0;
71}
72
73static void debug_stage(const char *label, const struct cache_entry *ce,
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,
84 oid_to_hex(&ce->oid));
85}
86
87static int debug_merge(const struct cache_entry * const *stages,
88 struct unpack_trees_options *o)
89{
90 int i;
91
92 printf("* %d-way merge\n", o->internal.merge_size);
93 debug_stage("index", stages[0], o);
94 for (i = 1; i <= o->internal.merge_size; i++) {
95 char buf[24];
96 xsnprintf(buf, sizeof(buf), "ent#%d", i);
97 debug_stage(buf, stages[i], o);
98 }
99 return 0;
100}
101
102static int git_read_tree_config(const char *var, const char *value,
103 const struct config_context *ctx, void *cb)
104{
105 if (!strcmp(var, "submodule.recurse"))
106 return git_default_submodule_config(var, value, cb);
107
108 return git_default_config(var, value, ctx, cb);
109}
110
111int cmd_read_tree(int argc,
112 const char **argv,
113 const char *cmd_prefix,
114 struct repository *repo UNUSED)
115{
116 int i, stage = 0;
117 struct object_id oid;
118 struct tree_desc t[MAX_UNPACK_TREES];
119 struct unpack_trees_options opts;
120 int prefix_set = 0;
121 struct lock_file lock_file = LOCK_INIT;
122 const struct option read_tree_options[] = {
123 OPT__SUPER_PREFIX(&opts.super_prefix),
124 OPT_CALLBACK_F(0, "index-output", NULL, N_("file"),
125 N_("write resulting index to <file>"),
126 PARSE_OPT_NONEG, index_output_cb),
127 OPT_BOOL(0, "empty", &read_empty,
128 N_("only empty the index")),
129 OPT__VERBOSE(&opts.verbose_update, N_("be verbose")),
130 OPT_GROUP(N_("Merging")),
131 OPT_BOOL('m', NULL, &opts.merge,
132 N_("perform a merge in addition to a read")),
133 OPT_BOOL(0, "trivial", &opts.trivial_merges_only,
134 N_("3-way merge if no file level merging required")),
135 OPT_BOOL(0, "aggressive", &opts.aggressive,
136 N_("3-way merge in presence of adds and removes")),
137 OPT_BOOL(0, "reset", &opts.reset,
138 N_("same as -m, but discard unmerged entries")),
139 {
140 .type = OPTION_STRING,
141 .long_name = "prefix",
142 .value = &opts.prefix,
143 .argh = N_("<subdirectory>/"),
144 .help = N_("read the tree into the index under <subdirectory>/"),
145 .flags = PARSE_OPT_NONEG,
146 },
147 OPT_BOOL('u', NULL, &opts.update,
148 N_("update working tree with merge result")),
149 OPT_CALLBACK_F(0, "exclude-per-directory", &opts,
150 N_("gitignore"),
151 N_("allow explicitly ignored files to be overwritten"),
152 PARSE_OPT_NONEG, exclude_per_directory_cb),
153 OPT_BOOL('i', NULL, &opts.index_only,
154 N_("don't check the working tree after merging")),
155 OPT__DRY_RUN(&opts.dry_run, N_("don't update the index or the work tree")),
156 OPT_BOOL(0, "no-sparse-checkout", &opts.skip_sparse_checkout,
157 N_("skip applying sparse checkout filter")),
158 OPT_BOOL(0, "debug-unpack", &opts.internal.debug_unpack,
159 N_("debug unpack-trees")),
160 OPT_CALLBACK_F(0, "recurse-submodules", NULL,
161 "checkout", "control recursive updating of submodules",
162 PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater),
163 OPT__QUIET(&opts.quiet, N_("suppress feedback messages")),
164 OPT_END()
165 };
166
167 memset(&opts, 0, sizeof(opts));
168 opts.head_idx = -1;
169 opts.src_index = the_repository->index;
170 opts.dst_index = the_repository->index;
171
172 repo_config(the_repository, git_read_tree_config, NULL);
173
174 argc = parse_options(argc, argv, cmd_prefix, read_tree_options,
175 read_tree_usage, 0);
176
177 prefix_set = opts.prefix ? 1 : 0;
178 if (1 < opts.merge + opts.reset + prefix_set)
179 die("Which one? -m, --reset, or --prefix?");
180
181 /* Prefix should not start with a directory separator */
182 if (opts.prefix && opts.prefix[0] == '/')
183 die("Invalid prefix, prefix cannot start with '/'");
184
185 if (opts.reset)
186 opts.reset = UNPACK_RESET_OVERWRITE_UNTRACKED;
187
188 prepare_repo_settings(the_repository);
189 the_repository->settings.command_requires_full_index = 0;
190
191 repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
192
193 /*
194 * NEEDSWORK
195 *
196 * The old index should be read anyway even if we're going to
197 * destroy all index entries because we still need to preserve
198 * certain information such as index version or split-index
199 * mode.
200 */
201
202 if (opts.reset || opts.merge || opts.prefix) {
203 if (repo_read_index_unmerged(the_repository) && (opts.prefix || opts.merge))
204 die(_("You need to resolve your current index first"));
205 stage = opts.merge = 1;
206 }
207 resolve_undo_clear_index(the_repository->index);
208
209 for (i = 0; i < argc; i++) {
210 const char *arg = argv[i];
211
212 if (repo_get_oid(the_repository, arg, &oid))
213 die("Not a valid object name %s", arg);
214 if (list_tree(&oid) < 0)
215 die("failed to unpack tree object %s", arg);
216 stage++;
217 }
218 if (!nr_trees && !read_empty && !opts.merge)
219 warning("read-tree: emptying the index with no arguments is deprecated; use --empty");
220 else if (nr_trees > 0 && read_empty)
221 die("passing trees as arguments contradicts --empty");
222
223 if (1 < opts.index_only + opts.update)
224 die("-u and -i at the same time makes no sense");
225 if ((opts.update || opts.index_only) && !opts.merge)
226 die("%s is meaningless without -m, --reset, or --prefix",
227 opts.update ? "-u" : "-i");
228 if (opts.update && !opts.reset)
229 opts.preserve_ignored = 0;
230 /* otherwise, opts.preserve_ignored is irrelevant */
231 if (opts.merge && !opts.index_only)
232 setup_work_tree();
233
234 if (opts.skip_sparse_checkout)
235 ensure_full_index(the_repository->index);
236
237 if (opts.merge) {
238 switch (stage - 1) {
239 case 0:
240 die("you must specify at least one tree to merge");
241 break;
242 case 1:
243 opts.fn = opts.prefix ? bind_merge : oneway_merge;
244 break;
245 case 2:
246 opts.fn = twoway_merge;
247 opts.initial_checkout = is_index_unborn(the_repository->index);
248 break;
249 case 3:
250 default:
251 opts.fn = threeway_merge;
252 break;
253 }
254
255 if (stage - 1 >= 3)
256 opts.head_idx = stage - 2;
257 else
258 opts.head_idx = 1;
259 }
260
261 if (opts.internal.debug_unpack)
262 opts.fn = debug_merge;
263
264 /* If we're going to prime_cache_tree later, skip cache tree update */
265 if (nr_trees == 1 && !opts.prefix)
266 opts.skip_cache_tree_update = 1;
267
268 cache_tree_free(&the_repository->index->cache_tree);
269 for (i = 0; i < nr_trees; i++) {
270 struct tree *tree = trees[i];
271 if (parse_tree(tree) < 0)
272 return 128;
273 init_tree_desc(t+i, &tree->object.oid, tree->buffer, tree->size);
274 }
275 if (unpack_trees(nr_trees, t, &opts))
276 return 128;
277
278 if (opts.internal.debug_unpack || opts.dry_run)
279 return 0; /* do not write the index out */
280
281 /*
282 * When reading only one tree (either the most basic form,
283 * "-m ent" or "--reset ent" form), we can obtain a fully
284 * valid cache-tree because the index must match exactly
285 * what came from the tree.
286 */
287 if (nr_trees == 1 && !opts.prefix)
288 prime_cache_tree(the_repository,
289 the_repository->index,
290 trees[0]);
291
292 if (write_locked_index(the_repository->index, &lock_file, COMMIT_LOCK))
293 die("unable to write new index file");
294 return 0;
295}