stash: simplify parsing fixes
[alt-git.git] / builtin / rerere.c
blob642bf35587ed994948d3eeac0a189ae29e39bf11
1 #include "builtin.h"
2 #include "cache.h"
3 #include "dir.h"
4 #include "parse-options.h"
5 #include "string-list.h"
6 #include "rerere.h"
7 #include "xdiff/xdiff.h"
8 #include "xdiff-interface.h"
10 static const char * const rerere_usage[] = {
11 "git rerere [clear | status | diff | gc]",
12 NULL,
15 /* these values are days */
16 static int cutoff_noresolve = 15;
17 static int cutoff_resolve = 60;
19 static time_t rerere_created_at(const char *name)
21 struct stat st;
22 return stat(rerere_path(name, "preimage"), &st) ? (time_t) 0 : st.st_mtime;
25 static time_t rerere_last_used_at(const char *name)
27 struct stat st;
28 return stat(rerere_path(name, "postimage"), &st) ? (time_t) 0 : st.st_mtime;
31 static void unlink_rr_item(const char *name)
33 unlink(rerere_path(name, "thisimage"));
34 unlink(rerere_path(name, "preimage"));
35 unlink(rerere_path(name, "postimage"));
36 rmdir(git_path("rr-cache/%s", name));
39 static int git_rerere_gc_config(const char *var, const char *value, void *cb)
41 if (!strcmp(var, "gc.rerereresolved"))
42 cutoff_resolve = git_config_int(var, value);
43 else if (!strcmp(var, "gc.rerereunresolved"))
44 cutoff_noresolve = git_config_int(var, value);
45 else
46 return git_default_config(var, value, cb);
47 return 0;
50 static void garbage_collect(struct string_list *rr)
52 struct string_list to_remove = STRING_LIST_INIT_DUP;
53 DIR *dir;
54 struct dirent *e;
55 int i, cutoff;
56 time_t now = time(NULL), then;
58 git_config(git_rerere_gc_config, NULL);
59 dir = opendir(git_path("rr-cache"));
60 if (!dir)
61 die_errno("unable to open rr-cache directory");
62 while ((e = readdir(dir))) {
63 if (is_dot_or_dotdot(e->d_name))
64 continue;
66 then = rerere_last_used_at(e->d_name);
67 if (then) {
68 cutoff = cutoff_resolve;
69 } else {
70 then = rerere_created_at(e->d_name);
71 if (!then)
72 continue;
73 cutoff = cutoff_noresolve;
75 if (then < now - cutoff * 86400)
76 string_list_append(&to_remove, e->d_name);
78 for (i = 0; i < to_remove.nr; i++)
79 unlink_rr_item(to_remove.items[i].string);
80 string_list_clear(&to_remove, 0);
83 static int outf(void *dummy, mmbuffer_t *ptr, int nbuf)
85 int i;
86 for (i = 0; i < nbuf; i++)
87 if (write_in_full(1, ptr[i].ptr, ptr[i].size) != ptr[i].size)
88 return -1;
89 return 0;
92 static int diff_two(const char *file1, const char *label1,
93 const char *file2, const char *label2)
95 xpparam_t xpp;
96 xdemitconf_t xecfg;
97 xdemitcb_t ecb;
98 mmfile_t minus, plus;
100 if (read_mmfile(&minus, file1) || read_mmfile(&plus, file2))
101 return 1;
103 printf("--- a/%s\n+++ b/%s\n", label1, label2);
104 fflush(stdout);
105 memset(&xpp, 0, sizeof(xpp));
106 xpp.flags = 0;
107 memset(&xecfg, 0, sizeof(xecfg));
108 xecfg.ctxlen = 3;
109 ecb.outf = outf;
110 xdi_diff(&minus, &plus, &xpp, &xecfg, &ecb);
112 free(minus.ptr);
113 free(plus.ptr);
114 return 0;
117 int cmd_rerere(int argc, const char **argv, const char *prefix)
119 struct string_list merge_rr = STRING_LIST_INIT_DUP;
120 int i, fd, autoupdate = -1, flags = 0;
122 struct option options[] = {
123 OPT_SET_INT(0, "rerere-autoupdate", &autoupdate,
124 "register clean resolutions in index", 1),
125 OPT_END(),
128 argc = parse_options(argc, argv, prefix, options, rerere_usage, 0);
130 if (autoupdate == 1)
131 flags = RERERE_AUTOUPDATE;
132 if (autoupdate == 0)
133 flags = RERERE_NOAUTOUPDATE;
135 if (argc < 1)
136 return rerere(flags);
138 if (!strcmp(argv[0], "forget")) {
139 const char **pathspec = get_pathspec(prefix, argv + 1);
140 return rerere_forget(pathspec);
143 fd = setup_rerere(&merge_rr, flags);
144 if (fd < 0)
145 return 0;
147 if (!strcmp(argv[0], "clear")) {
148 for (i = 0; i < merge_rr.nr; i++) {
149 const char *name = (const char *)merge_rr.items[i].util;
150 if (!has_rerere_resolution(name))
151 unlink_rr_item(name);
153 unlink_or_warn(git_path("MERGE_RR"));
154 } else if (!strcmp(argv[0], "gc"))
155 garbage_collect(&merge_rr);
156 else if (!strcmp(argv[0], "status"))
157 for (i = 0; i < merge_rr.nr; i++)
158 printf("%s\n", merge_rr.items[i].string);
159 else if (!strcmp(argv[0], "diff"))
160 for (i = 0; i < merge_rr.nr; i++) {
161 const char *path = merge_rr.items[i].string;
162 const char *name = (const char *)merge_rr.items[i].util;
163 diff_two(rerere_path(name, "preimage"), path, path, path);
165 else
166 usage_with_options(rerere_usage, options);
168 string_list_clear(&merge_rr, 1);
169 return 0;