bf74b262f0cc232ba118d34dd7676ce67d3a1d9e
   1#include "cache.h"
   2#include "string-list.h"
   3#include "rerere.h"
   4#include "xdiff/xdiff.h"
   5#include "xdiff-interface.h"
   6
   7/* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
   8static int rerere_enabled = -1;
   9
  10/* automatically update cleanly resolved paths to the index */
  11static int rerere_autoupdate;
  12
  13static char *merge_rr_path;
  14
  15static const char *rr_path(const char *name, const char *file)
  16{
  17        return git_path("rr-cache/%s/%s", name, file);
  18}
  19
  20static int has_resolution(const char *name)
  21{
  22        struct stat st;
  23        return !stat(rr_path(name, "postimage"), &st);
  24}
  25
  26static void read_rr(struct string_list *rr)
  27{
  28        unsigned char sha1[20];
  29        char buf[PATH_MAX];
  30        FILE *in = fopen(merge_rr_path, "r");
  31        if (!in)
  32                return;
  33        while (fread(buf, 40, 1, in) == 1) {
  34                int i;
  35                char *name;
  36                if (get_sha1_hex(buf, sha1))
  37                        die("corrupt MERGE_RR");
  38                buf[40] = '\0';
  39                name = xstrdup(buf);
  40                if (fgetc(in) != '\t')
  41                        die("corrupt MERGE_RR");
  42                for (i = 0; i < sizeof(buf) && (buf[i] = fgetc(in)); i++)
  43                        ; /* do nothing */
  44                if (i == sizeof(buf))
  45                        die("filename too long");
  46                string_list_insert(buf, rr)->util = name;
  47        }
  48        fclose(in);
  49}
  50
  51static struct lock_file write_lock;
  52
  53static int write_rr(struct string_list *rr, int out_fd)
  54{
  55        int i;
  56        for (i = 0; i < rr->nr; i++) {
  57                const char *path;
  58                int length;
  59                if (!rr->items[i].util)
  60                        continue;
  61                path = rr->items[i].string;
  62                length = strlen(path) + 1;
  63                if (write_in_full(out_fd, rr->items[i].util, 40) != 40 ||
  64                    write_in_full(out_fd, "\t", 1) != 1 ||
  65                    write_in_full(out_fd, path, length) != length)
  66                        die("unable to write rerere record");
  67        }
  68        if (commit_lock_file(&write_lock) != 0)
  69                die("unable to write rerere record");
  70        return 0;
  71}
  72
  73static int handle_file(const char *path,
  74         unsigned char *sha1, const char *output)
  75{
  76        SHA_CTX ctx;
  77        char buf[1024];
  78        int hunk_no = 0;
  79        enum {
  80                RR_CONTEXT = 0, RR_SIDE_1, RR_SIDE_2,
  81        } hunk = RR_CONTEXT;
  82        struct strbuf one, two;
  83        FILE *f = fopen(path, "r");
  84        FILE *out = NULL;
  85
  86        if (!f)
  87                return error("Could not open %s", path);
  88
  89        if (output) {
  90                out = fopen(output, "w");
  91                if (!out) {
  92                        fclose(f);
  93                        return error("Could not write %s", output);
  94                }
  95        }
  96
  97        if (sha1)
  98                SHA1_Init(&ctx);
  99
 100        strbuf_init(&one, 0);
 101        strbuf_init(&two,  0);
 102        while (fgets(buf, sizeof(buf), f)) {
 103                if (!prefixcmp(buf, "<<<<<<< ")) {
 104                        if (hunk != RR_CONTEXT)
 105                                goto bad;
 106                        hunk = RR_SIDE_1;
 107                } else if (!prefixcmp(buf, "=======") && isspace(buf[7])) {
 108                        if (hunk != RR_SIDE_1)
 109                                goto bad;
 110                        hunk = RR_SIDE_2;
 111                } else if (!prefixcmp(buf, ">>>>>>> ")) {
 112                        if (hunk != RR_SIDE_2)
 113                                goto bad;
 114                        if (strbuf_cmp(&one, &two) > 0)
 115                                strbuf_swap(&one, &two);
 116                        hunk_no++;
 117                        hunk = RR_CONTEXT;
 118                        if (out) {
 119                                fputs("<<<<<<<\n", out);
 120                                fwrite(one.buf, one.len, 1, out);
 121                                fputs("=======\n", out);
 122                                fwrite(two.buf, two.len, 1, out);
 123                                fputs(">>>>>>>\n", out);
 124                        }
 125                        if (sha1) {
 126                                SHA1_Update(&ctx, one.buf ? one.buf : "",
 127                                            one.len + 1);
 128                                SHA1_Update(&ctx, two.buf ? two.buf : "",
 129                                            two.len + 1);
 130                        }
 131                        strbuf_reset(&one);
 132                        strbuf_reset(&two);
 133                } else if (hunk == RR_SIDE_1)
 134                        strbuf_addstr(&one, buf);
 135                else if (hunk == RR_SIDE_2)
 136                        strbuf_addstr(&two, buf);
 137                else if (out)
 138                        fputs(buf, out);
 139                continue;
 140        bad:
 141                hunk = 99; /* force error exit */
 142                break;
 143        }
 144        strbuf_release(&one);
 145        strbuf_release(&two);
 146
 147        fclose(f);
 148        if (out)
 149                fclose(out);
 150        if (sha1)
 151                SHA1_Final(sha1, &ctx);
 152        if (hunk != RR_CONTEXT) {
 153                if (output)
 154                        unlink(output);
 155                return error("Could not parse conflict hunks in %s", path);
 156        }
 157        return hunk_no;
 158}
 159
 160static int find_conflict(struct string_list *conflict)
 161{
 162        int i;
 163        if (read_cache() < 0)
 164                return error("Could not read index");
 165        for (i = 0; i+1 < active_nr; i++) {
 166                struct cache_entry *e2 = active_cache[i];
 167                struct cache_entry *e3 = active_cache[i+1];
 168                if (ce_stage(e2) == 2 &&
 169                    ce_stage(e3) == 3 &&
 170                    ce_same_name(e2, e3) &&
 171                    S_ISREG(e2->ce_mode) &&
 172                    S_ISREG(e3->ce_mode)) {
 173                        string_list_insert((const char *)e2->name, conflict);
 174                        i++; /* skip over both #2 and #3 */
 175                }
 176        }
 177        return 0;
 178}
 179
 180static int merge(const char *name, const char *path)
 181{
 182        int ret;
 183        mmfile_t cur, base, other;
 184        mmbuffer_t result = {NULL, 0};
 185        xpparam_t xpp = {XDF_NEED_MINIMAL};
 186
 187        if (handle_file(path, NULL, rr_path(name, "thisimage")) < 0)
 188                return 1;
 189
 190        if (read_mmfile(&cur, rr_path(name, "thisimage")) ||
 191                        read_mmfile(&base, rr_path(name, "preimage")) ||
 192                        read_mmfile(&other, rr_path(name, "postimage")))
 193                return 1;
 194        ret = xdl_merge(&base, &cur, "", &other, "",
 195                        &xpp, XDL_MERGE_ZEALOUS, &result);
 196        if (!ret) {
 197                FILE *f = fopen(path, "w");
 198                if (!f)
 199                        return error("Could not write to %s", path);
 200                fwrite(result.ptr, result.size, 1, f);
 201                fclose(f);
 202        }
 203
 204        free(cur.ptr);
 205        free(base.ptr);
 206        free(other.ptr);
 207        free(result.ptr);
 208
 209        return ret;
 210}
 211
 212static struct lock_file index_lock;
 213
 214static int update_paths(struct string_list *update)
 215{
 216        int i;
 217        int fd = hold_locked_index(&index_lock, 0);
 218        int status = 0;
 219
 220        if (fd < 0)
 221                return -1;
 222
 223        for (i = 0; i < update->nr; i++) {
 224                struct string_list_item *item = &update->items[i];
 225                if (add_file_to_cache(item->string, ADD_CACHE_IGNORE_ERRORS))
 226                        status = -1;
 227        }
 228
 229        if (!status && active_cache_changed) {
 230                if (write_cache(fd, active_cache, active_nr) ||
 231                    commit_locked_index(&index_lock))
 232                        die("Unable to write new index file");
 233        } else if (fd >= 0)
 234                rollback_lock_file(&index_lock);
 235        return status;
 236}
 237
 238static int do_plain_rerere(struct string_list *rr, int fd)
 239{
 240        struct string_list conflict = { NULL, 0, 0, 1 };
 241        struct string_list update = { NULL, 0, 0, 1 };
 242        int i;
 243
 244        find_conflict(&conflict);
 245
 246        /*
 247         * MERGE_RR records paths with conflicts immediately after merge
 248         * failed.  Some of the conflicted paths might have been hand resolved
 249         * in the working tree since then, but the initial run would catch all
 250         * and register their preimages.
 251         */
 252
 253        for (i = 0; i < conflict.nr; i++) {
 254                const char *path = conflict.items[i].string;
 255                if (!string_list_has_string(rr, path)) {
 256                        unsigned char sha1[20];
 257                        char *hex;
 258                        int ret;
 259                        ret = handle_file(path, sha1, NULL);
 260                        if (ret < 1)
 261                                continue;
 262                        hex = xstrdup(sha1_to_hex(sha1));
 263                        string_list_insert(path, rr)->util = hex;
 264                        if (mkdir(git_path("rr-cache/%s", hex), 0755))
 265                                continue;;
 266                        handle_file(path, NULL, rr_path(hex, "preimage"));
 267                        fprintf(stderr, "Recorded preimage for '%s'\n", path);
 268                }
 269        }
 270
 271        /*
 272         * Now some of the paths that had conflicts earlier might have been
 273         * hand resolved.  Others may be similar to a conflict already that
 274         * was resolved before.
 275         */
 276
 277        for (i = 0; i < rr->nr; i++) {
 278                int ret;
 279                const char *path = rr->items[i].string;
 280                const char *name = (const char *)rr->items[i].util;
 281
 282                if (has_resolution(name)) {
 283                        if (!merge(name, path)) {
 284                                if (rerere_autoupdate)
 285                                        string_list_insert(path, &update);
 286                                fprintf(stderr,
 287                                        "%s '%s' using previous resolution.\n",
 288                                        rerere_autoupdate
 289                                        ? "Staged" : "Resolved",
 290                                        path);
 291                                goto mark_resolved;
 292                        }
 293                }
 294
 295                /* Let's see if we have resolved it. */
 296                ret = handle_file(path, NULL, NULL);
 297                if (ret)
 298                        continue;
 299
 300                fprintf(stderr, "Recorded resolution for '%s'.\n", path);
 301                copy_file(rr_path(name, "postimage"), path, 0666);
 302        mark_resolved:
 303                rr->items[i].util = NULL;
 304        }
 305
 306        if (update.nr)
 307                update_paths(&update);
 308
 309        return write_rr(rr, fd);
 310}
 311
 312static int git_rerere_config(const char *var, const char *value, void *cb)
 313{
 314        if (!strcmp(var, "rerere.enabled"))
 315                rerere_enabled = git_config_bool(var, value);
 316        else if (!strcmp(var, "rerere.autoupdate"))
 317                rerere_autoupdate = git_config_bool(var, value);
 318        else
 319                return git_default_config(var, value, cb);
 320        return 0;
 321}
 322
 323static int is_rerere_enabled(void)
 324{
 325        struct stat st;
 326        const char *rr_cache;
 327        int rr_cache_exists;
 328
 329        if (!rerere_enabled)
 330                return 0;
 331
 332        rr_cache = git_path("rr-cache");
 333        rr_cache_exists = !stat(rr_cache, &st) && S_ISDIR(st.st_mode);
 334        if (rerere_enabled < 0)
 335                return rr_cache_exists;
 336
 337        if (!rr_cache_exists &&
 338            (mkdir(rr_cache, 0777) || adjust_shared_perm(rr_cache)))
 339                die("Could not create directory %s", rr_cache);
 340        return 1;
 341}
 342
 343int setup_rerere(struct string_list *merge_rr)
 344{
 345        int fd;
 346
 347        git_config(git_rerere_config, NULL);
 348        if (!is_rerere_enabled())
 349                return -1;
 350
 351        merge_rr_path = xstrdup(git_path("MERGE_RR"));
 352        fd = hold_lock_file_for_update(&write_lock, merge_rr_path, 1);
 353        read_rr(merge_rr);
 354        return fd;
 355}
 356
 357int rerere(void)
 358{
 359        struct string_list merge_rr = { NULL, 0, 0, 1 };
 360        int fd;
 361
 362        fd = setup_rerere(&merge_rr);
 363        if (fd < 0)
 364                return 0;
 365        return do_plain_rerere(&merge_rr, fd);
 366}