1/*
2* GIT - The information manager from hell
3*
4* Copyright (C) Linus Torvalds, 2005
5*/
67
#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"
2122
static int nr_trees;
23static int read_empty;
24static struct tree *trees[MAX_UNPACK_TREES];
2526
static int list_tree(struct object_id *oid)
27{
28struct tree *tree;
2930
if (nr_trees >= MAX_UNPACK_TREES)
31die("I cannot read more than %d trees", MAX_UNPACK_TREES);
32tree = parse_tree_indirect(oid);
33if (!tree)
34return -1;
35trees[nr_trees++] = tree;
36return 0;
37}
3839
static const char * const read_tree_usage[] = {
40N_("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>]])"),
41NULL
42};
4344
static int index_output_cb(const struct option *opt, const char *arg,
45int unset)
46{
47set_alternate_index_output(arg);
48return 0;
49}
5051
static int exclude_per_directory_cb(const struct option *opt, const char *arg,
52int unset)
53{
54struct dir_struct *dir;
55struct unpack_trees_options *opts;
5657
opts = (struct unpack_trees_options *)opt->value;
5859
if (opts->dir)
60die("more than one --exclude-per-directory given.");
6162
dir = xcalloc(1, sizeof(*opts->dir));
63dir->flags |= DIR_SHOW_IGNORED;
64dir->exclude_per_dir = arg;
65opts->dir = dir;
66/* We do not need to nor want to do read-directory
67* here; we are merely interested in reusing the
68* per directory ignore stack mechanism.
69*/
70return 0;
71}
7273
static void debug_stage(const char *label, const struct cache_entry *ce,
74struct unpack_trees_options *o)
75{
76printf("%s ", label);
77if (!ce)
78printf("(missing)\n");
79else if (ce == o->df_conflict_entry)
80printf("(conflict)\n");
81else
82printf("%06o #%d %s %.8s\n",
83ce->ce_mode, ce_stage(ce), ce->name,
84oid_to_hex(&ce->oid));
85}
8687
static int debug_merge(const struct cache_entry * const *stages,
88struct unpack_trees_options *o)
89{
90int i;
9192
printf("* %d-way merge\n", o->merge_size);
93debug_stage("index", stages[0], o);
94for (i = 1; i <= o->merge_size; i++) {
95char buf[24];
96xsnprintf(buf, sizeof(buf), "ent#%d", i);
97debug_stage(buf, stages[i], o);
98}
99return 0;
100}
101102
static int git_read_tree_config(const char *var, const char *value, void *cb)
103{
104if (!strcmp(var, "submodule.recurse"))
105return git_default_submodule_config(var, value, cb);
106107
return git_default_config(var, value, cb);
108}
109110
int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
111{
112int i, stage = 0;
113struct object_id oid;
114struct tree_desc t[MAX_UNPACK_TREES];
115struct unpack_trees_options opts;
116int prefix_set = 0;
117struct lock_file lock_file = LOCK_INIT;
118const struct option read_tree_options[] = {
119{ OPTION_CALLBACK, 0, "index-output", NULL, N_("file"),
120N_("write resulting index to <file>"),
121PARSE_OPT_NONEG, index_output_cb },
122OPT_BOOL(0, "empty", &read_empty,
123N_("only empty the index")),
124OPT__VERBOSE(&opts.verbose_update, N_("be verbose")),
125OPT_GROUP(N_("Merging")),
126OPT_BOOL('m', NULL, &opts.merge,
127N_("perform a merge in addition to a read")),
128OPT_BOOL(0, "trivial", &opts.trivial_merges_only,
129N_("3-way merge if no file level merging required")),
130OPT_BOOL(0, "aggressive", &opts.aggressive,
131N_("3-way merge in presence of adds and removes")),
132OPT_BOOL(0, "reset", &opts.reset,
133N_("same as -m, but discard unmerged entries")),
134{ OPTION_STRING, 0, "prefix", &opts.prefix, N_("<subdirectory>/"),
135N_("read the tree into the index under <subdirectory>/"),
136PARSE_OPT_NONEG },
137OPT_BOOL('u', NULL, &opts.update,
138N_("update working tree with merge result")),
139{ OPTION_CALLBACK, 0, "exclude-per-directory", &opts,
140N_("gitignore"),
141N_("allow explicitly ignored files to be overwritten"),
142PARSE_OPT_NONEG, exclude_per_directory_cb },
143OPT_BOOL('i', NULL, &opts.index_only,
144N_("don't check the working tree after merging")),
145OPT__DRY_RUN(&opts.dry_run, N_("don't update the index or the work tree")),
146OPT_BOOL(0, "no-sparse-checkout", &opts.skip_sparse_checkout,
147N_("skip applying sparse checkout filter")),
148OPT_BOOL(0, "debug-unpack", &opts.debug_unpack,
149N_("debug unpack-trees")),
150{ OPTION_CALLBACK, 0, "recurse-submodules", NULL,
151"checkout", "control recursive updating of submodules",
152PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater },
153OPT_END()
154};
155156
memset(&opts, 0, sizeof(opts));
157opts.head_idx = -1;
158opts.src_index = &the_index;
159opts.dst_index = &the_index;
160161
git_config(git_read_tree_config, NULL);
162163
argc = parse_options(argc, argv, unused_prefix, read_tree_options,
164read_tree_usage, 0);
165166
hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
167168
prefix_set = opts.prefix ? 1 : 0;
169if (1 < opts.merge + opts.reset + prefix_set)
170die("Which one? -m, --reset, or --prefix?");
171172
/*
173* NEEDSWORK
174*
175* The old index should be read anyway even if we're going to
176* destroy all index entries because we still need to preserve
177* certain information such as index version or split-index
178* mode.
179*/
180181
if (opts.reset || opts.merge || opts.prefix) {
182if (read_cache_unmerged() && (opts.prefix || opts.merge))
183die("You need to resolve your current index first");
184stage = opts.merge = 1;
185}
186resolve_undo_clear();
187188
for (i = 0; i < argc; i++) {
189const char *arg = argv[i];
190191
if (get_oid(arg, &oid))
192die("Not a valid object name %s", arg);
193if (list_tree(&oid) < 0)
194die("failed to unpack tree object %s", arg);
195stage++;
196}
197if (!nr_trees && !read_empty && !opts.merge)
198warning("read-tree: emptying the index with no arguments is deprecated; use --empty");
199else if (nr_trees > 0 && read_empty)
200die("passing trees as arguments contradicts --empty");
201202
if (1 < opts.index_only + opts.update)
203die("-u and -i at the same time makes no sense");
204if ((opts.update || opts.index_only) && !opts.merge)
205die("%s is meaningless without -m, --reset, or --prefix",
206opts.update ? "-u" : "-i");
207if ((opts.dir && !opts.update))
208die("--exclude-per-directory is meaningless unless -u");
209if (opts.merge && !opts.index_only)
210setup_work_tree();
211212
if (opts.merge) {
213switch (stage - 1) {
214case 0:
215die("you must specify at least one tree to merge");
216break;
217case 1:
218opts.fn = opts.prefix ? bind_merge : oneway_merge;
219break;
220case 2:
221opts.fn = twoway_merge;
222opts.initial_checkout = is_cache_unborn();
223break;
224case 3:
225default:
226opts.fn = threeway_merge;
227break;
228}
229230
if (stage - 1 >= 3)
231opts.head_idx = stage - 2;
232else
233opts.head_idx = 1;
234}
235236
if (opts.debug_unpack)
237opts.fn = debug_merge;
238239
cache_tree_free(&active_cache_tree);
240for (i = 0; i < nr_trees; i++) {
241struct tree *tree = trees[i];
242parse_tree(tree);
243init_tree_desc(t+i, tree->buffer, tree->size);
244}
245if (unpack_trees(nr_trees, t, &opts))
246return 128;
247248
if (opts.debug_unpack || opts.dry_run)
249return 0; /* do not write the index out */
250251
/*
252* When reading only one tree (either the most basic form,
253* "-m ent" or "--reset ent" form), we can obtain a fully
254* valid cache-tree because the index must match exactly
255* what came from the tree.
256*/
257if (nr_trees == 1 && !opts.prefix)
258prime_cache_tree(&the_index, trees[0]);
259260
if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
261die("unable to write new index file");
262return 0;
263}