1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6#include "cache.h"
7
8static int stage = 0;
9static int update = 0;
10
11static int unpack_tree(unsigned char *sha1)
12{
13 void *buffer;
14 unsigned long size;
15 int ret;
16
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;
23}
24
25static int path_matches(struct cache_entry *a, struct cache_entry *b)
26{
27 int len = ce_namelen(a);
28 return ce_namelen(b) == len &&
29 !memcmp(a->name, b->name, len);
30}
31
32static int same(struct cache_entry *a, struct cache_entry *b)
33{
34 return a->ce_mode == b->ce_mode &&
35 !memcmp(a->sha1, b->sha1, 20);
36}
37
38
39/*
40 * This removes all trivial merges that don't change the tree
41 * and collapses them to state 0.
42 */
43static struct cache_entry *merge_entries(struct cache_entry *a,
44 struct cache_entry *b,
45 struct cache_entry *c)
46{
47 /*
48 * Ok, all three entries describe the same
49 * filename, but maybe the contents or file
50 * mode have changed?
51 *
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.
57 *
58 * The "all entries exactly the same" case falls out as
59 * a special case of any of the "two same" cases.
60 *
61 * Here "a" is "original", and "b" and "c" are the two
62 * trees we are merging.
63 */
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;
71 }
72 return NULL;
73}
74
75/*
76 * When a CE gets turned into an unmerged entry, we
77 * want it to be up-to-date
78 */
79static void verify_uptodate(struct cache_entry *ce)
80{
81 struct stat st;
82
83 if (!lstat(ce->name, &st)) {
84 unsigned changed = ce_match_stat(ce, &st);
85 if (!changed)
86 return;
87 errno = 0;
88 }
89 if (errno == ENOENT)
90 return;
91 die("Entry '%s' not uptodate. Cannot merge.", ce->name);
92}
93
94/*
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).
99 */
100static void reject_merge(struct cache_entry *ce)
101{
102 die("Entry '%s' would be overwritten by merge. Cannot merge.", ce->name);
103}
104
105static int merged_entry(struct cache_entry *merge, struct cache_entry *old, struct cache_entry **dst)
106{
107 merge->ce_flags |= htons(CE_UPDATE);
108 if (old) {
109 /*
110 * See if we can re-use the old CE directly?
111 * That way we get the uptodate stat info.
112 *
113 * This also removes the UPDATE flag on
114 * a match.
115 */
116 if (same(old, merge)) {
117 *merge = *old;
118 } else {
119 verify_uptodate(old);
120 }
121 }
122 merge->ce_flags &= ~htons(CE_STAGEMASK);
123 *dst++ = merge;
124 return 1;
125}
126
127static int deleted_entry(struct cache_entry *ce, struct cache_entry *old, struct cache_entry **dst)
128{
129 if (old)
130 verify_uptodate(old);
131 ce->ce_mode = 0;
132 *dst++ = ce;
133 return 1;
134}
135
136static int threeway_merge(struct cache_entry *stages[4], struct cache_entry **dst)
137{
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;
141 int count;
142
143 /*
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
146 * branch", aka "b").
147 */
148 if (old) {
149 if (!b || !same(old, b))
150 return -1;
151 }
152 merge = merge_entries(a, b, c);
153 if (merge)
154 return merged_entry(merge, old, dst);
155 if (old)
156 verify_uptodate(old);
157 count = 0;
158 if (a) { *dst++ = a; count++; }
159 if (b) { *dst++ = b; count++; }
160 if (c) { *dst++ = c; count++; }
161 return count;
162}
163
164/*
165 * Two-way merge.
166 *
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>.
171 *
172 */
173static int twoway_merge(struct cache_entry **src, struct cache_entry **dst)
174{
175 struct cache_entry *current = src[0];
176 struct cache_entry *oldtree = src[1], *newtree = src[2];
177
178 if (src[3])
179 return -1;
180
181 if (current) {
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))) {
190 *dst++ = current;
191 return 1;
192 }
193 else if (oldtree && !newtree && same(current, oldtree)) {
194 /* 10 or 11 */
195 return deleted_entry(oldtree, current, dst);
196 }
197 else if (oldtree && newtree &&
198 same(current, oldtree) && !same(current, newtree)) {
199 /* 20 or 21 */
200 return merged_entry(newtree, current, dst);
201 }
202 else
203 /* all other failures */
204 return -1;
205 }
206 else if (newtree)
207 return merged_entry(newtree, current, dst);
208 else
209 return deleted_entry(oldtree, current, dst);
210}
211
212/*
213 * One-way merge.
214 *
215 * The rule is:
216 * - take the stat information from stage0, take the data from stage1
217 */
218static int oneway_merge(struct cache_entry **src, struct cache_entry **dst)
219{
220 struct cache_entry *old = src[0];
221 struct cache_entry *a = src[1];
222
223 if (src[2] || src[3])
224 return -1;
225
226 if (!a)
227 return 0;
228 if (old && same(old, a)) {
229 *dst++ = old;
230 return 1;
231 }
232 return merged_entry(a, NULL, dst);
233}
234
235static void check_updates(struct cache_entry **src, int nr)
236{
237 static struct checkout state = {
238 .base_dir = "",
239 .force = 1,
240 .quiet = 1,
241 .refresh_cache = 1,
242 };
243 unsigned short mask = htons(CE_UPDATE);
244 while (nr--) {
245 struct cache_entry *ce = *src++;
246 if (!ce->ce_mode) {
247 if (update)
248 unlink(ce->name);
249 continue;
250 }
251 if (ce->ce_flags & mask) {
252 ce->ce_flags &= ~mask;
253 if (update)
254 checkout_entry(ce, &state);
255 }
256 }
257}
258
259typedef int (*merge_fn_t)(struct cache_entry **, struct cache_entry **);
260
261static void merge_cache(struct cache_entry **src, int nr, merge_fn_t fn)
262{
263 struct cache_entry **dst = src;
264
265 while (nr) {
266 int entries;
267 struct cache_entry *name, *ce, *stages[4] = { NULL, };
268
269 name = ce = *src;
270 for (;;) {
271 int stage = ce_stage(ce);
272 stages[stage] = ce;
273 ce = *++src;
274 active_nr--;
275 if (!--nr)
276 break;
277 if (!path_matches(ce, name))
278 break;
279 }
280
281 entries = fn(stages, dst);
282 if (entries < 0)
283 reject_merge(name);
284 dst += entries;
285 active_nr += entries;
286 }
287 check_updates(active_cache, active_nr);
288}
289
290static int read_cache_unmerged(void)
291{
292 int i, deleted;
293 struct cache_entry **dst;
294
295 read_cache();
296 dst = active_cache;
297 deleted = 0;
298 for (i = 0; i < active_nr; i++) {
299 struct cache_entry *ce = active_cache[i];
300 if (ce_stage(ce)) {
301 deleted++;
302 continue;
303 }
304 if (deleted)
305 *dst = ce;
306 dst++;
307 }
308 active_nr -= deleted;
309 return deleted;
310}
311
312static char *read_tree_usage = "git-read-tree (<sha> | -m [-u] <sha1> [<sha2> [<sha3>]])";
313
314static struct cache_file cache_file;
315
316int main(int argc, char **argv)
317{
318 int i, newfd, merge, reset;
319 unsigned char sha1[20];
320
321 newfd = hold_index_file_for_update(&cache_file, get_index_file());
322 if (newfd < 0)
323 die("unable to create new cachefile");
324
325 merge = 0;
326 reset = 0;
327 for (i = 1; i < argc; i++) {
328 const char *arg = argv[i];
329
330 /* "-u" means "update", meaning that a merge will update the working directory */
331 if (!strcmp(arg, "-u")) {
332 update = 1;
333 continue;
334 }
335
336 /* This differs from "-m" in that we'll silently ignore unmerged entries */
337 if (!strcmp(arg, "--reset")) {
338 if (stage || merge)
339 usage(read_tree_usage);
340 reset = 1;
341 merge = 1;
342 stage = 1;
343 read_cache_unmerged();
344 }
345
346 /* "-m" stands for "merge", meaning we start in stage 1 */
347 if (!strcmp(arg, "-m")) {
348 if (stage || merge)
349 usage(read_tree_usage);
350 if (read_cache_unmerged())
351 die("you need to resolve your current index first");
352 stage = 1;
353 merge = 1;
354 continue;
355 }
356 if (get_sha1(arg, sha1) < 0)
357 usage(read_tree_usage);
358 if (stage > 3)
359 usage(read_tree_usage);
360 if (unpack_tree(sha1) < 0)
361 die("failed to unpack tree object %s", arg);
362 stage++;
363 }
364 if (update && !merge)
365 usage(read_tree_usage);
366 if (merge) {
367 static const merge_fn_t merge_function[] = {
368 [1] = oneway_merge,
369 [2] = twoway_merge,
370 [3] = threeway_merge,
371 };
372 if (stage < 2 || stage > 4)
373 die("just how do you expect me to merge %d trees?", stage-1);
374 merge_cache(active_cache, active_nr, merge_function[stage-1]);
375 }
376 if (write_cache(newfd, active_cache, active_nr) ||
377 commit_index_file(&cache_file))
378 die("unable to write new index file");
379 return 0;
380}