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{
47BUG_ON_OPT_NEG(unset);
48set_alternate_index_output(arg);
49return 0;
50}
5152
static int exclude_per_directory_cb(const struct option *opt, const char *arg,
53int unset)
54{
55struct dir_struct *dir;
56struct unpack_trees_options *opts;
5758
BUG_ON_OPT_NEG(unset);
5960
opts = (struct unpack_trees_options *)opt->value;
6162
if (opts->dir)
63die("more than one --exclude-per-directory given.");
6465
dir = xcalloc(1, sizeof(*opts->dir));
66dir->flags |= DIR_SHOW_IGNORED;
67dir->exclude_per_dir = arg;
68opts->dir = dir;
69/* We do not need to nor want to do read-directory
70* here; we are merely interested in reusing the
71* per directory ignore stack mechanism.
72*/
73return 0;
74}
7576
static void debug_stage(const char *label, const struct cache_entry *ce,
77struct unpack_trees_options *o)
78{
79printf("%s ", label);
80if (!ce)
81printf("(missing)\n");
82else if (ce == o->df_conflict_entry)
83printf("(conflict)\n");
84else
85printf("%06o #%d %s %.8s\n",
86ce->ce_mode, ce_stage(ce), ce->name,
87oid_to_hex(&ce->oid));
88}
8990
static int debug_merge(const struct cache_entry * const *stages,
91struct unpack_trees_options *o)
92{
93int i;
9495
printf("* %d-way merge\n", o->merge_size);
96debug_stage("index", stages[0], o);
97for (i = 1; i <= o->merge_size; i++) {
98char buf[24];
99xsnprintf(buf, sizeof(buf), "ent#%d", i);
100debug_stage(buf, stages[i], o);
101}
102return 0;
103}
104105
static int git_read_tree_config(const char *var, const char *value, void *cb)
106{
107if (!strcmp(var, "submodule.recurse"))
108return git_default_submodule_config(var, value, cb);
109110
return git_default_config(var, value, cb);
111}
112113
int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
114{
115int i, stage = 0;
116struct object_id oid;
117struct tree_desc t[MAX_UNPACK_TREES];
118struct unpack_trees_options opts;
119int prefix_set = 0;
120struct lock_file lock_file = LOCK_INIT;
121const struct option read_tree_options[] = {
122{ OPTION_CALLBACK, 0, "index-output", NULL, N_("file"),
123N_("write resulting index to <file>"),
124PARSE_OPT_NONEG, index_output_cb },
125OPT_BOOL(0, "empty", &read_empty,
126N_("only empty the index")),
127OPT__VERBOSE(&opts.verbose_update, N_("be verbose")),
128OPT_GROUP(N_("Merging")),
129OPT_BOOL('m', NULL, &opts.merge,
130N_("perform a merge in addition to a read")),
131OPT_BOOL(0, "trivial", &opts.trivial_merges_only,
132N_("3-way merge if no file level merging required")),
133OPT_BOOL(0, "aggressive", &opts.aggressive,
134N_("3-way merge in presence of adds and removes")),
135OPT_BOOL(0, "reset", &opts.reset,
136N_("same as -m, but discard unmerged entries")),
137{ OPTION_STRING, 0, "prefix", &opts.prefix, N_("<subdirectory>/"),
138N_("read the tree into the index under <subdirectory>/"),
139PARSE_OPT_NONEG },
140OPT_BOOL('u', NULL, &opts.update,
141N_("update working tree with merge result")),
142{ OPTION_CALLBACK, 0, "exclude-per-directory", &opts,
143N_("gitignore"),
144N_("allow explicitly ignored files to be overwritten"),
145PARSE_OPT_NONEG, exclude_per_directory_cb },
146OPT_BOOL('i', NULL, &opts.index_only,
147N_("don't check the working tree after merging")),
148OPT__DRY_RUN(&opts.dry_run, N_("don't update the index or the work tree")),
149OPT_BOOL(0, "no-sparse-checkout", &opts.skip_sparse_checkout,
150N_("skip applying sparse checkout filter")),
151OPT_BOOL(0, "debug-unpack", &opts.debug_unpack,
152N_("debug unpack-trees")),
153{ OPTION_CALLBACK, 0, "recurse-submodules", NULL,
154"checkout", "control recursive updating of submodules",
155PARSE_OPT_OPTARG, option_parse_recurse_submodules_worktree_updater },
156OPT_END()
157};
158159
memset(&opts, 0, sizeof(opts));
160opts.head_idx = -1;
161opts.src_index = &the_index;
162opts.dst_index = &the_index;
163164
git_config(git_read_tree_config, NULL);
165166
argc = parse_options(argc, argv, unused_prefix, read_tree_options,
167read_tree_usage, 0);
168169
hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
170171
prefix_set = opts.prefix ? 1 : 0;
172if (1 < opts.merge + opts.reset + prefix_set)
173die("Which one? -m, --reset, or --prefix?");
174175
/*
176* NEEDSWORK
177*
178* The old index should be read anyway even if we're going to
179* destroy all index entries because we still need to preserve
180* certain information such as index version or split-index
181* mode.
182*/
183184
if (opts.reset || opts.merge || opts.prefix) {
185if (read_cache_unmerged() && (opts.prefix || opts.merge))
186die("You need to resolve your current index first");
187stage = opts.merge = 1;
188}
189resolve_undo_clear();
190191
for (i = 0; i < argc; i++) {
192const char *arg = argv[i];
193194
if (get_oid(arg, &oid))
195die("Not a valid object name %s", arg);
196if (list_tree(&oid) < 0)
197die("failed to unpack tree object %s", arg);
198stage++;
199}
200if (!nr_trees && !read_empty && !opts.merge)
201warning("read-tree: emptying the index with no arguments is deprecated; use --empty");
202else if (nr_trees > 0 && read_empty)
203die("passing trees as arguments contradicts --empty");
204205
if (1 < opts.index_only + opts.update)
206die("-u and -i at the same time makes no sense");
207if ((opts.update || opts.index_only) && !opts.merge)
208die("%s is meaningless without -m, --reset, or --prefix",
209opts.update ? "-u" : "-i");
210if ((opts.dir && !opts.update))
211die("--exclude-per-directory is meaningless unless -u");
212if (opts.merge && !opts.index_only)
213setup_work_tree();
214215
if (opts.merge) {
216switch (stage - 1) {
217case 0:
218die("you must specify at least one tree to merge");
219break;
220case 1:
221opts.fn = opts.prefix ? bind_merge : oneway_merge;
222break;
223case 2:
224opts.fn = twoway_merge;
225opts.initial_checkout = is_cache_unborn();
226break;
227case 3:
228default:
229opts.fn = threeway_merge;
230break;
231}
232233
if (stage - 1 >= 3)
234opts.head_idx = stage - 2;
235else
236opts.head_idx = 1;
237}
238239
if (opts.debug_unpack)
240opts.fn = debug_merge;
241242
cache_tree_free(&active_cache_tree);
243for (i = 0; i < nr_trees; i++) {
244struct tree *tree = trees[i];
245parse_tree(tree);
246init_tree_desc(t+i, tree->buffer, tree->size);
247}
248if (unpack_trees(nr_trees, t, &opts))
249return 128;
250251
if (opts.debug_unpack || opts.dry_run)
252return 0; /* do not write the index out */
253254
/*
255* When reading only one tree (either the most basic form,
256* "-m ent" or "--reset ent" form), we can obtain a fully
257* valid cache-tree because the index must match exactly
258* what came from the tree.
259*/
260if (nr_trees == 1 && !opts.prefix)
261prime_cache_tree(the_repository,
262the_repository->index,
263trees[0]);
264265
if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
266die("unable to write new index file");
267return 0;
268}