Code

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