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"
16 static struct object_list *trees;
18 static int list_tree(unsigned char *sha1)
20 struct tree *tree = parse_tree_indirect(sha1);
23 object_list_append(&tree->object, &trees);
27 static int read_cache_unmerged(void)
30 struct cache_entry **dst;
31 struct cache_entry *last = NULL;
35 for (i = 0; i < active_nr; i++) {
36 struct cache_entry *ce = active_cache[i];
38 if (last && !strcmp(ce->name, last->name))
40 cache_tree_invalidate_path(active_cache_tree, ce->name);
43 ce->ce_flags &= ~htons(CE_STAGEMASK);
47 active_nr = dst - active_cache;
51 static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
53 struct tree_desc desc;
54 struct name_entry entry;
57 hashcpy(it->sha1, tree->object.sha1);
58 desc.buf = tree->buffer;
59 desc.size = tree->size;
61 while (tree_entry(&desc, &entry)) {
62 if (!S_ISDIR(entry.mode))
65 struct cache_tree_sub *sub;
66 struct tree *subtree = lookup_tree(entry.sha1);
67 if (!subtree->object.parsed)
69 sub = cache_tree_sub(it, entry.path);
70 sub->cache_tree = cache_tree();
71 prime_cache_tree_rec(sub->cache_tree, subtree);
72 cnt += sub->cache_tree->entry_count;
75 it->entry_count = cnt;
78 static void prime_cache_tree(void)
80 struct tree *tree = (struct tree *)trees->item;
83 active_cache_tree = cache_tree();
84 prime_cache_tree_rec(active_cache_tree, tree);
88 static const char read_tree_usage[] = "git-read-tree (<sha> | [[-m [--aggressive] | --reset | --prefix=<prefix>] [-u | -i]] [--exclude-per-directory=<gitignore>] <sha1> [<sha2> [<sha3>]])";
90 static struct lock_file lock_file;
92 int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
94 int i, newfd, stage = 0;
95 unsigned char sha1[20];
96 struct unpack_trees_options opts;
98 memset(&opts, 0, sizeof(opts));
101 setup_git_directory();
102 git_config(git_default_config);
104 newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
106 git_config(git_default_config);
108 for (i = 1; i < argc; i++) {
109 const char *arg = argv[i];
111 /* "-u" means "update", meaning that a merge will update
114 if (!strcmp(arg, "-u")) {
119 if (!strcmp(arg, "-v")) {
120 opts.verbose_update = 1;
124 /* "-i" means "index only", meaning that a merge will
125 * not even look at the working tree.
127 if (!strcmp(arg, "-i")) {
132 /* "--prefix=<subdirectory>/" means keep the current index
133 * entries and put the entries from the tree under the
134 * given subdirectory.
136 if (!strncmp(arg, "--prefix=", 9)) {
137 if (stage || opts.merge || opts.prefix)
138 usage(read_tree_usage);
139 opts.prefix = arg + 9;
142 if (read_cache_unmerged())
143 die("you need to resolve your current index first");
147 /* This differs from "-m" in that we'll silently ignore
148 * unmerged entries and overwrite working tree files that
149 * correspond to them.
151 if (!strcmp(arg, "--reset")) {
152 if (stage || opts.merge || opts.prefix)
153 usage(read_tree_usage);
157 read_cache_unmerged();
161 if (!strcmp(arg, "--trivial")) {
162 opts.trivial_merges_only = 1;
166 if (!strcmp(arg, "--aggressive")) {
171 /* "-m" stands for "merge", meaning we start in stage 1 */
172 if (!strcmp(arg, "-m")) {
173 if (stage || opts.merge || opts.prefix)
174 usage(read_tree_usage);
175 if (read_cache_unmerged())
176 die("you need to resolve your current index first");
182 if (!strncmp(arg, "--exclude-per-directory=", 24)) {
183 struct dir_struct *dir;
186 die("more than one --exclude-per-directory are given.");
188 dir = calloc(1, sizeof(*opts.dir));
189 dir->show_ignored = 1;
190 dir->exclude_per_dir = arg + 24;
192 /* We do not need to nor want to do read-directory
193 * here; we are merely interested in reusing the
194 * per directory ignore stack mechanism.
199 /* using -u and -i at the same time makes no sense */
200 if (1 < opts.index_only + opts.update)
201 usage(read_tree_usage);
203 if (get_sha1(arg, sha1))
204 die("Not a valid object name %s", arg);
205 if (list_tree(sha1) < 0)
206 die("failed to unpack tree object %s", arg);
209 if ((opts.update||opts.index_only) && !opts.merge)
210 usage(read_tree_usage);
211 if ((opts.dir && !opts.update))
212 die("--exclude-per-directory is meaningless unless -u");
215 int pfxlen = strlen(opts.prefix);
217 if (opts.prefix[pfxlen-1] != '/')
218 die("prefix must end with /");
220 die("binding merge takes only one tree");
221 pos = cache_name_pos(opts.prefix, pfxlen);
223 die("corrupt index file");
225 if (pos < active_nr &&
226 !strncmp(active_cache[pos]->name, opts.prefix, pfxlen))
227 die("subdirectory '%s' already exists.", opts.prefix);
228 pos = cache_name_pos(opts.prefix, pfxlen-1);
230 die("file '%.*s' already exists.",
231 pfxlen-1, opts.prefix);
236 die("just how do you expect me to merge %d trees?", stage-1);
239 opts.fn = opts.prefix ? bind_merge : oneway_merge;
242 opts.fn = twoway_merge;
246 opts.fn = threeway_merge;
247 cache_tree_free(&active_cache_tree);
252 opts.head_idx = stage - 2;
257 unpack_trees(trees, &opts);
260 * When reading only one tree (either the most basic form,
261 * "-m ent" or "--reset ent" form), we can obtain a fully
262 * valid cache-tree because the index must match exactly
263 * what came from the tree.
265 if (trees && trees->item && !opts.prefix && (!opts.merge || (stage == 2))) {
266 cache_tree_free(&active_cache_tree);
270 if (write_cache(newfd, active_cache, active_nr) ||
271 close(newfd) || commit_lock_file(&lock_file))
272 die("unable to write new index file");