2 #include "string-list.h"
4 #include "xdiff-interface.h"
6 #include "resolve-undo.h"
12 #define THREE_STAGED 2
13 void *RERERE_RESOLVED
= &RERERE_RESOLVED
;
15 /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
16 static int rerere_enabled
= -1;
18 /* automatically update cleanly resolved paths to the index */
19 static int rerere_autoupdate
;
21 static char *merge_rr_path
;
23 const char *rerere_path(const char *hex
, const char *file
)
25 return git_path("rr-cache/%s/%s", hex
, file
);
28 int has_rerere_resolution(const char *hex
)
31 return !stat(rerere_path(hex
, "postimage"), &st
);
34 static void read_rr(struct string_list
*rr
)
36 unsigned char sha1
[20];
38 FILE *in
= fopen(merge_rr_path
, "r");
41 while (fread(buf
, 40, 1, in
) == 1) {
44 if (get_sha1_hex(buf
, sha1
))
45 die("corrupt MERGE_RR");
48 if (fgetc(in
) != '\t')
49 die("corrupt MERGE_RR");
50 for (i
= 0; i
< sizeof(buf
); i
++) {
53 die("corrupt MERGE_RR");
59 die("filename too long");
60 string_list_insert(rr
, buf
)->util
= name
;
65 static struct lock_file write_lock
;
67 static int write_rr(struct string_list
*rr
, int out_fd
)
70 for (i
= 0; i
< rr
->nr
; i
++) {
73 if (!rr
->items
[i
].util
)
75 path
= rr
->items
[i
].string
;
76 length
= strlen(path
) + 1;
77 if (write_in_full(out_fd
, rr
->items
[i
].util
, 40) != 40 ||
78 write_str_in_full(out_fd
, "\t") != 1 ||
79 write_in_full(out_fd
, path
, length
) != length
)
80 die("unable to write rerere record");
82 if (commit_lock_file(&write_lock
) != 0)
83 die("unable to write rerere record");
87 static void ferr_write(const void *p
, size_t count
, FILE *fp
, int *err
)
91 if (fwrite(p
, count
, 1, fp
) != 1)
95 static inline void ferr_puts(const char *s
, FILE *fp
, int *err
)
97 ferr_write(s
, strlen(s
), fp
, err
);
101 int (*getline
)(struct strbuf
*, struct rerere_io
*);
104 /* some more stuff */
107 static void rerere_io_putstr(const char *str
, struct rerere_io
*io
)
110 ferr_puts(str
, io
->output
, &io
->wrerror
);
113 static void rerere_io_putconflict(int ch
, int size
, struct rerere_io
*io
)
118 if (size
< sizeof(buf
) - 2) {
119 memset(buf
, ch
, size
);
121 buf
[size
+ 1] = '\0';
124 int sz
= sizeof(buf
) - 1;
126 sz
-= (sz
- size
) + 1;
131 rerere_io_putstr(buf
, io
);
135 static void rerere_io_putmem(const char *mem
, size_t sz
, struct rerere_io
*io
)
138 ferr_write(mem
, sz
, io
->output
, &io
->wrerror
);
141 struct rerere_io_file
{
146 static int rerere_file_getline(struct strbuf
*sb
, struct rerere_io
*io_
)
148 struct rerere_io_file
*io
= (struct rerere_io_file
*)io_
;
149 return strbuf_getwholeline(sb
, io
->input
, '\n');
152 static int is_cmarker(char *buf
, int marker_char
, int marker_size
, int want_sp
)
154 while (marker_size
--)
155 if (*buf
++ != marker_char
)
157 if (want_sp
&& *buf
!= ' ')
159 return isspace(*buf
);
162 static int handle_path(unsigned char *sha1
, struct rerere_io
*io
, int marker_size
)
167 RR_CONTEXT
= 0, RR_SIDE_1
, RR_SIDE_2
, RR_ORIGINAL
169 struct strbuf one
= STRBUF_INIT
, two
= STRBUF_INIT
;
170 struct strbuf buf
= STRBUF_INIT
;
175 while (!io
->getline(&buf
, io
)) {
176 if (is_cmarker(buf
.buf
, '<', marker_size
, 1)) {
177 if (hunk
!= RR_CONTEXT
)
180 } else if (is_cmarker(buf
.buf
, '|', marker_size
, 0)) {
181 if (hunk
!= RR_SIDE_1
)
184 } else if (is_cmarker(buf
.buf
, '=', marker_size
, 0)) {
185 if (hunk
!= RR_SIDE_1
&& hunk
!= RR_ORIGINAL
)
188 } else if (is_cmarker(buf
.buf
, '>', marker_size
, 1)) {
189 if (hunk
!= RR_SIDE_2
)
191 if (strbuf_cmp(&one
, &two
) > 0)
192 strbuf_swap(&one
, &two
);
195 rerere_io_putconflict('<', marker_size
, io
);
196 rerere_io_putmem(one
.buf
, one
.len
, io
);
197 rerere_io_putconflict('=', marker_size
, io
);
198 rerere_io_putmem(two
.buf
, two
.len
, io
);
199 rerere_io_putconflict('>', marker_size
, io
);
201 git_SHA1_Update(&ctx
, one
.buf
? one
.buf
: "",
203 git_SHA1_Update(&ctx
, two
.buf
? two
.buf
: "",
208 } else if (hunk
== RR_SIDE_1
)
209 strbuf_addstr(&one
, buf
.buf
);
210 else if (hunk
== RR_ORIGINAL
)
212 else if (hunk
== RR_SIDE_2
)
213 strbuf_addstr(&two
, buf
.buf
);
215 rerere_io_putstr(buf
.buf
, io
);
218 hunk
= 99; /* force error exit */
221 strbuf_release(&one
);
222 strbuf_release(&two
);
223 strbuf_release(&buf
);
226 git_SHA1_Final(sha1
, &ctx
);
227 if (hunk
!= RR_CONTEXT
)
232 static int handle_file(const char *path
, unsigned char *sha1
, const char *output
)
235 struct rerere_io_file io
;
236 int marker_size
= ll_merge_marker_size(path
);
238 memset(&io
, 0, sizeof(io
));
239 io
.io
.getline
= rerere_file_getline
;
240 io
.input
= fopen(path
, "r");
243 return error("Could not open %s", path
);
246 io
.io
.output
= fopen(output
, "w");
249 return error("Could not write %s", output
);
253 hunk_no
= handle_path(sha1
, (struct rerere_io
*)&io
, marker_size
);
257 error("There were errors while writing %s (%s)",
258 path
, strerror(io
.io
.wrerror
));
259 if (io
.io
.output
&& fclose(io
.io
.output
))
260 io
.io
.wrerror
= error("Failed to flush %s: %s",
261 path
, strerror(errno
));
265 unlink_or_warn(output
);
266 return error("Could not parse conflict hunks in %s", path
);
273 struct rerere_io_mem
{
278 static int rerere_mem_getline(struct strbuf
*sb
, struct rerere_io
*io_
)
280 struct rerere_io_mem
*io
= (struct rerere_io_mem
*)io_
;
287 ep
= strchrnul(io
->input
.buf
, '\n');
290 len
= ep
- io
->input
.buf
;
291 strbuf_add(sb
, io
->input
.buf
, len
);
292 strbuf_remove(&io
->input
, 0, len
);
296 static int handle_cache(const char *path
, unsigned char *sha1
, const char *output
)
299 mmbuffer_t result
= {NULL
, 0};
300 struct cache_entry
*ce
;
301 int pos
, len
, i
, hunk_no
;
302 struct rerere_io_mem io
;
303 int marker_size
= ll_merge_marker_size(path
);
306 * Reproduce the conflicted merge in-core
309 pos
= cache_name_pos(path
, len
);
314 for (i
= 0; i
< 3; i
++) {
315 enum object_type type
;
319 mmfile
[i
].ptr
= NULL
;
320 if (active_nr
<= pos
)
322 ce
= active_cache
[pos
++];
323 if (ce_namelen(ce
) != len
|| memcmp(ce
->name
, path
, len
)
324 || ce_stage(ce
) != i
+ 1)
326 mmfile
[i
].ptr
= read_sha1_file(ce
->sha1
, &type
, &size
);
327 mmfile
[i
].size
= size
;
329 for (i
= 0; i
< 3; i
++) {
330 if (!mmfile
[i
].ptr
&& !mmfile
[i
].size
)
331 mmfile
[i
].ptr
= xstrdup("");
334 * NEEDSWORK: handle conflicts from merges with
335 * merge.renormalize set, too
337 ll_merge(&result
, path
, &mmfile
[0], NULL
,
339 &mmfile
[2], "theirs", NULL
);
340 for (i
= 0; i
< 3; i
++)
343 memset(&io
, 0, sizeof(io
));
344 io
.io
.getline
= rerere_mem_getline
;
346 io
.io
.output
= fopen(output
, "w");
349 strbuf_init(&io
.input
, 0);
350 strbuf_attach(&io
.input
, result
.ptr
, result
.size
, result
.size
);
352 hunk_no
= handle_path(sha1
, (struct rerere_io
*)&io
, marker_size
);
353 strbuf_release(&io
.input
);
355 fclose(io
.io
.output
);
359 static int check_one_conflict(int i
, int *type
)
361 struct cache_entry
*e
= active_cache
[i
];
369 if (ce_stage(e
) == 1) {
370 if (active_nr
<= ++i
)
374 /* Only handle regular files with both stages #2 and #3 */
375 if (i
+ 1 < active_nr
) {
376 struct cache_entry
*e2
= active_cache
[i
];
377 struct cache_entry
*e3
= active_cache
[i
+ 1];
378 if (ce_stage(e2
) == 2 &&
380 ce_same_name(e
, e3
) &&
381 S_ISREG(e2
->ce_mode
) &&
382 S_ISREG(e3
->ce_mode
))
383 *type
= THREE_STAGED
;
386 /* Skip the entries with the same name */
387 while (i
< active_nr
&& ce_same_name(e
, active_cache
[i
]))
392 static int find_conflict(struct string_list
*conflict
)
395 if (read_cache() < 0)
396 return error("Could not read index");
398 for (i
= 0; i
< active_nr
;) {
400 struct cache_entry
*e
= active_cache
[i
];
401 i
= check_one_conflict(i
, &conflict_type
);
402 if (conflict_type
== THREE_STAGED
)
403 string_list_insert(conflict
, (const char *)e
->name
);
408 int rerere_remaining(struct string_list
*merge_rr
)
411 if (read_cache() < 0)
412 return error("Could not read index");
414 for (i
= 0; i
< active_nr
;) {
416 struct cache_entry
*e
= active_cache
[i
];
417 i
= check_one_conflict(i
, &conflict_type
);
418 if (conflict_type
== PUNTED
)
419 string_list_insert(merge_rr
, (const char *)e
->name
);
420 else if (conflict_type
== RESOLVED
) {
421 struct string_list_item
*it
;
422 it
= string_list_lookup(merge_rr
, (const char *)e
->name
);
425 it
->util
= RERERE_RESOLVED
;
432 static int merge(const char *name
, const char *path
)
435 mmfile_t cur
= {NULL
, 0}, base
= {NULL
, 0}, other
= {NULL
, 0};
436 mmbuffer_t result
= {NULL
, 0};
438 if (handle_file(path
, NULL
, rerere_path(name
, "thisimage")) < 0)
441 if (read_mmfile(&cur
, rerere_path(name
, "thisimage")) ||
442 read_mmfile(&base
, rerere_path(name
, "preimage")) ||
443 read_mmfile(&other
, rerere_path(name
, "postimage"))) {
447 ret
= ll_merge(&result
, path
, &base
, NULL
, &cur
, "", &other
, "", NULL
);
451 if (utime(rerere_path(name
, "postimage"), NULL
) < 0)
452 warning("failed utime() on %s: %s",
453 rerere_path(name
, "postimage"),
455 f
= fopen(path
, "w");
457 return error("Could not open %s: %s", path
,
459 if (fwrite(result
.ptr
, result
.size
, 1, f
) != 1)
460 error("Could not write %s: %s", path
, strerror(errno
));
462 return error("Writing %s failed: %s", path
,
475 static struct lock_file index_lock
;
477 static int update_paths(struct string_list
*update
)
480 int fd
= hold_locked_index(&index_lock
, 0);
486 for (i
= 0; i
< update
->nr
; i
++) {
487 struct string_list_item
*item
= &update
->items
[i
];
488 if (add_file_to_cache(item
->string
, ADD_CACHE_IGNORE_ERRORS
))
492 if (!status
&& active_cache_changed
) {
493 if (write_cache(fd
, active_cache
, active_nr
) ||
494 commit_locked_index(&index_lock
))
495 die("Unable to write new index file");
497 rollback_lock_file(&index_lock
);
501 static int do_plain_rerere(struct string_list
*rr
, int fd
)
503 struct string_list conflict
= STRING_LIST_INIT_DUP
;
504 struct string_list update
= STRING_LIST_INIT_DUP
;
507 find_conflict(&conflict
);
510 * MERGE_RR records paths with conflicts immediately after merge
511 * failed. Some of the conflicted paths might have been hand resolved
512 * in the working tree since then, but the initial run would catch all
513 * and register their preimages.
516 for (i
= 0; i
< conflict
.nr
; i
++) {
517 const char *path
= conflict
.items
[i
].string
;
518 if (!string_list_has_string(rr
, path
)) {
519 unsigned char sha1
[20];
522 ret
= handle_file(path
, sha1
, NULL
);
525 hex
= xstrdup(sha1_to_hex(sha1
));
526 string_list_insert(rr
, path
)->util
= hex
;
527 if (mkdir_in_gitdir(git_path("rr-cache/%s", hex
)))
529 handle_file(path
, NULL
, rerere_path(hex
, "preimage"));
530 fprintf(stderr
, "Recorded preimage for '%s'\n", path
);
535 * Now some of the paths that had conflicts earlier might have been
536 * hand resolved. Others may be similar to a conflict already that
537 * was resolved before.
540 for (i
= 0; i
< rr
->nr
; i
++) {
542 const char *path
= rr
->items
[i
].string
;
543 const char *name
= (const char *)rr
->items
[i
].util
;
545 if (has_rerere_resolution(name
)) {
546 if (!merge(name
, path
)) {
548 if (rerere_autoupdate
) {
549 string_list_insert(&update
, path
);
550 msg
= "Staged '%s' using previous resolution.\n";
552 msg
= "Resolved '%s' using previous resolution.\n";
553 fprintf(stderr
, msg
, path
);
558 /* Let's see if we have resolved it. */
559 ret
= handle_file(path
, NULL
, NULL
);
563 fprintf(stderr
, "Recorded resolution for '%s'.\n", path
);
564 copy_file(rerere_path(name
, "postimage"), path
, 0666);
566 rr
->items
[i
].util
= NULL
;
570 update_paths(&update
);
572 return write_rr(rr
, fd
);
575 static int git_rerere_config(const char *var
, const char *value
, void *cb
)
577 if (!strcmp(var
, "rerere.enabled"))
578 rerere_enabled
= git_config_bool(var
, value
);
579 else if (!strcmp(var
, "rerere.autoupdate"))
580 rerere_autoupdate
= git_config_bool(var
, value
);
582 return git_default_config(var
, value
, cb
);
586 static int is_rerere_enabled(void)
588 const char *rr_cache
;
594 rr_cache
= git_path("rr-cache");
595 rr_cache_exists
= is_directory(rr_cache
);
596 if (rerere_enabled
< 0)
597 return rr_cache_exists
;
599 if (!rr_cache_exists
&& mkdir_in_gitdir(rr_cache
))
600 die("Could not create directory %s", rr_cache
);
604 int setup_rerere(struct string_list
*merge_rr
, int flags
)
608 git_config(git_rerere_config
, NULL
);
609 if (!is_rerere_enabled())
612 if (flags
& (RERERE_AUTOUPDATE
|RERERE_NOAUTOUPDATE
))
613 rerere_autoupdate
= !!(flags
& RERERE_AUTOUPDATE
);
614 merge_rr_path
= git_pathdup("MERGE_RR");
615 fd
= hold_lock_file_for_update(&write_lock
, merge_rr_path
,
621 int rerere(int flags
)
623 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
626 fd
= setup_rerere(&merge_rr
, flags
);
629 return do_plain_rerere(&merge_rr
, fd
);
632 static int rerere_forget_one_path(const char *path
, struct string_list
*rr
)
634 const char *filename
;
636 unsigned char sha1
[20];
639 ret
= handle_cache(path
, sha1
, NULL
);
641 return error("Could not parse conflict hunks in '%s'", path
);
642 hex
= xstrdup(sha1_to_hex(sha1
));
643 filename
= rerere_path(hex
, "postimage");
644 if (unlink(filename
))
645 return (errno
== ENOENT
646 ? error("no remembered resolution for %s", path
)
647 : error("cannot unlink %s: %s", filename
, strerror(errno
)));
649 handle_cache(path
, sha1
, rerere_path(hex
, "preimage"));
650 fprintf(stderr
, "Updated preimage for '%s'\n", path
);
653 string_list_insert(rr
, path
)->util
= hex
;
654 fprintf(stderr
, "Forgot resolution for %s\n", path
);
658 int rerere_forget(const char **pathspec
)
661 struct string_list conflict
= STRING_LIST_INIT_DUP
;
662 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
664 if (read_cache() < 0)
665 return error("Could not read index");
667 fd
= setup_rerere(&merge_rr
, RERERE_NOAUTOUPDATE
);
669 unmerge_cache(pathspec
);
670 find_conflict(&conflict
);
671 for (i
= 0; i
< conflict
.nr
; i
++) {
672 struct string_list_item
*it
= &conflict
.items
[i
];
673 if (!match_pathspec(pathspec
, it
->string
, strlen(it
->string
),
676 rerere_forget_one_path(it
->string
, &merge_rr
);
678 return write_rr(&merge_rr
, fd
);
681 static time_t rerere_created_at(const char *name
)
684 return stat(rerere_path(name
, "preimage"), &st
) ? (time_t) 0 : st
.st_mtime
;
687 static time_t rerere_last_used_at(const char *name
)
690 return stat(rerere_path(name
, "postimage"), &st
) ? (time_t) 0 : st
.st_mtime
;
693 static void unlink_rr_item(const char *name
)
695 unlink(rerere_path(name
, "thisimage"));
696 unlink(rerere_path(name
, "preimage"));
697 unlink(rerere_path(name
, "postimage"));
698 rmdir(git_path("rr-cache/%s", name
));
701 struct rerere_gc_config_cb
{
702 int cutoff_noresolve
;
706 static int git_rerere_gc_config(const char *var
, const char *value
, void *cb
)
708 struct rerere_gc_config_cb
*cf
= cb
;
710 if (!strcmp(var
, "gc.rerereresolved"))
711 cf
->cutoff_resolve
= git_config_int(var
, value
);
712 else if (!strcmp(var
, "gc.rerereunresolved"))
713 cf
->cutoff_noresolve
= git_config_int(var
, value
);
715 return git_default_config(var
, value
, cb
);
719 void rerere_gc(struct string_list
*rr
)
721 struct string_list to_remove
= STRING_LIST_INIT_DUP
;
725 time_t now
= time(NULL
), then
;
726 struct rerere_gc_config_cb cf
= { 15, 60 };
728 git_config(git_rerere_gc_config
, &cf
);
729 dir
= opendir(git_path("rr-cache"));
731 die_errno("unable to open rr-cache directory");
732 while ((e
= readdir(dir
))) {
733 if (is_dot_or_dotdot(e
->d_name
))
736 then
= rerere_last_used_at(e
->d_name
);
738 cutoff
= cf
.cutoff_resolve
;
740 then
= rerere_created_at(e
->d_name
);
743 cutoff
= cf
.cutoff_noresolve
;
745 if (then
< now
- cutoff
* 86400)
746 string_list_append(&to_remove
, e
->d_name
);
749 for (i
= 0; i
< to_remove
.nr
; i
++)
750 unlink_rr_item(to_remove
.items
[i
].string
);
751 string_list_clear(&to_remove
, 0);
754 void rerere_clear(struct string_list
*merge_rr
)
758 for (i
= 0; i
< merge_rr
->nr
; i
++) {
759 const char *name
= (const char *)merge_rr
->items
[i
].util
;
760 if (!has_rerere_resolution(name
))
761 unlink_rr_item(name
);
763 unlink_or_warn(git_path("MERGE_RR"));