rerere: refactor rerere logic to make it independent from I/O
[git/git.git] / rerere.c
CommitLineData
5b2fd956 1#include "cache.h"
c455c87c 2#include "string-list.h"
5b2fd956
SB
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 */
8static int rerere_enabled = -1;
9
10/* automatically update cleanly resolved paths to the index */
11static int rerere_autoupdate;
12
13static char *merge_rr_path;
14
90056966 15const char *rerere_path(const char *hex, const char *file)
5b2fd956 16{
90056966 17 return git_path("rr-cache/%s/%s", hex, file);
5b2fd956
SB
18}
19
90056966 20int has_rerere_resolution(const char *hex)
5b2fd956
SB
21{
22 struct stat st;
90056966 23 return !stat(rerere_path(hex, "postimage"), &st);
5b2fd956
SB
24}
25
c455c87c 26static void read_rr(struct string_list *rr)
5b2fd956
SB
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");
c455c87c 46 string_list_insert(buf, rr)->util = name;
5b2fd956
SB
47 }
48 fclose(in);
49}
50
51static struct lock_file write_lock;
52
c455c87c 53static int write_rr(struct string_list *rr, int out_fd)
5b2fd956
SB
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;
c455c87c 61 path = rr->items[i].string;
5b2fd956
SB
62 length = strlen(path) + 1;
63 if (write_in_full(out_fd, rr->items[i].util, 40) != 40 ||
2b7ca830 64 write_str_in_full(out_fd, "\t") != 1 ||
5b2fd956
SB
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
47d32af2
AR
73static void ferr_write(const void *p, size_t count, FILE *fp, int *err)
74{
75 if (!count || *err)
76 return;
77 if (fwrite(p, count, 1, fp) != 1)
78 *err = errno;
79}
80
81static inline void ferr_puts(const char *s, FILE *fp, int *err)
82{
83 ferr_write(s, strlen(s), fp, err);
84}
85
27d6b085
JH
86struct rerere_io {
87 int (*getline)(struct strbuf *, struct rerere_io *);
88 FILE *output;
89 int wrerror;
90 /* some more stuff */
91};
92
93static void rerere_io_putstr(const char *str, struct rerere_io *io)
94{
95 if (io->output)
96 ferr_puts(str, io->output, &io->wrerror);
97}
98
99static void rerere_io_putmem(const char *mem, size_t sz, struct rerere_io *io)
100{
101 if (io->output)
102 ferr_write(mem, sz, io->output, &io->wrerror);
103}
104
105struct rerere_io_file {
106 struct rerere_io io;
107 FILE *input;
108};
109
110static int rerere_file_getline(struct strbuf *sb, struct rerere_io *io_)
111{
112 struct rerere_io_file *io = (struct rerere_io_file *)io_;
113 return strbuf_getwholeline(sb, io->input, '\n');
114}
115
116static int handle_path(unsigned char *sha1, struct rerere_io *io)
5b2fd956 117{
9126f009 118 git_SHA_CTX ctx;
cc58d7df
JH
119 int hunk_no = 0;
120 enum {
387c9d49 121 RR_CONTEXT = 0, RR_SIDE_1, RR_SIDE_2, RR_ORIGINAL,
cc58d7df 122 } hunk = RR_CONTEXT;
f285a2d7 123 struct strbuf one = STRBUF_INIT, two = STRBUF_INIT;
d58ee6db 124 struct strbuf buf = STRBUF_INIT;
5b2fd956
SB
125
126 if (sha1)
9126f009 127 git_SHA1_Init(&ctx);
5b2fd956 128
27d6b085 129 while (!io->getline(&buf, io)) {
d58ee6db 130 if (!prefixcmp(buf.buf, "<<<<<<< ")) {
cc58d7df 131 if (hunk != RR_CONTEXT)
5b2fd956 132 goto bad;
cc58d7df 133 hunk = RR_SIDE_1;
d58ee6db 134 } else if (!prefixcmp(buf.buf, "|||||||") && isspace(buf.buf[7])) {
cc58d7df 135 if (hunk != RR_SIDE_1)
5b2fd956 136 goto bad;
387c9d49 137 hunk = RR_ORIGINAL;
d58ee6db 138 } else if (!prefixcmp(buf.buf, "=======") && isspace(buf.buf[7])) {
387c9d49 139 if (hunk != RR_SIDE_1 && hunk != RR_ORIGINAL)
5b2fd956 140 goto bad;
cc58d7df 141 hunk = RR_SIDE_2;
d58ee6db 142 } else if (!prefixcmp(buf.buf, ">>>>>>> ")) {
cc58d7df 143 if (hunk != RR_SIDE_2)
5b2fd956
SB
144 goto bad;
145 if (strbuf_cmp(&one, &two) > 0)
146 strbuf_swap(&one, &two);
147 hunk_no++;
cc58d7df 148 hunk = RR_CONTEXT;
27d6b085
JH
149 rerere_io_putstr("<<<<<<<\n", io);
150 rerere_io_putmem(one.buf, one.len, io);
151 rerere_io_putstr("=======\n", io);
152 rerere_io_putmem(two.buf, two.len, io);
153 rerere_io_putstr(">>>>>>>\n", io);
5b2fd956 154 if (sha1) {
9126f009 155 git_SHA1_Update(&ctx, one.buf ? one.buf : "",
5b2fd956 156 one.len + 1);
9126f009 157 git_SHA1_Update(&ctx, two.buf ? two.buf : "",
5b2fd956
SB
158 two.len + 1);
159 }
160 strbuf_reset(&one);
161 strbuf_reset(&two);
cc58d7df 162 } else if (hunk == RR_SIDE_1)
d58ee6db 163 strbuf_addstr(&one, buf.buf);
387c9d49
JH
164 else if (hunk == RR_ORIGINAL)
165 ; /* discard */
cc58d7df 166 else if (hunk == RR_SIDE_2)
d58ee6db 167 strbuf_addstr(&two, buf.buf);
27d6b085
JH
168 else
169 rerere_io_putstr(buf.buf, io);
5b2fd956
SB
170 continue;
171 bad:
172 hunk = 99; /* force error exit */
173 break;
174 }
175 strbuf_release(&one);
176 strbuf_release(&two);
d58ee6db 177 strbuf_release(&buf);
5b2fd956 178
5b2fd956 179 if (sha1)
9126f009 180 git_SHA1_Final(sha1, &ctx);
27d6b085
JH
181 if (hunk != RR_CONTEXT)
182 return -1;
183 return hunk_no;
184}
185
186static int handle_file(const char *path, unsigned char *sha1, const char *output)
187{
188 int hunk_no = 0;
189 struct rerere_io_file io;
190
191 memset(&io, 0, sizeof(io));
192 io.io.getline = rerere_file_getline;
193 io.input = fopen(path, "r");
194 io.io.wrerror = 0;
195 if (!io.input)
196 return error("Could not open %s", path);
197
198 if (output) {
199 io.io.output = fopen(output, "w");
200 if (!io.io.output) {
201 fclose(io.input);
202 return error("Could not write %s", output);
203 }
204 }
205
206 hunk_no = handle_path(sha1, (struct rerere_io *)&io);
207
208 fclose(io.input);
209 if (io.io.wrerror)
210 error("There were errors while writing %s (%s)",
211 path, strerror(io.io.wrerror));
212 if (io.io.output && fclose(io.io.output))
213 io.io.wrerror = error("Failed to flush %s: %s",
214 path, strerror(errno));
215
216 if (hunk_no < 0) {
5b2fd956 217 if (output)
691f1a28 218 unlink_or_warn(output);
5b2fd956
SB
219 return error("Could not parse conflict hunks in %s", path);
220 }
27d6b085 221 if (io.io.wrerror)
47d32af2 222 return -1;
5b2fd956
SB
223 return hunk_no;
224}
225
c455c87c 226static int find_conflict(struct string_list *conflict)
5b2fd956
SB
227{
228 int i;
229 if (read_cache() < 0)
230 return error("Could not read index");
231 for (i = 0; i+1 < active_nr; i++) {
232 struct cache_entry *e2 = active_cache[i];
233 struct cache_entry *e3 = active_cache[i+1];
234 if (ce_stage(e2) == 2 &&
235 ce_stage(e3) == 3 &&
236 ce_same_name(e2, e3) &&
237 S_ISREG(e2->ce_mode) &&
238 S_ISREG(e3->ce_mode)) {
c455c87c 239 string_list_insert((const char *)e2->name, conflict);
5b2fd956
SB
240 i++; /* skip over both #2 and #3 */
241 }
242 }
243 return 0;
244}
245
246static int merge(const char *name, const char *path)
247{
248 int ret;
249 mmfile_t cur, base, other;
250 mmbuffer_t result = {NULL, 0};
251 xpparam_t xpp = {XDF_NEED_MINIMAL};
252
90056966 253 if (handle_file(path, NULL, rerere_path(name, "thisimage")) < 0)
5b2fd956
SB
254 return 1;
255
90056966
SG
256 if (read_mmfile(&cur, rerere_path(name, "thisimage")) ||
257 read_mmfile(&base, rerere_path(name, "preimage")) ||
258 read_mmfile(&other, rerere_path(name, "postimage")))
5b2fd956
SB
259 return 1;
260 ret = xdl_merge(&base, &cur, "", &other, "",
261 &xpp, XDL_MERGE_ZEALOUS, &result);
262 if (!ret) {
263 FILE *f = fopen(path, "w");
264 if (!f)
47d32af2
AR
265 return error("Could not open %s: %s", path,
266 strerror(errno));
267 if (fwrite(result.ptr, result.size, 1, f) != 1)
268 error("Could not write %s: %s", path, strerror(errno));
269 if (fclose(f))
270 return error("Writing %s failed: %s", path,
271 strerror(errno));
5b2fd956
SB
272 }
273
274 free(cur.ptr);
275 free(base.ptr);
276 free(other.ptr);
277 free(result.ptr);
278
279 return ret;
280}
281
282static struct lock_file index_lock;
283
c455c87c 284static int update_paths(struct string_list *update)
5b2fd956
SB
285{
286 int i;
287 int fd = hold_locked_index(&index_lock, 0);
288 int status = 0;
289
290 if (fd < 0)
291 return -1;
292
293 for (i = 0; i < update->nr; i++) {
c455c87c
JS
294 struct string_list_item *item = &update->items[i];
295 if (add_file_to_cache(item->string, ADD_CACHE_IGNORE_ERRORS))
5b2fd956
SB
296 status = -1;
297 }
298
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;
306}
307
c455c87c 308static int do_plain_rerere(struct string_list *rr, int fd)
5b2fd956 309{
c455c87c
JS
310 struct string_list conflict = { NULL, 0, 0, 1 };
311 struct string_list update = { NULL, 0, 0, 1 };
5b2fd956
SB
312 int i;
313
314 find_conflict(&conflict);
315
316 /*
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.
321 */
322
323 for (i = 0; i < conflict.nr; i++) {
c455c87c
JS
324 const char *path = conflict.items[i].string;
325 if (!string_list_has_string(rr, path)) {
5b2fd956
SB
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));
c455c87c 333 string_list_insert(path, rr)->util = hex;
5b2fd956 334 if (mkdir(git_path("rr-cache/%s", hex), 0755))
ba19a808 335 continue;
90056966 336 handle_file(path, NULL, rerere_path(hex, "preimage"));
5b2fd956
SB
337 fprintf(stderr, "Recorded preimage for '%s'\n", path);
338 }
339 }
340
341 /*
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.
345 */
346
347 for (i = 0; i < rr->nr; i++) {
348 int ret;
c455c87c 349 const char *path = rr->items[i].string;
5b2fd956
SB
350 const char *name = (const char *)rr->items[i].util;
351
90056966 352 if (has_rerere_resolution(name)) {
5b2fd956 353 if (!merge(name, path)) {
5b2fd956 354 if (rerere_autoupdate)
c455c87c 355 string_list_insert(path, &update);
9196e825
JH
356 fprintf(stderr,
357 "%s '%s' using previous resolution.\n",
358 rerere_autoupdate
359 ? "Staged" : "Resolved",
360 path);
5b2fd956
SB
361 goto mark_resolved;
362 }
363 }
364
365 /* Let's see if we have resolved it. */
366 ret = handle_file(path, NULL, NULL);
367 if (ret)
368 continue;
369
370 fprintf(stderr, "Recorded resolution for '%s'.\n", path);
90056966 371 copy_file(rerere_path(name, "postimage"), path, 0666);
5b2fd956
SB
372 mark_resolved:
373 rr->items[i].util = NULL;
374 }
375
376 if (update.nr)
377 update_paths(&update);
378
379 return write_rr(rr, fd);
380}
381
382static int git_rerere_config(const char *var, const char *value, void *cb)
383{
384 if (!strcmp(var, "rerere.enabled"))
385 rerere_enabled = git_config_bool(var, value);
386 else if (!strcmp(var, "rerere.autoupdate"))
387 rerere_autoupdate = git_config_bool(var, value);
388 else
389 return git_default_config(var, value, cb);
390 return 0;
391}
392
393static int is_rerere_enabled(void)
394{
5b2fd956
SB
395 const char *rr_cache;
396 int rr_cache_exists;
397
398 if (!rerere_enabled)
399 return 0;
400
401 rr_cache = git_path("rr-cache");
90b4a71c 402 rr_cache_exists = is_directory(rr_cache);
5b2fd956
SB
403 if (rerere_enabled < 0)
404 return rr_cache_exists;
405
406 if (!rr_cache_exists &&
407 (mkdir(rr_cache, 0777) || adjust_shared_perm(rr_cache)))
408 die("Could not create directory %s", rr_cache);
409 return 1;
410}
411
c455c87c 412int setup_rerere(struct string_list *merge_rr)
5b2fd956
SB
413{
414 int fd;
415
416 git_config(git_rerere_config, NULL);
417 if (!is_rerere_enabled())
418 return -1;
419
a4f34cbb 420 merge_rr_path = git_pathdup("MERGE_RR");
acd3b9ec
JH
421 fd = hold_lock_file_for_update(&write_lock, merge_rr_path,
422 LOCK_DIE_ON_ERROR);
5b2fd956
SB
423 read_rr(merge_rr);
424 return fd;
425}
426
427int rerere(void)
428{
c455c87c 429 struct string_list merge_rr = { NULL, 0, 0, 1 };
5b2fd956
SB
430 int fd;
431
432 fd = setup_rerere(&merge_rr);
433 if (fd < 0)
434 return 0;
435 return do_plain_rerere(&merge_rr, fd);
436}