4 #include "parse-options.h"
5 #include "string-list.h"
7 #include "xdiff/xdiff.h"
8 #include "xdiff-interface.h"
10 static const char * const rerere_usage[] = {
11 "git rerere [clear | forget path... | status | remaining | diff | gc]",
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)
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)
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);
46 return git_default_config(var, value, cb);
50 static void garbage_collect(struct string_list *rr)
52 struct string_list to_remove = STRING_LIST_INIT_DUP;
56 time_t now = time(NULL), then;
58 git_config(git_rerere_gc_config, NULL);
59 dir = opendir(git_path("rr-cache"));
61 die_errno("unable to open rr-cache directory");
62 while ((e = readdir(dir))) {
63 if (is_dot_or_dotdot(e->d_name))
66 then = rerere_last_used_at(e->d_name);
68 cutoff = cutoff_resolve;
70 then = rerere_created_at(e->d_name);
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)
86 for (i = 0; i < nbuf; i++)
87 if (write_in_full(1, ptr[i].ptr, ptr[i].size) != ptr[i].size)
92 static int diff_two(const char *file1, const char *label1,
93 const char *file2, const char *label2)
100 if (read_mmfile(&minus, file1) || read_mmfile(&plus, file2))
103 printf("--- a/%s\n+++ b/%s\n", label1, label2);
105 memset(&xpp, 0, sizeof(xpp));
107 memset(&xecfg, 0, sizeof(xecfg));
110 xdi_diff(&minus, &plus, &xpp, &xecfg, &ecb);
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),
128 argc = parse_options(argc, argv, prefix, options, rerere_usage, 0);
131 flags = RERERE_AUTOUPDATE;
133 flags = RERERE_NOAUTOUPDATE;
136 return rerere(flags);
138 if (!strcmp(argv[0], "forget")) {
139 const char **pathspec;
141 warning("'git rerere forget' without paths is deprecated");
142 pathspec = get_pathspec(prefix, argv + 1);
143 return rerere_forget(pathspec);
146 fd = setup_rerere(&merge_rr, flags);
150 if (!strcmp(argv[0], "clear")) {
151 for (i = 0; i < merge_rr.nr; i++) {
152 const char *name = (const char *)merge_rr.items[i].util;
153 if (!has_rerere_resolution(name))
154 unlink_rr_item(name);
156 unlink_or_warn(git_path("MERGE_RR"));
157 } else if (!strcmp(argv[0], "gc"))
158 garbage_collect(&merge_rr);
159 else if (!strcmp(argv[0], "status"))
160 for (i = 0; i < merge_rr.nr; i++)
161 printf("%s\n", merge_rr.items[i].string);
162 else if (!strcmp(argv[0], "remaining")) {
163 rerere_remaining(&merge_rr);
164 for (i = 0; i < merge_rr.nr; i++) {
165 if (merge_rr.items[i].util != RERERE_RESOLVED)
166 printf("%s\n", merge_rr.items[i].string);
168 /* prepare for later call to
169 * string_list_clear() */
170 merge_rr.items[i].util = NULL;
172 } else if (!strcmp(argv[0], "diff"))
173 for (i = 0; i < merge_rr.nr; i++) {
174 const char *path = merge_rr.items[i].string;
175 const char *name = (const char *)merge_rr.items[i].util;
176 diff_two(rerere_path(name, "preimage"), path, path, path);
179 usage_with_options(rerere_usage, options);
181 string_list_clear(&merge_rr, 1);