1#include"cache.h" 2#include"lockfile.h" 3#include"string-list.h" 4#include"rerere.h" 5#include"xdiff-interface.h" 6#include"dir.h" 7#include"resolve-undo.h" 8#include"ll-merge.h" 9#include"attr.h" 10#include"pathspec.h" 11 12#define RESOLVED 0 13#define PUNTED 1 14#define THREE_STAGED 2 15void*RERERE_RESOLVED = &RERERE_RESOLVED; 16 17/* if rerere_enabled == -1, fall back to detection of .git/rr-cache */ 18static int rerere_enabled = -1; 19 20/* automatically update cleanly resolved paths to the index */ 21static int rerere_autoupdate; 22 23static voidfree_rerere_id(struct string_list_item *item) 24{ 25free(item->util); 26} 27 28static const char*rerere_id_hex(const struct rerere_id *id) 29{ 30return id->hex; 31} 32 33const char*rerere_path(const struct rerere_id *id,const char*file) 34{ 35if(!file) 36returngit_path("rr-cache/%s",rerere_id_hex(id)); 37 38returngit_path("rr-cache/%s/%s",rerere_id_hex(id), file); 39} 40 41static inthas_rerere_resolution(const struct rerere_id *id) 42{ 43struct stat st; 44 45return!stat(rerere_path(id,"postimage"), &st); 46} 47 48static struct rerere_id *new_rerere_id_hex(char*hex) 49{ 50struct rerere_id *id =xmalloc(sizeof(*id)); 51xsnprintf(id->hex,sizeof(id->hex),"%s", hex); 52return id; 53} 54 55static struct rerere_id *new_rerere_id(unsigned char*sha1) 56{ 57returnnew_rerere_id_hex(sha1_to_hex(sha1)); 58} 59 60/* 61 * $GIT_DIR/MERGE_RR file is a collection of records, each of which is 62 * "conflict ID", a HT and pathname, terminated with a NUL, and is 63 * used to keep track of the set of paths that "rerere" may need to 64 * work on (i.e. what is left by the previous invocation of "git 65 * rerere" during the current conflict resolution session). 66 */ 67static voidread_rr(struct string_list *rr) 68{ 69struct strbuf buf = STRBUF_INIT; 70FILE*in =fopen(git_path_merge_rr(),"r"); 71 72if(!in) 73return; 74while(!strbuf_getwholeline(&buf, in,'\0')) { 75char*path; 76unsigned char sha1[20]; 77struct rerere_id *id; 78 79/* There has to be the hash, tab, path and then NUL */ 80if(buf.len <42||get_sha1_hex(buf.buf, sha1)) 81die("corrupt MERGE_RR"); 82 83if(buf.buf[40] !='\t') 84die("corrupt MERGE_RR"); 85 buf.buf[40] ='\0'; 86 path = buf.buf +41; 87 id =new_rerere_id_hex(buf.buf); 88string_list_insert(rr, path)->util = id; 89} 90strbuf_release(&buf); 91fclose(in); 92} 93 94static struct lock_file write_lock; 95 96static intwrite_rr(struct string_list *rr,int out_fd) 97{ 98int i; 99for(i =0; i < rr->nr; i++) { 100struct strbuf buf = STRBUF_INIT; 101struct rerere_id *id; 102 103assert(rr->items[i].util != RERERE_RESOLVED); 104 105 id = rr->items[i].util; 106if(!id) 107continue; 108strbuf_addf(&buf,"%s\t%s%c", 109rerere_id_hex(id), 110 rr->items[i].string,0); 111if(write_in_full(out_fd, buf.buf, buf.len) != buf.len) 112die("unable to write rerere record"); 113 114strbuf_release(&buf); 115} 116if(commit_lock_file(&write_lock) !=0) 117die("unable to write rerere record"); 118return0; 119} 120 121/* 122 * "rerere" interacts with conflicted file contents using this I/O 123 * abstraction. It reads a conflicted contents from one place via 124 * "getline()" method, and optionally can write it out after 125 * normalizing the conflicted hunks to the "output". Subclasses of 126 * rerere_io embed this structure at the beginning of their own 127 * rerere_io object. 128 */ 129struct rerere_io { 130int(*getline)(struct strbuf *,struct rerere_io *); 131FILE*output; 132int wrerror; 133/* some more stuff */ 134}; 135 136static voidferr_write(const void*p,size_t count,FILE*fp,int*err) 137{ 138if(!count || *err) 139return; 140if(fwrite(p, count,1, fp) !=1) 141*err = errno; 142} 143 144staticinlinevoidferr_puts(const char*s,FILE*fp,int*err) 145{ 146ferr_write(s,strlen(s), fp, err); 147} 148 149static voidrerere_io_putstr(const char*str,struct rerere_io *io) 150{ 151if(io->output) 152ferr_puts(str, io->output, &io->wrerror); 153} 154 155/* 156 * Write a conflict marker to io->output (if defined). 157 */ 158static voidrerere_io_putconflict(int ch,int size,struct rerere_io *io) 159{ 160char buf[64]; 161 162while(size) { 163if(size <=sizeof(buf) -2) { 164memset(buf, ch, size); 165 buf[size] ='\n'; 166 buf[size +1] ='\0'; 167 size =0; 168}else{ 169int sz =sizeof(buf) -1; 170 171/* 172 * Make sure we will not write everything out 173 * in this round by leaving at least 1 byte 174 * for the next round, giving the next round 175 * a chance to add the terminating LF. Yuck. 176 */ 177if(size <= sz) 178 sz -= (sz - size) +1; 179memset(buf, ch, sz); 180 buf[sz] ='\0'; 181 size -= sz; 182} 183rerere_io_putstr(buf, io); 184} 185} 186 187static voidrerere_io_putmem(const char*mem,size_t sz,struct rerere_io *io) 188{ 189if(io->output) 190ferr_write(mem, sz, io->output, &io->wrerror); 191} 192 193/* 194 * Subclass of rerere_io that reads from an on-disk file 195 */ 196struct rerere_io_file { 197struct rerere_io io; 198FILE*input; 199}; 200 201/* 202 * ... and its getline() method implementation 203 */ 204static intrerere_file_getline(struct strbuf *sb,struct rerere_io *io_) 205{ 206struct rerere_io_file *io = (struct rerere_io_file *)io_; 207returnstrbuf_getwholeline(sb, io->input,'\n'); 208} 209 210/* 211 * Require the exact number of conflict marker letters, no more, no 212 * less, followed by SP or any whitespace 213 * (including LF). 214 */ 215static intis_cmarker(char*buf,int marker_char,int marker_size) 216{ 217int want_sp; 218 219/* 220 * The beginning of our version and the end of their version 221 * always are labeled like "<<<<< ours" or ">>>>> theirs", 222 * hence we set want_sp for them. Note that the version from 223 * the common ancestor in diff3-style output is not always 224 * labelled (e.g. "||||| common" is often seen but "|||||" 225 * alone is also valid), so we do not set want_sp. 226 */ 227 want_sp = (marker_char =='<') || (marker_char =='>'); 228 229while(marker_size--) 230if(*buf++ != marker_char) 231return0; 232if(want_sp && *buf !=' ') 233return0; 234returnisspace(*buf); 235} 236 237/* 238 * Read contents a file with conflicts, normalize the conflicts 239 * by (1) discarding the common ancestor version in diff3-style, 240 * (2) reordering our side and their side so that whichever sorts 241 * alphabetically earlier comes before the other one, while 242 * computing the "conflict ID", which is just an SHA-1 hash of 243 * one side of the conflict, NUL, the other side of the conflict, 244 * and NUL concatenated together. 245 * 246 * Return the number of conflict hunks found. 247 * 248 * NEEDSWORK: the logic and theory of operation behind this conflict 249 * normalization may deserve to be documented somewhere, perhaps in 250 * Documentation/technical/rerere.txt. 251 */ 252static inthandle_path(unsigned char*sha1,struct rerere_io *io,int marker_size) 253{ 254 git_SHA_CTX ctx; 255int hunk_no =0; 256enum{ 257 RR_CONTEXT =0, RR_SIDE_1, RR_SIDE_2, RR_ORIGINAL 258} hunk = RR_CONTEXT; 259struct strbuf one = STRBUF_INIT, two = STRBUF_INIT; 260struct strbuf buf = STRBUF_INIT; 261 262if(sha1) 263git_SHA1_Init(&ctx); 264 265while(!io->getline(&buf, io)) { 266if(is_cmarker(buf.buf,'<', marker_size)) { 267if(hunk != RR_CONTEXT) 268goto bad; 269 hunk = RR_SIDE_1; 270}else if(is_cmarker(buf.buf,'|', marker_size)) { 271if(hunk != RR_SIDE_1) 272goto bad; 273 hunk = RR_ORIGINAL; 274}else if(is_cmarker(buf.buf,'=', marker_size)) { 275if(hunk != RR_SIDE_1 && hunk != RR_ORIGINAL) 276goto bad; 277 hunk = RR_SIDE_2; 278}else if(is_cmarker(buf.buf,'>', marker_size)) { 279if(hunk != RR_SIDE_2) 280goto bad; 281if(strbuf_cmp(&one, &two) >0) 282strbuf_swap(&one, &two); 283 hunk_no++; 284 hunk = RR_CONTEXT; 285rerere_io_putconflict('<', marker_size, io); 286rerere_io_putmem(one.buf, one.len, io); 287rerere_io_putconflict('=', marker_size, io); 288rerere_io_putmem(two.buf, two.len, io); 289rerere_io_putconflict('>', marker_size, io); 290if(sha1) { 291git_SHA1_Update(&ctx, one.buf ? one.buf :"", 292 one.len +1); 293git_SHA1_Update(&ctx, two.buf ? two.buf :"", 294 two.len +1); 295} 296strbuf_reset(&one); 297strbuf_reset(&two); 298}else if(hunk == RR_SIDE_1) 299strbuf_addbuf(&one, &buf); 300else if(hunk == RR_ORIGINAL) 301;/* discard */ 302else if(hunk == RR_SIDE_2) 303strbuf_addbuf(&two, &buf); 304else 305rerere_io_putstr(buf.buf, io); 306continue; 307 bad: 308 hunk =99;/* force error exit */ 309break; 310} 311strbuf_release(&one); 312strbuf_release(&two); 313strbuf_release(&buf); 314 315if(sha1) 316git_SHA1_Final(sha1, &ctx); 317if(hunk != RR_CONTEXT) 318return-1; 319return hunk_no; 320} 321 322/* 323 * Scan the path for conflicts, do the "handle_path()" thing above, and 324 * return the number of conflict hunks found. 325 */ 326static inthandle_file(const char*path,unsigned char*sha1,const char*output) 327{ 328int hunk_no =0; 329struct rerere_io_file io; 330int marker_size =ll_merge_marker_size(path); 331 332memset(&io,0,sizeof(io)); 333 io.io.getline = rerere_file_getline; 334 io.input =fopen(path,"r"); 335 io.io.wrerror =0; 336if(!io.input) 337returnerror("Could not open%s", path); 338 339if(output) { 340 io.io.output =fopen(output,"w"); 341if(!io.io.output) { 342fclose(io.input); 343returnerror("Could not write%s", output); 344} 345} 346 347 hunk_no =handle_path(sha1, (struct rerere_io *)&io, marker_size); 348 349fclose(io.input); 350if(io.io.wrerror) 351error("There were errors while writing%s(%s)", 352 path,strerror(io.io.wrerror)); 353if(io.io.output &&fclose(io.io.output)) 354 io.io.wrerror =error("Failed to flush%s:%s", 355 path,strerror(errno)); 356 357if(hunk_no <0) { 358if(output) 359unlink_or_warn(output); 360returnerror("Could not parse conflict hunks in%s", path); 361} 362if(io.io.wrerror) 363return-1; 364return hunk_no; 365} 366 367/* 368 * Subclass of rerere_io that reads from an in-core buffer that is a 369 * strbuf 370 */ 371struct rerere_io_mem { 372struct rerere_io io; 373struct strbuf input; 374}; 375 376/* 377 * ... and its getline() method implementation 378 */ 379static intrerere_mem_getline(struct strbuf *sb,struct rerere_io *io_) 380{ 381struct rerere_io_mem *io = (struct rerere_io_mem *)io_; 382char*ep; 383size_t len; 384 385strbuf_release(sb); 386if(!io->input.len) 387return-1; 388 ep =memchr(io->input.buf,'\n', io->input.len); 389if(!ep) 390 ep = io->input.buf + io->input.len; 391else if(*ep =='\n') 392 ep++; 393 len = ep - io->input.buf; 394strbuf_add(sb, io->input.buf, len); 395strbuf_remove(&io->input,0, len); 396return0; 397} 398 399static inthandle_cache(const char*path,unsigned char*sha1,const char*output) 400{ 401 mmfile_t mmfile[3] = {{NULL}}; 402 mmbuffer_t result = {NULL,0}; 403const struct cache_entry *ce; 404int pos, len, i, hunk_no; 405struct rerere_io_mem io; 406int marker_size =ll_merge_marker_size(path); 407 408/* 409 * Reproduce the conflicted merge in-core 410 */ 411 len =strlen(path); 412 pos =cache_name_pos(path, len); 413if(0<= pos) 414return-1; 415 pos = -pos -1; 416 417while(pos < active_nr) { 418enum object_type type; 419unsigned long size; 420 421 ce = active_cache[pos++]; 422if(ce_namelen(ce) != len ||memcmp(ce->name, path, len)) 423break; 424 i =ce_stage(ce) -1; 425if(!mmfile[i].ptr) { 426 mmfile[i].ptr =read_sha1_file(ce->sha1, &type, &size); 427 mmfile[i].size = size; 428} 429} 430for(i =0; i <3; i++) 431if(!mmfile[i].ptr && !mmfile[i].size) 432 mmfile[i].ptr =xstrdup(""); 433 434/* 435 * NEEDSWORK: handle conflicts from merges with 436 * merge.renormalize set, too 437 */ 438ll_merge(&result, path, &mmfile[0], NULL, 439&mmfile[1],"ours", 440&mmfile[2],"theirs", NULL); 441for(i =0; i <3; i++) 442free(mmfile[i].ptr); 443 444memset(&io,0,sizeof(io)); 445 io.io.getline = rerere_mem_getline; 446if(output) 447 io.io.output =fopen(output,"w"); 448else 449 io.io.output = NULL; 450strbuf_init(&io.input,0); 451strbuf_attach(&io.input, result.ptr, result.size, result.size); 452 453/* 454 * Grab the conflict ID and optionally write the original 455 * contents with conflict markers out. 456 */ 457 hunk_no =handle_path(sha1, (struct rerere_io *)&io, marker_size); 458strbuf_release(&io.input); 459if(io.io.output) 460fclose(io.io.output); 461return hunk_no; 462} 463 464/* 465 * Look at a cache entry at "i" and see if it is not conflicting, 466 * conflicting and we are willing to handle, or conflicting and 467 * we are unable to handle, and return the determination in *type. 468 * Return the cache index to be looked at next, by skipping the 469 * stages we have already looked at in this invocation of this 470 * function. 471 */ 472static intcheck_one_conflict(int i,int*type) 473{ 474const struct cache_entry *e = active_cache[i]; 475 476if(!ce_stage(e)) { 477*type = RESOLVED; 478return i +1; 479} 480 481*type = PUNTED; 482while(ce_stage(active_cache[i]) ==1) 483 i++; 484 485/* Only handle regular files with both stages #2 and #3 */ 486if(i +1< active_nr) { 487const struct cache_entry *e2 = active_cache[i]; 488const struct cache_entry *e3 = active_cache[i +1]; 489if(ce_stage(e2) ==2&& 490ce_stage(e3) ==3&& 491ce_same_name(e, e3) && 492S_ISREG(e2->ce_mode) && 493S_ISREG(e3->ce_mode)) 494*type = THREE_STAGED; 495} 496 497/* Skip the entries with the same name */ 498while(i < active_nr &&ce_same_name(e, active_cache[i])) 499 i++; 500return i; 501} 502 503/* 504 * Scan the index and find paths that have conflicts that rerere can 505 * handle, i.e. the ones that has both stages #2 and #3. 506 * 507 * NEEDSWORK: we do not record or replay a previous "resolve by 508 * deletion" for a delete-modify conflict, as that is inherently risky 509 * without knowing what modification is being discarded. The only 510 * safe case, i.e. both side doing the deletion and modification that 511 * are identical to the previous round, might want to be handled, 512 * though. 513 */ 514static intfind_conflict(struct string_list *conflict) 515{ 516int i; 517if(read_cache() <0) 518returnerror("Could not read index"); 519 520for(i =0; i < active_nr;) { 521int conflict_type; 522const struct cache_entry *e = active_cache[i]; 523 i =check_one_conflict(i, &conflict_type); 524if(conflict_type == THREE_STAGED) 525string_list_insert(conflict, (const char*)e->name); 526} 527return0; 528} 529 530/* 531 * The merge_rr list is meant to hold outstanding conflicted paths 532 * that rerere could handle. Abuse the list by adding other types of 533 * entries to allow the caller to show "rerere remaining". 534 * 535 * - Conflicted paths that rerere does not handle are added 536 * - Conflicted paths that have been resolved are marked as such 537 * by storing RERERE_RESOLVED to .util field (where conflict ID 538 * is expected to be stored). 539 * 540 * Do *not* write MERGE_RR file out after calling this function. 541 * 542 * NEEDSWORK: we may want to fix the caller that implements "rerere 543 * remaining" to do this without abusing merge_rr. 544 */ 545intrerere_remaining(struct string_list *merge_rr) 546{ 547int i; 548if(setup_rerere(merge_rr, RERERE_READONLY)) 549return0; 550if(read_cache() <0) 551returnerror("Could not read index"); 552 553for(i =0; i < active_nr;) { 554int conflict_type; 555const struct cache_entry *e = active_cache[i]; 556 i =check_one_conflict(i, &conflict_type); 557if(conflict_type == PUNTED) 558string_list_insert(merge_rr, (const char*)e->name); 559else if(conflict_type == RESOLVED) { 560struct string_list_item *it; 561 it =string_list_lookup(merge_rr, (const char*)e->name); 562if(it != NULL) { 563free_rerere_id(it); 564 it->util = RERERE_RESOLVED; 565} 566} 567} 568return0; 569} 570 571/* 572 * Find the conflict identified by "id"; the change between its 573 * "preimage" (i.e. a previous contents with conflict markers) and its 574 * "postimage" (i.e. the corresponding contents with conflicts 575 * resolved) may apply cleanly to the contents stored in "path", i.e. 576 * the conflict this time around. 577 * 578 * Returns 0 for successful replay of recorded resolution, or non-zero 579 * for failure. 580 */ 581static intmerge(const struct rerere_id *id,const char*path) 582{ 583FILE*f; 584int ret; 585 mmfile_t cur = {NULL,0}, base = {NULL,0}, other = {NULL,0}; 586 mmbuffer_t result = {NULL,0}; 587 588/* 589 * Normalize the conflicts in path and write it out to 590 * "thisimage" temporary file. 591 */ 592if(handle_file(path, NULL,rerere_path(id,"thisimage")) <0) { 593 ret =1; 594goto out; 595} 596 597if(read_mmfile(&cur,rerere_path(id,"thisimage")) || 598read_mmfile(&base,rerere_path(id,"preimage")) || 599read_mmfile(&other,rerere_path(id,"postimage"))) { 600 ret =1; 601goto out; 602} 603 604/* 605 * A three-way merge. Note that this honors user-customizable 606 * low-level merge driver settings. 607 */ 608 ret =ll_merge(&result, path, &base, NULL, &cur,"", &other,"", NULL); 609if(ret) 610goto out; 611 612/* 613 * A successful replay of recorded resolution. 614 * Mark that "postimage" was used to help gc. 615 */ 616if(utime(rerere_path(id,"postimage"), NULL) <0) 617warning("failed utime() on%s:%s", 618rerere_path(id,"postimage"), 619strerror(errno)); 620 621/* Update "path" with the resolution */ 622 f =fopen(path,"w"); 623if(!f) 624returnerror("Could not open%s:%s", path, 625strerror(errno)); 626if(fwrite(result.ptr, result.size,1, f) !=1) 627error("Could not write%s:%s", path,strerror(errno)); 628if(fclose(f)) 629returnerror("Writing%sfailed:%s", path, 630strerror(errno)); 631 632out: 633free(cur.ptr); 634free(base.ptr); 635free(other.ptr); 636free(result.ptr); 637 638return ret; 639} 640 641static struct lock_file index_lock; 642 643static voidupdate_paths(struct string_list *update) 644{ 645int i; 646 647hold_locked_index(&index_lock,1); 648 649for(i =0; i < update->nr; i++) { 650struct string_list_item *item = &update->items[i]; 651if(add_file_to_cache(item->string,0)) 652exit(128); 653fprintf(stderr,"Staged '%s' using previous resolution.\n", 654 item->string); 655} 656 657if(active_cache_changed) { 658if(write_locked_index(&the_index, &index_lock, COMMIT_LOCK)) 659die("Unable to write new index file"); 660}else 661rollback_lock_file(&index_lock); 662} 663 664/* 665 * The path indicated by rr_item may still have conflict for which we 666 * have a recorded resolution, in which case replay it and optionally 667 * update it. Or it may have been resolved by the user and we may 668 * only have the preimage for that conflict, in which case the result 669 * needs to be recorded as a resolution in a postimage file. 670 */ 671static voiddo_rerere_one_path(struct string_list_item *rr_item, 672struct string_list *update) 673{ 674const char*path = rr_item->string; 675const struct rerere_id *id = rr_item->util; 676 677/* Is there a recorded resolution we could attempt to apply? */ 678if(has_rerere_resolution(id)) { 679if(merge(id, path)) 680return;/* failed to replay */ 681 682if(rerere_autoupdate) 683string_list_insert(update, path); 684else 685fprintf(stderr, 686"Resolved '%s' using previous resolution.\n", 687 path); 688}else if(!handle_file(path, NULL, NULL)) { 689/* The user has resolved it. */ 690copy_file(rerere_path(id,"postimage"), path,0666); 691fprintf(stderr,"Recorded resolution for '%s'.\n", path); 692}else{ 693return; 694} 695free_rerere_id(rr_item); 696 rr_item->util = NULL; 697} 698 699static intdo_plain_rerere(struct string_list *rr,int fd) 700{ 701struct string_list conflict = STRING_LIST_INIT_DUP; 702struct string_list update = STRING_LIST_INIT_DUP; 703int i; 704 705find_conflict(&conflict); 706 707/* 708 * MERGE_RR records paths with conflicts immediately after 709 * merge failed. Some of the conflicted paths might have been 710 * hand resolved in the working tree since then, but the 711 * initial run would catch all and register their preimages. 712 */ 713for(i =0; i < conflict.nr; i++) { 714struct rerere_id *id; 715unsigned char sha1[20]; 716const char*path = conflict.items[i].string; 717int ret; 718 719if(string_list_has_string(rr, path)) 720continue; 721 722/* 723 * Ask handle_file() to scan and assign a 724 * conflict ID. No need to write anything out 725 * yet. 726 */ 727 ret =handle_file(path, sha1, NULL); 728if(ret <1) 729continue; 730 731 id =new_rerere_id(sha1); 732string_list_insert(rr, path)->util = id; 733 734/* 735 * If the directory does not exist, create 736 * it. mkdir_in_gitdir() will fail with 737 * EEXIST if there already is one. 738 * 739 * NEEDSWORK: make sure "gc" does not remove 740 * preimage without removing the directory. 741 */ 742if(mkdir_in_gitdir(rerere_path(id, NULL))) 743continue; 744 745/* 746 * We are the first to encounter this 747 * conflict. Ask handle_file() to write the 748 * normalized contents to the "preimage" file. 749 */ 750handle_file(path, NULL,rerere_path(id,"preimage")); 751fprintf(stderr,"Recorded preimage for '%s'\n", path); 752} 753 754for(i =0; i < rr->nr; i++) 755do_rerere_one_path(&rr->items[i], &update); 756 757if(update.nr) 758update_paths(&update); 759 760returnwrite_rr(rr, fd); 761} 762 763static voidgit_rerere_config(void) 764{ 765git_config_get_bool("rerere.enabled", &rerere_enabled); 766git_config_get_bool("rerere.autoupdate", &rerere_autoupdate); 767git_config(git_default_config, NULL); 768} 769 770staticGIT_PATH_FUNC(git_path_rr_cache,"rr-cache") 771 772static intis_rerere_enabled(void) 773{ 774int rr_cache_exists; 775 776if(!rerere_enabled) 777return0; 778 779 rr_cache_exists =is_directory(git_path_rr_cache()); 780if(rerere_enabled <0) 781return rr_cache_exists; 782 783if(!rr_cache_exists &&mkdir_in_gitdir(git_path_rr_cache())) 784die("Could not create directory%s",git_path_rr_cache()); 785return1; 786} 787 788intsetup_rerere(struct string_list *merge_rr,int flags) 789{ 790int fd; 791 792git_rerere_config(); 793if(!is_rerere_enabled()) 794return-1; 795 796if(flags & (RERERE_AUTOUPDATE|RERERE_NOAUTOUPDATE)) 797 rerere_autoupdate = !!(flags & RERERE_AUTOUPDATE); 798if(flags & RERERE_READONLY) 799 fd =0; 800else 801 fd =hold_lock_file_for_update(&write_lock,git_path_merge_rr(), 802 LOCK_DIE_ON_ERROR); 803read_rr(merge_rr); 804return fd; 805} 806 807/* 808 * The main entry point that is called internally from codepaths that 809 * perform mergy operations, possibly leaving conflicted index entries 810 * and working tree files. 811 */ 812intrerere(int flags) 813{ 814struct string_list merge_rr = STRING_LIST_INIT_DUP; 815int fd; 816 817 fd =setup_rerere(&merge_rr, flags); 818if(fd <0) 819return0; 820returndo_plain_rerere(&merge_rr, fd); 821} 822 823static intrerere_forget_one_path(const char*path,struct string_list *rr) 824{ 825const char*filename; 826struct rerere_id *id; 827unsigned char sha1[20]; 828int ret; 829struct string_list_item *item; 830 831/* 832 * Recreate the original conflict from the stages in the 833 * index and compute the conflict ID 834 */ 835 ret =handle_cache(path, sha1, NULL); 836if(ret <1) 837returnerror("Could not parse conflict hunks in '%s'", path); 838 839/* Nuke the recorded resolution for the conflict */ 840 id =new_rerere_id(sha1); 841 filename =rerere_path(id,"postimage"); 842if(unlink(filename)) 843return(errno == ENOENT 844?error("no remembered resolution for%s", path) 845:error("cannot unlink%s:%s", filename,strerror(errno))); 846 847/* 848 * Update the preimage so that the user can resolve the 849 * conflict in the working tree, run us again to record 850 * the postimage. 851 */ 852handle_cache(path, sha1,rerere_path(id,"preimage")); 853fprintf(stderr,"Updated preimage for '%s'\n", path); 854 855/* 856 * And remember that we can record resolution for this 857 * conflict when the user is done. 858 */ 859 item =string_list_insert(rr, path); 860free_rerere_id(item); 861 item->util = id; 862fprintf(stderr,"Forgot resolution for%s\n", path); 863return0; 864} 865 866intrerere_forget(struct pathspec *pathspec) 867{ 868int i, fd; 869struct string_list conflict = STRING_LIST_INIT_DUP; 870struct string_list merge_rr = STRING_LIST_INIT_DUP; 871 872if(read_cache() <0) 873returnerror("Could not read index"); 874 875 fd =setup_rerere(&merge_rr, RERERE_NOAUTOUPDATE); 876if(fd <0) 877return0; 878 879/* 880 * The paths may have been resolved (incorrectly); 881 * recover the original conflicted state and then 882 * find the conflicted paths. 883 */ 884unmerge_cache(pathspec); 885find_conflict(&conflict); 886for(i =0; i < conflict.nr; i++) { 887struct string_list_item *it = &conflict.items[i]; 888if(!match_pathspec(pathspec, it->string, 889strlen(it->string),0, NULL,0)) 890continue; 891rerere_forget_one_path(it->string, &merge_rr); 892} 893returnwrite_rr(&merge_rr, fd); 894} 895 896/* 897 * Garbage collection support 898 */ 899 900/* 901 * Note that this is not reentrant but is used only one-at-a-time 902 * so it does not matter right now. 903 */ 904static struct rerere_id *dirname_to_id(const char*name) 905{ 906static struct rerere_id id; 907xsnprintf(id.hex,sizeof(id.hex),"%s", name); 908return&id; 909} 910 911static time_trerere_created_at(const char*dir_name) 912{ 913struct stat st; 914struct rerere_id *id =dirname_to_id(dir_name); 915 916returnstat(rerere_path(id,"preimage"), &st) ? (time_t)0: st.st_mtime; 917} 918 919static time_trerere_last_used_at(const char*dir_name) 920{ 921struct stat st; 922struct rerere_id *id =dirname_to_id(dir_name); 923 924returnstat(rerere_path(id,"postimage"), &st) ? (time_t)0: st.st_mtime; 925} 926 927/* 928 * Remove the recorded resolution for a given conflict ID 929 */ 930static voidunlink_rr_item(struct rerere_id *id) 931{ 932unlink(rerere_path(id,"thisimage")); 933unlink(rerere_path(id,"preimage")); 934unlink(rerere_path(id,"postimage")); 935/* 936 * NEEDSWORK: what if this rmdir() fails? Wouldn't we then 937 * assume that we already have preimage recorded in 938 * do_plain_rerere()? 939 */ 940rmdir(rerere_path(id, NULL)); 941} 942 943voidrerere_gc(struct string_list *rr) 944{ 945struct string_list to_remove = STRING_LIST_INIT_DUP; 946DIR*dir; 947struct dirent *e; 948int i, cutoff; 949time_t now =time(NULL), then; 950int cutoff_noresolve =15; 951int cutoff_resolve =60; 952 953if(setup_rerere(rr,0) <0) 954return; 955 956git_config_get_int("gc.rerereresolved", &cutoff_resolve); 957git_config_get_int("gc.rerereunresolved", &cutoff_noresolve); 958git_config(git_default_config, NULL); 959 dir =opendir(git_path("rr-cache")); 960if(!dir) 961die_errno("unable to open rr-cache directory"); 962/* Collect stale conflict IDs ... */ 963while((e =readdir(dir))) { 964if(is_dot_or_dotdot(e->d_name)) 965continue; 966 967 then =rerere_last_used_at(e->d_name); 968if(then) { 969 cutoff = cutoff_resolve; 970}else{ 971 then =rerere_created_at(e->d_name); 972if(!then) 973continue; 974 cutoff = cutoff_noresolve; 975} 976if(then < now - cutoff *86400) 977string_list_append(&to_remove, e->d_name); 978} 979closedir(dir); 980/* ... and then remove them one-by-one */ 981for(i =0; i < to_remove.nr; i++) 982unlink_rr_item(dirname_to_id(to_remove.items[i].string)); 983string_list_clear(&to_remove,0); 984rollback_lock_file(&write_lock); 985} 986 987/* 988 * During a conflict resolution, after "rerere" recorded the 989 * preimages, abandon them if the user did not resolve them or 990 * record their resolutions. And drop $GIT_DIR/MERGE_RR. 991 * 992 * NEEDSWORK: shouldn't we be calling this from "reset --hard"? 993 */ 994voidrerere_clear(struct string_list *merge_rr) 995{ 996int i; 997 998if(setup_rerere(merge_rr,0) <0) 999return;10001001for(i =0; i < merge_rr->nr; i++) {1002struct rerere_id *id = merge_rr->items[i].util;1003if(!has_rerere_resolution(id))1004unlink_rr_item(id);1005}1006unlink_or_warn(git_path_merge_rr());1007rollback_lock_file(&write_lock);1008}