read-tree: move merge functions to the library
[git/git-svn.git] / builtin-read-tree.c
blob8869cedea1f9b07d78520858876cca614283d01b
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
7 #include "cache.h"
8 #include "object.h"
9 #include "tree.h"
10 #include "tree-walk.h"
11 #include "cache-tree.h"
12 #include "unpack-trees.h"
13 #include "builtin.h"
15 static struct object_list *trees = NULL;
17 static int list_tree(unsigned char *sha1)
19 struct tree *tree = parse_tree_indirect(sha1);
20 if (!tree)
21 return -1;
22 object_list_append(&tree->object, &trees);
23 return 0;
26 static int read_cache_unmerged(void)
28 int i;
29 struct cache_entry **dst;
30 struct cache_entry *last = NULL;
32 read_cache();
33 dst = active_cache;
34 for (i = 0; i < active_nr; i++) {
35 struct cache_entry *ce = active_cache[i];
36 if (ce_stage(ce)) {
37 if (last && !strcmp(ce->name, last->name))
38 continue;
39 cache_tree_invalidate_path(active_cache_tree, ce->name);
40 last = ce;
41 ce->ce_mode = 0;
42 ce->ce_flags &= ~htons(CE_STAGEMASK);
44 *dst++ = ce;
46 active_nr = dst - active_cache;
47 return !!last;
50 static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
52 struct tree_desc desc;
53 struct name_entry entry;
54 int cnt;
56 memcpy(it->sha1, tree->object.sha1, 20);
57 desc.buf = tree->buffer;
58 desc.size = tree->size;
59 cnt = 0;
60 while (tree_entry(&desc, &entry)) {
61 if (!S_ISDIR(entry.mode))
62 cnt++;
63 else {
64 struct cache_tree_sub *sub;
65 struct tree *subtree = lookup_tree(entry.sha1);
66 if (!subtree->object.parsed)
67 parse_tree(subtree);
68 sub = cache_tree_sub(it, entry.path);
69 sub->cache_tree = cache_tree();
70 prime_cache_tree_rec(sub->cache_tree, subtree);
71 cnt += sub->cache_tree->entry_count;
74 it->entry_count = cnt;
77 static void prime_cache_tree(void)
79 struct tree *tree = (struct tree *)trees->item;
80 if (!tree)
81 return;
82 active_cache_tree = cache_tree();
83 prime_cache_tree_rec(active_cache_tree, tree);
87 static const char read_tree_usage[] = "git-read-tree (<sha> | [[-m [--aggressive] | --reset | --prefix=<prefix>] [-u | -i]] <sha1> [<sha2> [<sha3>]])";
89 static struct lock_file lock_file;
91 int cmd_read_tree(int argc, const char **argv, const char *prefix)
93 int i, newfd, stage = 0;
94 unsigned char sha1[20];
95 struct unpack_trees_options opts;
97 memset(&opts, 0, sizeof(opts));
98 opts.head_idx = -1;
100 setup_git_directory();
101 git_config(git_default_config);
103 newfd = hold_lock_file_for_update(&lock_file, get_index_file());
104 if (newfd < 0)
105 die("unable to create new index file");
107 git_config(git_default_config);
109 for (i = 1; i < argc; i++) {
110 const char *arg = argv[i];
112 /* "-u" means "update", meaning that a merge will update
113 * the working tree.
115 if (!strcmp(arg, "-u")) {
116 opts.update = 1;
117 continue;
120 if (!strcmp(arg, "-v")) {
121 opts.verbose_update = 1;
122 continue;
125 /* "-i" means "index only", meaning that a merge will
126 * not even look at the working tree.
128 if (!strcmp(arg, "-i")) {
129 opts.index_only = 1;
130 continue;
133 /* "--prefix=<subdirectory>/" means keep the current index
134 * entries and put the entries from the tree under the
135 * given subdirectory.
137 if (!strncmp(arg, "--prefix=", 9)) {
138 if (stage || opts.merge || opts.prefix)
139 usage(read_tree_usage);
140 opts.prefix = arg + 9;
141 opts.merge = 1;
142 stage = 1;
143 if (read_cache_unmerged())
144 die("you need to resolve your current index first");
145 continue;
148 /* This differs from "-m" in that we'll silently ignore
149 * unmerged entries and overwrite working tree files that
150 * correspond to them.
152 if (!strcmp(arg, "--reset")) {
153 if (stage || opts.merge || opts.prefix)
154 usage(read_tree_usage);
155 opts.reset = 1;
156 opts.merge = 1;
157 stage = 1;
158 read_cache_unmerged();
159 continue;
162 if (!strcmp(arg, "--trivial")) {
163 opts.trivial_merges_only = 1;
164 continue;
167 if (!strcmp(arg, "--aggressive")) {
168 opts.aggressive = 1;
169 continue;
172 /* "-m" stands for "merge", meaning we start in stage 1 */
173 if (!strcmp(arg, "-m")) {
174 if (stage || opts.merge || opts.prefix)
175 usage(read_tree_usage);
176 if (read_cache_unmerged())
177 die("you need to resolve your current index first");
178 stage = 1;
179 opts.merge = 1;
180 continue;
183 /* using -u and -i at the same time makes no sense */
184 if (1 < opts.index_only + opts.update)
185 usage(read_tree_usage);
187 if (get_sha1(arg, sha1))
188 die("Not a valid object name %s", arg);
189 if (list_tree(sha1) < 0)
190 die("failed to unpack tree object %s", arg);
191 stage++;
193 if ((opts.update||opts.index_only) && !opts.merge)
194 usage(read_tree_usage);
196 if (opts.prefix) {
197 int pfxlen = strlen(opts.prefix);
198 int pos;
199 if (opts.prefix[pfxlen-1] != '/')
200 die("prefix must end with /");
201 if (stage != 2)
202 die("binding merge takes only one tree");
203 pos = cache_name_pos(opts.prefix, pfxlen);
204 if (0 <= pos)
205 die("corrupt index file");
206 pos = -pos-1;
207 if (pos < active_nr &&
208 !strncmp(active_cache[pos]->name, opts.prefix, pfxlen))
209 die("subdirectory '%s' already exists.", opts.prefix);
210 pos = cache_name_pos(opts.prefix, pfxlen-1);
211 if (0 <= pos)
212 die("file '%.*s' already exists.",
213 pfxlen-1, opts.prefix);
216 if (opts.merge) {
217 if (stage < 2)
218 die("just how do you expect me to merge %d trees?", stage-1);
219 switch (stage - 1) {
220 case 1:
221 opts.fn = opts.prefix ? bind_merge : oneway_merge;
222 break;
223 case 2:
224 opts.fn = twoway_merge;
225 break;
226 case 3:
227 default:
228 opts.fn = threeway_merge;
229 cache_tree_free(&active_cache_tree);
230 break;
233 if (stage - 1 >= 3)
234 opts.head_idx = stage - 2;
235 else
236 opts.head_idx = 1;
239 unpack_trees(trees, &opts);
242 * When reading only one tree (either the most basic form,
243 * "-m ent" or "--reset ent" form), we can obtain a fully
244 * valid cache-tree because the index must match exactly
245 * what came from the tree.
247 if (trees && trees->item && !opts.prefix && (!opts.merge || (stage == 2))) {
248 cache_tree_free(&active_cache_tree);
249 prime_cache_tree();
252 if (write_cache(newfd, active_cache, active_nr) ||
253 close(newfd) || commit_lock_file(&lock_file))
254 die("unable to write new index file");
255 return 0;