5f332cb25c240d9a26ec48bd3daa36ae1ced9d7c
   1#include "cache.h"
   2#include "string-list.h"
   3#include "rerere.h"
   4#include "xdiff-interface.h"
   5#include "dir.h"
   6#include "resolve-undo.h"
   7#include "ll-merge.h"
   8
   9/* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
  10static int rerere_enabled = -1;
  11
  12/* automatically update cleanly resolved paths to the index */
  13static int rerere_autoupdate;
  14
  15static char *merge_rr_path;
  16
  17const char *rerere_path(const char *hex, const char *file)
  18{
  19        return git_path("rr-cache/%s/%s", hex, file);
  20}
  21
  22int has_rerere_resolution(const char *hex)
  23{
  24        struct stat st;
  25        return !stat(rerere_path(hex, "postimage"), &st);
  26}
  27
  28static void read_rr(struct string_list *rr)
  29{
  30        unsigned char sha1[20];
  31        char buf[PATH_MAX];
  32        FILE *in = fopen(merge_rr_path, "r");
  33        if (!in)
  34                return;
  35        while (fread(buf, 40, 1, in) == 1) {
  36                int i;
  37                char *name;
  38                if (get_sha1_hex(buf, sha1))
  39                        die("corrupt MERGE_RR");
  40                buf[40] = '\0';
  41                name = xstrdup(buf);
  42                if (fgetc(in) != '\t')
  43                        die("corrupt MERGE_RR");
  44                for (i = 0; i < sizeof(buf) && (buf[i] = fgetc(in)); i++)
  45                        ; /* do nothing */
  46                if (i == sizeof(buf))
  47                        die("filename too long");
  48                string_list_insert(buf, rr)->util = name;
  49        }
  50        fclose(in);
  51}
  52
  53static struct lock_file write_lock;
  54
  55static int write_rr(struct string_list *rr, int out_fd)
  56{
  57        int i;
  58        for (i = 0; i < rr->nr; i++) {
  59                const char *path;
  60                int length;
  61                if (!rr->items[i].util)
  62                        continue;
  63                path = rr->items[i].string;
  64                length = strlen(path) + 1;
  65                if (write_in_full(out_fd, rr->items[i].util, 40) != 40 ||
  66                    write_str_in_full(out_fd, "\t") != 1 ||
  67                    write_in_full(out_fd, path, length) != length)
  68                        die("unable to write rerere record");
  69        }
  70        if (commit_lock_file(&write_lock) != 0)
  71                die("unable to write rerere record");
  72        return 0;
  73}
  74
  75static void ferr_write(const void *p, size_t count, FILE *fp, int *err)
  76{
  77        if (!count || *err)
  78                return;
  79        if (fwrite(p, count, 1, fp) != 1)
  80                *err = errno;
  81}
  82
  83static inline void ferr_puts(const char *s, FILE *fp, int *err)
  84{
  85        ferr_write(s, strlen(s), fp, err);
  86}
  87
  88struct rerere_io {
  89        int (*getline)(struct strbuf *, struct rerere_io *);
  90        FILE *output;
  91        int wrerror;
  92        /* some more stuff */
  93};
  94
  95static void rerere_io_putstr(const char *str, struct rerere_io *io)
  96{
  97        if (io->output)
  98                ferr_puts(str, io->output, &io->wrerror);
  99}
 100
 101static void rerere_io_putconflict(int ch, int size, struct rerere_io *io)
 102{
 103        char buf[64];
 104
 105        while (size) {
 106                if (size < sizeof(buf) - 2) {
 107                        memset(buf, ch, size);
 108                        buf[size] = '\n';
 109                        buf[size + 1] = '\0';
 110                        size = 0;
 111                } else {
 112                        int sz = sizeof(buf) - 1;
 113                        if (size <= sz)
 114                                sz -= (sz - size) + 1;
 115                        memset(buf, ch, sz);
 116                        buf[sz] = '\0';
 117                        size -= sz;
 118                }
 119                rerere_io_putstr(buf, io);
 120        }
 121}
 122
 123static void rerere_io_putmem(const char *mem, size_t sz, struct rerere_io *io)
 124{
 125        if (io->output)
 126                ferr_write(mem, sz, io->output, &io->wrerror);
 127}
 128
 129struct rerere_io_file {
 130        struct rerere_io io;
 131        FILE *input;
 132};
 133
 134static int rerere_file_getline(struct strbuf *sb, struct rerere_io *io_)
 135{
 136        struct rerere_io_file *io = (struct rerere_io_file *)io_;
 137        return strbuf_getwholeline(sb, io->input, '\n');
 138}
 139
 140static int is_cmarker(char *buf, int marker_char, int marker_size, int want_sp)
 141{
 142        while (marker_size--)
 143                if (*buf++ != marker_char)
 144                        return 0;
 145        if (want_sp && *buf != ' ')
 146                return 0;
 147        return isspace(*buf);
 148}
 149
 150static int handle_path(unsigned char *sha1, struct rerere_io *io, int marker_size)
 151{
 152        git_SHA_CTX ctx;
 153        int hunk_no = 0;
 154        enum {
 155                RR_CONTEXT = 0, RR_SIDE_1, RR_SIDE_2, RR_ORIGINAL,
 156        } hunk = RR_CONTEXT;
 157        struct strbuf one = STRBUF_INIT, two = STRBUF_INIT;
 158        struct strbuf buf = STRBUF_INIT;
 159
 160        if (sha1)
 161                git_SHA1_Init(&ctx);
 162
 163        while (!io->getline(&buf, io)) {
 164                if (is_cmarker(buf.buf, '<', marker_size, 1)) {
 165                        if (hunk != RR_CONTEXT)
 166                                goto bad;
 167                        hunk = RR_SIDE_1;
 168                } else if (is_cmarker(buf.buf, '|', marker_size, 0)) {
 169                        if (hunk != RR_SIDE_1)
 170                                goto bad;
 171                        hunk = RR_ORIGINAL;
 172                } else if (is_cmarker(buf.buf, '=', marker_size, 0)) {
 173                        if (hunk != RR_SIDE_1 && hunk != RR_ORIGINAL)
 174                                goto bad;
 175                        hunk = RR_SIDE_2;
 176                } else if (is_cmarker(buf.buf, '>', marker_size, 1)) {
 177                        if (hunk != RR_SIDE_2)
 178                                goto bad;
 179                        if (strbuf_cmp(&one, &two) > 0)
 180                                strbuf_swap(&one, &two);
 181                        hunk_no++;
 182                        hunk = RR_CONTEXT;
 183                        rerere_io_putconflict('<', marker_size, io);
 184                        rerere_io_putmem(one.buf, one.len, io);
 185                        rerere_io_putconflict('=', marker_size, io);
 186                        rerere_io_putmem(two.buf, two.len, io);
 187                        rerere_io_putconflict('>', marker_size, io);
 188                        if (sha1) {
 189                                git_SHA1_Update(&ctx, one.buf ? one.buf : "",
 190                                            one.len + 1);
 191                                git_SHA1_Update(&ctx, two.buf ? two.buf : "",
 192                                            two.len + 1);
 193                        }
 194                        strbuf_reset(&one);
 195                        strbuf_reset(&two);
 196                } else if (hunk == RR_SIDE_1)
 197                        strbuf_addstr(&one, buf.buf);
 198                else if (hunk == RR_ORIGINAL)
 199                        ; /* discard */
 200                else if (hunk == RR_SIDE_2)
 201                        strbuf_addstr(&two, buf.buf);
 202                else
 203                        rerere_io_putstr(buf.buf, io);
 204                continue;
 205        bad:
 206                hunk = 99; /* force error exit */
 207                break;
 208        }
 209        strbuf_release(&one);
 210        strbuf_release(&two);
 211        strbuf_release(&buf);
 212
 213        if (sha1)
 214                git_SHA1_Final(sha1, &ctx);
 215        if (hunk != RR_CONTEXT)
 216                return -1;
 217        return hunk_no;
 218}
 219
 220static int handle_file(const char *path, unsigned char *sha1, const char *output)
 221{
 222        int hunk_no = 0;
 223        struct rerere_io_file io;
 224        int marker_size = 7;
 225
 226        memset(&io, 0, sizeof(io));
 227        io.io.getline = rerere_file_getline;
 228        io.input = fopen(path, "r");
 229        io.io.wrerror = 0;
 230        if (!io.input)
 231                return error("Could not open %s", path);
 232
 233        if (output) {
 234                io.io.output = fopen(output, "w");
 235                if (!io.io.output) {
 236                        fclose(io.input);
 237                        return error("Could not write %s", output);
 238                }
 239        }
 240
 241        hunk_no = handle_path(sha1, (struct rerere_io *)&io, marker_size);
 242
 243        fclose(io.input);
 244        if (io.io.wrerror)
 245                error("There were errors while writing %s (%s)",
 246                      path, strerror(io.io.wrerror));
 247        if (io.io.output && fclose(io.io.output))
 248                io.io.wrerror = error("Failed to flush %s: %s",
 249                                      path, strerror(errno));
 250
 251        if (hunk_no < 0) {
 252                if (output)
 253                        unlink_or_warn(output);
 254                return error("Could not parse conflict hunks in %s", path);
 255        }
 256        if (io.io.wrerror)
 257                return -1;
 258        return hunk_no;
 259}
 260
 261struct rerere_io_mem {
 262        struct rerere_io io;
 263        struct strbuf input;
 264};
 265
 266static int rerere_mem_getline(struct strbuf *sb, struct rerere_io *io_)
 267{
 268        struct rerere_io_mem *io = (struct rerere_io_mem *)io_;
 269        char *ep;
 270        size_t len;
 271
 272        strbuf_release(sb);
 273        if (!io->input.len)
 274                return -1;
 275        ep = strchrnul(io->input.buf, '\n');
 276        if (*ep == '\n')
 277                ep++;
 278        len = ep - io->input.buf;
 279        strbuf_add(sb, io->input.buf, len);
 280        strbuf_remove(&io->input, 0, len);
 281        return 0;
 282}
 283
 284static int handle_cache(const char *path, unsigned char *sha1, const char *output)
 285{
 286        mmfile_t mmfile[3];
 287        mmbuffer_t result = {NULL, 0};
 288        struct cache_entry *ce;
 289        int pos, len, i, hunk_no;
 290        struct rerere_io_mem io;
 291        int marker_size = 7;
 292
 293        /*
 294         * Reproduce the conflicted merge in-core
 295         */
 296        len = strlen(path);
 297        pos = cache_name_pos(path, len);
 298        if (0 <= pos)
 299                return -1;
 300        pos = -pos - 1;
 301
 302        for (i = 0; i < 3; i++) {
 303                enum object_type type;
 304                unsigned long size;
 305
 306                mmfile[i].size = 0;
 307                mmfile[i].ptr = NULL;
 308                if (active_nr <= pos)
 309                        break;
 310                ce = active_cache[pos++];
 311                if (ce_namelen(ce) != len || memcmp(ce->name, path, len)
 312                    || ce_stage(ce) != i + 1)
 313                        break;
 314                mmfile[i].ptr = read_sha1_file(ce->sha1, &type, &size);
 315                mmfile[i].size = size;
 316        }
 317        for (i = 0; i < 3; i++) {
 318                if (!mmfile[i].ptr && !mmfile[i].size)
 319                        mmfile[i].ptr = xstrdup("");
 320        }
 321        ll_merge(&result, path, &mmfile[0],
 322                 &mmfile[1], "ours",
 323                 &mmfile[2], "theirs", 0);
 324        for (i = 0; i < 3; i++)
 325                free(mmfile[i].ptr);
 326
 327        memset(&io, 0, sizeof(&io));
 328        io.io.getline = rerere_mem_getline;
 329        if (output)
 330                io.io.output = fopen(output, "w");
 331        else
 332                io.io.output = NULL;
 333        strbuf_init(&io.input, 0);
 334        strbuf_attach(&io.input, result.ptr, result.size, result.size);
 335
 336        hunk_no = handle_path(sha1, (struct rerere_io *)&io, marker_size);
 337        strbuf_release(&io.input);
 338        if (io.io.output)
 339                fclose(io.io.output);
 340        return hunk_no;
 341}
 342
 343static int find_conflict(struct string_list *conflict)
 344{
 345        int i;
 346        if (read_cache() < 0)
 347                return error("Could not read index");
 348        for (i = 0; i+1 < active_nr; i++) {
 349                struct cache_entry *e2 = active_cache[i];
 350                struct cache_entry *e3 = active_cache[i+1];
 351                if (ce_stage(e2) == 2 &&
 352                    ce_stage(e3) == 3 &&
 353                    ce_same_name(e2, e3) &&
 354                    S_ISREG(e2->ce_mode) &&
 355                    S_ISREG(e3->ce_mode)) {
 356                        string_list_insert((const char *)e2->name, conflict);
 357                        i++; /* skip over both #2 and #3 */
 358                }
 359        }
 360        return 0;
 361}
 362
 363static int merge(const char *name, const char *path)
 364{
 365        int ret;
 366        mmfile_t cur, base, other;
 367        mmbuffer_t result = {NULL, 0};
 368
 369        if (handle_file(path, NULL, rerere_path(name, "thisimage")) < 0)
 370                return 1;
 371
 372        if (read_mmfile(&cur, rerere_path(name, "thisimage")) ||
 373                        read_mmfile(&base, rerere_path(name, "preimage")) ||
 374                        read_mmfile(&other, rerere_path(name, "postimage")))
 375                return 1;
 376        ret = ll_merge(&result, path, &base, &cur, "", &other, "", 0);
 377        if (!ret) {
 378                FILE *f = fopen(path, "w");
 379                if (!f)
 380                        return error("Could not open %s: %s", path,
 381                                     strerror(errno));
 382                if (fwrite(result.ptr, result.size, 1, f) != 1)
 383                        error("Could not write %s: %s", path, strerror(errno));
 384                if (fclose(f))
 385                        return error("Writing %s failed: %s", path,
 386                                     strerror(errno));
 387        }
 388
 389        free(cur.ptr);
 390        free(base.ptr);
 391        free(other.ptr);
 392        free(result.ptr);
 393
 394        return ret;
 395}
 396
 397static struct lock_file index_lock;
 398
 399static int update_paths(struct string_list *update)
 400{
 401        int i;
 402        int fd = hold_locked_index(&index_lock, 0);
 403        int status = 0;
 404
 405        if (fd < 0)
 406                return -1;
 407
 408        for (i = 0; i < update->nr; i++) {
 409                struct string_list_item *item = &update->items[i];
 410                if (add_file_to_cache(item->string, ADD_CACHE_IGNORE_ERRORS))
 411                        status = -1;
 412        }
 413
 414        if (!status && active_cache_changed) {
 415                if (write_cache(fd, active_cache, active_nr) ||
 416                    commit_locked_index(&index_lock))
 417                        die("Unable to write new index file");
 418        } else if (fd >= 0)
 419                rollback_lock_file(&index_lock);
 420        return status;
 421}
 422
 423static int do_plain_rerere(struct string_list *rr, int fd)
 424{
 425        struct string_list conflict = { NULL, 0, 0, 1 };
 426        struct string_list update = { NULL, 0, 0, 1 };
 427        int i;
 428
 429        find_conflict(&conflict);
 430
 431        /*
 432         * MERGE_RR records paths with conflicts immediately after merge
 433         * failed.  Some of the conflicted paths might have been hand resolved
 434         * in the working tree since then, but the initial run would catch all
 435         * and register their preimages.
 436         */
 437
 438        for (i = 0; i < conflict.nr; i++) {
 439                const char *path = conflict.items[i].string;
 440                if (!string_list_has_string(rr, path)) {
 441                        unsigned char sha1[20];
 442                        char *hex;
 443                        int ret;
 444                        ret = handle_file(path, sha1, NULL);
 445                        if (ret < 1)
 446                                continue;
 447                        hex = xstrdup(sha1_to_hex(sha1));
 448                        string_list_insert(path, rr)->util = hex;
 449                        if (mkdir(git_path("rr-cache/%s", hex), 0755))
 450                                continue;
 451                        handle_file(path, NULL, rerere_path(hex, "preimage"));
 452                        fprintf(stderr, "Recorded preimage for '%s'\n", path);
 453                }
 454        }
 455
 456        /*
 457         * Now some of the paths that had conflicts earlier might have been
 458         * hand resolved.  Others may be similar to a conflict already that
 459         * was resolved before.
 460         */
 461
 462        for (i = 0; i < rr->nr; i++) {
 463                int ret;
 464                const char *path = rr->items[i].string;
 465                const char *name = (const char *)rr->items[i].util;
 466
 467                if (has_rerere_resolution(name)) {
 468                        if (!merge(name, path)) {
 469                                if (rerere_autoupdate)
 470                                        string_list_insert(path, &update);
 471                                fprintf(stderr,
 472                                        "%s '%s' using previous resolution.\n",
 473                                        rerere_autoupdate
 474                                        ? "Staged" : "Resolved",
 475                                        path);
 476                                goto mark_resolved;
 477                        }
 478                }
 479
 480                /* Let's see if we have resolved it. */
 481                ret = handle_file(path, NULL, NULL);
 482                if (ret)
 483                        continue;
 484
 485                fprintf(stderr, "Recorded resolution for '%s'.\n", path);
 486                copy_file(rerere_path(name, "postimage"), path, 0666);
 487        mark_resolved:
 488                rr->items[i].util = NULL;
 489        }
 490
 491        if (update.nr)
 492                update_paths(&update);
 493
 494        return write_rr(rr, fd);
 495}
 496
 497static int git_rerere_config(const char *var, const char *value, void *cb)
 498{
 499        if (!strcmp(var, "rerere.enabled"))
 500                rerere_enabled = git_config_bool(var, value);
 501        else if (!strcmp(var, "rerere.autoupdate"))
 502                rerere_autoupdate = git_config_bool(var, value);
 503        else
 504                return git_default_config(var, value, cb);
 505        return 0;
 506}
 507
 508static int is_rerere_enabled(void)
 509{
 510        const char *rr_cache;
 511        int rr_cache_exists;
 512
 513        if (!rerere_enabled)
 514                return 0;
 515
 516        rr_cache = git_path("rr-cache");
 517        rr_cache_exists = is_directory(rr_cache);
 518        if (rerere_enabled < 0)
 519                return rr_cache_exists;
 520
 521        if (!rr_cache_exists &&
 522            (mkdir(rr_cache, 0777) || adjust_shared_perm(rr_cache)))
 523                die("Could not create directory %s", rr_cache);
 524        return 1;
 525}
 526
 527int setup_rerere(struct string_list *merge_rr)
 528{
 529        int fd;
 530
 531        git_config(git_rerere_config, NULL);
 532        if (!is_rerere_enabled())
 533                return -1;
 534
 535        merge_rr_path = git_pathdup("MERGE_RR");
 536        fd = hold_lock_file_for_update(&write_lock, merge_rr_path,
 537                                       LOCK_DIE_ON_ERROR);
 538        read_rr(merge_rr);
 539        return fd;
 540}
 541
 542int rerere(void)
 543{
 544        struct string_list merge_rr = { NULL, 0, 0, 1 };
 545        int fd;
 546
 547        fd = setup_rerere(&merge_rr);
 548        if (fd < 0)
 549                return 0;
 550        return do_plain_rerere(&merge_rr, fd);
 551}
 552
 553static int rerere_forget_one_path(const char *path, struct string_list *rr)
 554{
 555        const char *filename;
 556        char *hex;
 557        unsigned char sha1[20];
 558        int ret;
 559
 560        ret = handle_cache(path, sha1, NULL);
 561        if (ret < 1)
 562                return error("Could not parse conflict hunks in '%s'", path);
 563        hex = xstrdup(sha1_to_hex(sha1));
 564        filename = rerere_path(hex, "postimage");
 565        if (unlink(filename))
 566                return (errno == ENOENT
 567                        ? error("no remembered resolution for %s", path)
 568                        : error("cannot unlink %s: %s", filename, strerror(errno)));
 569
 570        handle_cache(path, sha1, rerere_path(hex, "preimage"));
 571        fprintf(stderr, "Updated preimage for '%s'\n", path);
 572
 573
 574        string_list_insert(path, rr)->util = hex;
 575        fprintf(stderr, "Forgot resolution for %s\n", path);
 576        return 0;
 577}
 578
 579int rerere_forget(const char **pathspec)
 580{
 581        int i, fd;
 582        struct string_list conflict = { NULL, 0, 0, 1 };
 583        struct string_list merge_rr = { NULL, 0, 0, 1 };
 584
 585        if (read_cache() < 0)
 586                return error("Could not read index");
 587
 588        fd = setup_rerere(&merge_rr);
 589
 590        unmerge_cache(pathspec);
 591        find_conflict(&conflict);
 592        for (i = 0; i < conflict.nr; i++) {
 593                struct string_list_item *it = &conflict.items[i];
 594                if (!match_pathspec(pathspec, it->string, strlen(it->string),
 595                                    0, NULL))
 596                        continue;
 597                rerere_forget_one_path(it->string, &merge_rr);
 598        }
 599        return write_rr(&merge_rr, fd);
 600}