builtin-rerere.con commit Merge branch 'tf/doc-pt-br' (178071b)
   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 void unlink_rr_item(const char *name)
  23{
  24        unlink(rerere_path(name, "thisimage"));
  25        unlink(rerere_path(name, "preimage"));
  26        unlink(rerere_path(name, "postimage"));
  27        rmdir(git_path("rr-cache/%s", name));
  28}
  29
  30static int git_rerere_gc_config(const char *var, const char *value, void *cb)
  31{
  32        if (!strcmp(var, "gc.rerereresolved"))
  33                cutoff_resolve = git_config_int(var, value);
  34        else if (!strcmp(var, "gc.rerereunresolved"))
  35                cutoff_noresolve = git_config_int(var, value);
  36        else
  37                return git_default_config(var, value, cb);
  38        return 0;
  39}
  40
  41static void garbage_collect(struct string_list *rr)
  42{
  43        struct string_list to_remove = { NULL, 0, 0, 1 };
  44        DIR *dir;
  45        struct dirent *e;
  46        int i, cutoff;
  47        time_t now = time(NULL), then;
  48
  49        git_config(git_rerere_gc_config, NULL);
  50        dir = opendir(git_path("rr-cache"));
  51        while ((e = readdir(dir))) {
  52                if (is_dot_or_dotdot(e->d_name))
  53                        continue;
  54                then = rerere_created_at(e->d_name);
  55                if (!then)
  56                        continue;
  57                cutoff = (has_rerere_resolution(e->d_name)
  58                          ? cutoff_resolve : cutoff_noresolve);
  59                if (then < now - cutoff * 86400)
  60                        string_list_append(e->d_name, &to_remove);
  61        }
  62        for (i = 0; i < to_remove.nr; i++)
  63                unlink_rr_item(to_remove.items[i].string);
  64        string_list_clear(&to_remove, 0);
  65}
  66
  67static int outf(void *dummy, mmbuffer_t *ptr, int nbuf)
  68{
  69        int i;
  70        for (i = 0; i < nbuf; i++)
  71                if (write_in_full(1, ptr[i].ptr, ptr[i].size) != ptr[i].size)
  72                        return -1;
  73        return 0;
  74}
  75
  76static int diff_two(const char *file1, const char *label1,
  77                const char *file2, const char *label2)
  78{
  79        xpparam_t xpp;
  80        xdemitconf_t xecfg;
  81        xdemitcb_t ecb;
  82        mmfile_t minus, plus;
  83
  84        if (read_mmfile(&minus, file1) || read_mmfile(&plus, file2))
  85                return 1;
  86
  87        printf("--- a/%s\n+++ b/%s\n", label1, label2);
  88        fflush(stdout);
  89        memset(&xpp, 0, sizeof(xpp));
  90        xpp.flags = XDF_NEED_MINIMAL;
  91        memset(&xecfg, 0, sizeof(xecfg));
  92        xecfg.ctxlen = 3;
  93        ecb.outf = outf;
  94        xdi_diff(&minus, &plus, &xpp, &xecfg, &ecb);
  95
  96        free(minus.ptr);
  97        free(plus.ptr);
  98        return 0;
  99}
 100
 101int cmd_rerere(int argc, const char **argv, const char *prefix)
 102{
 103        struct string_list merge_rr = { NULL, 0, 0, 1 };
 104        int i, fd;
 105
 106        if (argc < 2)
 107                return rerere();
 108
 109        fd = setup_rerere(&merge_rr);
 110        if (fd < 0)
 111                return 0;
 112
 113        if (!strcmp(argv[1], "clear")) {
 114                for (i = 0; i < merge_rr.nr; i++) {
 115                        const char *name = (const char *)merge_rr.items[i].util;
 116                        if (!has_rerere_resolution(name))
 117                                unlink_rr_item(name);
 118                }
 119                unlink_or_warn(git_path("rr-cache/MERGE_RR"));
 120        } else if (!strcmp(argv[1], "gc"))
 121                garbage_collect(&merge_rr);
 122        else if (!strcmp(argv[1], "status"))
 123                for (i = 0; i < merge_rr.nr; i++)
 124                        printf("%s\n", merge_rr.items[i].string);
 125        else if (!strcmp(argv[1], "diff"))
 126                for (i = 0; i < merge_rr.nr; i++) {
 127                        const char *path = merge_rr.items[i].string;
 128                        const char *name = (const char *)merge_rr.items[i].util;
 129                        diff_two(rerere_path(name, "preimage"), path, path, path);
 130                }
 131        else
 132                usage(git_rerere_usage);
 133
 134        string_list_clear(&merge_rr, 1);
 135        return 0;
 136}