Make builtin-rerere use of strbuf nicer and more efficient.
[git/dscho.git] / builtin-rerere.c
blobd331772e100d87592d091aa5ad9c38d36bea86c2
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 one, two;
76 FILE *f = fopen(path, "r");
77 FILE *out = NULL;
79 if (!f)
80 return error("Could not open %s", path);
82 if (output) {
83 out = fopen(output, "w");
84 if (!out) {
85 fclose(f);
86 return error("Could not write %s", output);
90 if (sha1)
91 SHA1_Init(&ctx);
93 strbuf_init(&one, 0);
94 strbuf_init(&two, 0);
95 while (fgets(buf, sizeof(buf), f)) {
96 if (!prefixcmp(buf, "<<<<<<< "))
97 hunk = 1;
98 else if (!prefixcmp(buf, "======="))
99 hunk = 2;
100 else if (!prefixcmp(buf, ">>>>>>> ")) {
101 int cmp = strbuf_cmp(&one, &two);
103 hunk_no++;
104 hunk = 0;
105 if (cmp > 0) {
106 strbuf_swap(&one, &two);
108 if (out) {
109 fputs("<<<<<<<\n", out);
110 fwrite(one.buf, one.len, 1, out);
111 fputs("=======\n", out);
112 fwrite(two.buf, two.len, 1, out);
113 fputs(">>>>>>>\n", out);
115 if (sha1) {
116 SHA1_Update(&ctx, one.buf, one.len + 1);
117 SHA1_Update(&ctx, two.buf, two.len + 1);
119 strbuf_reset(&one);
120 strbuf_reset(&two);
121 } else if (hunk == 1)
122 strbuf_addstr(&one, buf);
123 else if (hunk == 2)
124 strbuf_addstr(&two, buf);
125 else if (out)
126 fputs(buf, out);
128 strbuf_release(&one);
129 strbuf_release(&two);
131 fclose(f);
132 if (out)
133 fclose(out);
134 if (sha1)
135 SHA1_Final(sha1, &ctx);
136 return hunk_no;
139 static int find_conflict(struct path_list *conflict)
141 int i;
142 if (read_cache() < 0)
143 return error("Could not read index");
144 for (i = 0; i+1 < active_nr; i++) {
145 struct cache_entry *e2 = active_cache[i];
146 struct cache_entry *e3 = active_cache[i+1];
147 if (ce_stage(e2) == 2 &&
148 ce_stage(e3) == 3 &&
149 ce_same_name(e2, e3) &&
150 S_ISREG(ntohl(e2->ce_mode)) &&
151 S_ISREG(ntohl(e3->ce_mode))) {
152 path_list_insert((const char *)e2->name, conflict);
153 i++; /* skip over both #2 and #3 */
156 return 0;
159 static int merge(const char *name, const char *path)
161 int ret;
162 mmfile_t cur, base, other;
163 mmbuffer_t result = {NULL, 0};
164 xpparam_t xpp = {XDF_NEED_MINIMAL};
166 if (handle_file(path, NULL, rr_path(name, "thisimage")) < 0)
167 return 1;
169 if (read_mmfile(&cur, rr_path(name, "thisimage")) ||
170 read_mmfile(&base, rr_path(name, "preimage")) ||
171 read_mmfile(&other, rr_path(name, "postimage")))
172 return 1;
173 ret = xdl_merge(&base, &cur, "", &other, "",
174 &xpp, XDL_MERGE_ZEALOUS, &result);
175 if (!ret) {
176 FILE *f = fopen(path, "w");
177 if (!f)
178 return error("Could not write to %s", path);
179 fwrite(result.ptr, result.size, 1, f);
180 fclose(f);
183 free(cur.ptr);
184 free(base.ptr);
185 free(other.ptr);
186 free(result.ptr);
188 return ret;
191 static void unlink_rr_item(const char *name)
193 unlink(rr_path(name, "thisimage"));
194 unlink(rr_path(name, "preimage"));
195 unlink(rr_path(name, "postimage"));
196 rmdir(git_path("rr-cache/%s", name));
199 static void garbage_collect(struct path_list *rr)
201 struct path_list to_remove = { NULL, 0, 0, 1 };
202 char buf[1024];
203 DIR *dir;
204 struct dirent *e;
205 int len, i, cutoff;
206 time_t now = time(NULL), then;
208 strlcpy(buf, git_path("rr-cache"), sizeof(buf));
209 len = strlen(buf);
210 dir = opendir(buf);
211 strcpy(buf + len++, "/");
212 while ((e = readdir(dir))) {
213 const char *name = e->d_name;
214 struct stat st;
215 if (name[0] == '.' && (name[1] == '\0' ||
216 (name[1] == '.' && name[2] == '\0')))
217 continue;
218 i = snprintf(buf + len, sizeof(buf) - len, "%s", name);
219 strlcpy(buf + len + i, "/preimage", sizeof(buf) - len - i);
220 if (stat(buf, &st))
221 continue;
222 then = st.st_mtime;
223 strlcpy(buf + len + i, "/postimage", sizeof(buf) - len - i);
224 cutoff = stat(buf, &st) ? cutoff_noresolve : cutoff_resolve;
225 if (then < now - cutoff * 86400) {
226 buf[len + i] = '\0';
227 path_list_insert(xstrdup(name), &to_remove);
230 for (i = 0; i < to_remove.nr; i++)
231 unlink_rr_item(to_remove.items[i].path);
232 path_list_clear(&to_remove, 0);
235 static int outf(void *dummy, mmbuffer_t *ptr, int nbuf)
237 int i;
238 for (i = 0; i < nbuf; i++)
239 if (write_in_full(1, ptr[i].ptr, ptr[i].size) != ptr[i].size)
240 return -1;
241 return 0;
244 static int diff_two(const char *file1, const char *label1,
245 const char *file2, const char *label2)
247 xpparam_t xpp;
248 xdemitconf_t xecfg;
249 xdemitcb_t ecb;
250 mmfile_t minus, plus;
252 if (read_mmfile(&minus, file1) || read_mmfile(&plus, file2))
253 return 1;
255 printf("--- a/%s\n+++ b/%s\n", label1, label2);
256 fflush(stdout);
257 xpp.flags = XDF_NEED_MINIMAL;
258 memset(&xecfg, 0, sizeof(xecfg));
259 xecfg.ctxlen = 3;
260 ecb.outf = outf;
261 xdl_diff(&minus, &plus, &xpp, &xecfg, &ecb);
263 free(minus.ptr);
264 free(plus.ptr);
265 return 0;
268 static int copy_file(const char *src, const char *dest)
270 FILE *in, *out;
271 char buffer[32768];
272 int count;
274 if (!(in = fopen(src, "r")))
275 return error("Could not open %s", src);
276 if (!(out = fopen(dest, "w")))
277 return error("Could not open %s", dest);
278 while ((count = fread(buffer, 1, sizeof(buffer), in)))
279 fwrite(buffer, 1, count, out);
280 fclose(in);
281 fclose(out);
282 return 0;
285 static int do_plain_rerere(struct path_list *rr, int fd)
287 struct path_list conflict = { NULL, 0, 0, 1 };
288 int i;
290 find_conflict(&conflict);
293 * MERGE_RR records paths with conflicts immediately after merge
294 * failed. Some of the conflicted paths might have been hand resolved
295 * in the working tree since then, but the initial run would catch all
296 * and register their preimages.
299 for (i = 0; i < conflict.nr; i++) {
300 const char *path = conflict.items[i].path;
301 if (!path_list_has_path(rr, path)) {
302 unsigned char sha1[20];
303 char *hex;
304 int ret;
305 ret = handle_file(path, sha1, NULL);
306 if (ret < 1)
307 continue;
308 hex = xstrdup(sha1_to_hex(sha1));
309 path_list_insert(path, rr)->util = hex;
310 if (mkdir(git_path("rr-cache/%s", hex), 0755))
311 continue;;
312 handle_file(path, NULL, rr_path(hex, "preimage"));
313 fprintf(stderr, "Recorded preimage for '%s'\n", path);
318 * Now some of the paths that had conflicts earlier might have been
319 * hand resolved. Others may be similar to a conflict already that
320 * was resolved before.
323 for (i = 0; i < rr->nr; i++) {
324 struct stat st;
325 int ret;
326 const char *path = rr->items[i].path;
327 const char *name = (const char *)rr->items[i].util;
329 if (!stat(rr_path(name, "preimage"), &st) &&
330 !stat(rr_path(name, "postimage"), &st)) {
331 if (!merge(name, path)) {
332 fprintf(stderr, "Resolved '%s' using "
333 "previous resolution.\n", path);
334 goto tail_optimization;
338 /* Let's see if we have resolved it. */
339 ret = handle_file(path, NULL, NULL);
340 if (ret)
341 continue;
343 fprintf(stderr, "Recorded resolution for '%s'.\n", path);
344 copy_file(path, rr_path(name, "postimage"));
345 tail_optimization:
346 if (i < rr->nr - 1)
347 memmove(rr->items + i,
348 rr->items + i + 1,
349 sizeof(rr->items[0]) * (rr->nr - i - 1));
350 rr->nr--;
351 i--;
354 return write_rr(rr, fd);
357 static int git_rerere_config(const char *var, const char *value)
359 if (!strcmp(var, "gc.rerereresolved"))
360 cutoff_resolve = git_config_int(var, value);
361 else if (!strcmp(var, "gc.rerereunresolved"))
362 cutoff_noresolve = git_config_int(var, value);
363 else if (!strcmp(var, "rerere.enabled"))
364 rerere_enabled = git_config_bool(var, value);
365 else
366 return git_default_config(var, value);
367 return 0;
370 static int is_rerere_enabled(void)
372 struct stat st;
373 const char *rr_cache;
374 int rr_cache_exists;
376 if (!rerere_enabled)
377 return 0;
379 rr_cache = git_path("rr-cache");
380 rr_cache_exists = !stat(rr_cache, &st) && S_ISDIR(st.st_mode);
381 if (rerere_enabled < 0)
382 return rr_cache_exists;
384 if (!rr_cache_exists &&
385 (mkdir(rr_cache, 0777) || adjust_shared_perm(rr_cache)))
386 die("Could not create directory %s", rr_cache);
387 return 1;
390 int cmd_rerere(int argc, const char **argv, const char *prefix)
392 struct path_list merge_rr = { NULL, 0, 0, 1 };
393 int i, fd = -1;
395 git_config(git_rerere_config);
396 if (!is_rerere_enabled())
397 return 0;
399 merge_rr_path = xstrdup(git_path("rr-cache/MERGE_RR"));
400 fd = hold_lock_file_for_update(&write_lock, merge_rr_path, 1);
401 read_rr(&merge_rr);
403 if (argc < 2)
404 return do_plain_rerere(&merge_rr, fd);
405 else if (!strcmp(argv[1], "clear")) {
406 for (i = 0; i < merge_rr.nr; i++) {
407 struct stat st;
408 const char *name = (const char *)merge_rr.items[i].util;
409 if (!stat(git_path("rr-cache/%s", name), &st) &&
410 S_ISDIR(st.st_mode) &&
411 stat(rr_path(name, "postimage"), &st))
412 unlink_rr_item(name);
414 unlink(merge_rr_path);
415 } else if (!strcmp(argv[1], "gc"))
416 garbage_collect(&merge_rr);
417 else if (!strcmp(argv[1], "status"))
418 for (i = 0; i < merge_rr.nr; i++)
419 printf("%s\n", merge_rr.items[i].path);
420 else if (!strcmp(argv[1], "diff"))
421 for (i = 0; i < merge_rr.nr; i++) {
422 const char *path = merge_rr.items[i].path;
423 const char *name = (const char *)merge_rr.items[i].util;
424 diff_two(rr_path(name, "preimage"), path, path, path);
426 else
427 usage(git_rerere_usage);
429 path_list_clear(&merge_rr, 1);
430 return 0;