2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
11 static int unpack_tree(unsigned char *sha1)
17 buffer = read_object_with_reference(sha1, "tree", &size, NULL);
20 ret = read_tree(buffer, size, stage);
25 static int path_matches(struct cache_entry *a, struct cache_entry *b)
27 int len = ce_namelen(a);
28 return ce_namelen(b) == len &&
29 !memcmp(a->name, b->name, len);
32 static int same(struct cache_entry *a, struct cache_entry *b)
34 return a->ce_mode == b->ce_mode &&
35 !memcmp(a->sha1, b->sha1, 20);
40 * This removes all trivial merges that don't change the tree
41 * and collapses them to state 0.
43 static struct cache_entry *merge_entries(struct cache_entry *a,
44 struct cache_entry *b,
45 struct cache_entry *c)
48 * Ok, all three entries describe the same
49 * filename, but maybe the contents or file
52 * The trivial cases end up being the ones where two
53 * out of three files are the same:
54 * - both destinations the same, trivially take either
55 * - one of the destination versions hasn't changed,
58 * The "all entries exactly the same" case falls out as
59 * a special case of any of the "two same" cases.
61 * Here "a" is "original", and "b" and "c" are the two
62 * trees we are merging.
76 * When a CE gets turned into an unmerged entry, we
77 * want it to be up-to-date
79 static void verify_uptodate(struct cache_entry *ce)
83 if (!lstat(ce->name, &st)) {
84 unsigned changed = ce_match_stat(ce, &st);
91 die("Entry '%s' not uptodate. Cannot merge.", ce->name);
95 * If the old tree contained a CE that isn't even in the
96 * result, that's always a problem, regardless of whether
97 * it's up-to-date or not (ie it can be a file that we
98 * have updated but not committed yet).
100 static void reject_merge(struct cache_entry *ce)
102 die("Entry '%s' would be overwritten by merge. Cannot merge.", ce->name);
105 static int merged_entry(struct cache_entry *merge, struct cache_entry *old, struct cache_entry **dst)
107 merge->ce_flags |= htons(CE_UPDATE);
110 * See if we can re-use the old CE directly?
111 * That way we get the uptodate stat info.
113 * This also removes the UPDATE flag on
116 if (same(old, merge)) {
119 verify_uptodate(old);
122 merge->ce_flags &= ~htons(CE_STAGEMASK);
127 static int deleted_entry(struct cache_entry *ce, struct cache_entry *old, struct cache_entry **dst)
130 verify_uptodate(old);
136 static int threeway_merge(struct cache_entry *stages[4], struct cache_entry **dst)
138 struct cache_entry *old = stages[0];
139 struct cache_entry *a = stages[1], *b = stages[2], *c = stages[3];
140 struct cache_entry *merge;
144 * If we have an entry in the index cache ("old"), then we want
145 * to make sure that it matches any entries in stage 2 ("first
149 if (!b || !same(old, b))
152 merge = merge_entries(a, b, c);
154 return merged_entry(merge, old, dst);
156 verify_uptodate(old);
158 if (a) { *dst++ = a; count++; }
159 if (b) { *dst++ = b; count++; }
160 if (c) { *dst++ = c; count++; }
167 * The rule is to "carry forward" what is in the index without losing
168 * information across a "fast forward", favoring a successful merge
169 * over a merge failure when it makes sense. For details of the
170 * "carry forward" rule, please see <Documentation/git-read-tree.txt>.
173 static int twoway_merge(struct cache_entry **src, struct cache_entry **dst)
175 struct cache_entry *current = src[0];
176 struct cache_entry *oldtree = src[1], *newtree = src[2];
182 if ((!oldtree && !newtree) || /* 4 and 5 */
183 (!oldtree && newtree &&
184 same(current, newtree)) || /* 6 and 7 */
185 (oldtree && newtree &&
186 same(oldtree, newtree)) || /* 14 and 15 */
187 (oldtree && newtree &&
188 !same(oldtree, newtree) && /* 18 and 19*/
189 same(current, newtree))) {
193 else if (oldtree && !newtree && same(current, oldtree)) {
195 return deleted_entry(oldtree, current, dst);
197 else if (oldtree && newtree &&
198 same(current, oldtree) && !same(current, newtree)) {
200 return merged_entry(newtree, current, dst);
203 /* all other failures */
207 return merged_entry(newtree, current, dst);
209 return deleted_entry(oldtree, current, dst);
213 * Two-way merge emulated with three-way merge.
215 * This treats "read-tree -m H M" by transforming it internally
216 * into "read-tree -m H I+H M", where I+H is a tree that would
217 * contain the contents of the current index file, overlayed on
218 * top of H. Unlike the traditional two-way merge, this leaves
219 * the stages in the resulting index file and lets the user resolve
220 * the merge conflicts using standard tools for three-way merge.
222 * This function is just to set-up such an arrangement, and the
223 * actual merge uses threeway_merge() function.
225 static void setup_emu23(void)
227 /* stage0 contains I, stage1 H, stage2 M.
228 * move stage2 to stage3, and create stage2 entries
229 * by scanning stage0 and stage1 entries.
231 int i, namelen, size;
232 struct cache_entry *ce, *stage2;
234 for (i = 0; i < active_nr; i++) {
235 ce = active_cache[i];
236 if (ce_stage(ce) != 2)
238 /* hoist them up to stage 3 */
239 namelen = ce_namelen(ce);
240 ce->ce_flags = create_ce_flags(namelen, 3);
243 for (i = 0; i < active_nr; i++) {
244 ce = active_cache[i];
245 if (ce_stage(ce) > 1)
247 namelen = ce_namelen(ce);
248 size = cache_entry_size(namelen);
249 stage2 = xmalloc(size);
250 memcpy(stage2, ce, size);
251 stage2->ce_flags = create_ce_flags(namelen, 2);
252 if (add_cache_entry(stage2, ADD_CACHE_OK_TO_ADD) < 0)
253 die("cannot merge index and our head tree");
255 /* We are done with this name, so skip to next name */
256 while (i < active_nr &&
257 ce_namelen(active_cache[i]) == namelen &&
258 !memcmp(active_cache[i]->name, ce->name, namelen))
260 i--; /* compensate for the loop control */
268 * - take the stat information from stage0, take the data from stage1
270 static int oneway_merge(struct cache_entry **src, struct cache_entry **dst)
272 struct cache_entry *old = src[0];
273 struct cache_entry *a = src[1];
275 if (src[2] || src[3])
280 if (old && same(old, a)) {
284 return merged_entry(a, NULL, dst);
287 static void check_updates(struct cache_entry **src, int nr)
289 static struct checkout state = {
295 unsigned short mask = htons(CE_UPDATE);
297 struct cache_entry *ce = *src++;
303 if (ce->ce_flags & mask) {
304 ce->ce_flags &= ~mask;
306 checkout_entry(ce, &state);
311 typedef int (*merge_fn_t)(struct cache_entry **, struct cache_entry **);
313 static void merge_cache(struct cache_entry **src, int nr, merge_fn_t fn)
315 struct cache_entry **dst = src;
319 struct cache_entry *name, *ce, *stages[4] = { NULL, };
323 int stage = ce_stage(ce);
329 if (!path_matches(ce, name))
333 entries = fn(stages, dst);
337 active_nr += entries;
339 check_updates(active_cache, active_nr);
342 static int read_cache_unmerged(void)
345 struct cache_entry **dst;
350 for (i = 0; i < active_nr; i++) {
351 struct cache_entry *ce = active_cache[i];
360 active_nr -= deleted;
364 static char *read_tree_usage = "git-read-tree (<sha> | -m [-u] <sha1> [<sha2> [<sha3>]])";
366 static struct cache_file cache_file;
368 int main(int argc, char **argv)
370 int i, newfd, merge, reset, emu23;
371 unsigned char sha1[20];
373 newfd = hold_index_file_for_update(&cache_file, get_index_file());
375 die("unable to create new cachefile");
380 for (i = 1; i < argc; i++) {
381 const char *arg = argv[i];
383 /* "-u" means "update", meaning that a merge will update the working directory */
384 if (!strcmp(arg, "-u")) {
389 /* This differs from "-m" in that we'll silently ignore unmerged entries */
390 if (!strcmp(arg, "--reset")) {
391 if (stage || merge || emu23)
392 usage(read_tree_usage);
396 read_cache_unmerged();
399 /* "-m" stands for "merge", meaning we start in stage 1 */
400 if (!strcmp(arg, "-m")) {
401 if (stage || merge || emu23)
402 usage(read_tree_usage);
403 if (read_cache_unmerged())
404 die("you need to resolve your current index first");
410 /* "-emu23" uses 3-way merge logic to perform fast-forward */
411 if (!strcmp(arg, "--emu23")) {
412 if (stage || merge || emu23)
413 usage(read_tree_usage);
414 if (read_cache_unmerged())
415 die("you need to resolve your current index first");
416 merge = emu23 = stage = 1;
420 if (get_sha1(arg, sha1) < 0)
421 usage(read_tree_usage);
423 usage(read_tree_usage);
424 if (unpack_tree(sha1) < 0)
425 die("failed to unpack tree object %s", arg);
428 if (update && !merge)
429 usage(read_tree_usage);
431 static const merge_fn_t merge_function[] = {
434 [3] = threeway_merge,
438 if (stage < 2 || stage > 4)
439 die("just how do you expect me to merge %d trees?", stage-1);
440 if (emu23 && stage != 3)
441 die("--emu23 takes only two trees");
442 fn = merge_function[stage-1];
443 if (stage == 3 && emu23) {
445 fn = merge_function[3];
447 merge_cache(active_cache, active_nr, fn);
449 if (write_cache(newfd, active_cache, active_nr) ||
450 commit_index_file(&cache_file))
451 die("unable to write new index file");