1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6
7#include "cache.h"
8#include "config.h"
9#include "lockfile.h"
10#include "object.h"
11#include "tree.h"
12#include "tree-walk.h"
13#include "cache-tree.h"
14#include "unpack-trees.h"
15#include "dir.h"
16#include "builtin.h"
17#include "parse-options.h"
18#include "resolve-undo.h"
19#include "submodule.h"
20#include "submodule-config.h"
21
22static int nr_trees;
23static int read_empty;
24static struct tree *trees[MAX_UNPACK_TREES];
25static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
26
27static int list_tree(unsigned char *sha1)
28{
29 struct tree *tree;
30
31 if (nr_trees >= MAX_UNPACK_TREES)
32 die("I cannot read more than %d trees", MAX_UNPACK_TREES);
33 tree = parse_tree_indirect(sha1);
34 if (!tree)
35 return -1;
36 trees[nr_trees++] = tree;
37 return 0;
38}
39
40static const char * const read_tree_usage[] = {
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>]])"),
42 NULL
43};
44
45static int index_output_cb(const struct option *opt, const char *arg,
46 int unset)
47{
48 set_alternate_index_output(arg);
49 return 0;
50}
51
52static int exclude_per_directory_cb(const struct option *opt, const char *arg,
53 int unset)
54{
55 struct dir_struct *dir;
56 struct unpack_trees_options *opts;
57
58 opts = (struct unpack_trees_options *)opt->value;
59
60 if (opts->dir)
61 die("more than one --exclude-per-directory given.");
62
63 dir = xcalloc(1, sizeof(*opts->dir));
64 dir->flags |= DIR_SHOW_IGNORED;
65 dir->exclude_per_dir = arg;
66 opts->dir = dir;
67 /* We do not need to nor want to do read-directory
68 * here; we are merely interested in reusing the
69 * per directory ignore stack mechanism.
70 */
71 return 0;
72}
73
74static void debug_stage(const char *label, const struct cache_entry *ce,
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,
85 oid_to_hex(&ce->oid));
86}
87
88static int debug_merge(const struct cache_entry * const *stages,
89 struct unpack_trees_options *o)
90{
91 int i;
92
93 printf("* %d-way merge\n", o->merge_size);
94 debug_stage("index", stages[0], o);
95 for (i = 1; i <= o->merge_size; i++) {
96 char buf[24];
97 xsnprintf(buf, sizeof(buf), "ent#%d", i);
98 debug_stage(buf, stages[i], o);
99 }
100 return 0;
101}
102
103static int option_parse_recurse_submodules(const struct option *opt,
104 const char *arg, int unset)
105{
106 if (unset) {
107 recurse_submodules = RECURSE_SUBMODULES_OFF;
108 return 0;
109 }
110 if (arg)
111 recurse_submodules =
112 parse_update_recurse_submodules_arg(opt->long_name,
113 arg);
114 else
115 recurse_submodules = RECURSE_SUBMODULES_ON;
116
117 return 0;
118}
119
120static struct lock_file lock_file;
121
122int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
123{
124 int i, stage = 0;
125 unsigned char sha1[20];
126 struct tree_desc t[MAX_UNPACK_TREES];
127 struct unpack_trees_options opts;
128 int prefix_set = 0;
129 const struct option read_tree_options[] = {
130 { OPTION_CALLBACK, 0, "index-output", NULL, N_("file"),
131 N_("write resulting index to <file>"),
132 PARSE_OPT_NONEG, index_output_cb },
133 OPT_BOOL(0, "empty", &read_empty,
134 N_("only empty the index")),
135 OPT__VERBOSE(&opts.verbose_update, N_("be verbose")),
136 OPT_GROUP(N_("Merging")),
137 OPT_BOOL('m', NULL, &opts.merge,
138 N_("perform a merge in addition to a read")),
139 OPT_BOOL(0, "trivial", &opts.trivial_merges_only,
140 N_("3-way merge if no file level merging required")),
141 OPT_BOOL(0, "aggressive", &opts.aggressive,
142 N_("3-way merge in presence of adds and removes")),
143 OPT_BOOL(0, "reset", &opts.reset,
144 N_("same as -m, but discard unmerged entries")),
145 { OPTION_STRING, 0, "prefix", &opts.prefix, N_("<subdirectory>/"),
146 N_("read the tree into the index under <subdirectory>/"),
147 PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP },
148 OPT_BOOL('u', NULL, &opts.update,
149 N_("update working tree with merge result")),
150 { OPTION_CALLBACK, 0, "exclude-per-directory", &opts,
151 N_("gitignore"),
152 N_("allow explicitly ignored files to be overwritten"),
153 PARSE_OPT_NONEG, exclude_per_directory_cb },
154 OPT_BOOL('i', NULL, &opts.index_only,
155 N_("don't check the working tree after merging")),
156 OPT__DRY_RUN(&opts.dry_run, N_("don't update the index or the work tree")),
157 OPT_BOOL(0, "no-sparse-checkout", &opts.skip_sparse_checkout,
158 N_("skip applying sparse checkout filter")),
159 OPT_BOOL(0, "debug-unpack", &opts.debug_unpack,
160 N_("debug unpack-trees")),
161 { OPTION_CALLBACK, 0, "recurse-submodules", &recurse_submodules,
162 "checkout", "control recursive updating of submodules",
163 PARSE_OPT_OPTARG, option_parse_recurse_submodules },
164 OPT_END()
165 };
166
167 memset(&opts, 0, sizeof(opts));
168 opts.head_idx = -1;
169 opts.src_index = &the_index;
170 opts.dst_index = &the_index;
171
172 git_config(git_default_config, NULL);
173
174 argc = parse_options(argc, argv, unused_prefix, read_tree_options,
175 read_tree_usage, 0);
176
177 hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
178
179 if (recurse_submodules != RECURSE_SUBMODULES_DEFAULT) {
180 gitmodules_config();
181 git_config(submodule_config, NULL);
182 set_config_update_recurse_submodules(RECURSE_SUBMODULES_ON);
183 }
184
185 prefix_set = opts.prefix ? 1 : 0;
186 if (1 < opts.merge + opts.reset + prefix_set)
187 die("Which one? -m, --reset, or --prefix?");
188
189 /*
190 * NEEDSWORK
191 *
192 * The old index should be read anyway even if we're going to
193 * destroy all index entries because we still need to preserve
194 * certain information such as index version or split-index
195 * mode.
196 */
197
198 if (opts.reset || opts.merge || opts.prefix) {
199 if (read_cache_unmerged() && (opts.prefix || opts.merge))
200 die("You need to resolve your current index first");
201 stage = opts.merge = 1;
202 }
203 resolve_undo_clear();
204
205 for (i = 0; i < argc; i++) {
206 const char *arg = argv[i];
207
208 if (get_sha1(arg, sha1))
209 die("Not a valid object name %s", arg);
210 if (list_tree(sha1) < 0)
211 die("failed to unpack tree object %s", arg);
212 stage++;
213 }
214 if (nr_trees == 0 && !read_empty)
215 warning("read-tree: emptying the index with no arguments is deprecated; use --empty");
216 else if (nr_trees > 0 && read_empty)
217 die("passing trees as arguments contradicts --empty");
218
219 if (1 < opts.index_only + opts.update)
220 die("-u and -i at the same time makes no sense");
221 if ((opts.update || opts.index_only) && !opts.merge)
222 die("%s is meaningless without -m, --reset, or --prefix",
223 opts.update ? "-u" : "-i");
224 if ((opts.dir && !opts.update))
225 die("--exclude-per-directory is meaningless unless -u");
226 if (opts.merge && !opts.index_only)
227 setup_work_tree();
228
229 if (opts.merge) {
230 if (stage < 2)
231 die("just how do you expect me to merge %d trees?", stage-1);
232 switch (stage - 1) {
233 case 1:
234 opts.fn = opts.prefix ? bind_merge : oneway_merge;
235 break;
236 case 2:
237 opts.fn = twoway_merge;
238 opts.initial_checkout = is_cache_unborn();
239 break;
240 case 3:
241 default:
242 opts.fn = threeway_merge;
243 break;
244 }
245
246 if (stage - 1 >= 3)
247 opts.head_idx = stage - 2;
248 else
249 opts.head_idx = 1;
250 }
251
252 if (opts.debug_unpack)
253 opts.fn = debug_merge;
254
255 cache_tree_free(&active_cache_tree);
256 for (i = 0; i < nr_trees; i++) {
257 struct tree *tree = trees[i];
258 parse_tree(tree);
259 init_tree_desc(t+i, tree->buffer, tree->size);
260 }
261 if (unpack_trees(nr_trees, t, &opts))
262 return 128;
263
264 if (opts.debug_unpack || opts.dry_run)
265 return 0; /* do not write the index out */
266
267 /*
268 * When reading only one tree (either the most basic form,
269 * "-m ent" or "--reset ent" form), we can obtain a fully
270 * valid cache-tree because the index must match exactly
271 * what came from the tree.
272 */
273 if (nr_trees == 1 && !opts.prefix)
274 prime_cache_tree(&the_index, trees[0]);
275
276 if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
277 die("unable to write new index file");
278 return 0;
279}