Merge branch 'maint'
[git] / rerere.c
1 #include "cache.h"
2 #include "string-list.h"
3 #include "rerere.h"
4 #include "xdiff/xdiff.h"
5 #include "xdiff-interface.h"
6
7 /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
8 static int rerere_enabled = -1;
9
10 /* automatically update cleanly resolved paths to the index */
11 static int rerere_autoupdate;
12
13 static char *merge_rr_path;
14
15 static const char *rr_path(const char *name, const char *file)
16 {
17         return git_path("rr-cache/%s/%s", name, file);
18 }
19
20 static int has_resolution(const char *name)
21 {
22         struct stat st;
23         return !stat(rr_path(name, "postimage"), &st);
24 }
25
26 static void read_rr(struct string_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                 string_list_insert(buf, rr)->util = name;
47         }
48         fclose(in);
49 }
50
51 static struct lock_file write_lock;
52
53 static int write_rr(struct string_list *rr, int out_fd)
54 {
55         int i;
56         for (i = 0; i < rr->nr; i++) {
57                 const char *path;
58                 int length;
59                 if (!rr->items[i].util)
60                         continue;
61                 path = rr->items[i].string;
62                 length = strlen(path) + 1;
63                 if (write_in_full(out_fd, rr->items[i].util, 40) != 40 ||
64                     write_in_full(out_fd, "\t", 1) != 1 ||
65                     write_in_full(out_fd, path, length) != length)
66                         die("unable to write rerere record");
67         }
68         if (commit_lock_file(&write_lock) != 0)
69                 die("unable to write rerere record");
70         return 0;
71 }
72
73 static int handle_file(const char *path,
74          unsigned char *sha1, const char *output)
75 {
76         SHA_CTX ctx;
77         char buf[1024];
78         int hunk = 0, hunk_no = 0;
79         struct strbuf one, two;
80         FILE *f = fopen(path, "r");
81         FILE *out = NULL;
82
83         if (!f)
84                 return error("Could not open %s", path);
85
86         if (output) {
87                 out = fopen(output, "w");
88                 if (!out) {
89                         fclose(f);
90                         return error("Could not write %s", output);
91                 }
92         }
93
94         if (sha1)
95                 SHA1_Init(&ctx);
96
97         strbuf_init(&one, 0);
98         strbuf_init(&two,  0);
99         while (fgets(buf, sizeof(buf), f)) {
100                 if (!prefixcmp(buf, "<<<<<<< ")) {
101                         if (hunk)
102                                 goto bad;
103                         hunk = 1;
104                 } else if (!prefixcmp(buf, "=======") && isspace(buf[7])) {
105                         if (hunk != 1)
106                                 goto bad;
107                         hunk = 2;
108                 } else if (!prefixcmp(buf, ">>>>>>> ")) {
109                         if (hunk != 2)
110                                 goto bad;
111                         if (strbuf_cmp(&one, &two) > 0)
112                                 strbuf_swap(&one, &two);
113                         hunk_no++;
114                         hunk = 0;
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);
121                         }
122                         if (sha1) {
123                                 SHA1_Update(&ctx, one.buf ? one.buf : "",
124                                             one.len + 1);
125                                 SHA1_Update(&ctx, two.buf ? two.buf : "",
126                                             two.len + 1);
127                         }
128                         strbuf_reset(&one);
129                         strbuf_reset(&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);
136                 continue;
137         bad:
138                 hunk = 99; /* force error exit */
139                 break;
140         }
141         strbuf_release(&one);
142         strbuf_release(&two);
143
144         fclose(f);
145         if (out)
146                 fclose(out);
147         if (sha1)
148                 SHA1_Final(sha1, &ctx);
149         if (hunk) {
150                 if (output)
151                         unlink(output);
152                 return error("Could not parse conflict hunks in %s", path);
153         }
154         return hunk_no;
155 }
156
157 static int find_conflict(struct string_list *conflict)
158 {
159         int i;
160         if (read_cache() < 0)
161                 return error("Could not read index");
162         for (i = 0; i+1 < active_nr; i++) {
163                 struct cache_entry *e2 = active_cache[i];
164                 struct cache_entry *e3 = active_cache[i+1];
165                 if (ce_stage(e2) == 2 &&
166                     ce_stage(e3) == 3 &&
167                     ce_same_name(e2, e3) &&
168                     S_ISREG(e2->ce_mode) &&
169                     S_ISREG(e3->ce_mode)) {
170                         string_list_insert((const char *)e2->name, conflict);
171                         i++; /* skip over both #2 and #3 */
172                 }
173         }
174         return 0;
175 }
176
177 static int merge(const char *name, const char *path)
178 {
179         int ret;
180         mmfile_t cur, base, other;
181         mmbuffer_t result = {NULL, 0};
182         xpparam_t xpp = {XDF_NEED_MINIMAL};
183
184         if (handle_file(path, NULL, rr_path(name, "thisimage")) < 0)
185                 return 1;
186
187         if (read_mmfile(&cur, rr_path(name, "thisimage")) ||
188                         read_mmfile(&base, rr_path(name, "preimage")) ||
189                         read_mmfile(&other, rr_path(name, "postimage")))
190                 return 1;
191         ret = xdl_merge(&base, &cur, "", &other, "",
192                         &xpp, XDL_MERGE_ZEALOUS, &result);
193         if (!ret) {
194                 FILE *f = fopen(path, "w");
195                 if (!f)
196                         return error("Could not write to %s", path);
197                 fwrite(result.ptr, result.size, 1, f);
198                 fclose(f);
199         }
200
201         free(cur.ptr);
202         free(base.ptr);
203         free(other.ptr);
204         free(result.ptr);
205
206         return ret;
207 }
208
209 static struct lock_file index_lock;
210
211 static int update_paths(struct string_list *update)
212 {
213         int i;
214         int fd = hold_locked_index(&index_lock, 0);
215         int status = 0;
216
217         if (fd < 0)
218                 return -1;
219
220         for (i = 0; i < update->nr; i++) {
221                 struct string_list_item *item = &update->items[i];
222                 if (add_file_to_cache(item->string, ADD_CACHE_IGNORE_ERRORS))
223                         status = -1;
224         }
225
226         if (!status && active_cache_changed) {
227                 if (write_cache(fd, active_cache, active_nr) ||
228                     commit_locked_index(&index_lock))
229                         die("Unable to write new index file");
230         } else if (fd >= 0)
231                 rollback_lock_file(&index_lock);
232         return status;
233 }
234
235 static int do_plain_rerere(struct string_list *rr, int fd)
236 {
237         struct string_list conflict = { NULL, 0, 0, 1 };
238         struct string_list update = { NULL, 0, 0, 1 };
239         int i;
240
241         find_conflict(&conflict);
242
243         /*
244          * MERGE_RR records paths with conflicts immediately after merge
245          * failed.  Some of the conflicted paths might have been hand resolved
246          * in the working tree since then, but the initial run would catch all
247          * and register their preimages.
248          */
249
250         for (i = 0; i < conflict.nr; i++) {
251                 const char *path = conflict.items[i].string;
252                 if (!string_list_has_string(rr, path)) {
253                         unsigned char sha1[20];
254                         char *hex;
255                         int ret;
256                         ret = handle_file(path, sha1, NULL);
257                         if (ret < 1)
258                                 continue;
259                         hex = xstrdup(sha1_to_hex(sha1));
260                         string_list_insert(path, rr)->util = hex;
261                         if (mkdir(git_path("rr-cache/%s", hex), 0755))
262                                 continue;;
263                         handle_file(path, NULL, rr_path(hex, "preimage"));
264                         fprintf(stderr, "Recorded preimage for '%s'\n", path);
265                 }
266         }
267
268         /*
269          * Now some of the paths that had conflicts earlier might have been
270          * hand resolved.  Others may be similar to a conflict already that
271          * was resolved before.
272          */
273
274         for (i = 0; i < rr->nr; i++) {
275                 int ret;
276                 const char *path = rr->items[i].string;
277                 const char *name = (const char *)rr->items[i].util;
278
279                 if (has_resolution(name)) {
280                         if (!merge(name, path)) {
281                                 if (rerere_autoupdate)
282                                         string_list_insert(path, &update);
283                                 fprintf(stderr,
284                                         "%s '%s' using previous resolution.\n",
285                                         rerere_autoupdate
286                                         ? "Staged" : "Resolved",
287                                         path);
288                                 goto mark_resolved;
289                         }
290                 }
291
292                 /* Let's see if we have resolved it. */
293                 ret = handle_file(path, NULL, NULL);
294                 if (ret)
295                         continue;
296
297                 fprintf(stderr, "Recorded resolution for '%s'.\n", path);
298                 copy_file(rr_path(name, "postimage"), path, 0666);
299         mark_resolved:
300                 rr->items[i].util = NULL;
301         }
302
303         if (update.nr)
304                 update_paths(&update);
305
306         return write_rr(rr, fd);
307 }
308
309 static int git_rerere_config(const char *var, const char *value, void *cb)
310 {
311         if (!strcmp(var, "rerere.enabled"))
312                 rerere_enabled = git_config_bool(var, value);
313         else if (!strcmp(var, "rerere.autoupdate"))
314                 rerere_autoupdate = git_config_bool(var, value);
315         else
316                 return git_default_config(var, value, cb);
317         return 0;
318 }
319
320 static int is_rerere_enabled(void)
321 {
322         struct stat st;
323         const char *rr_cache;
324         int rr_cache_exists;
325
326         if (!rerere_enabled)
327                 return 0;
328
329         rr_cache = git_path("rr-cache");
330         rr_cache_exists = !stat(rr_cache, &st) && S_ISDIR(st.st_mode);
331         if (rerere_enabled < 0)
332                 return rr_cache_exists;
333
334         if (!rr_cache_exists &&
335             (mkdir(rr_cache, 0777) || adjust_shared_perm(rr_cache)))
336                 die("Could not create directory %s", rr_cache);
337         return 1;
338 }
339
340 int setup_rerere(struct string_list *merge_rr)
341 {
342         int fd;
343
344         git_config(git_rerere_config, NULL);
345         if (!is_rerere_enabled())
346                 return -1;
347
348         merge_rr_path = xstrdup(git_path("MERGE_RR"));
349         fd = hold_lock_file_for_update(&write_lock, merge_rr_path, 1);
350         read_rr(merge_rr);
351         return fd;
352 }
353
354 int rerere(void)
355 {
356         struct string_list merge_rr = { NULL, 0, 0, 1 };
357         int fd;
358
359         fd = setup_rerere(&merge_rr);
360         if (fd < 0)
361                 return 0;
362         return do_plain_rerere(&merge_rr, fd);
363 }