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