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 intunpack_tree(unsigned char*sha1) 12{ 13void*buffer; 14unsigned long size; 15int ret; 16 17 buffer =read_object_with_reference(sha1,"tree", &size, NULL); 18if(!buffer) 19return-1; 20 ret =read_tree(buffer, size, stage); 21free(buffer); 22return ret; 23} 24 25static intpath_matches(struct cache_entry *a,struct cache_entry *b) 26{ 27int len =ce_namelen(a); 28returnce_namelen(b) == len && 29!memcmp(a->name, b->name, len); 30} 31 32static intsame(struct cache_entry *a,struct cache_entry *b) 33{ 34return 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, 44struct cache_entry *b, 45struct 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 */ 64if(a && b && c) { 65if(same(b,c)) 66return c; 67if(same(a,b)) 68return c; 69if(same(a,c)) 70return b; 71} 72return 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 voidverify_uptodate(struct cache_entry *ce) 80{ 81struct stat st; 82 83if(!lstat(ce->name, &st)) { 84unsigned changed =ce_match_stat(ce, &st); 85if(!changed) 86return; 87 errno =0; 88} 89if(errno == ENOENT) 90return; 91die("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 voidreject_merge(struct cache_entry *ce) 101{ 102die("Entry '%s' would be overwritten by merge. Cannot merge.", ce->name); 103} 104 105static intmerged_entry_internal(struct cache_entry *merge,struct cache_entry *old,struct cache_entry **dst,int allow_dirty) 106{ 107 merge->ce_flags |=htons(CE_UPDATE); 108if(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 */ 116if(same(old, merge)) { 117*merge = *old; 118}else if(!allow_dirty) { 119verify_uptodate(old); 120} 121} 122 merge->ce_flags &= ~htons(CE_STAGEMASK); 123*dst++ = merge; 124return1; 125} 126 127static intmerged_entry_allow_dirty(struct cache_entry *merge,struct cache_entry *old,struct cache_entry **dst) 128{ 129returnmerged_entry_internal(merge, old, dst,1); 130} 131 132static intmerged_entry(struct cache_entry *merge,struct cache_entry *old,struct cache_entry **dst) 133{ 134returnmerged_entry_internal(merge, old, dst,0); 135} 136 137static intdeleted_entry(struct cache_entry *ce,struct cache_entry *old,struct cache_entry **dst) 138{ 139if(old) 140verify_uptodate(old); 141 ce->ce_mode =0; 142*dst++ = ce; 143return1; 144} 145 146static intcauses_df_conflict(struct cache_entry *ce,int stage, 147struct cache_entry **dst_, 148struct cache_entry **next_, 149int tail) 150{ 151/* This is called during the merge operation and walking 152 * the active_cache[] array is messy, because it is in the 153 * middle of overlapping copy operation. The invariants 154 * are: 155 * (1) active_cache points at the first (zeroth) entry. 156 * (2) up to dst pointer are resolved entries. 157 * (3) from the next pointer (head-inclusive) to the tail 158 * of the active_cache array have the remaining paths 159 * to be processed. There can be a gap between dst 160 * and next. Note that next is called "src" in the 161 * merge_cache() function, and tail is the original 162 * end of active_cache array when merge_cache() started. 163 * (4) the path corresponding to *ce is not found in (2) 164 * or (3). It is in the gap. 165 * 166 * active_cache -----......+++++++++++++. 167 * ^dst ^next ^tail 168 */ 169int i, next, dst; 170const char*path = ce->name; 171int namelen =ce_namelen(ce); 172 173 next = next_ - active_cache; 174 dst = dst_ - active_cache; 175 176for(i =0; i < tail; i++) { 177int entlen, len; 178const char*one, *two; 179if(dst <= i && i < next) 180continue; 181 ce = active_cache[i]; 182if(ce_stage(ce) != stage) 183continue; 184/* If ce->name is a prefix of path, then path is a file 185 * that hangs underneath ce->name, which is bad. 186 * If path is a prefix of ce->name, then it is the 187 * other way around which also is bad. 188 */ 189 entlen =ce_namelen(ce); 190if(namelen == entlen) 191continue; 192if(namelen < entlen) { 193 len = namelen; 194 one = path; 195 two = ce->name; 196}else{ 197 len = entlen; 198 one = ce->name; 199 two = path; 200} 201if(memcmp(one, two, len)) 202continue; 203if(two[len] =='/') 204return1; 205} 206return0; 207} 208 209static intthreeway_merge(struct cache_entry *stages[4], 210struct cache_entry **dst, 211struct cache_entry **next,int tail) 212{ 213struct cache_entry *old = stages[0]; 214struct cache_entry *a = stages[1], *b = stages[2], *c = stages[3]; 215struct cache_entry *merge; 216int count; 217 218/* #5ALT */ 219if(!a && b && c &&same(b, c)) { 220if(old && !same(b, old)) 221return-1; 222returnmerged_entry_allow_dirty(b, old, dst); 223} 224/* #2ALT and #3ALT */ 225if(!a && (!!b != !!c)) { 226/* 227 * The reason we need to worry about directory/file 228 * conflicts only in #2ALT and #3ALT case is this: 229 * 230 * (1) For all other cases that read-tree internally 231 * resolves a path, we always have such a path in 232 * *both* stage2 and stage3 when we begin. 233 * Traditionally, the behaviour has been even 234 * stricter and we did not resolve a path without 235 * initially being in all of stage1, 2, and 3. 236 * 237 * (2) When read-tree finishes, all resolved paths (i.e. 238 * the paths that are in stage0) must have come from 239 * either stage2 or stage3. It is not possible to 240 * have a stage0 path as a result of a merge if 241 * neither stage2 nor stage3 had that path. 242 * 243 * (3) It is guaranteed that just after reading the 244 * stages, each stage cannot have directory/file 245 * conflicts on its own, because they are populated 246 * by reading hierarchy of a tree. Combined with 247 * (1) and (2) above, this means that no matter what 248 * combination of paths we take from stage2 and 249 * stage3 as a result of a merge, they cannot cause 250 * a directory/file conflict situation (otherwise 251 * the "guilty" path would have already had such a 252 * conflict in the original stage, either stage2 253 * or stage3). Although its stage2 is synthesized 254 * by overlaying the current index on top of "our 255 * head" tree, --emu23 case also has this guarantee, 256 * by calling add_cache_entry() to create such stage2 257 * entries. 258 * 259 * (4) Only #2ALT and #3ALT lack the guarantee (1). 260 * They resolve paths that exist only in stage2 261 * or stage3. The stage2 tree may have a file DF 262 * while stage3 tree may have a file DF/DF. If 263 * #2ALT and #3ALT rules happen to apply to both 264 * of them, we would end up having DF (coming from 265 * stage2) and DF/DF (from stage3) in the result. 266 * When we attempt to resolve a path that exists 267 * only in stage2, we need to make sure there is 268 * no path that would conflict with it in stage3 269 * and vice versa. 270 */ 271if(c) {/* #2ALT */ 272if(!causes_df_conflict(c,2, dst, next, tail) && 273(!old ||same(c, old))) 274returnmerged_entry_allow_dirty(c, old, dst); 275} 276else{/* #3ALT */ 277if(!causes_df_conflict(b,3, dst, next, tail) && 278(!old ||same(b, old))) 279returnmerged_entry_allow_dirty(b, old, dst); 280} 281/* otherwise we will apply the original rule */ 282} 283/* 284 * If we have an entry in the index cache ("old"), then we want 285 * to make sure that it matches any entries in stage 2 ("first 286 * branch", aka "b"). 287 */ 288if(old) { 289if(!b || !same(old, b)) 290return-1; 291} 292 merge =merge_entries(a, b, c); 293if(merge) 294returnmerged_entry(merge, old, dst); 295if(old) 296verify_uptodate(old); 297 count =0; 298if(a) { *dst++ = a; count++; } 299if(b) { *dst++ = b; count++; } 300if(c) { *dst++ = c; count++; } 301return count; 302} 303 304/* 305 * Two-way merge. 306 * 307 * The rule is to "carry forward" what is in the index without losing 308 * information across a "fast forward", favoring a successful merge 309 * over a merge failure when it makes sense. For details of the 310 * "carry forward" rule, please see <Documentation/git-read-tree.txt>. 311 * 312 */ 313static inttwoway_merge(struct cache_entry **src,struct cache_entry **dst, 314struct cache_entry **next,int tail) 315{ 316struct cache_entry *current = src[0]; 317struct cache_entry *oldtree = src[1], *newtree = src[2]; 318 319if(src[3]) 320return-1; 321 322if(current) { 323if((!oldtree && !newtree) ||/* 4 and 5 */ 324(!oldtree && newtree && 325same(current, newtree)) ||/* 6 and 7 */ 326(oldtree && newtree && 327same(oldtree, newtree)) ||/* 14 and 15 */ 328(oldtree && newtree && 329!same(oldtree, newtree) &&/* 18 and 19*/ 330same(current, newtree))) { 331*dst++ = current; 332return1; 333} 334else if(oldtree && !newtree &&same(current, oldtree)) { 335/* 10 or 11 */ 336returndeleted_entry(oldtree, current, dst); 337} 338else if(oldtree && newtree && 339same(current, oldtree) && !same(current, newtree)) { 340/* 20 or 21 */ 341returnmerged_entry(newtree, current, dst); 342} 343else 344/* all other failures */ 345return-1; 346} 347else if(newtree) 348returnmerged_entry(newtree, current, dst); 349else 350returndeleted_entry(oldtree, current, dst); 351} 352 353/* 354 * Two-way merge emulated with three-way merge. 355 * 356 * This treats "read-tree -m H M" by transforming it internally 357 * into "read-tree -m H I+H M", where I+H is a tree that would 358 * contain the contents of the current index file, overlayed on 359 * top of H. Unlike the traditional two-way merge, this leaves 360 * the stages in the resulting index file and lets the user resolve 361 * the merge conflicts using standard tools for three-way merge. 362 * 363 * This function is just to set-up such an arrangement, and the 364 * actual merge uses threeway_merge() function. 365 */ 366static voidsetup_emu23(void) 367{ 368/* stage0 contains I, stage1 H, stage2 M. 369 * move stage2 to stage3, and create stage2 entries 370 * by scanning stage0 and stage1 entries. 371 */ 372int i, namelen, size; 373struct cache_entry *ce, *stage2; 374 375for(i =0; i < active_nr; i++) { 376 ce = active_cache[i]; 377if(ce_stage(ce) !=2) 378continue; 379/* hoist them up to stage 3 */ 380 namelen =ce_namelen(ce); 381 ce->ce_flags =create_ce_flags(namelen,3); 382} 383 384for(i =0; i < active_nr; i++) { 385 ce = active_cache[i]; 386if(ce_stage(ce) >1) 387continue; 388 namelen =ce_namelen(ce); 389 size =cache_entry_size(namelen); 390 stage2 =xmalloc(size); 391memcpy(stage2, ce, size); 392 stage2->ce_flags =create_ce_flags(namelen,2); 393if(add_cache_entry(stage2, ADD_CACHE_OK_TO_ADD) <0) 394die("cannot merge index and our head tree"); 395 396/* We are done with this name, so skip to next name */ 397while(i < active_nr && 398ce_namelen(active_cache[i]) == namelen && 399!memcmp(active_cache[i]->name, ce->name, namelen)) 400 i++; 401 i--;/* compensate for the loop control */ 402} 403} 404 405/* 406 * One-way merge. 407 * 408 * The rule is: 409 * - take the stat information from stage0, take the data from stage1 410 */ 411static intoneway_merge(struct cache_entry **src,struct cache_entry **dst, 412struct cache_entry **next,int tail) 413{ 414struct cache_entry *old = src[0]; 415struct cache_entry *a = src[1]; 416 417if(src[2] || src[3]) 418return-1; 419 420if(!a) 421return0; 422if(old &&same(old, a)) { 423*dst++ = old; 424return1; 425} 426returnmerged_entry(a, NULL, dst); 427} 428 429static voidcheck_updates(struct cache_entry **src,int nr) 430{ 431static struct checkout state = { 432.base_dir ="", 433.force =1, 434.quiet =1, 435.refresh_cache =1, 436}; 437unsigned short mask =htons(CE_UPDATE); 438while(nr--) { 439struct cache_entry *ce = *src++; 440if(!ce->ce_mode) { 441if(update) 442unlink(ce->name); 443continue; 444} 445if(ce->ce_flags & mask) { 446 ce->ce_flags &= ~mask; 447if(update) 448checkout_entry(ce, &state); 449} 450} 451} 452 453typedefint(*merge_fn_t)(struct cache_entry **,struct cache_entry **,struct cache_entry **,int); 454 455static voidmerge_cache(struct cache_entry **src,int nr, merge_fn_t fn) 456{ 457struct cache_entry **dst = src; 458int tail = nr; 459 460while(nr) { 461int entries; 462struct cache_entry *name, *ce, *stages[4] = { NULL, }; 463 464 name = ce = *src; 465for(;;) { 466int stage =ce_stage(ce); 467 stages[stage] = ce; 468 ce = *++src; 469 active_nr--; 470if(!--nr) 471break; 472if(!path_matches(ce, name)) 473break; 474} 475 476 entries =fn(stages, dst, src, tail); 477if(entries <0) 478reject_merge(name); 479 dst += entries; 480 active_nr += entries; 481} 482check_updates(active_cache, active_nr); 483} 484 485static intread_cache_unmerged(void) 486{ 487int i, deleted; 488struct cache_entry **dst; 489 490read_cache(); 491 dst = active_cache; 492 deleted =0; 493for(i =0; i < active_nr; i++) { 494struct cache_entry *ce = active_cache[i]; 495if(ce_stage(ce)) { 496 deleted++; 497continue; 498} 499if(deleted) 500*dst = ce; 501 dst++; 502} 503 active_nr -= deleted; 504return deleted; 505} 506 507static char*read_tree_usage ="git-read-tree (<sha> | -m [-u] <sha1> [<sha2> [<sha3>]])"; 508 509static struct cache_file cache_file; 510 511intmain(int argc,char**argv) 512{ 513int i, newfd, merge, reset, emu23; 514unsigned char sha1[20]; 515 516 newfd =hold_index_file_for_update(&cache_file,get_index_file()); 517if(newfd <0) 518die("unable to create new cachefile"); 519 520 merge =0; 521 reset =0; 522 emu23 =0; 523for(i =1; i < argc; i++) { 524const char*arg = argv[i]; 525 526/* "-u" means "update", meaning that a merge will update the working directory */ 527if(!strcmp(arg,"-u")) { 528 update =1; 529continue; 530} 531 532/* This differs from "-m" in that we'll silently ignore unmerged entries */ 533if(!strcmp(arg,"--reset")) { 534if(stage || merge || emu23) 535usage(read_tree_usage); 536 reset =1; 537 merge =1; 538 stage =1; 539read_cache_unmerged(); 540} 541 542/* "-m" stands for "merge", meaning we start in stage 1 */ 543if(!strcmp(arg,"-m")) { 544if(stage || merge || emu23) 545usage(read_tree_usage); 546if(read_cache_unmerged()) 547die("you need to resolve your current index first"); 548 stage =1; 549 merge =1; 550continue; 551} 552 553/* "-emu23" uses 3-way merge logic to perform fast-forward */ 554if(!strcmp(arg,"--emu23")) { 555if(stage || merge || emu23) 556usage(read_tree_usage); 557if(read_cache_unmerged()) 558die("you need to resolve your current index first"); 559 merge = emu23 = stage =1; 560continue; 561} 562 563if(get_sha1(arg, sha1) <0) 564usage(read_tree_usage); 565if(stage >3) 566usage(read_tree_usage); 567if(unpack_tree(sha1) <0) 568die("failed to unpack tree object%s", arg); 569 stage++; 570} 571if(update && !merge) 572usage(read_tree_usage); 573if(merge) { 574static const merge_fn_t merge_function[] = { 575[1] = oneway_merge, 576[2] = twoway_merge, 577[3] = threeway_merge, 578}; 579 merge_fn_t fn; 580 581if(stage <2|| stage >4) 582die("just how do you expect me to merge%dtrees?", stage-1); 583if(emu23 && stage !=3) 584die("--emu23 takes only two trees"); 585 fn = merge_function[stage-1]; 586if(stage ==3&& emu23) { 587setup_emu23(); 588 fn = merge_function[3]; 589} 590merge_cache(active_cache, active_nr, fn); 591} 592if(write_cache(newfd, active_cache, active_nr) || 593commit_index_file(&cache_file)) 594die("unable to write new index file"); 595return0; 596}