Full rework of quote_c_style and write_name_quoted.
[git/dscho.git] / builtin-rerere.c
blob58288f61a3861df757f8299e168e847943915e6b
1 #include "builtin.h"
2 #include "cache.h"
3 #include "path-list.h"
4 #include "xdiff/xdiff.h"
5 #include "xdiff-interface.h"
7 #include <time.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 /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
17 static int rerere_enabled = -1;
19 static char *merge_rr_path;
21 static const char *rr_path(const char *name, const char *file)
23 return git_path("rr-cache/%s/%s", name, file);
26 static void read_rr(struct path_list *rr)
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);
48 fclose(in);
51 static struct lock_file write_lock;
53 static int write_rr(struct path_list *rr, int out_fd)
55 int i;
56 for (i = 0; i < rr->nr; i++) {
57 const char *path = rr->items[i].path;
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");
64 if (close(out_fd) != 0)
65 die("unable to write rerere record");
66 return commit_lock_file(&write_lock);
69 static int handle_file(const char *path,
70 unsigned char *sha1, const char *output)
72 SHA_CTX ctx;
73 char buf[1024];
74 int hunk = 0, hunk_no = 0;
75 struct strbuf minus, plus;
76 struct strbuf *one = &minus, *two = &plus;
77 FILE *f = fopen(path, "r");
78 FILE *out;
80 strbuf_init(&minus, 0);
81 strbuf_init(&plus, 0);
83 if (!f)
84 return error("Could not open %s", path);
86 if (output) {
87 out = fopen(output, "w");
88 if (!out) {
89 fclose(f);
90 return error("Could not write %s", output);
92 } else
93 out = NULL;
95 if (sha1)
96 SHA1_Init(&ctx);
98 while (fgets(buf, sizeof(buf), f)) {
99 if (!prefixcmp(buf, "<<<<<<< "))
100 hunk = 1;
101 else if (!prefixcmp(buf, "======="))
102 hunk = 2;
103 else if (!prefixcmp(buf, ">>>>>>> ")) {
104 int one_is_longer = (one->len > two->len);
105 int common_len = one_is_longer ? two->len : one->len;
106 int cmp = memcmp(one->buf, two->buf, common_len);
108 hunk_no++;
109 hunk = 0;
110 if ((cmp > 0) || ((cmp == 0) && one_is_longer)) {
111 struct strbuf *swap = one;
112 one = two;
113 two = swap;
115 if (out) {
116 fputs("<<<<<<<\n", out);
117 fwrite(one->buf, one->len, 1, out);
118 fputs("=======\n", out);
119 fwrite(two->buf, two->len, 1, out);
120 fputs(">>>>>>>\n", out);
122 if (sha1) {
123 SHA1_Update(&ctx, one->buf, one->len);
124 SHA1_Update(&ctx, "\0", 1);
125 SHA1_Update(&ctx, two->buf, two->len);
126 SHA1_Update(&ctx, "\0", 1);
128 strbuf_release(one);
129 strbuf_release(two);
130 } else if (hunk == 1)
131 strbuf_addstr(one, buf);
132 else if (hunk == 2)
133 strbuf_addstr(two, buf);
134 else if (out)
135 fputs(buf, out);
138 fclose(f);
139 if (out)
140 fclose(out);
141 if (sha1)
142 SHA1_Final(sha1, &ctx);
143 return hunk_no;
146 static int find_conflict(struct path_list *conflict)
148 int i;
149 if (read_cache() < 0)
150 return error("Could not read index");
151 for (i = 0; i+1 < active_nr; i++) {
152 struct cache_entry *e2 = active_cache[i];
153 struct cache_entry *e3 = active_cache[i+1];
154 if (ce_stage(e2) == 2 &&
155 ce_stage(e3) == 3 &&
156 ce_same_name(e2, e3) &&
157 S_ISREG(ntohl(e2->ce_mode)) &&
158 S_ISREG(ntohl(e3->ce_mode))) {
159 path_list_insert((const char *)e2->name, conflict);
160 i++; /* skip over both #2 and #3 */
163 return 0;
166 static int merge(const char *name, const char *path)
168 int ret;
169 mmfile_t cur, base, other;
170 mmbuffer_t result = {NULL, 0};
171 xpparam_t xpp = {XDF_NEED_MINIMAL};
173 if (handle_file(path, NULL, rr_path(name, "thisimage")) < 0)
174 return 1;
176 if (read_mmfile(&cur, rr_path(name, "thisimage")) ||
177 read_mmfile(&base, rr_path(name, "preimage")) ||
178 read_mmfile(&other, rr_path(name, "postimage")))
179 return 1;
180 ret = xdl_merge(&base, &cur, "", &other, "",
181 &xpp, XDL_MERGE_ZEALOUS, &result);
182 if (!ret) {
183 FILE *f = fopen(path, "w");
184 if (!f)
185 return error("Could not write to %s", path);
186 fwrite(result.ptr, result.size, 1, f);
187 fclose(f);
190 free(cur.ptr);
191 free(base.ptr);
192 free(other.ptr);
193 free(result.ptr);
195 return ret;
198 static void unlink_rr_item(const char *name)
200 unlink(rr_path(name, "thisimage"));
201 unlink(rr_path(name, "preimage"));
202 unlink(rr_path(name, "postimage"));
203 rmdir(git_path("rr-cache/%s", name));
206 static void garbage_collect(struct path_list *rr)
208 struct path_list to_remove = { NULL, 0, 0, 1 };
209 char buf[1024];
210 DIR *dir;
211 struct dirent *e;
212 int len, i, cutoff;
213 time_t now = time(NULL), then;
215 strlcpy(buf, git_path("rr-cache"), sizeof(buf));
216 len = strlen(buf);
217 dir = opendir(buf);
218 strcpy(buf + len++, "/");
219 while ((e = readdir(dir))) {
220 const char *name = e->d_name;
221 struct stat st;
222 if (name[0] == '.' && (name[1] == '\0' ||
223 (name[1] == '.' && name[2] == '\0')))
224 continue;
225 i = snprintf(buf + len, sizeof(buf) - len, "%s", name);
226 strlcpy(buf + len + i, "/preimage", sizeof(buf) - len - i);
227 if (stat(buf, &st))
228 continue;
229 then = st.st_mtime;
230 strlcpy(buf + len + i, "/postimage", sizeof(buf) - len - i);
231 cutoff = stat(buf, &st) ? cutoff_noresolve : cutoff_resolve;
232 if (then < now - cutoff * 86400) {
233 buf[len + i] = '\0';
234 path_list_insert(xstrdup(name), &to_remove);
237 for (i = 0; i < to_remove.nr; i++)
238 unlink_rr_item(to_remove.items[i].path);
239 path_list_clear(&to_remove, 0);
242 static int outf(void *dummy, mmbuffer_t *ptr, int nbuf)
244 int i;
245 for (i = 0; i < nbuf; i++)
246 if (write_in_full(1, ptr[i].ptr, ptr[i].size) != ptr[i].size)
247 return -1;
248 return 0;
251 static int diff_two(const char *file1, const char *label1,
252 const char *file2, const char *label2)
254 xpparam_t xpp;
255 xdemitconf_t xecfg;
256 xdemitcb_t ecb;
257 mmfile_t minus, plus;
259 if (read_mmfile(&minus, file1) || read_mmfile(&plus, file2))
260 return 1;
262 printf("--- a/%s\n+++ b/%s\n", label1, label2);
263 fflush(stdout);
264 xpp.flags = XDF_NEED_MINIMAL;
265 memset(&xecfg, 0, sizeof(xecfg));
266 xecfg.ctxlen = 3;
267 ecb.outf = outf;
268 xdl_diff(&minus, &plus, &xpp, &xecfg, &ecb);
270 free(minus.ptr);
271 free(plus.ptr);
272 return 0;
275 static int copy_file(const char *src, const char *dest)
277 FILE *in, *out;
278 char buffer[32768];
279 int count;
281 if (!(in = fopen(src, "r")))
282 return error("Could not open %s", src);
283 if (!(out = fopen(dest, "w")))
284 return error("Could not open %s", dest);
285 while ((count = fread(buffer, 1, sizeof(buffer), in)))
286 fwrite(buffer, 1, count, out);
287 fclose(in);
288 fclose(out);
289 return 0;
292 static int do_plain_rerere(struct path_list *rr, int fd)
294 struct path_list conflict = { NULL, 0, 0, 1 };
295 int i;
297 find_conflict(&conflict);
300 * MERGE_RR records paths with conflicts immediately after merge
301 * failed. Some of the conflicted paths might have been hand resolved
302 * in the working tree since then, but the initial run would catch all
303 * and register their preimages.
306 for (i = 0; i < conflict.nr; i++) {
307 const char *path = conflict.items[i].path;
308 if (!path_list_has_path(rr, path)) {
309 unsigned char sha1[20];
310 char *hex;
311 int ret;
312 ret = handle_file(path, sha1, NULL);
313 if (ret < 1)
314 continue;
315 hex = xstrdup(sha1_to_hex(sha1));
316 path_list_insert(path, rr)->util = hex;
317 if (mkdir(git_path("rr-cache/%s", hex), 0755))
318 continue;;
319 handle_file(path, NULL, rr_path(hex, "preimage"));
320 fprintf(stderr, "Recorded preimage for '%s'\n", path);
325 * Now some of the paths that had conflicts earlier might have been
326 * hand resolved. Others may be similar to a conflict already that
327 * was resolved before.
330 for (i = 0; i < rr->nr; i++) {
331 struct stat st;
332 int ret;
333 const char *path = rr->items[i].path;
334 const char *name = (const char *)rr->items[i].util;
336 if (!stat(rr_path(name, "preimage"), &st) &&
337 !stat(rr_path(name, "postimage"), &st)) {
338 if (!merge(name, path)) {
339 fprintf(stderr, "Resolved '%s' using "
340 "previous resolution.\n", path);
341 goto tail_optimization;
345 /* Let's see if we have resolved it. */
346 ret = handle_file(path, NULL, NULL);
347 if (ret)
348 continue;
350 fprintf(stderr, "Recorded resolution for '%s'.\n", path);
351 copy_file(path, rr_path(name, "postimage"));
352 tail_optimization:
353 if (i < rr->nr - 1)
354 memmove(rr->items + i,
355 rr->items + i + 1,
356 sizeof(rr->items[0]) * (rr->nr - i - 1));
357 rr->nr--;
358 i--;
361 return write_rr(rr, fd);
364 static int git_rerere_config(const char *var, const char *value)
366 if (!strcmp(var, "gc.rerereresolved"))
367 cutoff_resolve = git_config_int(var, value);
368 else if (!strcmp(var, "gc.rerereunresolved"))
369 cutoff_noresolve = git_config_int(var, value);
370 else if (!strcmp(var, "rerere.enabled"))
371 rerere_enabled = git_config_bool(var, value);
372 else
373 return git_default_config(var, value);
374 return 0;
377 static int is_rerere_enabled(void)
379 struct stat st;
380 const char *rr_cache;
381 int rr_cache_exists;
383 if (!rerere_enabled)
384 return 0;
386 rr_cache = git_path("rr-cache");
387 rr_cache_exists = !stat(rr_cache, &st) && S_ISDIR(st.st_mode);
388 if (rerere_enabled < 0)
389 return rr_cache_exists;
391 if (!rr_cache_exists &&
392 (mkdir(rr_cache, 0777) || adjust_shared_perm(rr_cache)))
393 die("Could not create directory %s", rr_cache);
394 return 1;
397 int cmd_rerere(int argc, const char **argv, const char *prefix)
399 struct path_list merge_rr = { NULL, 0, 0, 1 };
400 int i, fd = -1;
402 git_config(git_rerere_config);
403 if (!is_rerere_enabled())
404 return 0;
406 merge_rr_path = xstrdup(git_path("rr-cache/MERGE_RR"));
407 fd = hold_lock_file_for_update(&write_lock, merge_rr_path, 1);
408 read_rr(&merge_rr);
410 if (argc < 2)
411 return do_plain_rerere(&merge_rr, fd);
412 else if (!strcmp(argv[1], "clear")) {
413 for (i = 0; i < merge_rr.nr; i++) {
414 struct stat st;
415 const char *name = (const char *)merge_rr.items[i].util;
416 if (!stat(git_path("rr-cache/%s", name), &st) &&
417 S_ISDIR(st.st_mode) &&
418 stat(rr_path(name, "postimage"), &st))
419 unlink_rr_item(name);
421 unlink(merge_rr_path);
422 } else if (!strcmp(argv[1], "gc"))
423 garbage_collect(&merge_rr);
424 else if (!strcmp(argv[1], "status"))
425 for (i = 0; i < merge_rr.nr; i++)
426 printf("%s\n", merge_rr.items[i].path);
427 else if (!strcmp(argv[1], "diff"))
428 for (i = 0; i < merge_rr.nr; i++) {
429 const char *path = merge_rr.items[i].path;
430 const char *name = (const char *)merge_rr.items[i].util;
431 diff_two(rr_path(name, "preimage"), path, path, path);
433 else
434 usage(git_rerere_usage);
436 path_list_clear(&merge_rr, 1);
437 return 0;