0eec1f937321024187c185226764c39f6534017a
1#include "builtin.h"
2#include "cache.h"
3#include "path-list.h"
4#include "xdiff/xdiff.h"
5#include "xdiff-interface.h"
6
7#include <time.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
16/* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
17static int rerere_enabled = -1;
18
19static char *merge_rr_path;
20
21static const char *rr_path(const char *name, const char *file)
22{
23 return git_path("rr-cache/%s/%s", name, file);
24}
25
26static time_t rerere_created_at(const char *name)
27{
28 struct stat st;
29 return stat(rr_path(name, "preimage"), &st) ? (time_t) 0 : st.st_mtime;
30}
31
32static int has_resolution(const char *name)
33{
34 struct stat st;
35 return !stat(rr_path(name, "postimage"), &st);
36}
37
38static void read_rr(struct path_list *rr)
39{
40 unsigned char sha1[20];
41 char buf[PATH_MAX];
42 FILE *in = fopen(merge_rr_path, "r");
43 if (!in)
44 return;
45 while (fread(buf, 40, 1, in) == 1) {
46 int i;
47 char *name;
48 if (get_sha1_hex(buf, sha1))
49 die("corrupt MERGE_RR");
50 buf[40] = '\0';
51 name = xstrdup(buf);
52 if (fgetc(in) != '\t')
53 die("corrupt MERGE_RR");
54 for (i = 0; i < sizeof(buf) && (buf[i] = fgetc(in)); i++)
55 ; /* do nothing */
56 if (i == sizeof(buf))
57 die("filename too long");
58 path_list_insert(buf, rr)->util = name;
59 }
60 fclose(in);
61}
62
63static struct lock_file write_lock;
64
65static int write_rr(struct path_list *rr, int out_fd)
66{
67 int i;
68 for (i = 0; i < rr->nr; i++) {
69 const char *path;
70 int length;
71 if (!rr->items[i].util)
72 continue;
73 path = rr->items[i].path;
74 length = strlen(path) + 1;
75 if (write_in_full(out_fd, rr->items[i].util, 40) != 40 ||
76 write_in_full(out_fd, "\t", 1) != 1 ||
77 write_in_full(out_fd, path, length) != length)
78 die("unable to write rerere record");
79 }
80 if (commit_lock_file(&write_lock) != 0)
81 die("unable to write rerere record");
82 return 0;
83}
84
85static int handle_file(const char *path,
86 unsigned char *sha1, const char *output)
87{
88 SHA_CTX ctx;
89 char buf[1024];
90 int hunk = 0, hunk_no = 0;
91 struct strbuf one, two;
92 FILE *f = fopen(path, "r");
93 FILE *out = NULL;
94
95 if (!f)
96 return error("Could not open %s", path);
97
98 if (output) {
99 out = fopen(output, "w");
100 if (!out) {
101 fclose(f);
102 return error("Could not write %s", output);
103 }
104 }
105
106 if (sha1)
107 SHA1_Init(&ctx);
108
109 strbuf_init(&one, 0);
110 strbuf_init(&two, 0);
111 while (fgets(buf, sizeof(buf), f)) {
112 if (!prefixcmp(buf, "<<<<<<< "))
113 hunk = 1;
114 else if (!prefixcmp(buf, "======="))
115 hunk = 2;
116 else if (!prefixcmp(buf, ">>>>>>> ")) {
117 if (strbuf_cmp(&one, &two) > 0)
118 strbuf_swap(&one, &two);
119 hunk_no++;
120 hunk = 0;
121 if (out) {
122 fputs("<<<<<<<\n", out);
123 fwrite(one.buf, one.len, 1, out);
124 fputs("=======\n", out);
125 fwrite(two.buf, two.len, 1, out);
126 fputs(">>>>>>>\n", out);
127 }
128 if (sha1) {
129 SHA1_Update(&ctx, one.buf ? one.buf : "",
130 one.len + 1);
131 SHA1_Update(&ctx, two.buf ? two.buf : "",
132 two.len + 1);
133 }
134 strbuf_reset(&one);
135 strbuf_reset(&two);
136 } else if (hunk == 1)
137 strbuf_addstr(&one, buf);
138 else if (hunk == 2)
139 strbuf_addstr(&two, buf);
140 else if (out)
141 fputs(buf, out);
142 }
143 strbuf_release(&one);
144 strbuf_release(&two);
145
146 fclose(f);
147 if (out)
148 fclose(out);
149 if (sha1)
150 SHA1_Final(sha1, &ctx);
151 if (hunk) {
152 if (output)
153 unlink(output);
154 return error("Could not parse conflict hunks in %s", path);
155 }
156 return hunk_no;
157}
158
159static int find_conflict(struct path_list *conflict)
160{
161 int i;
162 if (read_cache() < 0)
163 return error("Could not read index");
164 for (i = 0; i+1 < active_nr; i++) {
165 struct cache_entry *e2 = active_cache[i];
166 struct cache_entry *e3 = active_cache[i+1];
167 if (ce_stage(e2) == 2 &&
168 ce_stage(e3) == 3 &&
169 ce_same_name(e2, e3) &&
170 S_ISREG(e2->ce_mode) &&
171 S_ISREG(e3->ce_mode)) {
172 path_list_insert((const char *)e2->name, conflict);
173 i++; /* skip over both #2 and #3 */
174 }
175 }
176 return 0;
177}
178
179static int merge(const char *name, const char *path)
180{
181 int ret;
182 mmfile_t cur, base, other;
183 mmbuffer_t result = {NULL, 0};
184 xpparam_t xpp = {XDF_NEED_MINIMAL};
185
186 if (handle_file(path, NULL, rr_path(name, "thisimage")) < 0)
187 return 1;
188
189 if (read_mmfile(&cur, rr_path(name, "thisimage")) ||
190 read_mmfile(&base, rr_path(name, "preimage")) ||
191 read_mmfile(&other, rr_path(name, "postimage")))
192 return 1;
193 ret = xdl_merge(&base, &cur, "", &other, "",
194 &xpp, XDL_MERGE_ZEALOUS, &result);
195 if (!ret) {
196 FILE *f = fopen(path, "w");
197 if (!f)
198 return error("Could not write to %s", path);
199 fwrite(result.ptr, result.size, 1, f);
200 fclose(f);
201 }
202
203 free(cur.ptr);
204 free(base.ptr);
205 free(other.ptr);
206 free(result.ptr);
207
208 return ret;
209}
210
211static void unlink_rr_item(const char *name)
212{
213 unlink(rr_path(name, "thisimage"));
214 unlink(rr_path(name, "preimage"));
215 unlink(rr_path(name, "postimage"));
216 rmdir(git_path("rr-cache/%s", name));
217}
218
219static void garbage_collect(struct path_list *rr)
220{
221 struct path_list to_remove = { NULL, 0, 0, 1 };
222 DIR *dir;
223 struct dirent *e;
224 int i, cutoff;
225 time_t now = time(NULL), then;
226
227 dir = opendir(git_path("rr-cache"));
228 while ((e = readdir(dir))) {
229 const char *name = e->d_name;
230 if (name[0] == '.' &&
231 (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')))
232 continue;
233 then = rerere_created_at(name);
234 if (!then)
235 continue;
236 cutoff = (has_resolution(name)
237 ? cutoff_resolve : cutoff_noresolve);
238 if (then < now - cutoff * 86400)
239 path_list_append(name, &to_remove);
240 }
241 for (i = 0; i < to_remove.nr; i++)
242 unlink_rr_item(to_remove.items[i].path);
243 path_list_clear(&to_remove, 0);
244}
245
246static int outf(void *dummy, mmbuffer_t *ptr, int nbuf)
247{
248 int i;
249 for (i = 0; i < nbuf; i++)
250 if (write_in_full(1, ptr[i].ptr, ptr[i].size) != ptr[i].size)
251 return -1;
252 return 0;
253}
254
255static int diff_two(const char *file1, const char *label1,
256 const char *file2, const char *label2)
257{
258 xpparam_t xpp;
259 xdemitconf_t xecfg;
260 xdemitcb_t ecb;
261 mmfile_t minus, plus;
262
263 if (read_mmfile(&minus, file1) || read_mmfile(&plus, file2))
264 return 1;
265
266 printf("--- a/%s\n+++ b/%s\n", label1, label2);
267 fflush(stdout);
268 xpp.flags = XDF_NEED_MINIMAL;
269 memset(&xecfg, 0, sizeof(xecfg));
270 xecfg.ctxlen = 3;
271 ecb.outf = outf;
272 xdi_diff(&minus, &plus, &xpp, &xecfg, &ecb);
273
274 free(minus.ptr);
275 free(plus.ptr);
276 return 0;
277}
278
279static int do_plain_rerere(struct path_list *rr, int fd)
280{
281 struct path_list conflict = { NULL, 0, 0, 1 };
282 int i;
283
284 find_conflict(&conflict);
285
286 /*
287 * MERGE_RR records paths with conflicts immediately after merge
288 * failed. Some of the conflicted paths might have been hand resolved
289 * in the working tree since then, but the initial run would catch all
290 * and register their preimages.
291 */
292
293 for (i = 0; i < conflict.nr; i++) {
294 const char *path = conflict.items[i].path;
295 if (!path_list_has_path(rr, path)) {
296 unsigned char sha1[20];
297 char *hex;
298 int ret;
299 ret = handle_file(path, sha1, NULL);
300 if (ret < 1)
301 continue;
302 hex = xstrdup(sha1_to_hex(sha1));
303 path_list_insert(path, rr)->util = hex;
304 if (mkdir(git_path("rr-cache/%s", hex), 0755))
305 continue;;
306 handle_file(path, NULL, rr_path(hex, "preimage"));
307 fprintf(stderr, "Recorded preimage for '%s'\n", path);
308 }
309 }
310
311 /*
312 * Now some of the paths that had conflicts earlier might have been
313 * hand resolved. Others may be similar to a conflict already that
314 * was resolved before.
315 */
316
317 for (i = 0; i < rr->nr; i++) {
318 int ret;
319 const char *path = rr->items[i].path;
320 const char *name = (const char *)rr->items[i].util;
321
322 if (has_resolution(name)) {
323 if (!merge(name, path)) {
324 fprintf(stderr, "Resolved '%s' using "
325 "previous resolution.\n", path);
326 goto mark_resolved;
327 }
328 }
329
330 /* Let's see if we have resolved it. */
331 ret = handle_file(path, NULL, NULL);
332 if (ret)
333 continue;
334
335 fprintf(stderr, "Recorded resolution for '%s'.\n", path);
336 copy_file(rr_path(name, "postimage"), path, 0666);
337 mark_resolved:
338 rr->items[i].util = NULL;
339 }
340
341 return write_rr(rr, fd);
342}
343
344static int git_rerere_config(const char *var, const char *value, void *cb)
345{
346 if (!strcmp(var, "gc.rerereresolved"))
347 cutoff_resolve = git_config_int(var, value);
348 else if (!strcmp(var, "gc.rerereunresolved"))
349 cutoff_noresolve = git_config_int(var, value);
350 else if (!strcmp(var, "rerere.enabled"))
351 rerere_enabled = git_config_bool(var, value);
352 else
353 return git_default_config(var, value, cb);
354 return 0;
355}
356
357static int is_rerere_enabled(void)
358{
359 struct stat st;
360 const char *rr_cache;
361 int rr_cache_exists;
362
363 if (!rerere_enabled)
364 return 0;
365
366 rr_cache = git_path("rr-cache");
367 rr_cache_exists = !stat(rr_cache, &st) && S_ISDIR(st.st_mode);
368 if (rerere_enabled < 0)
369 return rr_cache_exists;
370
371 if (!rr_cache_exists &&
372 (mkdir(rr_cache, 0777) || adjust_shared_perm(rr_cache)))
373 die("Could not create directory %s", rr_cache);
374 return 1;
375}
376
377static int setup_rerere(struct path_list *merge_rr)
378{
379 int fd;
380
381 git_config(git_rerere_config, NULL);
382 if (!is_rerere_enabled())
383 return -1;
384
385 merge_rr_path = xstrdup(git_path("rr-cache/MERGE_RR"));
386 fd = hold_lock_file_for_update(&write_lock, merge_rr_path, 1);
387 read_rr(merge_rr);
388 return fd;
389}
390
391int rerere(void)
392{
393 struct path_list merge_rr = { NULL, 0, 0, 1 };
394 int fd;
395
396 fd = setup_rerere(&merge_rr);
397 if (fd < 0)
398 return 0;
399 return do_plain_rerere(&merge_rr, fd);
400}
401
402int cmd_rerere(int argc, const char **argv, const char *prefix)
403{
404 struct path_list merge_rr = { NULL, 0, 0, 1 };
405 int i, fd;
406
407 fd = setup_rerere(&merge_rr);
408 if (fd < 0)
409 return 0;
410
411 if (argc < 2)
412 return do_plain_rerere(&merge_rr, fd);
413 else if (!strcmp(argv[1], "clear")) {
414 for (i = 0; i < merge_rr.nr; i++) {
415 const char *name = (const char *)merge_rr.items[i].util;
416 if (!has_resolution(name))
417 unlink_rr_item(name);
418 }
419 unlink(merge_rr_path);
420 } else if (!strcmp(argv[1], "gc"))
421 garbage_collect(&merge_rr);
422 else if (!strcmp(argv[1], "status"))
423 for (i = 0; i < merge_rr.nr; i++)
424 printf("%s\n", merge_rr.items[i].path);
425 else if (!strcmp(argv[1], "diff"))
426 for (i = 0; i < merge_rr.nr; i++) {
427 const char *path = merge_rr.items[i].path;
428 const char *name = (const char *)merge_rr.items[i].util;
429 diff_two(rr_path(name, "preimage"), path, path, path);
430 }
431 else
432 usage(git_rerere_usage);
433
434 path_list_clear(&merge_rr, 1);
435 return 0;
436}