builtin-rerere.con commit Merge branch 'master' into ph/strbuf (39bd2eb)
   1#include "builtin.h"
   2#include "cache.h"
   3#include "path-list.h"
   4#include "xdiff/xdiff.h"
   5#include "xdiff-interface.h"
   6
   7#include <time.h>
   8
   9static const char git_rerere_usage[] =
  10"git-rerere [clear | status | diff | gc]";
  11
  12/* these values are days */
  13static int cutoff_noresolve = 15;
  14static int cutoff_resolve = 60;
  15
  16/* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
  17static int rerere_enabled = -1;
  18
  19static char *merge_rr_path;
  20
  21static const char *rr_path(const char *name, const char *file)
  22{
  23        return git_path("rr-cache/%s/%s", name, file);
  24}
  25
  26static void read_rr(struct path_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                path_list_insert(buf, rr)->util = xstrdup(name);
  47        }
  48        fclose(in);
  49}
  50
  51static struct lock_file write_lock;
  52
  53static int write_rr(struct path_list *rr, int out_fd)
  54{
  55        int i;
  56        for (i = 0; i < rr->nr; i++) {
  57                const char *path = rr->items[i].path;
  58                int length = strlen(path) + 1;
  59                if (write_in_full(out_fd, rr->items[i].util, 40) != 40 ||
  60                    write_in_full(out_fd, "\t", 1) != 1 ||
  61                    write_in_full(out_fd, path, length) != length)
  62                        die("unable to write rerere record");
  63        }
  64        if (close(out_fd) != 0)
  65                die("unable to write rerere record");
  66        return commit_lock_file(&write_lock);
  67}
  68
  69static int handle_file(const char *path,
  70         unsigned char *sha1, const char *output)
  71{
  72        SHA_CTX ctx;
  73        char buf[1024];
  74        int hunk = 0, hunk_no = 0;
  75        struct strbuf minus, plus;
  76        struct strbuf *one = &minus, *two = &plus;
  77        FILE *f = fopen(path, "r");
  78        FILE *out;
  79
  80        strbuf_init(&minus, 0);
  81        strbuf_init(&plus,  0);
  82
  83        if (!f)
  84                return error("Could not open %s", path);
  85
  86        if (output) {
  87                out = fopen(output, "w");
  88                if (!out) {
  89                        fclose(f);
  90                        return error("Could not write %s", output);
  91                }
  92        } else
  93                out = NULL;
  94
  95        if (sha1)
  96                SHA1_Init(&ctx);
  97
  98        while (fgets(buf, sizeof(buf), f)) {
  99                if (!prefixcmp(buf, "<<<<<<< "))
 100                        hunk = 1;
 101                else if (!prefixcmp(buf, "======="))
 102                        hunk = 2;
 103                else if (!prefixcmp(buf, ">>>>>>> ")) {
 104                        int one_is_longer = (one->len > two->len);
 105                        int common_len = one_is_longer ? two->len : one->len;
 106                        int cmp = memcmp(one->buf, two->buf, common_len);
 107
 108                        hunk_no++;
 109                        hunk = 0;
 110                        if ((cmp > 0) || ((cmp == 0) && one_is_longer)) {
 111                                struct strbuf *swap = one;
 112                                one = two;
 113                                two = swap;
 114                        }
 115                        if (out) {
 116                                fputs("<<<<<<<\n", out);
 117                                fwrite(one->buf, one->len, 1, out);
 118                                fputs("=======\n", out);
 119                                fwrite(two->buf, two->len, 1, out);
 120                                fputs(">>>>>>>\n", out);
 121                        }
 122                        if (sha1) {
 123                                SHA1_Update(&ctx, one->buf, one->len);
 124                                SHA1_Update(&ctx, "\0", 1);
 125                                SHA1_Update(&ctx, two->buf, two->len);
 126                                SHA1_Update(&ctx, "\0", 1);
 127                        }
 128                        strbuf_release(one);
 129                        strbuf_release(two);
 130                } else if (hunk == 1)
 131                        strbuf_addstr(one, buf);
 132                else if (hunk == 2)
 133                        strbuf_addstr(two, buf);
 134                else if (out)
 135                        fputs(buf, out);
 136        }
 137
 138        fclose(f);
 139        if (out)
 140                fclose(out);
 141        if (sha1)
 142                SHA1_Final(sha1, &ctx);
 143        return hunk_no;
 144}
 145
 146static int find_conflict(struct path_list *conflict)
 147{
 148        int i;
 149        if (read_cache() < 0)
 150                return error("Could not read index");
 151        for (i = 0; i+1 < active_nr; i++) {
 152                struct cache_entry *e2 = active_cache[i];
 153                struct cache_entry *e3 = active_cache[i+1];
 154                if (ce_stage(e2) == 2 &&
 155                    ce_stage(e3) == 3 &&
 156                    ce_same_name(e2, e3) &&
 157                    S_ISREG(ntohl(e2->ce_mode)) &&
 158                    S_ISREG(ntohl(e3->ce_mode))) {
 159                        path_list_insert((const char *)e2->name, conflict);
 160                        i++; /* skip over both #2 and #3 */
 161                }
 162        }
 163        return 0;
 164}
 165
 166static int merge(const char *name, const char *path)
 167{
 168        int ret;
 169        mmfile_t cur, base, other;
 170        mmbuffer_t result = {NULL, 0};
 171        xpparam_t xpp = {XDF_NEED_MINIMAL};
 172
 173        if (handle_file(path, NULL, rr_path(name, "thisimage")) < 0)
 174                return 1;
 175
 176        if (read_mmfile(&cur, rr_path(name, "thisimage")) ||
 177                        read_mmfile(&base, rr_path(name, "preimage")) ||
 178                        read_mmfile(&other, rr_path(name, "postimage")))
 179                return 1;
 180        ret = xdl_merge(&base, &cur, "", &other, "",
 181                        &xpp, XDL_MERGE_ZEALOUS, &result);
 182        if (!ret) {
 183                FILE *f = fopen(path, "w");
 184                if (!f)
 185                        return error("Could not write to %s", path);
 186                fwrite(result.ptr, result.size, 1, f);
 187                fclose(f);
 188        }
 189
 190        free(cur.ptr);
 191        free(base.ptr);
 192        free(other.ptr);
 193        free(result.ptr);
 194
 195        return ret;
 196}
 197
 198static void unlink_rr_item(const char *name)
 199{
 200        unlink(rr_path(name, "thisimage"));
 201        unlink(rr_path(name, "preimage"));
 202        unlink(rr_path(name, "postimage"));
 203        rmdir(git_path("rr-cache/%s", name));
 204}
 205
 206static void garbage_collect(struct path_list *rr)
 207{
 208        struct path_list to_remove = { NULL, 0, 0, 1 };
 209        char buf[1024];
 210        DIR *dir;
 211        struct dirent *e;
 212        int len, i, cutoff;
 213        time_t now = time(NULL), then;
 214
 215        strlcpy(buf, git_path("rr-cache"), sizeof(buf));
 216        len = strlen(buf);
 217        dir = opendir(buf);
 218        strcpy(buf + len++, "/");
 219        while ((e = readdir(dir))) {
 220                const char *name = e->d_name;
 221                struct stat st;
 222                if (name[0] == '.' && (name[1] == '\0' ||
 223                                        (name[1] == '.' && name[2] == '\0')))
 224                        continue;
 225                i = snprintf(buf + len, sizeof(buf) - len, "%s", name);
 226                strlcpy(buf + len + i, "/preimage", sizeof(buf) - len - i);
 227                if (stat(buf, &st))
 228                        continue;
 229                then = st.st_mtime;
 230                strlcpy(buf + len + i, "/postimage", sizeof(buf) - len - i);
 231                cutoff = stat(buf, &st) ? cutoff_noresolve : cutoff_resolve;
 232                if (then < now - cutoff * 86400) {
 233                        buf[len + i] = '\0';
 234                        path_list_insert(xstrdup(name), &to_remove);
 235                }
 236        }
 237        for (i = 0; i < to_remove.nr; i++)
 238                unlink_rr_item(to_remove.items[i].path);
 239        path_list_clear(&to_remove, 0);
 240}
 241
 242static int outf(void *dummy, mmbuffer_t *ptr, int nbuf)
 243{
 244        int i;
 245        for (i = 0; i < nbuf; i++)
 246                if (write_in_full(1, ptr[i].ptr, ptr[i].size) != ptr[i].size)
 247                        return -1;
 248        return 0;
 249}
 250
 251static int diff_two(const char *file1, const char *label1,
 252                const char *file2, const char *label2)
 253{
 254        xpparam_t xpp;
 255        xdemitconf_t xecfg;
 256        xdemitcb_t ecb;
 257        mmfile_t minus, plus;
 258
 259        if (read_mmfile(&minus, file1) || read_mmfile(&plus, file2))
 260                return 1;
 261
 262        printf("--- a/%s\n+++ b/%s\n", label1, label2);
 263        fflush(stdout);
 264        xpp.flags = XDF_NEED_MINIMAL;
 265        memset(&xecfg, 0, sizeof(xecfg));
 266        xecfg.ctxlen = 3;
 267        ecb.outf = outf;
 268        xdl_diff(&minus, &plus, &xpp, &xecfg, &ecb);
 269
 270        free(minus.ptr);
 271        free(plus.ptr);
 272        return 0;
 273}
 274
 275static int copy_file(const char *src, const char *dest)
 276{
 277        FILE *in, *out;
 278        char buffer[32768];
 279        int count;
 280
 281        if (!(in = fopen(src, "r")))
 282                return error("Could not open %s", src);
 283        if (!(out = fopen(dest, "w")))
 284                return error("Could not open %s", dest);
 285        while ((count = fread(buffer, 1, sizeof(buffer), in)))
 286                fwrite(buffer, 1, count, out);
 287        fclose(in);
 288        fclose(out);
 289        return 0;
 290}
 291
 292static int do_plain_rerere(struct path_list *rr, int fd)
 293{
 294        struct path_list conflict = { NULL, 0, 0, 1 };
 295        int i;
 296
 297        find_conflict(&conflict);
 298
 299        /*
 300         * MERGE_RR records paths with conflicts immediately after merge
 301         * failed.  Some of the conflicted paths might have been hand resolved
 302         * in the working tree since then, but the initial run would catch all
 303         * and register their preimages.
 304         */
 305
 306        for (i = 0; i < conflict.nr; i++) {
 307                const char *path = conflict.items[i].path;
 308                if (!path_list_has_path(rr, path)) {
 309                        unsigned char sha1[20];
 310                        char *hex;
 311                        int ret;
 312                        ret = handle_file(path, sha1, NULL);
 313                        if (ret < 1)
 314                                continue;
 315                        hex = xstrdup(sha1_to_hex(sha1));
 316                        path_list_insert(path, rr)->util = hex;
 317                        if (mkdir(git_path("rr-cache/%s", hex), 0755))
 318                                continue;;
 319                        handle_file(path, NULL, rr_path(hex, "preimage"));
 320                        fprintf(stderr, "Recorded preimage for '%s'\n", path);
 321                }
 322        }
 323
 324        /*
 325         * Now some of the paths that had conflicts earlier might have been
 326         * hand resolved.  Others may be similar to a conflict already that
 327         * was resolved before.
 328         */
 329
 330        for (i = 0; i < rr->nr; i++) {
 331                struct stat st;
 332                int ret;
 333                const char *path = rr->items[i].path;
 334                const char *name = (const char *)rr->items[i].util;
 335
 336                if (!stat(rr_path(name, "preimage"), &st) &&
 337                                !stat(rr_path(name, "postimage"), &st)) {
 338                        if (!merge(name, path)) {
 339                                fprintf(stderr, "Resolved '%s' using "
 340                                                "previous resolution.\n", path);
 341                                goto tail_optimization;
 342                        }
 343                }
 344
 345                /* Let's see if we have resolved it. */
 346                ret = handle_file(path, NULL, NULL);
 347                if (ret)
 348                        continue;
 349
 350                fprintf(stderr, "Recorded resolution for '%s'.\n", path);
 351                copy_file(path, rr_path(name, "postimage"));
 352tail_optimization:
 353                if (i < rr->nr - 1)
 354                        memmove(rr->items + i,
 355                                rr->items + i + 1,
 356                                sizeof(rr->items[0]) * (rr->nr - i - 1));
 357                rr->nr--;
 358                i--;
 359        }
 360
 361        return write_rr(rr, fd);
 362}
 363
 364static int git_rerere_config(const char *var, const char *value)
 365{
 366        if (!strcmp(var, "gc.rerereresolved"))
 367                cutoff_resolve = git_config_int(var, value);
 368        else if (!strcmp(var, "gc.rerereunresolved"))
 369                cutoff_noresolve = git_config_int(var, value);
 370        else if (!strcmp(var, "rerere.enabled"))
 371                rerere_enabled = git_config_bool(var, value);
 372        else
 373                return git_default_config(var, value);
 374        return 0;
 375}
 376
 377static int is_rerere_enabled(void)
 378{
 379        struct stat st;
 380        const char *rr_cache;
 381        int rr_cache_exists;
 382
 383        if (!rerere_enabled)
 384                return 0;
 385
 386        rr_cache = git_path("rr-cache");
 387        rr_cache_exists = !stat(rr_cache, &st) && S_ISDIR(st.st_mode);
 388        if (rerere_enabled < 0)
 389                return rr_cache_exists;
 390
 391        if (!rr_cache_exists &&
 392            (mkdir(rr_cache, 0777) || adjust_shared_perm(rr_cache)))
 393                die("Could not create directory %s", rr_cache);
 394        return 1;
 395}
 396
 397int cmd_rerere(int argc, const char **argv, const char *prefix)
 398{
 399        struct path_list merge_rr = { NULL, 0, 0, 1 };
 400        int i, fd = -1;
 401
 402        git_config(git_rerere_config);
 403        if (!is_rerere_enabled())
 404                return 0;
 405
 406        merge_rr_path = xstrdup(git_path("rr-cache/MERGE_RR"));
 407        fd = hold_lock_file_for_update(&write_lock, merge_rr_path, 1);
 408        read_rr(&merge_rr);
 409
 410        if (argc < 2)
 411                return do_plain_rerere(&merge_rr, fd);
 412        else if (!strcmp(argv[1], "clear")) {
 413                for (i = 0; i < merge_rr.nr; i++) {
 414                        struct stat st;
 415                        const char *name = (const char *)merge_rr.items[i].util;
 416                        if (!stat(git_path("rr-cache/%s", name), &st) &&
 417                                        S_ISDIR(st.st_mode) &&
 418                                        stat(rr_path(name, "postimage"), &st))
 419                                unlink_rr_item(name);
 420                }
 421                unlink(merge_rr_path);
 422        } else if (!strcmp(argv[1], "gc"))
 423                garbage_collect(&merge_rr);
 424        else if (!strcmp(argv[1], "status"))
 425                for (i = 0; i < merge_rr.nr; i++)
 426                        printf("%s\n", merge_rr.items[i].path);
 427        else if (!strcmp(argv[1], "diff"))
 428                for (i = 0; i < merge_rr.nr; i++) {
 429                        const char *path = merge_rr.items[i].path;
 430                        const char *name = (const char *)merge_rr.items[i].util;
 431                        diff_two(rr_path(name, "preimage"), path, path, path);
 432                }
 433        else
 434                usage(git_rerere_usage);
 435
 436        path_list_clear(&merge_rr, 1);
 437        return 0;
 438}