git-read-tree: simplify merge loops enormously
[alt-git.git] / read-tree.c
blob4acbb6b3f25cd5cb50dda3ac34b1b37edad639b1
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
8 static int stage = 0;
9 static int update = 0;
11 static int unpack_tree(unsigned char *sha1)
13 void *buffer;
14 unsigned long size;
15 int ret;
17 buffer = read_object_with_reference(sha1, "tree", &size, NULL);
18 if (!buffer)
19 return -1;
20 ret = read_tree(buffer, size, stage);
21 free(buffer);
22 return ret;
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
50 * mode have changed?
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,
56 * take the other.
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.
64 if (a && b && c) {
65 if (same(b,c))
66 return c;
67 if (same(a,b))
68 return c;
69 if (same(a,c))
70 return b;
72 return NULL;
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)
81 struct stat st;
83 if (!lstat(ce->name, &st)) {
84 unsigned changed = ce_match_stat(ce, &st);
85 if (!changed)
86 return;
87 errno = 0;
89 if (errno == ENOENT)
90 return;
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);
108 if (old) {
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
114 * a match.
116 if (same(old, merge)) {
117 *merge = *old;
118 } else {
119 verify_uptodate(old);
122 merge->ce_flags &= ~htons(CE_STAGEMASK);
123 *dst++ = merge;
124 return 1;
127 static int threeway_merge(struct cache_entry *stages[4], struct cache_entry **dst)
129 struct cache_entry *old = stages[0];
130 struct cache_entry *a = stages[1], *b = stages[2], *c = stages[3];
131 struct cache_entry *merge;
132 int count;
135 * If we have an entry in the index cache ("old"), then we want
136 * to make sure that it matches any entries in stage 2 ("first
137 * branch", aka "b").
139 if (old) {
140 if (!b || !same(old, b))
141 return -1;
143 merge = merge_entries(a, b, c);
144 if (merge)
145 return merged_entry(merge, old, dst);
146 if (old)
147 verify_uptodate(old);
148 count = 0;
149 if (a) { *dst++ = a; count++; }
150 if (b) { *dst++ = b; count++; }
151 if (c) { *dst++ = c; count++; }
152 return count;
156 * Two-way merge.
158 * The rule is:
159 * - every current entry has to match the old tree
160 * - if the current entry matches the new tree, we leave it
161 * as-is. Otherwise we require that it be up-to-date.
163 static int twoway_merge(struct cache_entry **src, struct cache_entry **dst)
165 struct cache_entry *old = src[0];
166 struct cache_entry *a = src[1], *b = src[2];
168 if (src[3])
169 return -1;
171 if (old) {
172 if (!a || !same(old, a))
173 return -1;
175 if (b)
176 return merged_entry(b, old, dst);
177 if (old)
178 verify_uptodate(old);
179 return 0;
183 * One-way merge.
185 * The rule is:
186 * - take the stat information from stage0, take the data from stage1
188 static int oneway_merge(struct cache_entry **src, struct cache_entry **dst)
190 struct cache_entry *old = src[0];
191 struct cache_entry *a = src[1];
193 if (src[2] || src[3])
194 return -1;
196 if (!a)
197 return 0;
198 if (old && same(old, a))
199 *a = *old;
200 a->ce_flags &= ~htons(CE_STAGEMASK);
201 *dst++ = a;
202 return 1;
205 static void check_updates(struct cache_entry **src, int nr)
207 static struct checkout state = {
208 .base_dir = "",
209 .force = 1,
210 .quiet = 1,
211 .refresh_cache = 1,
213 unsigned short mask = htons(CE_UPDATE);
214 while (nr--) {
215 struct cache_entry *ce = *src++;
216 if (ce->ce_flags & mask) {
217 ce->ce_flags &= ~mask;
218 if (update)
219 checkout_entry(ce, &state);
224 static void merge_cache(struct cache_entry **src, int nr, int (*fn)(struct cache_entry **, struct cache_entry **))
226 struct cache_entry **dst = src;
228 while (nr) {
229 int entries;
230 struct cache_entry *name, *ce, *stages[4] = { NULL, };
232 name = ce = *src;
233 for (;;) {
234 int stage = ce_stage(ce);
235 stages[stage] = ce;
236 ce = *++src;
237 active_nr--;
238 if (!--nr)
239 break;
240 if (!path_matches(ce, name))
241 break;
244 entries = fn(stages, dst);
245 if (entries < 0)
246 reject_merge(name);
247 dst += entries;
248 active_nr += entries;
250 check_updates(active_cache, active_nr);
253 static char *read_tree_usage = "git-read-tree (<sha> | -m <sha1> [<sha2> [<sha3>]])";
255 static struct cache_file cache_file;
257 int main(int argc, char **argv)
259 int i, newfd, merge;
260 unsigned char sha1[20];
262 newfd = hold_index_file_for_update(&cache_file, get_index_file());
263 if (newfd < 0)
264 die("unable to create new cachefile");
266 merge = 0;
267 for (i = 1; i < argc; i++) {
268 const char *arg = argv[i];
270 /* "-u" means "update", meaning that a merge will update the working directory */
271 if (!strcmp(arg, "-u")) {
272 update = 1;
273 continue;
276 /* "-m" stands for "merge", meaning we start in stage 1 */
277 if (!strcmp(arg, "-m")) {
278 int i;
279 if (stage)
280 die("-m needs to come first");
281 read_cache();
282 for (i = 0; i < active_nr; i++) {
283 if (ce_stage(active_cache[i]))
284 die("you need to resolve your current index first");
286 stage = 1;
287 merge = 1;
288 continue;
290 if (get_sha1(arg, sha1) < 0)
291 usage(read_tree_usage);
292 if (stage > 3)
293 usage(read_tree_usage);
294 if (unpack_tree(sha1) < 0)
295 die("failed to unpack tree object %s", arg);
296 stage++;
298 if (merge) {
299 switch (stage) {
300 case 4: /* Three-way merge */
301 merge_cache(active_cache, active_nr, threeway_merge);
302 break;
303 case 3: /* Update from one tree to another */
304 merge_cache(active_cache, active_nr, twoway_merge);
305 check_updates(active_cache, active_nr);
306 break;
307 case 2: /* Just read a tree, merge with old cache contents */
308 merge_cache(active_cache, active_nr, oneway_merge);
309 break;
310 default:
311 die("just how do you expect me to merge %d trees?", stage-1);
314 if (write_cache(newfd, active_cache, active_nr) ||
315 commit_index_file(&cache_file))
316 die("unable to write new index file");
317 return 0;