rerere: refactor rerere logic to make it independent from I/O
[git/dscho.git] / rerere.c
blobdb1d42f1b61777c0099de01c4e917a57e1f68dca
1 #include "cache.h"
2 #include "string-list.h"
3 #include "rerere.h"
4 #include "xdiff/xdiff.h"
5 #include "xdiff-interface.h"
7 /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
8 static int rerere_enabled = -1;
10 /* automatically update cleanly resolved paths to the index */
11 static int rerere_autoupdate;
13 static char *merge_rr_path;
15 const char *rerere_path(const char *hex, const char *file)
17 return git_path("rr-cache/%s/%s", hex, file);
20 int has_rerere_resolution(const char *hex)
22 struct stat st;
23 return !stat(rerere_path(hex, "postimage"), &st);
26 static void read_rr(struct string_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 string_list_insert(buf, rr)->util = name;
48 fclose(in);
51 static struct lock_file write_lock;
53 static int write_rr(struct string_list *rr, int out_fd)
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_str_in_full(out_fd, "\t") != 1 ||
65 write_in_full(out_fd, path, length) != length)
66 die("unable to write rerere record");
68 if (commit_lock_file(&write_lock) != 0)
69 die("unable to write rerere record");
70 return 0;
73 static void ferr_write(const void *p, size_t count, FILE *fp, int *err)
75 if (!count || *err)
76 return;
77 if (fwrite(p, count, 1, fp) != 1)
78 *err = errno;
81 static inline void ferr_puts(const char *s, FILE *fp, int *err)
83 ferr_write(s, strlen(s), fp, err);
86 struct rerere_io {
87 int (*getline)(struct strbuf *, struct rerere_io *);
88 FILE *output;
89 int wrerror;
90 /* some more stuff */
93 static void rerere_io_putstr(const char *str, struct rerere_io *io)
95 if (io->output)
96 ferr_puts(str, io->output, &io->wrerror);
99 static void rerere_io_putmem(const char *mem, size_t sz, struct rerere_io *io)
101 if (io->output)
102 ferr_write(mem, sz, io->output, &io->wrerror);
105 struct rerere_io_file {
106 struct rerere_io io;
107 FILE *input;
110 static int rerere_file_getline(struct strbuf *sb, struct rerere_io *io_)
112 struct rerere_io_file *io = (struct rerere_io_file *)io_;
113 return strbuf_getwholeline(sb, io->input, '\n');
116 static int handle_path(unsigned char *sha1, struct rerere_io *io)
118 git_SHA_CTX ctx;
119 int hunk_no = 0;
120 enum {
121 RR_CONTEXT = 0, RR_SIDE_1, RR_SIDE_2, RR_ORIGINAL,
122 } hunk = RR_CONTEXT;
123 struct strbuf one = STRBUF_INIT, two = STRBUF_INIT;
124 struct strbuf buf = STRBUF_INIT;
126 if (sha1)
127 git_SHA1_Init(&ctx);
129 while (!io->getline(&buf, io)) {
130 if (!prefixcmp(buf.buf, "<<<<<<< ")) {
131 if (hunk != RR_CONTEXT)
132 goto bad;
133 hunk = RR_SIDE_1;
134 } else if (!prefixcmp(buf.buf, "|||||||") && isspace(buf.buf[7])) {
135 if (hunk != RR_SIDE_1)
136 goto bad;
137 hunk = RR_ORIGINAL;
138 } else if (!prefixcmp(buf.buf, "=======") && isspace(buf.buf[7])) {
139 if (hunk != RR_SIDE_1 && hunk != RR_ORIGINAL)
140 goto bad;
141 hunk = RR_SIDE_2;
142 } else if (!prefixcmp(buf.buf, ">>>>>>> ")) {
143 if (hunk != RR_SIDE_2)
144 goto bad;
145 if (strbuf_cmp(&one, &two) > 0)
146 strbuf_swap(&one, &two);
147 hunk_no++;
148 hunk = RR_CONTEXT;
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);
154 if (sha1) {
155 git_SHA1_Update(&ctx, one.buf ? one.buf : "",
156 one.len + 1);
157 git_SHA1_Update(&ctx, two.buf ? two.buf : "",
158 two.len + 1);
160 strbuf_reset(&one);
161 strbuf_reset(&two);
162 } else if (hunk == RR_SIDE_1)
163 strbuf_addstr(&one, buf.buf);
164 else if (hunk == RR_ORIGINAL)
165 ; /* discard */
166 else if (hunk == RR_SIDE_2)
167 strbuf_addstr(&two, buf.buf);
168 else
169 rerere_io_putstr(buf.buf, io);
170 continue;
171 bad:
172 hunk = 99; /* force error exit */
173 break;
175 strbuf_release(&one);
176 strbuf_release(&two);
177 strbuf_release(&buf);
179 if (sha1)
180 git_SHA1_Final(sha1, &ctx);
181 if (hunk != RR_CONTEXT)
182 return -1;
183 return hunk_no;
186 static int handle_file(const char *path, unsigned char *sha1, const char *output)
188 int hunk_no = 0;
189 struct rerere_io_file io;
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);
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);
206 hunk_no = handle_path(sha1, (struct rerere_io *)&io);
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));
216 if (hunk_no < 0) {
217 if (output)
218 unlink_or_warn(output);
219 return error("Could not parse conflict hunks in %s", path);
221 if (io.io.wrerror)
222 return -1;
223 return hunk_no;
226 static int find_conflict(struct string_list *conflict)
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)) {
239 string_list_insert((const char *)e2->name, conflict);
240 i++; /* skip over both #2 and #3 */
243 return 0;
246 static int merge(const char *name, const char *path)
248 int ret;
249 mmfile_t cur, base, other;
250 mmbuffer_t result = {NULL, 0};
251 xpparam_t xpp = {XDF_NEED_MINIMAL};
253 if (handle_file(path, NULL, rerere_path(name, "thisimage")) < 0)
254 return 1;
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")))
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)
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));
274 free(cur.ptr);
275 free(base.ptr);
276 free(other.ptr);
277 free(result.ptr);
279 return ret;
282 static struct lock_file index_lock;
284 static int update_paths(struct string_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 string_list_item *item = &update->items[i];
295 if (add_file_to_cache(item->string, 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 string_list *rr, int fd)
310 struct string_list conflict = { NULL, 0, 0, 1 };
311 struct string_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].string;
325 if (!string_list_has_string(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 string_list_insert(path, rr)->util = hex;
334 if (mkdir(git_path("rr-cache/%s", hex), 0755))
335 continue;
336 handle_file(path, NULL, rerere_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].string;
350 const char *name = (const char *)rr->items[i].util;
352 if (has_rerere_resolution(name)) {
353 if (!merge(name, path)) {
354 if (rerere_autoupdate)
355 string_list_insert(path, &update);
356 fprintf(stderr,
357 "%s '%s' using previous resolution.\n",
358 rerere_autoupdate
359 ? "Staged" : "Resolved",
360 path);
361 goto mark_resolved;
365 /* Let's see if we have resolved it. */
366 ret = handle_file(path, NULL, NULL);
367 if (ret)
368 continue;
370 fprintf(stderr, "Recorded resolution for '%s'.\n", path);
371 copy_file(rerere_path(name, "postimage"), path, 0666);
372 mark_resolved:
373 rr->items[i].util = NULL;
376 if (update.nr)
377 update_paths(&update);
379 return write_rr(rr, fd);
382 static int git_rerere_config(const char *var, const char *value, void *cb)
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;
393 static int is_rerere_enabled(void)
395 const char *rr_cache;
396 int rr_cache_exists;
398 if (!rerere_enabled)
399 return 0;
401 rr_cache = git_path("rr-cache");
402 rr_cache_exists = is_directory(rr_cache);
403 if (rerere_enabled < 0)
404 return rr_cache_exists;
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;
412 int setup_rerere(struct string_list *merge_rr)
414 int fd;
416 git_config(git_rerere_config, NULL);
417 if (!is_rerere_enabled())
418 return -1;
420 merge_rr_path = git_pathdup("MERGE_RR");
421 fd = hold_lock_file_for_update(&write_lock, merge_rr_path,
422 LOCK_DIE_ON_ERROR);
423 read_rr(merge_rr);
424 return fd;
427 int rerere(void)
429 struct string_list merge_rr = { NULL, 0, 0, 1 };
430 int fd;
432 fd = setup_rerere(&merge_rr);
433 if (fd < 0)
434 return 0;
435 return do_plain_rerere(&merge_rr, fd);