2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
10 #include "tree-walk.h"
11 #include "cache-tree.h"
12 #include "unpack-trees.h"
17 static struct tree *trees[MAX_UNPACK_TREES];
19 static int list_tree(unsigned char *sha1)
23 if (nr_trees >= MAX_UNPACK_TREES)
24 die("I cannot read more than %d trees", MAX_UNPACK_TREES);
25 tree = parse_tree_indirect(sha1);
28 trees[nr_trees++] = tree;
32 static const char read_tree_usage[] = "git read-tree (<sha> | [[-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>] [-u | -i]] [--exclude-per-directory=<gitignore>] [--index-output=<file>] <sha1> [<sha2> [<sha3>]])";
34 static struct lock_file lock_file;
36 int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
38 int i, newfd, stage = 0;
39 unsigned char sha1[20];
40 struct tree_desc t[MAX_UNPACK_TREES];
41 struct unpack_trees_options opts;
43 memset(&opts, 0, sizeof(opts));
45 opts.src_index = &the_index;
46 opts.dst_index = &the_index;
48 git_config(git_default_config, NULL);
50 newfd = hold_locked_index(&lock_file, 1);
52 for (i = 1; i < argc; i++) {
53 const char *arg = argv[i];
55 /* "-u" means "update", meaning that a merge will update
58 if (!strcmp(arg, "-u")) {
63 if (!strcmp(arg, "-v")) {
64 opts.verbose_update = 1;
68 /* "-i" means "index only", meaning that a merge will
69 * not even look at the working tree.
71 if (!strcmp(arg, "-i")) {
76 if (!prefixcmp(arg, "--index-output=")) {
77 set_alternate_index_output(arg + 15);
81 /* "--prefix=<subdirectory>/" means keep the current index
82 * entries and put the entries from the tree under the
85 if (!prefixcmp(arg, "--prefix=")) {
86 if (stage || opts.merge || opts.prefix)
87 usage(read_tree_usage);
88 opts.prefix = arg + 9;
91 if (read_cache_unmerged())
92 die("you need to resolve your current index first");
96 /* This differs from "-m" in that we'll silently ignore
97 * unmerged entries and overwrite working tree files that
100 if (!strcmp(arg, "--reset")) {
101 if (stage || opts.merge || opts.prefix)
102 usage(read_tree_usage);
106 read_cache_unmerged();
110 if (!strcmp(arg, "--trivial")) {
111 opts.trivial_merges_only = 1;
115 if (!strcmp(arg, "--aggressive")) {
120 /* "-m" stands for "merge", meaning we start in stage 1 */
121 if (!strcmp(arg, "-m")) {
122 if (stage || opts.merge || opts.prefix)
123 usage(read_tree_usage);
124 if (read_cache_unmerged())
125 die("you need to resolve your current index first");
131 if (!prefixcmp(arg, "--exclude-per-directory=")) {
132 struct dir_struct *dir;
135 die("more than one --exclude-per-directory are given.");
137 dir = xcalloc(1, sizeof(*opts.dir));
138 dir->flags |= DIR_SHOW_IGNORED;
139 dir->exclude_per_dir = arg + 24;
141 /* We do not need to nor want to do read-directory
142 * here; we are merely interested in reusing the
143 * per directory ignore stack mechanism.
148 /* using -u and -i at the same time makes no sense */
149 if (1 < opts.index_only + opts.update)
150 usage(read_tree_usage);
152 if (get_sha1(arg, sha1))
153 die("Not a valid object name %s", arg);
154 if (list_tree(sha1) < 0)
155 die("failed to unpack tree object %s", arg);
158 if ((opts.update||opts.index_only) && !opts.merge)
159 usage(read_tree_usage);
160 if ((opts.dir && !opts.update))
161 die("--exclude-per-directory is meaningless unless -u");
162 if (opts.merge && !opts.index_only)
167 die("just how do you expect me to merge %d trees?", stage-1);
170 opts.fn = opts.prefix ? bind_merge : oneway_merge;
173 opts.fn = twoway_merge;
174 opts.initial_checkout = is_cache_unborn();
178 opts.fn = threeway_merge;
183 opts.head_idx = stage - 2;
188 cache_tree_free(&active_cache_tree);
189 for (i = 0; i < nr_trees; i++) {
190 struct tree *tree = trees[i];
192 init_tree_desc(t+i, tree->buffer, tree->size);
194 if (unpack_trees(nr_trees, t, &opts))
198 * When reading only one tree (either the most basic form,
199 * "-m ent" or "--reset ent" form), we can obtain a fully
200 * valid cache-tree because the index must match exactly
201 * what came from the tree.
203 * The same holds true if we are switching between two trees
204 * using read-tree -m A B. The index must match B after that.
206 if (nr_trees == 1 && !opts.prefix)
207 prime_cache_tree(&active_cache_tree, trees[0]);
208 else if (nr_trees == 2 && opts.merge)
209 prime_cache_tree(&active_cache_tree, trees[1]);
211 if (write_cache(newfd, active_cache, active_nr) ||
212 commit_locked_index(&lock_file))
213 die("unable to write new index file");