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