builtin / rerere.con commit Merge branch 'jn/paginate-fix' (633142d)
   1#include "builtin.h"
   2#include "cache.h"
   3#include "dir.h"
   4#include "string-list.h"
   5#include "rerere.h"
   6#include "xdiff/xdiff.h"
   7#include "xdiff-interface.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
  16static time_t rerere_created_at(const char *name)
  17{
  18        struct stat st;
  19        return stat(rerere_path(name, "preimage"), &st) ? (time_t) 0 : st.st_mtime;
  20}
  21
  22static time_t rerere_last_used_at(const char *name)
  23{
  24        struct stat st;
  25        return stat(rerere_path(name, "postimage"), &st) ? (time_t) 0 : st.st_mtime;
  26}
  27
  28static void unlink_rr_item(const char *name)
  29{
  30        unlink(rerere_path(name, "thisimage"));
  31        unlink(rerere_path(name, "preimage"));
  32        unlink(rerere_path(name, "postimage"));
  33        rmdir(git_path("rr-cache/%s", name));
  34}
  35
  36static int git_rerere_gc_config(const char *var, const char *value, void *cb)
  37{
  38        if (!strcmp(var, "gc.rerereresolved"))
  39                cutoff_resolve = git_config_int(var, value);
  40        else if (!strcmp(var, "gc.rerereunresolved"))
  41                cutoff_noresolve = git_config_int(var, value);
  42        else
  43                return git_default_config(var, value, cb);
  44        return 0;
  45}
  46
  47static void garbage_collect(struct string_list *rr)
  48{
  49        struct string_list to_remove = STRING_LIST_INIT_DUP;
  50        DIR *dir;
  51        struct dirent *e;
  52        int i, cutoff;
  53        time_t now = time(NULL), then;
  54
  55        git_config(git_rerere_gc_config, NULL);
  56        dir = opendir(git_path("rr-cache"));
  57        if (!dir)
  58                die_errno("unable to open rr-cache directory");
  59        while ((e = readdir(dir))) {
  60                if (is_dot_or_dotdot(e->d_name))
  61                        continue;
  62
  63                then = rerere_last_used_at(e->d_name);
  64                if (then) {
  65                        cutoff = cutoff_resolve;
  66                } else {
  67                        then = rerere_created_at(e->d_name);
  68                        if (!then)
  69                                continue;
  70                        cutoff = cutoff_noresolve;
  71                }
  72                if (then < now - cutoff * 86400)
  73                        string_list_append(&to_remove, e->d_name);
  74        }
  75        for (i = 0; i < to_remove.nr; i++)
  76                unlink_rr_item(to_remove.items[i].string);
  77        string_list_clear(&to_remove, 0);
  78}
  79
  80static int outf(void *dummy, mmbuffer_t *ptr, int nbuf)
  81{
  82        int i;
  83        for (i = 0; i < nbuf; i++)
  84                if (write_in_full(1, ptr[i].ptr, ptr[i].size) != ptr[i].size)
  85                        return -1;
  86        return 0;
  87}
  88
  89static int diff_two(const char *file1, const char *label1,
  90                const char *file2, const char *label2)
  91{
  92        xpparam_t xpp;
  93        xdemitconf_t xecfg;
  94        xdemitcb_t ecb;
  95        mmfile_t minus, plus;
  96
  97        if (read_mmfile(&minus, file1) || read_mmfile(&plus, file2))
  98                return 1;
  99
 100        printf("--- a/%s\n+++ b/%s\n", label1, label2);
 101        fflush(stdout);
 102        memset(&xpp, 0, sizeof(xpp));
 103        xpp.flags = 0;
 104        memset(&xecfg, 0, sizeof(xecfg));
 105        xecfg.ctxlen = 3;
 106        ecb.outf = outf;
 107        xdi_diff(&minus, &plus, &xpp, &xecfg, &ecb);
 108
 109        free(minus.ptr);
 110        free(plus.ptr);
 111        return 0;
 112}
 113
 114int cmd_rerere(int argc, const char **argv, const char *prefix)
 115{
 116        struct string_list merge_rr = STRING_LIST_INIT_DUP;
 117        int i, fd, flags = 0;
 118
 119        if (2 < argc) {
 120                if (!strcmp(argv[1], "-h"))
 121                        usage(git_rerere_usage);
 122                if (!strcmp(argv[1], "--rerere-autoupdate"))
 123                        flags = RERERE_AUTOUPDATE;
 124                else if (!strcmp(argv[1], "--no-rerere-autoupdate"))
 125                        flags = RERERE_NOAUTOUPDATE;
 126                if (flags) {
 127                        argc--;
 128                        argv++;
 129                }
 130        }
 131        if (argc < 2)
 132                return rerere(flags);
 133
 134        if (!strcmp(argv[1], "forget")) {
 135                const char **pathspec = get_pathspec(prefix, argv + 2);
 136                return rerere_forget(pathspec);
 137        }
 138
 139        fd = setup_rerere(&merge_rr, flags);
 140        if (fd < 0)
 141                return 0;
 142
 143        if (!strcmp(argv[1], "clear")) {
 144                for (i = 0; i < merge_rr.nr; i++) {
 145                        const char *name = (const char *)merge_rr.items[i].util;
 146                        if (!has_rerere_resolution(name))
 147                                unlink_rr_item(name);
 148                }
 149                unlink_or_warn(git_path("MERGE_RR"));
 150        } else if (!strcmp(argv[1], "gc"))
 151                garbage_collect(&merge_rr);
 152        else if (!strcmp(argv[1], "status"))
 153                for (i = 0; i < merge_rr.nr; i++)
 154                        printf("%s\n", merge_rr.items[i].string);
 155        else if (!strcmp(argv[1], "diff"))
 156                for (i = 0; i < merge_rr.nr; i++) {
 157                        const char *path = merge_rr.items[i].string;
 158                        const char *name = (const char *)merge_rr.items[i].util;
 159                        diff_two(rerere_path(name, "preimage"), path, path, path);
 160                }
 161        else
 162                usage(git_rerere_usage);
 163
 164        string_list_clear(&merge_rr, 1);
 165        return 0;
 166}