t0001-init.sh: change confusing directory name
[git/dscho.git] / builtin-rerere.c
blob839b26e8e0a10a498a81bf27d907543e7c256cb8
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 /* automatically update cleanly resolved paths to the index */
20 static int rerere_autoupdate;
22 static char *merge_rr_path;
24 static const char *rr_path(const char *name, const char *file)
26 return git_path("rr-cache/%s/%s", name, file);
29 static time_t rerere_created_at(const char *name)
31 struct stat st;
32 return stat(rr_path(name, "preimage"), &st) ? (time_t) 0 : st.st_mtime;
35 static int has_resolution(const char *name)
37 struct stat st;
38 return !stat(rr_path(name, "postimage"), &st);
41 static void read_rr(struct path_list *rr)
43 unsigned char sha1[20];
44 char buf[PATH_MAX];
45 FILE *in = fopen(merge_rr_path, "r");
46 if (!in)
47 return;
48 while (fread(buf, 40, 1, in) == 1) {
49 int i;
50 char *name;
51 if (get_sha1_hex(buf, sha1))
52 die("corrupt MERGE_RR");
53 buf[40] = '\0';
54 name = xstrdup(buf);
55 if (fgetc(in) != '\t')
56 die("corrupt MERGE_RR");
57 for (i = 0; i < sizeof(buf) && (buf[i] = fgetc(in)); i++)
58 ; /* do nothing */
59 if (i == sizeof(buf))
60 die("filename too long");
61 path_list_insert(buf, rr)->util = name;
63 fclose(in);
66 static struct lock_file write_lock;
68 static int write_rr(struct path_list *rr, int out_fd)
70 int i;
71 for (i = 0; i < rr->nr; i++) {
72 const char *path;
73 int length;
74 if (!rr->items[i].util)
75 continue;
76 path = rr->items[i].path;
77 length = strlen(path) + 1;
78 if (write_in_full(out_fd, rr->items[i].util, 40) != 40 ||
79 write_in_full(out_fd, "\t", 1) != 1 ||
80 write_in_full(out_fd, path, length) != length)
81 die("unable to write rerere record");
83 if (commit_lock_file(&write_lock) != 0)
84 die("unable to write rerere record");
85 return 0;
88 static int handle_file(const char *path,
89 unsigned char *sha1, const char *output)
91 SHA_CTX ctx;
92 char buf[1024];
93 int hunk = 0, hunk_no = 0;
94 struct strbuf one, two;
95 FILE *f = fopen(path, "r");
96 FILE *out = NULL;
98 if (!f)
99 return error("Could not open %s", path);
101 if (output) {
102 out = fopen(output, "w");
103 if (!out) {
104 fclose(f);
105 return error("Could not write %s", output);
109 if (sha1)
110 SHA1_Init(&ctx);
112 strbuf_init(&one, 0);
113 strbuf_init(&two, 0);
114 while (fgets(buf, sizeof(buf), f)) {
115 if (!prefixcmp(buf, "<<<<<<< "))
116 hunk = 1;
117 else if (!prefixcmp(buf, "======="))
118 hunk = 2;
119 else if (!prefixcmp(buf, ">>>>>>> ")) {
120 if (strbuf_cmp(&one, &two) > 0)
121 strbuf_swap(&one, &two);
122 hunk_no++;
123 hunk = 0;
124 if (out) {
125 fputs("<<<<<<<\n", out);
126 fwrite(one.buf, one.len, 1, out);
127 fputs("=======\n", out);
128 fwrite(two.buf, two.len, 1, out);
129 fputs(">>>>>>>\n", out);
131 if (sha1) {
132 SHA1_Update(&ctx, one.buf ? one.buf : "",
133 one.len + 1);
134 SHA1_Update(&ctx, two.buf ? two.buf : "",
135 two.len + 1);
137 strbuf_reset(&one);
138 strbuf_reset(&two);
139 } else if (hunk == 1)
140 strbuf_addstr(&one, buf);
141 else if (hunk == 2)
142 strbuf_addstr(&two, buf);
143 else if (out)
144 fputs(buf, out);
146 strbuf_release(&one);
147 strbuf_release(&two);
149 fclose(f);
150 if (out)
151 fclose(out);
152 if (sha1)
153 SHA1_Final(sha1, &ctx);
154 if (hunk) {
155 if (output)
156 unlink(output);
157 return error("Could not parse conflict hunks in %s", path);
159 return hunk_no;
162 static int find_conflict(struct path_list *conflict)
164 int i;
165 if (read_cache() < 0)
166 return error("Could not read index");
167 for (i = 0; i+1 < active_nr; i++) {
168 struct cache_entry *e2 = active_cache[i];
169 struct cache_entry *e3 = active_cache[i+1];
170 if (ce_stage(e2) == 2 &&
171 ce_stage(e3) == 3 &&
172 ce_same_name(e2, e3) &&
173 S_ISREG(e2->ce_mode) &&
174 S_ISREG(e3->ce_mode)) {
175 path_list_insert((const char *)e2->name, conflict);
176 i++; /* skip over both #2 and #3 */
179 return 0;
182 static int merge(const char *name, const char *path)
184 int ret;
185 mmfile_t cur, base, other;
186 mmbuffer_t result = {NULL, 0};
187 xpparam_t xpp = {XDF_NEED_MINIMAL};
189 if (handle_file(path, NULL, rr_path(name, "thisimage")) < 0)
190 return 1;
192 if (read_mmfile(&cur, rr_path(name, "thisimage")) ||
193 read_mmfile(&base, rr_path(name, "preimage")) ||
194 read_mmfile(&other, rr_path(name, "postimage")))
195 return 1;
196 ret = xdl_merge(&base, &cur, "", &other, "",
197 &xpp, XDL_MERGE_ZEALOUS, &result);
198 if (!ret) {
199 FILE *f = fopen(path, "w");
200 if (!f)
201 return error("Could not write to %s", path);
202 fwrite(result.ptr, result.size, 1, f);
203 fclose(f);
206 free(cur.ptr);
207 free(base.ptr);
208 free(other.ptr);
209 free(result.ptr);
211 return ret;
214 static void unlink_rr_item(const char *name)
216 unlink(rr_path(name, "thisimage"));
217 unlink(rr_path(name, "preimage"));
218 unlink(rr_path(name, "postimage"));
219 rmdir(git_path("rr-cache/%s", name));
222 static void garbage_collect(struct path_list *rr)
224 struct path_list to_remove = { NULL, 0, 0, 1 };
225 DIR *dir;
226 struct dirent *e;
227 int i, cutoff;
228 time_t now = time(NULL), then;
230 dir = opendir(git_path("rr-cache"));
231 while ((e = readdir(dir))) {
232 const char *name = e->d_name;
233 if (name[0] == '.' &&
234 (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')))
235 continue;
236 then = rerere_created_at(name);
237 if (!then)
238 continue;
239 cutoff = (has_resolution(name)
240 ? cutoff_resolve : cutoff_noresolve);
241 if (then < now - cutoff * 86400)
242 path_list_append(name, &to_remove);
244 for (i = 0; i < to_remove.nr; i++)
245 unlink_rr_item(to_remove.items[i].path);
246 path_list_clear(&to_remove, 0);
249 static int outf(void *dummy, mmbuffer_t *ptr, int nbuf)
251 int i;
252 for (i = 0; i < nbuf; i++)
253 if (write_in_full(1, ptr[i].ptr, ptr[i].size) != ptr[i].size)
254 return -1;
255 return 0;
258 static int diff_two(const char *file1, const char *label1,
259 const char *file2, const char *label2)
261 xpparam_t xpp;
262 xdemitconf_t xecfg;
263 xdemitcb_t ecb;
264 mmfile_t minus, plus;
266 if (read_mmfile(&minus, file1) || read_mmfile(&plus, file2))
267 return 1;
269 printf("--- a/%s\n+++ b/%s\n", label1, label2);
270 fflush(stdout);
271 xpp.flags = XDF_NEED_MINIMAL;
272 memset(&xecfg, 0, sizeof(xecfg));
273 xecfg.ctxlen = 3;
274 ecb.outf = outf;
275 xdi_diff(&minus, &plus, &xpp, &xecfg, &ecb);
277 free(minus.ptr);
278 free(plus.ptr);
279 return 0;
282 static struct lock_file index_lock;
284 static int update_paths(struct path_list *update)
286 int i;
287 int fd = hold_locked_index(&index_lock, 0);
288 int status = 0;
290 if (fd < 0)
291 return -1;
293 for (i = 0; i < update->nr; i++) {
294 struct path_list_item *item = &update->items[i];
295 if (add_file_to_cache(item->path, ADD_CACHE_IGNORE_ERRORS))
296 status = -1;
299 if (!status && active_cache_changed) {
300 if (write_cache(fd, active_cache, active_nr) ||
301 commit_locked_index(&index_lock))
302 die("Unable to write new index file");
303 } else if (fd >= 0)
304 rollback_lock_file(&index_lock);
305 return status;
308 static int do_plain_rerere(struct path_list *rr, int fd)
310 struct path_list conflict = { NULL, 0, 0, 1 };
311 struct path_list update = { NULL, 0, 0, 1 };
312 int i;
314 find_conflict(&conflict);
317 * MERGE_RR records paths with conflicts immediately after merge
318 * failed. Some of the conflicted paths might have been hand resolved
319 * in the working tree since then, but the initial run would catch all
320 * and register their preimages.
323 for (i = 0; i < conflict.nr; i++) {
324 const char *path = conflict.items[i].path;
325 if (!path_list_has_path(rr, path)) {
326 unsigned char sha1[20];
327 char *hex;
328 int ret;
329 ret = handle_file(path, sha1, NULL);
330 if (ret < 1)
331 continue;
332 hex = xstrdup(sha1_to_hex(sha1));
333 path_list_insert(path, rr)->util = hex;
334 if (mkdir(git_path("rr-cache/%s", hex), 0755))
335 continue;;
336 handle_file(path, NULL, rr_path(hex, "preimage"));
337 fprintf(stderr, "Recorded preimage for '%s'\n", path);
342 * Now some of the paths that had conflicts earlier might have been
343 * hand resolved. Others may be similar to a conflict already that
344 * was resolved before.
347 for (i = 0; i < rr->nr; i++) {
348 int ret;
349 const char *path = rr->items[i].path;
350 const char *name = (const char *)rr->items[i].util;
352 if (has_resolution(name)) {
353 if (!merge(name, path)) {
354 fprintf(stderr, "Resolved '%s' using "
355 "previous resolution.\n", path);
356 if (rerere_autoupdate)
357 path_list_insert(path, &update);
358 goto mark_resolved;
362 /* Let's see if we have resolved it. */
363 ret = handle_file(path, NULL, NULL);
364 if (ret)
365 continue;
367 fprintf(stderr, "Recorded resolution for '%s'.\n", path);
368 copy_file(rr_path(name, "postimage"), path, 0666);
369 mark_resolved:
370 rr->items[i].util = NULL;
373 if (update.nr)
374 update_paths(&update);
376 return write_rr(rr, fd);
379 static int git_rerere_config(const char *var, const char *value, void *cb)
381 if (!strcmp(var, "gc.rerereresolved"))
382 cutoff_resolve = git_config_int(var, value);
383 else if (!strcmp(var, "gc.rerereunresolved"))
384 cutoff_noresolve = git_config_int(var, value);
385 else if (!strcmp(var, "rerere.enabled"))
386 rerere_enabled = git_config_bool(var, value);
387 else if (!strcmp(var, "rerere.autoupdate"))
388 rerere_autoupdate = git_config_bool(var, value);
389 else
390 return git_default_config(var, value, cb);
391 return 0;
394 static int is_rerere_enabled(void)
396 struct stat st;
397 const char *rr_cache;
398 int rr_cache_exists;
400 if (!rerere_enabled)
401 return 0;
403 rr_cache = git_path("rr-cache");
404 rr_cache_exists = !stat(rr_cache, &st) && S_ISDIR(st.st_mode);
405 if (rerere_enabled < 0)
406 return rr_cache_exists;
408 if (!rr_cache_exists &&
409 (mkdir(rr_cache, 0777) || adjust_shared_perm(rr_cache)))
410 die("Could not create directory %s", rr_cache);
411 return 1;
414 static int setup_rerere(struct path_list *merge_rr)
416 int fd;
418 git_config(git_rerere_config, NULL);
419 if (!is_rerere_enabled())
420 return -1;
422 merge_rr_path = xstrdup(git_path("rr-cache/MERGE_RR"));
423 fd = hold_lock_file_for_update(&write_lock, merge_rr_path, 1);
424 read_rr(merge_rr);
425 return fd;
428 int rerere(void)
430 struct path_list merge_rr = { NULL, 0, 0, 1 };
431 int fd;
433 fd = setup_rerere(&merge_rr);
434 if (fd < 0)
435 return 0;
436 return do_plain_rerere(&merge_rr, fd);
439 int cmd_rerere(int argc, const char **argv, const char *prefix)
441 struct path_list merge_rr = { NULL, 0, 0, 1 };
442 int i, fd;
444 fd = setup_rerere(&merge_rr);
445 if (fd < 0)
446 return 0;
448 if (argc < 2)
449 return do_plain_rerere(&merge_rr, fd);
450 else if (!strcmp(argv[1], "clear")) {
451 for (i = 0; i < merge_rr.nr; i++) {
452 const char *name = (const char *)merge_rr.items[i].util;
453 if (!has_resolution(name))
454 unlink_rr_item(name);
456 unlink(merge_rr_path);
457 } else if (!strcmp(argv[1], "gc"))
458 garbage_collect(&merge_rr);
459 else if (!strcmp(argv[1], "status"))
460 for (i = 0; i < merge_rr.nr; i++)
461 printf("%s\n", merge_rr.items[i].path);
462 else if (!strcmp(argv[1], "diff"))
463 for (i = 0; i < merge_rr.nr; i++) {
464 const char *path = merge_rr.items[i].path;
465 const char *name = (const char *)merge_rr.items[i].util;
466 diff_two(rr_path(name, "preimage"), path, path, path);
468 else
469 usage(git_rerere_usage);
471 path_list_clear(&merge_rr, 1);
472 return 0;