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