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