e1d527c2506a461687ec89421f08b2de355dd10a
   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{
  27        return git_path("rr-cache/%s/%s", hex, file);
  28}
  29
  30static int has_rerere_resolution(const char *hex)
  31{
  32        struct stat st;
  33        return !stat(rerere_path(hex, "postimage"), &st);
  34}
  35
  36static void read_rr(struct string_list *rr)
  37{
  38        struct strbuf buf = STRBUF_INIT;
  39        FILE *in = fopen(merge_rr_path, "r");
  40
  41        if (!in)
  42                return;
  43        while (!strbuf_getwholeline(&buf, in, '\0')) {
  44                char *path;
  45                unsigned char sha1[20];
  46
  47                /* There has to be the hash, tab, path and then NUL */
  48                if (buf.len < 42 || get_sha1_hex(buf.buf, sha1))
  49                        die("corrupt MERGE_RR");
  50
  51                if (buf.buf[40] != '\t')
  52                        die("corrupt MERGE_RR");
  53                buf.buf[40] = '\0';
  54                path = buf.buf + 41;
  55
  56                string_list_insert(rr, path)->util = xstrdup(buf.buf);
  57        }
  58        strbuf_release(&buf);
  59        fclose(in);
  60}
  61
  62static struct lock_file write_lock;
  63
  64static int write_rr(struct string_list *rr, int out_fd)
  65{
  66        int i;
  67        for (i = 0; i < rr->nr; i++) {
  68                struct strbuf buf = STRBUF_INIT;
  69
  70                assert(rr->items[i].util != RERERE_RESOLVED);
  71                if (!rr->items[i].util)
  72                        continue;
  73                strbuf_addf(&buf, "%s\t%s%c",
  74                            (char *)rr->items[i].util,
  75                            rr->items[i].string, 0);
  76                if (write_in_full(out_fd, buf.buf, buf.len) != buf.len)
  77                        die("unable to write rerere record");
  78
  79                strbuf_release(&buf);
  80        }
  81        if (commit_lock_file(&write_lock) != 0)
  82                die("unable to write rerere record");
  83        return 0;
  84}
  85
  86static void ferr_write(const void *p, size_t count, FILE *fp, int *err)
  87{
  88        if (!count || *err)
  89                return;
  90        if (fwrite(p, count, 1, fp) != 1)
  91                *err = errno;
  92}
  93
  94static inline void ferr_puts(const char *s, FILE *fp, int *err)
  95{
  96        ferr_write(s, strlen(s), fp, err);
  97}
  98
  99struct rerere_io {
 100        int (*getline)(struct strbuf *, struct rerere_io *);
 101        FILE *output;
 102        int wrerror;
 103        /* some more stuff */
 104};
 105
 106static void rerere_io_putstr(const char *str, struct rerere_io *io)
 107{
 108        if (io->output)
 109                ferr_puts(str, io->output, &io->wrerror);
 110}
 111
 112static void rerere_io_putconflict(int ch, int size, struct rerere_io *io)
 113{
 114        char buf[64];
 115
 116        while (size) {
 117                if (size < sizeof(buf) - 2) {
 118                        memset(buf, ch, size);
 119                        buf[size] = '\n';
 120                        buf[size + 1] = '\0';
 121                        size = 0;
 122                } else {
 123                        int sz = sizeof(buf) - 1;
 124                        if (size <= sz)
 125                                sz -= (sz - size) + 1;
 126                        memset(buf, ch, sz);
 127                        buf[sz] = '\0';
 128                        size -= sz;
 129                }
 130                rerere_io_putstr(buf, io);
 131        }
 132}
 133
 134static void rerere_io_putmem(const char *mem, size_t sz, struct rerere_io *io)
 135{
 136        if (io->output)
 137                ferr_write(mem, sz, io->output, &io->wrerror);
 138}
 139
 140struct rerere_io_file {
 141        struct rerere_io io;
 142        FILE *input;
 143};
 144
 145static int rerere_file_getline(struct strbuf *sb, struct rerere_io *io_)
 146{
 147        struct rerere_io_file *io = (struct rerere_io_file *)io_;
 148        return strbuf_getwholeline(sb, io->input, '\n');
 149}
 150
 151/*
 152 * Require the exact number of conflict marker letters, no more, no
 153 * less, followed by SP or any whitespace
 154 * (including LF).
 155 */
 156static int is_cmarker(char *buf, int marker_char, int marker_size)
 157{
 158        int want_sp;
 159
 160        /*
 161         * The beginning of our version and the end of their version
 162         * always are labeled like "<<<<< ours" or ">>>>> theirs",
 163         * hence we set want_sp for them.  Note that the version from
 164         * the common ancestor in diff3-style output is not always
 165         * labelled (e.g. "||||| common" is often seen but "|||||"
 166         * alone is also valid), so we do not set want_sp.
 167         */
 168        want_sp = (marker_char == '<') || (marker_char == '>');
 169
 170        while (marker_size--)
 171                if (*buf++ != marker_char)
 172                        return 0;
 173        if (want_sp && *buf != ' ')
 174                return 0;
 175        return isspace(*buf);
 176}
 177
 178static int handle_path(unsigned char *sha1, struct rerere_io *io, int marker_size)
 179{
 180        git_SHA_CTX ctx;
 181        int hunk_no = 0;
 182        enum {
 183                RR_CONTEXT = 0, RR_SIDE_1, RR_SIDE_2, RR_ORIGINAL
 184        } hunk = RR_CONTEXT;
 185        struct strbuf one = STRBUF_INIT, two = STRBUF_INIT;
 186        struct strbuf buf = STRBUF_INIT;
 187
 188        if (sha1)
 189                git_SHA1_Init(&ctx);
 190
 191        while (!io->getline(&buf, io)) {
 192                if (is_cmarker(buf.buf, '<', marker_size)) {
 193                        if (hunk != RR_CONTEXT)
 194                                goto bad;
 195                        hunk = RR_SIDE_1;
 196                } else if (is_cmarker(buf.buf, '|', marker_size)) {
 197                        if (hunk != RR_SIDE_1)
 198                                goto bad;
 199                        hunk = RR_ORIGINAL;
 200                } else if (is_cmarker(buf.buf, '=', marker_size)) {
 201                        if (hunk != RR_SIDE_1 && hunk != RR_ORIGINAL)
 202                                goto bad;
 203                        hunk = RR_SIDE_2;
 204                } else if (is_cmarker(buf.buf, '>', marker_size)) {
 205                        if (hunk != RR_SIDE_2)
 206                                goto bad;
 207                        if (strbuf_cmp(&one, &two) > 0)
 208                                strbuf_swap(&one, &two);
 209                        hunk_no++;
 210                        hunk = RR_CONTEXT;
 211                        rerere_io_putconflict('<', marker_size, io);
 212                        rerere_io_putmem(one.buf, one.len, io);
 213                        rerere_io_putconflict('=', marker_size, io);
 214                        rerere_io_putmem(two.buf, two.len, io);
 215                        rerere_io_putconflict('>', marker_size, io);
 216                        if (sha1) {
 217                                git_SHA1_Update(&ctx, one.buf ? one.buf : "",
 218                                            one.len + 1);
 219                                git_SHA1_Update(&ctx, two.buf ? two.buf : "",
 220                                            two.len + 1);
 221                        }
 222                        strbuf_reset(&one);
 223                        strbuf_reset(&two);
 224                } else if (hunk == RR_SIDE_1)
 225                        strbuf_addbuf(&one, &buf);
 226                else if (hunk == RR_ORIGINAL)
 227                        ; /* discard */
 228                else if (hunk == RR_SIDE_2)
 229                        strbuf_addbuf(&two, &buf);
 230                else
 231                        rerere_io_putstr(buf.buf, io);
 232                continue;
 233        bad:
 234                hunk = 99; /* force error exit */
 235                break;
 236        }
 237        strbuf_release(&one);
 238        strbuf_release(&two);
 239        strbuf_release(&buf);
 240
 241        if (sha1)
 242                git_SHA1_Final(sha1, &ctx);
 243        if (hunk != RR_CONTEXT)
 244                return -1;
 245        return hunk_no;
 246}
 247
 248static int handle_file(const char *path, unsigned char *sha1, const char *output)
 249{
 250        int hunk_no = 0;
 251        struct rerere_io_file io;
 252        int marker_size = ll_merge_marker_size(path);
 253
 254        memset(&io, 0, sizeof(io));
 255        io.io.getline = rerere_file_getline;
 256        io.input = fopen(path, "r");
 257        io.io.wrerror = 0;
 258        if (!io.input)
 259                return error("Could not open %s", path);
 260
 261        if (output) {
 262                io.io.output = fopen(output, "w");
 263                if (!io.io.output) {
 264                        fclose(io.input);
 265                        return error("Could not write %s", output);
 266                }
 267        }
 268
 269        hunk_no = handle_path(sha1, (struct rerere_io *)&io, marker_size);
 270
 271        fclose(io.input);
 272        if (io.io.wrerror)
 273                error("There were errors while writing %s (%s)",
 274                      path, strerror(io.io.wrerror));
 275        if (io.io.output && fclose(io.io.output))
 276                io.io.wrerror = error("Failed to flush %s: %s",
 277                                      path, strerror(errno));
 278
 279        if (hunk_no < 0) {
 280                if (output)
 281                        unlink_or_warn(output);
 282                return error("Could not parse conflict hunks in %s", path);
 283        }
 284        if (io.io.wrerror)
 285                return -1;
 286        return hunk_no;
 287}
 288
 289struct rerere_io_mem {
 290        struct rerere_io io;
 291        struct strbuf input;
 292};
 293
 294static int rerere_mem_getline(struct strbuf *sb, struct rerere_io *io_)
 295{
 296        struct rerere_io_mem *io = (struct rerere_io_mem *)io_;
 297        char *ep;
 298        size_t len;
 299
 300        strbuf_release(sb);
 301        if (!io->input.len)
 302                return -1;
 303        ep = memchr(io->input.buf, '\n', io->input.len);
 304        if (!ep)
 305                ep = io->input.buf + io->input.len;
 306        else if (*ep == '\n')
 307                ep++;
 308        len = ep - io->input.buf;
 309        strbuf_add(sb, io->input.buf, len);
 310        strbuf_remove(&io->input, 0, len);
 311        return 0;
 312}
 313
 314static int handle_cache(const char *path, unsigned char *sha1, const char *output)
 315{
 316        mmfile_t mmfile[3] = {{NULL}};
 317        mmbuffer_t result = {NULL, 0};
 318        const struct cache_entry *ce;
 319        int pos, len, i, hunk_no;
 320        struct rerere_io_mem io;
 321        int marker_size = ll_merge_marker_size(path);
 322
 323        /*
 324         * Reproduce the conflicted merge in-core
 325         */
 326        len = strlen(path);
 327        pos = cache_name_pos(path, len);
 328        if (0 <= pos)
 329                return -1;
 330        pos = -pos - 1;
 331
 332        while (pos < active_nr) {
 333                enum object_type type;
 334                unsigned long size;
 335
 336                ce = active_cache[pos++];
 337                if (ce_namelen(ce) != len || memcmp(ce->name, path, len))
 338                        break;
 339                i = ce_stage(ce) - 1;
 340                mmfile[i].ptr = read_sha1_file(ce->sha1, &type, &size);
 341                mmfile[i].size = size;
 342        }
 343        for (i = 0; i < 3; i++)
 344                if (!mmfile[i].ptr && !mmfile[i].size)
 345                        mmfile[i].ptr = xstrdup("");
 346
 347        /*
 348         * NEEDSWORK: handle conflicts from merges with
 349         * merge.renormalize set, too
 350         */
 351        ll_merge(&result, path, &mmfile[0], NULL,
 352                 &mmfile[1], "ours",
 353                 &mmfile[2], "theirs", NULL);
 354        for (i = 0; i < 3; i++)
 355                free(mmfile[i].ptr);
 356
 357        memset(&io, 0, sizeof(io));
 358        io.io.getline = rerere_mem_getline;
 359        if (output)
 360                io.io.output = fopen(output, "w");
 361        else
 362                io.io.output = NULL;
 363        strbuf_init(&io.input, 0);
 364        strbuf_attach(&io.input, result.ptr, result.size, result.size);
 365
 366        hunk_no = handle_path(sha1, (struct rerere_io *)&io, marker_size);
 367        strbuf_release(&io.input);
 368        if (io.io.output)
 369                fclose(io.io.output);
 370        return hunk_no;
 371}
 372
 373static int check_one_conflict(int i, int *type)
 374{
 375        const struct cache_entry *e = active_cache[i];
 376
 377        if (!ce_stage(e)) {
 378                *type = RESOLVED;
 379                return i + 1;
 380        }
 381
 382        *type = PUNTED;
 383        while (ce_stage(active_cache[i]) == 1)
 384                i++;
 385
 386        /* Only handle regular files with both stages #2 and #3 */
 387        if (i + 1 < active_nr) {
 388                const struct cache_entry *e2 = active_cache[i];
 389                const struct cache_entry *e3 = active_cache[i + 1];
 390                if (ce_stage(e2) == 2 &&
 391                    ce_stage(e3) == 3 &&
 392                    ce_same_name(e, e3) &&
 393                    S_ISREG(e2->ce_mode) &&
 394                    S_ISREG(e3->ce_mode))
 395                        *type = THREE_STAGED;
 396        }
 397
 398        /* Skip the entries with the same name */
 399        while (i < active_nr && ce_same_name(e, active_cache[i]))
 400                i++;
 401        return i;
 402}
 403
 404static int find_conflict(struct string_list *conflict)
 405{
 406        int i;
 407        if (read_cache() < 0)
 408                return error("Could not read index");
 409
 410        for (i = 0; i < active_nr;) {
 411                int conflict_type;
 412                const struct cache_entry *e = active_cache[i];
 413                i = check_one_conflict(i, &conflict_type);
 414                if (conflict_type == THREE_STAGED)
 415                        string_list_insert(conflict, (const char *)e->name);
 416        }
 417        return 0;
 418}
 419
 420int rerere_remaining(struct string_list *merge_rr)
 421{
 422        int i;
 423        if (read_cache() < 0)
 424                return error("Could not read index");
 425
 426        for (i = 0; i < active_nr;) {
 427                int conflict_type;
 428                const struct cache_entry *e = active_cache[i];
 429                i = check_one_conflict(i, &conflict_type);
 430                if (conflict_type == PUNTED)
 431                        string_list_insert(merge_rr, (const char *)e->name);
 432                else if (conflict_type == RESOLVED) {
 433                        struct string_list_item *it;
 434                        it = string_list_lookup(merge_rr, (const char *)e->name);
 435                        if (it != NULL) {
 436                                free(it->util);
 437                                it->util = RERERE_RESOLVED;
 438                        }
 439                }
 440        }
 441        return 0;
 442}
 443
 444static int merge(const char *name, const char *path)
 445{
 446        int ret;
 447        mmfile_t cur = {NULL, 0}, base = {NULL, 0}, other = {NULL, 0};
 448        mmbuffer_t result = {NULL, 0};
 449
 450        if (handle_file(path, NULL, rerere_path(name, "thisimage")) < 0)
 451                return 1;
 452
 453        if (read_mmfile(&cur, rerere_path(name, "thisimage")) ||
 454                        read_mmfile(&base, rerere_path(name, "preimage")) ||
 455                        read_mmfile(&other, rerere_path(name, "postimage"))) {
 456                ret = 1;
 457                goto out;
 458        }
 459        ret = ll_merge(&result, path, &base, NULL, &cur, "", &other, "", NULL);
 460        if (!ret) {
 461                FILE *f;
 462
 463                if (utime(rerere_path(name, "postimage"), NULL) < 0)
 464                        warning("failed utime() on %s: %s",
 465                                        rerere_path(name, "postimage"),
 466                                        strerror(errno));
 467                f = fopen(path, "w");
 468                if (!f)
 469                        return error("Could not open %s: %s", path,
 470                                     strerror(errno));
 471                if (fwrite(result.ptr, result.size, 1, f) != 1)
 472                        error("Could not write %s: %s", path, strerror(errno));
 473                if (fclose(f))
 474                        return error("Writing %s failed: %s", path,
 475                                     strerror(errno));
 476        }
 477
 478out:
 479        free(cur.ptr);
 480        free(base.ptr);
 481        free(other.ptr);
 482        free(result.ptr);
 483
 484        return ret;
 485}
 486
 487static struct lock_file index_lock;
 488
 489static void update_paths(struct string_list *update)
 490{
 491        int i;
 492
 493        hold_locked_index(&index_lock, 1);
 494
 495        for (i = 0; i < update->nr; i++) {
 496                struct string_list_item *item = &update->items[i];
 497                if (add_file_to_cache(item->string, 0))
 498                        exit(128);
 499                fprintf(stderr, "Staged '%s' using previous resolution.\n",
 500                        item->string);
 501        }
 502
 503        if (active_cache_changed) {
 504                if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK))
 505                        die("Unable to write new index file");
 506        } else
 507                rollback_lock_file(&index_lock);
 508}
 509
 510static int do_plain_rerere(struct string_list *rr, int fd)
 511{
 512        struct string_list conflict = STRING_LIST_INIT_DUP;
 513        struct string_list update = STRING_LIST_INIT_DUP;
 514        int i;
 515
 516        find_conflict(&conflict);
 517
 518        /*
 519         * MERGE_RR records paths with conflicts immediately after merge
 520         * failed.  Some of the conflicted paths might have been hand resolved
 521         * in the working tree since then, but the initial run would catch all
 522         * and register their preimages.
 523         */
 524
 525        for (i = 0; i < conflict.nr; i++) {
 526                const char *path = conflict.items[i].string;
 527                if (!string_list_has_string(rr, path)) {
 528                        unsigned char sha1[20];
 529                        char *hex;
 530                        int ret;
 531                        ret = handle_file(path, sha1, NULL);
 532                        if (ret < 1)
 533                                continue;
 534                        hex = xstrdup(sha1_to_hex(sha1));
 535                        string_list_insert(rr, path)->util = hex;
 536                        if (mkdir_in_gitdir(git_path("rr-cache/%s", hex)))
 537                                continue;
 538                        handle_file(path, NULL, rerere_path(hex, "preimage"));
 539                        fprintf(stderr, "Recorded preimage for '%s'\n", path);
 540                }
 541        }
 542
 543        /*
 544         * Now some of the paths that had conflicts earlier might have been
 545         * hand resolved.  Others may be similar to a conflict already that
 546         * was resolved before.
 547         */
 548
 549        for (i = 0; i < rr->nr; i++) {
 550                int ret;
 551                const char *path = rr->items[i].string;
 552                const char *name = (const char *)rr->items[i].util;
 553
 554                if (has_rerere_resolution(name)) {
 555                        if (merge(name, path))
 556                                continue;
 557
 558                        if (rerere_autoupdate)
 559                                string_list_insert(&update, path);
 560                        else
 561                                fprintf(stderr,
 562                                        "Resolved '%s' using previous resolution.\n",
 563                                        path);
 564                        goto mark_resolved;
 565                }
 566
 567                /* Let's see if we have resolved it. */
 568                ret = handle_file(path, NULL, NULL);
 569                if (ret)
 570                        continue;
 571
 572                fprintf(stderr, "Recorded resolution for '%s'.\n", path);
 573                copy_file(rerere_path(name, "postimage"), path, 0666);
 574        mark_resolved:
 575                free(rr->items[i].util);
 576                rr->items[i].util = NULL;
 577        }
 578
 579        if (update.nr)
 580                update_paths(&update);
 581
 582        return write_rr(rr, fd);
 583}
 584
 585static void git_rerere_config(void)
 586{
 587        git_config_get_bool("rerere.enabled", &rerere_enabled);
 588        git_config_get_bool("rerere.autoupdate", &rerere_autoupdate);
 589        git_config(git_default_config, NULL);
 590}
 591
 592static int is_rerere_enabled(void)
 593{
 594        const char *rr_cache;
 595        int rr_cache_exists;
 596
 597        if (!rerere_enabled)
 598                return 0;
 599
 600        rr_cache = git_path("rr-cache");
 601        rr_cache_exists = is_directory(rr_cache);
 602        if (rerere_enabled < 0)
 603                return rr_cache_exists;
 604
 605        if (!rr_cache_exists && mkdir_in_gitdir(rr_cache))
 606                die("Could not create directory %s", rr_cache);
 607        return 1;
 608}
 609
 610int setup_rerere(struct string_list *merge_rr, int flags)
 611{
 612        int fd;
 613
 614        git_rerere_config();
 615        if (!is_rerere_enabled())
 616                return -1;
 617
 618        if (flags & (RERERE_AUTOUPDATE|RERERE_NOAUTOUPDATE))
 619                rerere_autoupdate = !!(flags & RERERE_AUTOUPDATE);
 620        merge_rr_path = git_pathdup("MERGE_RR");
 621        fd = hold_lock_file_for_update(&write_lock, merge_rr_path,
 622                                       LOCK_DIE_ON_ERROR);
 623        read_rr(merge_rr);
 624        return fd;
 625}
 626
 627int rerere(int flags)
 628{
 629        struct string_list merge_rr = STRING_LIST_INIT_DUP;
 630        int fd;
 631
 632        fd = setup_rerere(&merge_rr, flags);
 633        if (fd < 0)
 634                return 0;
 635        return do_plain_rerere(&merge_rr, fd);
 636}
 637
 638static int rerere_forget_one_path(const char *path, struct string_list *rr)
 639{
 640        const char *filename;
 641        char *hex;
 642        unsigned char sha1[20];
 643        int ret;
 644        struct string_list_item *item;
 645
 646        ret = handle_cache(path, sha1, NULL);
 647        if (ret < 1)
 648                return error("Could not parse conflict hunks in '%s'", path);
 649        hex = xstrdup(sha1_to_hex(sha1));
 650        filename = rerere_path(hex, "postimage");
 651        if (unlink(filename))
 652                return (errno == ENOENT
 653                        ? error("no remembered resolution for %s", path)
 654                        : error("cannot unlink %s: %s", filename, strerror(errno)));
 655
 656        handle_cache(path, sha1, rerere_path(hex, "preimage"));
 657        fprintf(stderr, "Updated preimage for '%s'\n", path);
 658
 659        item = string_list_insert(rr, path);
 660        free(item->util);
 661        item->util = hex;
 662        fprintf(stderr, "Forgot resolution for %s\n", path);
 663        return 0;
 664}
 665
 666int rerere_forget(struct pathspec *pathspec)
 667{
 668        int i, fd;
 669        struct string_list conflict = STRING_LIST_INIT_DUP;
 670        struct string_list merge_rr = STRING_LIST_INIT_DUP;
 671
 672        if (read_cache() < 0)
 673                return error("Could not read index");
 674
 675        fd = setup_rerere(&merge_rr, RERERE_NOAUTOUPDATE);
 676
 677        unmerge_cache(pathspec);
 678        find_conflict(&conflict);
 679        for (i = 0; i < conflict.nr; i++) {
 680                struct string_list_item *it = &conflict.items[i];
 681                if (!match_pathspec(pathspec, it->string,
 682                                    strlen(it->string), 0, NULL, 0))
 683                        continue;
 684                rerere_forget_one_path(it->string, &merge_rr);
 685        }
 686        return write_rr(&merge_rr, fd);
 687}
 688
 689static time_t rerere_created_at(const char *name)
 690{
 691        struct stat st;
 692        return stat(rerere_path(name, "preimage"), &st) ? (time_t) 0 : st.st_mtime;
 693}
 694
 695static time_t rerere_last_used_at(const char *name)
 696{
 697        struct stat st;
 698        return stat(rerere_path(name, "postimage"), &st) ? (time_t) 0 : st.st_mtime;
 699}
 700
 701static void unlink_rr_item(const char *name)
 702{
 703        unlink(rerere_path(name, "thisimage"));
 704        unlink(rerere_path(name, "preimage"));
 705        unlink(rerere_path(name, "postimage"));
 706        rmdir(git_path("rr-cache/%s", name));
 707}
 708
 709void rerere_gc(struct string_list *rr)
 710{
 711        struct string_list to_remove = STRING_LIST_INIT_DUP;
 712        DIR *dir;
 713        struct dirent *e;
 714        int i, cutoff;
 715        time_t now = time(NULL), then;
 716        int cutoff_noresolve = 15;
 717        int cutoff_resolve = 60;
 718
 719        git_config_get_int("gc.rerereresolved", &cutoff_resolve);
 720        git_config_get_int("gc.rerereunresolved", &cutoff_noresolve);
 721        git_config(git_default_config, NULL);
 722        dir = opendir(git_path("rr-cache"));
 723        if (!dir)
 724                die_errno("unable to open rr-cache directory");
 725        while ((e = readdir(dir))) {
 726                if (is_dot_or_dotdot(e->d_name))
 727                        continue;
 728
 729                then = rerere_last_used_at(e->d_name);
 730                if (then) {
 731                        cutoff = cutoff_resolve;
 732                } else {
 733                        then = rerere_created_at(e->d_name);
 734                        if (!then)
 735                                continue;
 736                        cutoff = cutoff_noresolve;
 737                }
 738                if (then < now - cutoff * 86400)
 739                        string_list_append(&to_remove, e->d_name);
 740        }
 741        closedir(dir);
 742        for (i = 0; i < to_remove.nr; i++)
 743                unlink_rr_item(to_remove.items[i].string);
 744        string_list_clear(&to_remove, 0);
 745}
 746
 747void rerere_clear(struct string_list *merge_rr)
 748{
 749        int i;
 750
 751        for (i = 0; i < merge_rr->nr; i++) {
 752                const char *name = (const char *)merge_rr->items[i].util;
 753                if (!has_rerere_resolution(name))
 754                        unlink_rr_item(name);
 755        }
 756        unlink_or_warn(git_path("MERGE_RR"));
 757}