]>
Commit | Line | Data |
---|---|---|
8bc9a0c7 LT |
1 | /* |
2 | * GIT - The information manager from hell | |
3 | * | |
4 | * Copyright (C) Linus Torvalds, 2005 | |
5 | */ | |
4d3fe0c5 JH |
6 | #define DBRT_DEBUG 1 |
7 | ||
e83c5163 LT |
8 | #include "cache.h" |
9 | ||
ee6566e8 DB |
10 | #include "object.h" |
11 | #include "tree.h" | |
12 | ||
13 | static int merge = 0; | |
220a0b52 | 14 | static int update = 0; |
d99082e0 | 15 | |
ee6566e8 DB |
16 | static int head_idx = -1; |
17 | static int merge_size = 0; | |
b12ec373 | 18 | |
ee6566e8 DB |
19 | static struct object_list *trees = NULL; |
20 | ||
21 | static struct cache_entry df_conflict_entry = { | |
22 | }; | |
23 | ||
24 | static struct tree_entry_list df_conflict_list = { | |
25 | .name = NULL, | |
26 | .next = &df_conflict_list | |
27 | }; | |
28 | ||
29 | typedef int (*merge_fn_t)(struct cache_entry **src); | |
30 | ||
31 | static int entcmp(char *name1, int dir1, char *name2, int dir2) | |
32 | { | |
33 | int len1 = strlen(name1); | |
34 | int len2 = strlen(name2); | |
35 | int len = len1 < len2 ? len1 : len2; | |
36 | int ret = memcmp(name1, name2, len); | |
37 | unsigned char c1, c2; | |
38 | if (ret) | |
39 | return ret; | |
40 | c1 = name1[len]; | |
41 | c2 = name2[len]; | |
42 | if (!c1 && dir1) | |
43 | c1 = '/'; | |
44 | if (!c2 && dir2) | |
45 | c2 = '/'; | |
46 | ret = (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0; | |
47 | if (c1 && c2 && !ret) | |
48 | ret = len1 - len2; | |
14242464 | 49 | return ret; |
b12ec373 JH |
50 | } |
51 | ||
ee6566e8 DB |
52 | static int unpack_trees_rec(struct tree_entry_list **posns, int len, |
53 | const char *base, merge_fn_t fn, int *indpos) | |
ca016f0e | 54 | { |
ee6566e8 DB |
55 | int baselen = strlen(base); |
56 | int src_size = len + 1; | |
57 | do { | |
58 | int i; | |
59 | char *first; | |
60 | int firstdir = 0; | |
61 | int pathlen; | |
62 | unsigned ce_size; | |
63 | struct tree_entry_list **subposns; | |
64 | struct cache_entry **src; | |
65 | int any_files = 0; | |
66 | int any_dirs = 0; | |
67 | char *cache_name; | |
68 | int ce_stage; | |
69 | ||
70 | /* Find the first name in the input. */ | |
71 | ||
72 | first = NULL; | |
73 | cache_name = NULL; | |
74 | ||
75 | /* Check the cache */ | |
76 | if (merge && *indpos < active_nr) { | |
77 | /* This is a bit tricky: */ | |
78 | /* If the index has a subdirectory (with | |
79 | * contents) as the first name, it'll get a | |
80 | * filename like "foo/bar". But that's after | |
81 | * "foo", so the entry in trees will get | |
82 | * handled first, at which point we'll go into | |
83 | * "foo", and deal with "bar" from the index, | |
84 | * because the base will be "foo/". The only | |
85 | * way we can actually have "foo/bar" first of | |
86 | * all the things is if the trees don't | |
87 | * contain "foo" at all, in which case we'll | |
88 | * handle "foo/bar" without going into the | |
89 | * directory, but that's fine (and will return | |
90 | * an error anyway, with the added unknown | |
91 | * file case. | |
92 | */ | |
93 | ||
94 | cache_name = active_cache[*indpos]->name; | |
95 | if (strlen(cache_name) > baselen && | |
96 | !memcmp(cache_name, base, baselen)) { | |
97 | cache_name += baselen; | |
98 | first = cache_name; | |
99 | } else { | |
100 | cache_name = NULL; | |
101 | } | |
102 | } | |
103 | ||
4d3fe0c5 | 104 | #if DBRT_DEBUG > 1 |
ee6566e8 DB |
105 | if (first) |
106 | printf("index %s\n", first); | |
2ab706a3 | 107 | #endif |
ee6566e8 DB |
108 | for (i = 0; i < len; i++) { |
109 | if (!posns[i] || posns[i] == &df_conflict_list) | |
110 | continue; | |
4d3fe0c5 | 111 | #if DBRT_DEBUG > 1 |
ee6566e8 | 112 | printf("%d %s\n", i + 1, posns[i]->name); |
2ab706a3 | 113 | #endif |
ee6566e8 DB |
114 | if (!first || entcmp(first, firstdir, |
115 | posns[i]->name, | |
116 | posns[i]->directory) > 0) { | |
117 | first = posns[i]->name; | |
118 | firstdir = posns[i]->directory; | |
119 | } | |
120 | } | |
121 | /* No name means we're done */ | |
122 | if (!first) | |
123 | return 0; | |
124 | ||
125 | pathlen = strlen(first); | |
126 | ce_size = cache_entry_size(baselen + pathlen); | |
127 | ||
128 | src = xmalloc(sizeof(struct cache_entry *) * src_size); | |
129 | memset(src, 0, sizeof(struct cache_entry *) * src_size); | |
130 | ||
131 | subposns = xmalloc(sizeof(struct tree_list_entry *) * len); | |
132 | memset(subposns, 0, sizeof(struct tree_list_entry *) * len); | |
133 | ||
134 | if (cache_name && !strcmp(cache_name, first)) { | |
135 | any_files = 1; | |
136 | src[0] = active_cache[*indpos]; | |
137 | remove_cache_entry_at(*indpos); | |
138 | } | |
139 | ||
140 | for (i = 0; i < len; i++) { | |
141 | struct cache_entry *ce; | |
142 | ||
143 | if (!posns[i] || | |
144 | (posns[i] != &df_conflict_list && | |
145 | strcmp(first, posns[i]->name))) { | |
146 | continue; | |
147 | } | |
148 | ||
149 | if (posns[i] == &df_conflict_list) { | |
150 | src[i + merge] = &df_conflict_entry; | |
151 | continue; | |
152 | } | |
153 | ||
154 | if (posns[i]->directory) { | |
155 | any_dirs = 1; | |
156 | parse_tree(posns[i]->item.tree); | |
157 | subposns[i] = posns[i]->item.tree->entries; | |
158 | posns[i] = posns[i]->next; | |
159 | src[i + merge] = &df_conflict_entry; | |
160 | continue; | |
161 | } | |
162 | ||
163 | if (!merge) | |
164 | ce_stage = 0; | |
165 | else if (i + 1 < head_idx) | |
166 | ce_stage = 1; | |
167 | else if (i + 1 > head_idx) | |
168 | ce_stage = 3; | |
169 | else | |
170 | ce_stage = 2; | |
171 | ||
172 | ce = xmalloc(ce_size); | |
173 | memset(ce, 0, ce_size); | |
174 | ce->ce_mode = create_ce_mode(posns[i]->mode); | |
175 | ce->ce_flags = create_ce_flags(baselen + pathlen, | |
176 | ce_stage); | |
177 | memcpy(ce->name, base, baselen); | |
178 | memcpy(ce->name + baselen, first, pathlen + 1); | |
179 | ||
180 | any_files = 1; | |
181 | ||
182 | memcpy(ce->sha1, posns[i]->item.any->sha1, 20); | |
183 | src[i + merge] = ce; | |
184 | subposns[i] = &df_conflict_list; | |
185 | posns[i] = posns[i]->next; | |
186 | } | |
187 | if (any_files) { | |
188 | if (merge) { | |
189 | int ret; | |
190 | ||
4d3fe0c5 | 191 | #if DBRT_DEBUG > 1 |
ee6566e8 DB |
192 | printf("%s:\n", first); |
193 | for (i = 0; i < src_size; i++) { | |
194 | printf(" %d ", i); | |
195 | if (src[i]) | |
196 | printf("%s\n", sha1_to_hex(src[i]->sha1)); | |
197 | else | |
198 | printf("\n"); | |
199 | } | |
2ab706a3 | 200 | #endif |
ee6566e8 DB |
201 | ret = fn(src); |
202 | ||
4d3fe0c5 | 203 | #if DBRT_DEBUG > 1 |
ee6566e8 | 204 | printf("Added %d entries\n", ret); |
2ab706a3 | 205 | #endif |
ee6566e8 DB |
206 | *indpos += ret; |
207 | } else { | |
208 | for (i = 0; i < src_size; i++) { | |
209 | if (src[i]) { | |
210 | add_cache_entry(src[i], ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK); | |
211 | } | |
212 | } | |
213 | } | |
214 | } | |
215 | if (any_dirs) { | |
216 | char *newbase = xmalloc(baselen + 2 + pathlen); | |
217 | memcpy(newbase, base, baselen); | |
218 | memcpy(newbase + baselen, first, pathlen); | |
219 | newbase[baselen + pathlen] = '/'; | |
220 | newbase[baselen + pathlen + 1] = '\0'; | |
221 | if (unpack_trees_rec(subposns, len, newbase, fn, | |
222 | indpos)) | |
223 | return -1; | |
bb97a2a8 | 224 | free(newbase); |
ee6566e8 DB |
225 | } |
226 | free(subposns); | |
227 | free(src); | |
228 | } while (1); | |
ca016f0e LT |
229 | } |
230 | ||
ee6566e8 | 231 | static void reject_merge(struct cache_entry *ce) |
43f91266 | 232 | { |
ee6566e8 DB |
233 | die("Entry '%s' would be overwritten by merge. Cannot merge.", |
234 | ce->name); | |
43f91266 LT |
235 | } |
236 | ||
ee6566e8 DB |
237 | static void check_updates(struct cache_entry **src, int nr) |
238 | { | |
239 | static struct checkout state = { | |
240 | .base_dir = "", | |
241 | .force = 1, | |
242 | .quiet = 1, | |
243 | .refresh_cache = 1, | |
244 | }; | |
245 | unsigned short mask = htons(CE_UPDATE); | |
246 | while (nr--) { | |
247 | struct cache_entry *ce = *src++; | |
248 | if (!ce->ce_mode) { | |
249 | if (update) | |
250 | unlink(ce->name); | |
251 | continue; | |
252 | } | |
253 | if (ce->ce_flags & mask) { | |
254 | ce->ce_flags &= ~mask; | |
255 | if (update) | |
256 | checkout_entry(ce, &state); | |
257 | } | |
258 | } | |
259 | } | |
43f91266 | 260 | |
ee6566e8 | 261 | static int unpack_trees(merge_fn_t fn) |
d99082e0 | 262 | { |
ee6566e8 DB |
263 | int indpos = 0; |
264 | unsigned len = object_list_length(trees); | |
265 | struct tree_entry_list **posns = | |
266 | xmalloc(len * sizeof(struct tree_entry_list *)); | |
267 | int i; | |
268 | struct object_list *posn = trees; | |
269 | merge_size = len; | |
270 | for (i = 0; i < len; i++) { | |
271 | posns[i] = ((struct tree *) posn->item)->entries; | |
272 | posn = posn->next; | |
d723c690 | 273 | } |
ee6566e8 DB |
274 | if (unpack_trees_rec(posns, len, "", fn, &indpos)) |
275 | return -1; | |
276 | ||
277 | check_updates(active_cache, active_nr); | |
278 | return 0; | |
d99082e0 LT |
279 | } |
280 | ||
ee6566e8 DB |
281 | static int list_tree(unsigned char *sha1) |
282 | { | |
283 | struct tree *tree = parse_tree_indirect(sha1); | |
284 | if (!tree) | |
285 | return -1; | |
286 | object_list_append(&tree->object, &trees); | |
287 | return 0; | |
288 | } | |
289 | ||
290 | static int same(struct cache_entry *a, struct cache_entry *b) | |
291 | { | |
292 | if (!!a != !!b) | |
293 | return 0; | |
294 | if (!a && !b) | |
295 | return 1; | |
296 | return a->ce_mode == b->ce_mode && | |
297 | !memcmp(a->sha1, b->sha1, 20); | |
298 | } | |
299 | ||
300 | ||
02ede67a LT |
301 | /* |
302 | * When a CE gets turned into an unmerged entry, we | |
303 | * want it to be up-to-date | |
304 | */ | |
305 | static void verify_uptodate(struct cache_entry *ce) | |
306 | { | |
307 | struct stat st; | |
308 | ||
309 | if (!lstat(ce->name, &st)) { | |
310 | unsigned changed = ce_match_stat(ce, &st); | |
311 | if (!changed) | |
312 | return; | |
313 | errno = 0; | |
314 | } | |
315 | if (errno == ENOENT) | |
316 | return; | |
317 | die("Entry '%s' not uptodate. Cannot merge.", ce->name); | |
318 | } | |
319 | ||
ee6566e8 | 320 | static int merged_entry(struct cache_entry *merge, struct cache_entry *old) |
d99082e0 | 321 | { |
d723c690 LT |
322 | merge->ce_flags |= htons(CE_UPDATE); |
323 | if (old) { | |
02ede67a | 324 | /* |
d723c690 LT |
325 | * See if we can re-use the old CE directly? |
326 | * That way we get the uptodate stat info. | |
327 | * | |
328 | * This also removes the UPDATE flag on | |
329 | * a match. | |
02ede67a | 330 | */ |
d723c690 LT |
331 | if (same(old, merge)) { |
332 | *merge = *old; | |
ee6566e8 | 333 | } else { |
d723c690 LT |
334 | verify_uptodate(old); |
335 | } | |
a3a65234 | 336 | } |
d723c690 | 337 | merge->ce_flags &= ~htons(CE_STAGEMASK); |
ee6566e8 | 338 | add_cache_entry(merge, ADD_CACHE_OK_TO_ADD); |
d723c690 | 339 | return 1; |
a3a65234 LT |
340 | } |
341 | ||
ee6566e8 | 342 | static int deleted_entry(struct cache_entry *ce, struct cache_entry *old) |
aa16021e LT |
343 | { |
344 | if (old) | |
345 | verify_uptodate(old); | |
346 | ce->ce_mode = 0; | |
ee6566e8 | 347 | add_cache_entry(ce, ADD_CACHE_OK_TO_ADD); |
aa16021e LT |
348 | return 1; |
349 | } | |
350 | ||
ee6566e8 | 351 | static int keep_entry(struct cache_entry *ce) |
32192e66 | 352 | { |
ee6566e8 DB |
353 | add_cache_entry(ce, ADD_CACHE_OK_TO_ADD); |
354 | return 1; | |
32192e66 JH |
355 | } |
356 | ||
4d3fe0c5 JH |
357 | #if DBRT_DEBUG |
358 | static void show_stage_entry(FILE *o, | |
359 | const char *label, const struct cache_entry *ce) | |
360 | { | |
361 | fprintf(stderr, "%s%06o %s %d\t%s\n", | |
362 | label, | |
363 | ntohl(ce->ce_mode), | |
364 | sha1_to_hex(ce->sha1), | |
365 | ce_stage(ce), | |
366 | ce->name); | |
367 | } | |
368 | #endif | |
369 | ||
ee6566e8 | 370 | static int threeway_merge(struct cache_entry **stages) |
e6ee623b | 371 | { |
ee6566e8 DB |
372 | struct cache_entry *index; |
373 | struct cache_entry *head; | |
374 | struct cache_entry *remote = stages[head_idx + 1]; | |
d723c690 | 375 | int count; |
ee6566e8 DB |
376 | int head_match = 0; |
377 | int remote_match = 0; | |
d723c690 | 378 | |
ee6566e8 DB |
379 | int df_conflict_head = 0; |
380 | int df_conflict_remote = 0; | |
381 | ||
382 | int any_anc_missing = 0; | |
383 | int i; | |
384 | ||
385 | for (i = 1; i < head_idx; i++) { | |
386 | if (!stages[i]) | |
387 | any_anc_missing = 1; | |
e7f9bc41 | 388 | } |
ee6566e8 DB |
389 | |
390 | index = stages[0]; | |
391 | head = stages[head_idx]; | |
392 | ||
393 | if (head == &df_conflict_entry) { | |
394 | df_conflict_head = 1; | |
395 | head = NULL; | |
396 | } | |
397 | ||
398 | if (remote == &df_conflict_entry) { | |
399 | df_conflict_remote = 1; | |
400 | remote = NULL; | |
401 | } | |
402 | ||
403 | /* First, if there's a #16 situation, note that to prevent #13 | |
404 | * and #14. | |
405 | */ | |
406 | if (!same(remote, head)) { | |
407 | for (i = 1; i < head_idx; i++) { | |
408 | if (same(stages[i], head)) { | |
4d3fe0c5 | 409 | head_match = i; |
ee6566e8 DB |
410 | } |
411 | if (same(stages[i], remote)) { | |
4d3fe0c5 | 412 | remote_match = i; |
ee6566e8 | 413 | } |
32192e66 | 414 | } |
32192e66 | 415 | } |
ee6566e8 DB |
416 | |
417 | /* We start with cases where the index is allowed to match | |
418 | * something other than the head: #14(ALT) and #2ALT, where it | |
419 | * is permitted to match the result instead. | |
420 | */ | |
421 | /* #14, #14ALT, #2ALT */ | |
422 | if (remote && !df_conflict_head && head_match && !remote_match) { | |
423 | if (index && !same(index, remote) && !same(index, head)) | |
424 | reject_merge(index); | |
425 | return merged_entry(remote, index); | |
036d51cc | 426 | } |
d723c690 | 427 | /* |
ee6566e8 DB |
428 | * If we have an entry in the index cache, then we want to |
429 | * make sure that it matches head. | |
d723c690 | 430 | */ |
ee6566e8 DB |
431 | if (index && !same(index, head)) { |
432 | reject_merge(index); | |
e6ee623b | 433 | } |
ee6566e8 DB |
434 | |
435 | if (head) { | |
436 | /* #5ALT, #15 */ | |
437 | if (same(head, remote)) | |
438 | return merged_entry(head, index); | |
439 | /* #13, #3ALT */ | |
440 | if (!df_conflict_remote && remote_match && !head_match) | |
441 | return merged_entry(head, index); | |
442 | } | |
443 | ||
444 | /* #1 */ | |
445 | if (!head && !remote && any_anc_missing) | |
446 | return 0; | |
447 | ||
448 | /* Below are "no merge" cases, which require that the index be | |
449 | * up-to-date to avoid the files getting overwritten with | |
450 | * conflict resolution files. | |
451 | */ | |
452 | if (index) { | |
453 | verify_uptodate(index); | |
454 | } | |
455 | ||
456 | /* #2, #3, #4, #6, #7, #9, #11. */ | |
d723c690 | 457 | count = 0; |
ee6566e8 DB |
458 | if (!head_match || !remote_match) { |
459 | for (i = 1; i < head_idx; i++) { | |
460 | if (stages[i]) { | |
461 | keep_entry(stages[i]); | |
462 | count++; | |
463 | break; | |
464 | } | |
465 | } | |
466 | } | |
4d3fe0c5 JH |
467 | #if DBRT_DEBUG |
468 | else { | |
469 | fprintf(stderr, "read-tree: warning #16 detected\n"); | |
470 | show_stage_entry(stderr, "head ", stages[head_match]); | |
471 | show_stage_entry(stderr, "remote ", stages[remote_match]); | |
472 | } | |
473 | #endif | |
ee6566e8 DB |
474 | if (head) { count += keep_entry(head); } |
475 | if (remote) { count += keep_entry(remote); } | |
d723c690 | 476 | return count; |
e6ee623b LT |
477 | } |
478 | ||
220a0b52 LT |
479 | /* |
480 | * Two-way merge. | |
481 | * | |
c8596009 JH |
482 | * The rule is to "carry forward" what is in the index without losing |
483 | * information across a "fast forward", favoring a successful merge | |
484 | * over a merge failure when it makes sense. For details of the | |
485 | * "carry forward" rule, please see <Documentation/git-read-tree.txt>. | |
486 | * | |
220a0b52 | 487 | */ |
ee6566e8 | 488 | static int twoway_merge(struct cache_entry **src) |
a3a65234 | 489 | { |
c8596009 JH |
490 | struct cache_entry *current = src[0]; |
491 | struct cache_entry *oldtree = src[1], *newtree = src[2]; | |
220a0b52 | 492 | |
ee6566e8 DB |
493 | if (merge_size != 2) |
494 | return error("Cannot do a twoway merge of %d trees\n", | |
495 | merge_size); | |
e6ee623b | 496 | |
c8596009 JH |
497 | if (current) { |
498 | if ((!oldtree && !newtree) || /* 4 and 5 */ | |
499 | (!oldtree && newtree && | |
500 | same(current, newtree)) || /* 6 and 7 */ | |
501 | (oldtree && newtree && | |
502 | same(oldtree, newtree)) || /* 14 and 15 */ | |
503 | (oldtree && newtree && | |
504 | !same(oldtree, newtree) && /* 18 and 19*/ | |
505 | same(current, newtree))) { | |
ee6566e8 | 506 | return keep_entry(current); |
c8596009 JH |
507 | } |
508 | else if (oldtree && !newtree && same(current, oldtree)) { | |
509 | /* 10 or 11 */ | |
ee6566e8 | 510 | return deleted_entry(oldtree, current); |
c8596009 JH |
511 | } |
512 | else if (oldtree && newtree && | |
513 | same(current, oldtree) && !same(current, newtree)) { | |
514 | /* 20 or 21 */ | |
ee6566e8 | 515 | return merged_entry(newtree, current); |
c8596009 | 516 | } |
ee6566e8 | 517 | else { |
c8596009 | 518 | /* all other failures */ |
ee6566e8 DB |
519 | if (oldtree) |
520 | reject_merge(oldtree); | |
521 | if (current) | |
522 | reject_merge(current); | |
523 | if (newtree) | |
524 | reject_merge(newtree); | |
d723c690 | 525 | return -1; |
ee6566e8 | 526 | } |
e6ee623b | 527 | } |
c8596009 | 528 | else if (newtree) |
ee6566e8 | 529 | return merged_entry(newtree, current); |
c8596009 | 530 | else |
ee6566e8 | 531 | return deleted_entry(oldtree, current); |
03efa6d9 JH |
532 | } |
533 | ||
d723c690 LT |
534 | /* |
535 | * One-way merge. | |
536 | * | |
537 | * The rule is: | |
538 | * - take the stat information from stage0, take the data from stage1 | |
539 | */ | |
ee6566e8 | 540 | static int oneway_merge(struct cache_entry **src) |
220a0b52 | 541 | { |
d723c690 LT |
542 | struct cache_entry *old = src[0]; |
543 | struct cache_entry *a = src[1]; | |
a3a65234 | 544 | |
ee6566e8 DB |
545 | if (merge_size != 1) |
546 | return error("Cannot do a oneway merge of %d trees\n", | |
547 | merge_size); | |
a3a65234 | 548 | |
d723c690 LT |
549 | if (!a) |
550 | return 0; | |
b5b42507 | 551 | if (old && same(old, a)) { |
ee6566e8 | 552 | return keep_entry(old); |
b5b42507 | 553 | } |
ee6566e8 | 554 | return merged_entry(a, NULL); |
d723c690 LT |
555 | } |
556 | ||
438195cc LT |
557 | static int read_cache_unmerged(void) |
558 | { | |
559 | int i, deleted; | |
560 | struct cache_entry **dst; | |
561 | ||
562 | read_cache(); | |
563 | dst = active_cache; | |
564 | deleted = 0; | |
565 | for (i = 0; i < active_nr; i++) { | |
566 | struct cache_entry *ce = active_cache[i]; | |
567 | if (ce_stage(ce)) { | |
568 | deleted++; | |
569 | continue; | |
570 | } | |
571 | if (deleted) | |
572 | *dst = ce; | |
573 | dst++; | |
574 | } | |
575 | active_nr -= deleted; | |
576 | return deleted; | |
577 | } | |
578 | ||
4d1f1190 | 579 | static const char read_tree_usage[] = "git-read-tree (<sha> | -m [-u] <sha1> [<sha2> [<sha3>]])"; |
c5bac17a | 580 | |
96cd5429 JH |
581 | static struct cache_file cache_file; |
582 | ||
e83c5163 LT |
583 | int main(int argc, char **argv) |
584 | { | |
ee6566e8 | 585 | int i, newfd, reset, stage = 0; |
e83c5163 | 586 | unsigned char sha1[20]; |
ee6566e8 | 587 | merge_fn_t fn = NULL; |
bb233d69 | 588 | |
96cd5429 | 589 | newfd = hold_index_file_for_update(&cache_file, get_index_file()); |
83adac3c | 590 | if (newfd < 0) |
2de381f9 | 591 | die("unable to create new cachefile"); |
83adac3c | 592 | |
ca016f0e | 593 | merge = 0; |
438195cc | 594 | reset = 0; |
83adac3c LT |
595 | for (i = 1; i < argc; i++) { |
596 | const char *arg = argv[i]; | |
597 | ||
220a0b52 LT |
598 | /* "-u" means "update", meaning that a merge will update the working directory */ |
599 | if (!strcmp(arg, "-u")) { | |
600 | update = 1; | |
601 | continue; | |
602 | } | |
603 | ||
438195cc LT |
604 | /* This differs from "-m" in that we'll silently ignore unmerged entries */ |
605 | if (!strcmp(arg, "--reset")) { | |
ee6566e8 | 606 | if (stage || merge) |
438195cc LT |
607 | usage(read_tree_usage); |
608 | reset = 1; | |
609 | merge = 1; | |
610 | stage = 1; | |
611 | read_cache_unmerged(); | |
7875b50d | 612 | continue; |
438195cc LT |
613 | } |
614 | ||
ee6566e8 DB |
615 | if (!strcmp(arg, "--head")) { |
616 | head_idx = stage - 1; | |
617 | fn = threeway_merge; | |
618 | } | |
619 | ||
d99082e0 | 620 | /* "-m" stands for "merge", meaning we start in stage 1 */ |
83adac3c | 621 | if (!strcmp(arg, "-m")) { |
ee6566e8 | 622 | if (stage || merge) |
438195cc LT |
623 | usage(read_tree_usage); |
624 | if (read_cache_unmerged()) | |
625 | die("you need to resolve your current index first"); | |
d99082e0 | 626 | stage = 1; |
ca016f0e | 627 | merge = 1; |
83adac3c LT |
628 | continue; |
629 | } | |
03efa6d9 | 630 | |
3c249c95 | 631 | if (get_sha1(arg, sha1) < 0) |
c5bac17a | 632 | usage(read_tree_usage); |
ee6566e8 | 633 | if (list_tree(sha1) < 0) |
2de381f9 | 634 | die("failed to unpack tree object %s", arg); |
d99082e0 | 635 | stage++; |
83adac3c | 636 | } |
a57f0b58 LT |
637 | if (update && !merge) |
638 | usage(read_tree_usage); | |
ee6566e8 DB |
639 | if (merge && !fn) { |
640 | if (stage < 2) | |
a3a65234 | 641 | die("just how do you expect me to merge %d trees?", stage-1); |
ee6566e8 DB |
642 | switch (stage - 1) { |
643 | case 1: | |
644 | fn = oneway_merge; | |
645 | break; | |
646 | case 2: | |
647 | fn = twoway_merge; | |
648 | break; | |
649 | case 3: | |
650 | fn = threeway_merge; | |
651 | break; | |
652 | default: | |
653 | fn = threeway_merge; | |
654 | break; | |
03efa6d9 | 655 | } |
ca016f0e | 656 | } |
ee6566e8 DB |
657 | |
658 | if (head_idx < 0) { | |
659 | if (stage - 1 >= 3) | |
660 | head_idx = stage - 2; | |
661 | else | |
662 | head_idx = 1; | |
663 | } | |
664 | ||
665 | unpack_trees(fn); | |
96cd5429 JH |
666 | if (write_cache(newfd, active_cache, active_nr) || |
667 | commit_index_file(&cache_file)) | |
2de381f9 | 668 | die("unable to write new index file"); |
9614b8dc | 669 | return 0; |
e83c5163 | 670 | } |