(squash) test for previous
[git.git] / builtin / rerere.c
blob0c7202eb9e637b69bcbbf3641d8ed72f44fd7684
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"
9 static const char git_rerere_usage[] =
10 "git rerere [clear | status | diff | gc]";
12 /* these values are days */
13 static int cutoff_noresolve = 15;
14 static int cutoff_resolve = 60;
16 static time_t rerere_created_at(const char *name)
18 struct stat st;
19 return stat(rerere_path(name, "preimage"), &st) ? (time_t) 0 : st.st_mtime;
22 static time_t rerere_last_used_at(const char *name)
24 struct stat st;
25 return stat(rerere_path(name, "postimage"), &st) ? (time_t) 0 : st.st_mtime;
28 static void unlink_rr_item(const char *name)
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));
36 static int git_rerere_gc_config(const char *var, const char *value, void *cb)
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;
47 static void garbage_collect(struct string_list *rr)
49 struct string_list to_remove = { NULL, 0, 0, 1 };
50 DIR *dir;
51 struct dirent *e;
52 int i, cutoff;
53 time_t now = time(NULL), then;
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;
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;
72 if (then < now - cutoff * 86400)
73 string_list_append(&to_remove, e->d_name);
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);
80 static int outf(void *dummy, mmbuffer_t *ptr, int nbuf)
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;
89 static int diff_two(const char *file1, const char *label1,
90 const char *file2, const char *label2)
92 xpparam_t xpp;
93 xdemitconf_t xecfg;
94 xdemitcb_t ecb;
95 mmfile_t minus, plus;
97 if (read_mmfile(&minus, file1) || read_mmfile(&plus, file2))
98 return 1;
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);
109 free(minus.ptr);
110 free(plus.ptr);
111 return 0;
114 int cmd_rerere(int argc, const char **argv, const char *prefix)
116 struct string_list merge_rr = { NULL, 0, 0, 1 };
117 int i, fd, flags = 0;
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++;
131 if (argc < 2)
132 return rerere(flags);
134 if (!strcmp(argv[1], "forget")) {
135 const char **pathspec = get_pathspec(prefix, argv + 2);
136 return rerere_forget(pathspec);
139 fd = setup_rerere(&merge_rr, flags);
140 if (fd < 0)
141 return 0;
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);
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);
161 else
162 usage(git_rerere_usage);
164 string_list_clear(&merge_rr, 1);
165 return 0;