2 #include "string-list.h"
4 #include "xdiff-interface.h"
6 #include "resolve-undo.h"
10 /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
11 static int rerere_enabled
= -1;
13 /* automatically update cleanly resolved paths to the index */
14 static int rerere_autoupdate
;
16 static char *merge_rr_path
;
18 const char *rerere_path(const char *hex
, const char *file
)
20 return git_path("rr-cache/%s/%s", hex
, file
);
23 int has_rerere_resolution(const char *hex
)
26 return !stat(rerere_path(hex
, "postimage"), &st
);
29 static void read_rr(struct string_list
*rr
)
31 unsigned char sha1
[20];
33 FILE *in
= fopen(merge_rr_path
, "r");
36 while (fread(buf
, 40, 1, in
) == 1) {
39 if (get_sha1_hex(buf
, sha1
))
40 die("corrupt MERGE_RR");
43 if (fgetc(in
) != '\t')
44 die("corrupt MERGE_RR");
45 for (i
= 0; i
< sizeof(buf
) && (buf
[i
] = fgetc(in
)); i
++)
48 die("filename too long");
49 string_list_insert(rr
, buf
)->util
= name
;
54 static struct lock_file write_lock
;
56 static int write_rr(struct string_list
*rr
, int out_fd
)
59 for (i
= 0; i
< rr
->nr
; i
++) {
62 if (!rr
->items
[i
].util
)
64 path
= rr
->items
[i
].string
;
65 length
= strlen(path
) + 1;
66 if (write_in_full(out_fd
, rr
->items
[i
].util
, 40) != 40 ||
67 write_str_in_full(out_fd
, "\t") != 1 ||
68 write_in_full(out_fd
, path
, length
) != length
)
69 die("unable to write rerere record");
71 if (commit_lock_file(&write_lock
) != 0)
72 die("unable to write rerere record");
76 static void ferr_write(const void *p
, size_t count
, FILE *fp
, int *err
)
80 if (fwrite(p
, count
, 1, fp
) != 1)
84 static inline void ferr_puts(const char *s
, FILE *fp
, int *err
)
86 ferr_write(s
, strlen(s
), fp
, err
);
90 int (*getline
)(struct strbuf
*, struct rerere_io
*);
96 static void rerere_io_putstr(const char *str
, struct rerere_io
*io
)
99 ferr_puts(str
, io
->output
, &io
->wrerror
);
102 static void rerere_io_putconflict(int ch
, int size
, struct rerere_io
*io
)
107 if (size
< sizeof(buf
) - 2) {
108 memset(buf
, ch
, size
);
110 buf
[size
+ 1] = '\0';
113 int sz
= sizeof(buf
) - 1;
115 sz
-= (sz
- size
) + 1;
120 rerere_io_putstr(buf
, io
);
124 static void rerere_io_putmem(const char *mem
, size_t sz
, struct rerere_io
*io
)
127 ferr_write(mem
, sz
, io
->output
, &io
->wrerror
);
130 struct rerere_io_file
{
135 static int rerere_file_getline(struct strbuf
*sb
, struct rerere_io
*io_
)
137 struct rerere_io_file
*io
= (struct rerere_io_file
*)io_
;
138 return strbuf_getwholeline(sb
, io
->input
, '\n');
141 static int is_cmarker(char *buf
, int marker_char
, int marker_size
, int want_sp
)
143 while (marker_size
--)
144 if (*buf
++ != marker_char
)
146 if (want_sp
&& *buf
!= ' ')
148 return isspace(*buf
);
151 static int handle_path(unsigned char *sha1
, struct rerere_io
*io
, int marker_size
)
156 RR_CONTEXT
= 0, RR_SIDE_1
, RR_SIDE_2
, RR_ORIGINAL
158 struct strbuf one
= STRBUF_INIT
, two
= STRBUF_INIT
;
159 struct strbuf buf
= STRBUF_INIT
;
164 while (!io
->getline(&buf
, io
)) {
165 if (is_cmarker(buf
.buf
, '<', marker_size
, 1)) {
166 if (hunk
!= RR_CONTEXT
)
169 } else if (is_cmarker(buf
.buf
, '|', marker_size
, 0)) {
170 if (hunk
!= RR_SIDE_1
)
173 } else if (is_cmarker(buf
.buf
, '=', marker_size
, 0)) {
174 if (hunk
!= RR_SIDE_1
&& hunk
!= RR_ORIGINAL
)
177 } else if (is_cmarker(buf
.buf
, '>', marker_size
, 1)) {
178 if (hunk
!= RR_SIDE_2
)
180 if (strbuf_cmp(&one
, &two
) > 0)
181 strbuf_swap(&one
, &two
);
184 rerere_io_putconflict('<', marker_size
, io
);
185 rerere_io_putmem(one
.buf
, one
.len
, io
);
186 rerere_io_putconflict('=', marker_size
, io
);
187 rerere_io_putmem(two
.buf
, two
.len
, io
);
188 rerere_io_putconflict('>', marker_size
, io
);
190 git_SHA1_Update(&ctx
, one
.buf
? one
.buf
: "",
192 git_SHA1_Update(&ctx
, two
.buf
? two
.buf
: "",
197 } else if (hunk
== RR_SIDE_1
)
198 strbuf_addstr(&one
, buf
.buf
);
199 else if (hunk
== RR_ORIGINAL
)
201 else if (hunk
== RR_SIDE_2
)
202 strbuf_addstr(&two
, buf
.buf
);
204 rerere_io_putstr(buf
.buf
, io
);
207 hunk
= 99; /* force error exit */
210 strbuf_release(&one
);
211 strbuf_release(&two
);
212 strbuf_release(&buf
);
215 git_SHA1_Final(sha1
, &ctx
);
216 if (hunk
!= RR_CONTEXT
)
221 static int handle_file(const char *path
, unsigned char *sha1
, const char *output
)
224 struct rerere_io_file io
;
225 int marker_size
= ll_merge_marker_size(path
);
227 memset(&io
, 0, sizeof(io
));
228 io
.io
.getline
= rerere_file_getline
;
229 io
.input
= fopen(path
, "r");
232 return error("Could not open %s", path
);
235 io
.io
.output
= fopen(output
, "w");
238 return error("Could not write %s", output
);
242 hunk_no
= handle_path(sha1
, (struct rerere_io
*)&io
, marker_size
);
246 error("There were errors while writing %s (%s)",
247 path
, strerror(io
.io
.wrerror
));
248 if (io
.io
.output
&& fclose(io
.io
.output
))
249 io
.io
.wrerror
= error("Failed to flush %s: %s",
250 path
, strerror(errno
));
254 unlink_or_warn(output
);
255 return error("Could not parse conflict hunks in %s", path
);
262 struct rerere_io_mem
{
267 static int rerere_mem_getline(struct strbuf
*sb
, struct rerere_io
*io_
)
269 struct rerere_io_mem
*io
= (struct rerere_io_mem
*)io_
;
276 ep
= strchrnul(io
->input
.buf
, '\n');
279 len
= ep
- io
->input
.buf
;
280 strbuf_add(sb
, io
->input
.buf
, len
);
281 strbuf_remove(&io
->input
, 0, len
);
285 static int handle_cache(const char *path
, unsigned char *sha1
, const char *output
)
288 mmbuffer_t result
= {NULL
, 0};
289 struct cache_entry
*ce
;
290 int pos
, len
, i
, hunk_no
;
291 struct rerere_io_mem io
;
292 int marker_size
= ll_merge_marker_size(path
);
295 * Reproduce the conflicted merge in-core
298 pos
= cache_name_pos(path
, len
);
303 for (i
= 0; i
< 3; i
++) {
304 enum object_type type
;
308 mmfile
[i
].ptr
= NULL
;
309 if (active_nr
<= pos
)
311 ce
= active_cache
[pos
++];
312 if (ce_namelen(ce
) != len
|| memcmp(ce
->name
, path
, len
)
313 || ce_stage(ce
) != i
+ 1)
315 mmfile
[i
].ptr
= read_sha1_file(ce
->sha1
, &type
, &size
);
316 mmfile
[i
].size
= size
;
318 for (i
= 0; i
< 3; i
++) {
319 if (!mmfile
[i
].ptr
&& !mmfile
[i
].size
)
320 mmfile
[i
].ptr
= xstrdup("");
323 * NEEDSWORK: handle conflicts from merges with
324 * merge.renormalize set, too
326 ll_merge(&result
, path
, &mmfile
[0], NULL
,
328 &mmfile
[2], "theirs", NULL
);
329 for (i
= 0; i
< 3; i
++)
332 memset(&io
, 0, sizeof(io
));
333 io
.io
.getline
= rerere_mem_getline
;
335 io
.io
.output
= fopen(output
, "w");
338 strbuf_init(&io
.input
, 0);
339 strbuf_attach(&io
.input
, result
.ptr
, result
.size
, result
.size
);
341 hunk_no
= handle_path(sha1
, (struct rerere_io
*)&io
, marker_size
);
342 strbuf_release(&io
.input
);
344 fclose(io
.io
.output
);
348 static int find_conflict(struct string_list
*conflict
)
351 if (read_cache() < 0)
352 return error("Could not read index");
353 for (i
= 0; i
+1 < active_nr
; i
++) {
354 struct cache_entry
*e2
= active_cache
[i
];
355 struct cache_entry
*e3
= active_cache
[i
+1];
356 if (ce_stage(e2
) == 2 &&
358 ce_same_name(e2
, e3
) &&
359 S_ISREG(e2
->ce_mode
) &&
360 S_ISREG(e3
->ce_mode
)) {
361 string_list_insert(conflict
, (const char *)e2
->name
);
362 i
++; /* skip over both #2 and #3 */
368 static int merge(const char *name
, const char *path
)
371 mmfile_t cur
= {NULL
, 0}, base
= {NULL
, 0}, other
= {NULL
, 0};
372 mmbuffer_t result
= {NULL
, 0};
374 if (handle_file(path
, NULL
, rerere_path(name
, "thisimage")) < 0)
377 if (read_mmfile(&cur
, rerere_path(name
, "thisimage")) ||
378 read_mmfile(&base
, rerere_path(name
, "preimage")) ||
379 read_mmfile(&other
, rerere_path(name
, "postimage"))) {
383 ret
= ll_merge(&result
, path
, &base
, NULL
, &cur
, "", &other
, "", 0);
387 if (utime(rerere_path(name
, "postimage"), NULL
) < 0)
388 warning("failed utime() on %s: %s",
389 rerere_path(name
, "postimage"),
391 f
= fopen(path
, "w");
393 return error("Could not open %s: %s", path
,
395 if (fwrite(result
.ptr
, result
.size
, 1, f
) != 1)
396 error("Could not write %s: %s", path
, strerror(errno
));
398 return error("Writing %s failed: %s", path
,
411 static struct lock_file index_lock
;
413 static int update_paths(struct string_list
*update
)
416 int fd
= hold_locked_index(&index_lock
, 0);
422 for (i
= 0; i
< update
->nr
; i
++) {
423 struct string_list_item
*item
= &update
->items
[i
];
424 if (add_file_to_cache(item
->string
, ADD_CACHE_IGNORE_ERRORS
))
428 if (!status
&& active_cache_changed
) {
429 if (write_cache(fd
, active_cache
, active_nr
) ||
430 commit_locked_index(&index_lock
))
431 die("Unable to write new index file");
433 rollback_lock_file(&index_lock
);
437 static int do_plain_rerere(struct string_list
*rr
, int fd
)
439 struct string_list conflict
= STRING_LIST_INIT_DUP
;
440 struct string_list update
= STRING_LIST_INIT_DUP
;
443 find_conflict(&conflict
);
446 * MERGE_RR records paths with conflicts immediately after merge
447 * failed. Some of the conflicted paths might have been hand resolved
448 * in the working tree since then, but the initial run would catch all
449 * and register their preimages.
452 for (i
= 0; i
< conflict
.nr
; i
++) {
453 const char *path
= conflict
.items
[i
].string
;
454 if (!string_list_has_string(rr
, path
)) {
455 unsigned char sha1
[20];
458 ret
= handle_file(path
, sha1
, NULL
);
461 hex
= xstrdup(sha1_to_hex(sha1
));
462 string_list_insert(rr
, path
)->util
= hex
;
463 if (mkdir(git_path("rr-cache/%s", hex
), 0755))
465 handle_file(path
, NULL
, rerere_path(hex
, "preimage"));
466 fprintf(stderr
, "Recorded preimage for '%s'\n", path
);
471 * Now some of the paths that had conflicts earlier might have been
472 * hand resolved. Others may be similar to a conflict already that
473 * was resolved before.
476 for (i
= 0; i
< rr
->nr
; i
++) {
478 const char *path
= rr
->items
[i
].string
;
479 const char *name
= (const char *)rr
->items
[i
].util
;
481 if (has_rerere_resolution(name
)) {
482 if (!merge(name
, path
)) {
483 if (rerere_autoupdate
)
484 string_list_insert(&update
, path
);
486 "%s '%s' using previous resolution.\n",
488 ? "Staged" : "Resolved",
494 /* Let's see if we have resolved it. */
495 ret
= handle_file(path
, NULL
, NULL
);
499 fprintf(stderr
, "Recorded resolution for '%s'.\n", path
);
500 copy_file(rerere_path(name
, "postimage"), path
, 0666);
502 rr
->items
[i
].util
= NULL
;
506 update_paths(&update
);
508 return write_rr(rr
, fd
);
511 static int git_rerere_config(const char *var
, const char *value
, void *cb
)
513 if (!strcmp(var
, "rerere.enabled"))
514 rerere_enabled
= git_config_bool(var
, value
);
515 else if (!strcmp(var
, "rerere.autoupdate"))
516 rerere_autoupdate
= git_config_bool(var
, value
);
518 return git_default_config(var
, value
, cb
);
522 static int is_rerere_enabled(void)
524 const char *rr_cache
;
530 rr_cache
= git_path("rr-cache");
531 rr_cache_exists
= is_directory(rr_cache
);
532 if (rerere_enabled
< 0)
533 return rr_cache_exists
;
535 if (!rr_cache_exists
&&
536 (mkdir(rr_cache
, 0777) || adjust_shared_perm(rr_cache
)))
537 die("Could not create directory %s", rr_cache
);
541 int setup_rerere(struct string_list
*merge_rr
, int flags
)
545 git_config(git_rerere_config
, NULL
);
546 if (!is_rerere_enabled())
549 if (flags
& (RERERE_AUTOUPDATE
|RERERE_NOAUTOUPDATE
))
550 rerere_autoupdate
= !!(flags
& RERERE_AUTOUPDATE
);
551 merge_rr_path
= git_pathdup("MERGE_RR");
552 fd
= hold_lock_file_for_update(&write_lock
, merge_rr_path
,
558 int rerere(int flags
)
560 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
563 fd
= setup_rerere(&merge_rr
, flags
);
566 return do_plain_rerere(&merge_rr
, fd
);
569 static int rerere_forget_one_path(const char *path
, struct string_list
*rr
)
571 const char *filename
;
573 unsigned char sha1
[20];
576 ret
= handle_cache(path
, sha1
, NULL
);
578 return error("Could not parse conflict hunks in '%s'", path
);
579 hex
= xstrdup(sha1_to_hex(sha1
));
580 filename
= rerere_path(hex
, "postimage");
581 if (unlink(filename
))
582 return (errno
== ENOENT
583 ? error("no remembered resolution for %s", path
)
584 : error("cannot unlink %s: %s", filename
, strerror(errno
)));
586 handle_cache(path
, sha1
, rerere_path(hex
, "preimage"));
587 fprintf(stderr
, "Updated preimage for '%s'\n", path
);
590 string_list_insert(rr
, path
)->util
= hex
;
591 fprintf(stderr
, "Forgot resolution for %s\n", path
);
595 int rerere_forget(const char **pathspec
)
598 struct string_list conflict
= STRING_LIST_INIT_DUP
;
599 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
601 if (read_cache() < 0)
602 return error("Could not read index");
604 fd
= setup_rerere(&merge_rr
, RERERE_NOAUTOUPDATE
);
606 unmerge_cache(pathspec
);
607 find_conflict(&conflict
);
608 for (i
= 0; i
< conflict
.nr
; i
++) {
609 struct string_list_item
*it
= &conflict
.items
[i
];
610 if (!match_pathspec(pathspec
, it
->string
, strlen(it
->string
),
613 rerere_forget_one_path(it
->string
, &merge_rr
);
615 return write_rr(&merge_rr
, fd
);