3 #include "string-list.h"
5 #include "xdiff-interface.h"
7 #include "resolve-undo.h"
14 #define THREE_STAGED 2
15 void *RERERE_RESOLVED
= &RERERE_RESOLVED
;
17 /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
18 static int rerere_enabled
= -1;
20 /* automatically update cleanly resolved paths to the index */
21 static int rerere_autoupdate
;
23 static void free_rerere_id(struct string_list_item
*item
)
28 static const char *rerere_id_hex(const struct rerere_id
*id
)
33 const char *rerere_path(const struct rerere_id
*id
, const char *file
)
36 return git_path("rr-cache/%s", rerere_id_hex(id
));
38 return git_path("rr-cache/%s/%s", rerere_id_hex(id
), file
);
41 static int has_rerere_resolution(const struct rerere_id
*id
)
45 return !stat(rerere_path(id
, "postimage"), &st
);
48 static struct rerere_id
*new_rerere_id_hex(char *hex
)
50 struct rerere_id
*id
= xmalloc(sizeof(*id
));
51 xsnprintf(id
->hex
, sizeof(id
->hex
), "%s", hex
);
55 static struct rerere_id
*new_rerere_id(unsigned char *sha1
)
57 return new_rerere_id_hex(sha1_to_hex(sha1
));
61 * $GIT_DIR/MERGE_RR file is a collection of records, each of which is
62 * "conflict ID", a HT and pathname, terminated with a NUL, and is
63 * used to keep track of the set of paths that "rerere" may need to
64 * work on (i.e. what is left by the previous invocation of "git
65 * rerere" during the current conflict resolution session).
67 static void read_rr(struct string_list
*rr
)
69 struct strbuf buf
= STRBUF_INIT
;
70 FILE *in
= fopen(git_path_merge_rr(), "r");
74 while (!strbuf_getwholeline(&buf
, in
, '\0')) {
76 unsigned char sha1
[20];
79 /* There has to be the hash, tab, path and then NUL */
80 if (buf
.len
< 42 || get_sha1_hex(buf
.buf
, sha1
))
81 die("corrupt MERGE_RR");
83 if (buf
.buf
[40] != '\t')
84 die("corrupt MERGE_RR");
87 id
= new_rerere_id_hex(buf
.buf
);
88 string_list_insert(rr
, path
)->util
= id
;
94 static struct lock_file write_lock
;
96 static int write_rr(struct string_list
*rr
, int out_fd
)
99 for (i
= 0; i
< rr
->nr
; i
++) {
100 struct strbuf buf
= STRBUF_INIT
;
101 struct rerere_id
*id
;
103 assert(rr
->items
[i
].util
!= RERERE_RESOLVED
);
105 id
= rr
->items
[i
].util
;
108 strbuf_addf(&buf
, "%s\t%s%c",
110 rr
->items
[i
].string
, 0);
111 if (write_in_full(out_fd
, buf
.buf
, buf
.len
) != buf
.len
)
112 die("unable to write rerere record");
114 strbuf_release(&buf
);
116 if (commit_lock_file(&write_lock
) != 0)
117 die("unable to write rerere record");
122 * "rerere" interacts with conflicted file contents using this I/O
123 * abstraction. It reads a conflicted contents from one place via
124 * "getline()" method, and optionally can write it out after
125 * normalizing the conflicted hunks to the "output". Subclasses of
126 * rerere_io embed this structure at the beginning of their own
130 int (*getline
)(struct strbuf
*, struct rerere_io
*);
133 /* some more stuff */
136 static void ferr_write(const void *p
, size_t count
, FILE *fp
, int *err
)
140 if (fwrite(p
, count
, 1, fp
) != 1)
144 static inline void ferr_puts(const char *s
, FILE *fp
, int *err
)
146 ferr_write(s
, strlen(s
), fp
, err
);
149 static void rerere_io_putstr(const char *str
, struct rerere_io
*io
)
152 ferr_puts(str
, io
->output
, &io
->wrerror
);
156 * Write a conflict marker to io->output (if defined).
158 static void rerere_io_putconflict(int ch
, int size
, struct rerere_io
*io
)
163 if (size
<= sizeof(buf
) - 2) {
164 memset(buf
, ch
, size
);
166 buf
[size
+ 1] = '\0';
169 int sz
= sizeof(buf
) - 1;
172 * Make sure we will not write everything out
173 * in this round by leaving at least 1 byte
174 * for the next round, giving the next round
175 * a chance to add the terminating LF. Yuck.
178 sz
-= (sz
- size
) + 1;
183 rerere_io_putstr(buf
, io
);
187 static void rerere_io_putmem(const char *mem
, size_t sz
, struct rerere_io
*io
)
190 ferr_write(mem
, sz
, io
->output
, &io
->wrerror
);
194 * Subclass of rerere_io that reads from an on-disk file
196 struct rerere_io_file
{
202 * ... and its getline() method implementation
204 static int rerere_file_getline(struct strbuf
*sb
, struct rerere_io
*io_
)
206 struct rerere_io_file
*io
= (struct rerere_io_file
*)io_
;
207 return strbuf_getwholeline(sb
, io
->input
, '\n');
211 * Require the exact number of conflict marker letters, no more, no
212 * less, followed by SP or any whitespace
215 static int is_cmarker(char *buf
, int marker_char
, int marker_size
)
220 * The beginning of our version and the end of their version
221 * always are labeled like "<<<<< ours" or ">>>>> theirs",
222 * hence we set want_sp for them. Note that the version from
223 * the common ancestor in diff3-style output is not always
224 * labelled (e.g. "||||| common" is often seen but "|||||"
225 * alone is also valid), so we do not set want_sp.
227 want_sp
= (marker_char
== '<') || (marker_char
== '>');
229 while (marker_size
--)
230 if (*buf
++ != marker_char
)
232 if (want_sp
&& *buf
!= ' ')
234 return isspace(*buf
);
238 * Read contents a file with conflicts, normalize the conflicts
239 * by (1) discarding the common ancestor version in diff3-style,
240 * (2) reordering our side and their side so that whichever sorts
241 * alphabetically earlier comes before the other one, while
242 * computing the "conflict ID", which is just an SHA-1 hash of
243 * one side of the conflict, NUL, the other side of the conflict,
244 * and NUL concatenated together.
246 * Return the number of conflict hunks found.
248 * NEEDSWORK: the logic and theory of operation behind this conflict
249 * normalization may deserve to be documented somewhere, perhaps in
250 * Documentation/technical/rerere.txt.
252 static int handle_path(unsigned char *sha1
, struct rerere_io
*io
, int marker_size
)
257 RR_CONTEXT
= 0, RR_SIDE_1
, RR_SIDE_2
, RR_ORIGINAL
259 struct strbuf one
= STRBUF_INIT
, two
= STRBUF_INIT
;
260 struct strbuf buf
= STRBUF_INIT
;
265 while (!io
->getline(&buf
, io
)) {
266 if (is_cmarker(buf
.buf
, '<', marker_size
)) {
267 if (hunk
!= RR_CONTEXT
)
270 } else if (is_cmarker(buf
.buf
, '|', marker_size
)) {
271 if (hunk
!= RR_SIDE_1
)
274 } else if (is_cmarker(buf
.buf
, '=', marker_size
)) {
275 if (hunk
!= RR_SIDE_1
&& hunk
!= RR_ORIGINAL
)
278 } else if (is_cmarker(buf
.buf
, '>', marker_size
)) {
279 if (hunk
!= RR_SIDE_2
)
281 if (strbuf_cmp(&one
, &two
) > 0)
282 strbuf_swap(&one
, &two
);
285 rerere_io_putconflict('<', marker_size
, io
);
286 rerere_io_putmem(one
.buf
, one
.len
, io
);
287 rerere_io_putconflict('=', marker_size
, io
);
288 rerere_io_putmem(two
.buf
, two
.len
, io
);
289 rerere_io_putconflict('>', marker_size
, io
);
291 git_SHA1_Update(&ctx
, one
.buf
? one
.buf
: "",
293 git_SHA1_Update(&ctx
, two
.buf
? two
.buf
: "",
298 } else if (hunk
== RR_SIDE_1
)
299 strbuf_addbuf(&one
, &buf
);
300 else if (hunk
== RR_ORIGINAL
)
302 else if (hunk
== RR_SIDE_2
)
303 strbuf_addbuf(&two
, &buf
);
305 rerere_io_putstr(buf
.buf
, io
);
308 hunk
= 99; /* force error exit */
311 strbuf_release(&one
);
312 strbuf_release(&two
);
313 strbuf_release(&buf
);
316 git_SHA1_Final(sha1
, &ctx
);
317 if (hunk
!= RR_CONTEXT
)
323 * Scan the path for conflicts, do the "handle_path()" thing above, and
324 * return the number of conflict hunks found.
326 static int handle_file(const char *path
, unsigned char *sha1
, const char *output
)
329 struct rerere_io_file io
;
330 int marker_size
= ll_merge_marker_size(path
);
332 memset(&io
, 0, sizeof(io
));
333 io
.io
.getline
= rerere_file_getline
;
334 io
.input
= fopen(path
, "r");
337 return error("Could not open %s", path
);
340 io
.io
.output
= fopen(output
, "w");
343 return error("Could not write %s", output
);
347 hunk_no
= handle_path(sha1
, (struct rerere_io
*)&io
, marker_size
);
351 error("There were errors while writing %s (%s)",
352 path
, strerror(io
.io
.wrerror
));
353 if (io
.io
.output
&& fclose(io
.io
.output
))
354 io
.io
.wrerror
= error("Failed to flush %s: %s",
355 path
, strerror(errno
));
359 unlink_or_warn(output
);
360 return error("Could not parse conflict hunks in %s", path
);
368 * Subclass of rerere_io that reads from an in-core buffer that is a
371 struct rerere_io_mem
{
377 * ... and its getline() method implementation
379 static int rerere_mem_getline(struct strbuf
*sb
, struct rerere_io
*io_
)
381 struct rerere_io_mem
*io
= (struct rerere_io_mem
*)io_
;
388 ep
= memchr(io
->input
.buf
, '\n', io
->input
.len
);
390 ep
= io
->input
.buf
+ io
->input
.len
;
391 else if (*ep
== '\n')
393 len
= ep
- io
->input
.buf
;
394 strbuf_add(sb
, io
->input
.buf
, len
);
395 strbuf_remove(&io
->input
, 0, len
);
399 static int handle_cache(const char *path
, unsigned char *sha1
, const char *output
)
401 mmfile_t mmfile
[3] = {{NULL
}};
402 mmbuffer_t result
= {NULL
, 0};
403 const struct cache_entry
*ce
;
404 int pos
, len
, i
, hunk_no
;
405 struct rerere_io_mem io
;
406 int marker_size
= ll_merge_marker_size(path
);
409 * Reproduce the conflicted merge in-core
412 pos
= cache_name_pos(path
, len
);
417 while (pos
< active_nr
) {
418 enum object_type type
;
421 ce
= active_cache
[pos
++];
422 if (ce_namelen(ce
) != len
|| memcmp(ce
->name
, path
, len
))
424 i
= ce_stage(ce
) - 1;
425 if (!mmfile
[i
].ptr
) {
426 mmfile
[i
].ptr
= read_sha1_file(ce
->sha1
, &type
, &size
);
427 mmfile
[i
].size
= size
;
430 for (i
= 0; i
< 3; i
++)
431 if (!mmfile
[i
].ptr
&& !mmfile
[i
].size
)
432 mmfile
[i
].ptr
= xstrdup("");
435 * NEEDSWORK: handle conflicts from merges with
436 * merge.renormalize set, too
438 ll_merge(&result
, path
, &mmfile
[0], NULL
,
440 &mmfile
[2], "theirs", NULL
);
441 for (i
= 0; i
< 3; i
++)
444 memset(&io
, 0, sizeof(io
));
445 io
.io
.getline
= rerere_mem_getline
;
447 io
.io
.output
= fopen(output
, "w");
450 strbuf_init(&io
.input
, 0);
451 strbuf_attach(&io
.input
, result
.ptr
, result
.size
, result
.size
);
454 * Grab the conflict ID and optionally write the original
455 * contents with conflict markers out.
457 hunk_no
= handle_path(sha1
, (struct rerere_io
*)&io
, marker_size
);
458 strbuf_release(&io
.input
);
460 fclose(io
.io
.output
);
465 * Look at a cache entry at "i" and see if it is not conflicting,
466 * conflicting and we are willing to handle, or conflicting and
467 * we are unable to handle, and return the determination in *type.
468 * Return the cache index to be looked at next, by skipping the
469 * stages we have already looked at in this invocation of this
472 static int check_one_conflict(int i
, int *type
)
474 const struct cache_entry
*e
= active_cache
[i
];
482 while (ce_stage(active_cache
[i
]) == 1)
485 /* Only handle regular files with both stages #2 and #3 */
486 if (i
+ 1 < active_nr
) {
487 const struct cache_entry
*e2
= active_cache
[i
];
488 const struct cache_entry
*e3
= active_cache
[i
+ 1];
489 if (ce_stage(e2
) == 2 &&
491 ce_same_name(e
, e3
) &&
492 S_ISREG(e2
->ce_mode
) &&
493 S_ISREG(e3
->ce_mode
))
494 *type
= THREE_STAGED
;
497 /* Skip the entries with the same name */
498 while (i
< active_nr
&& ce_same_name(e
, active_cache
[i
]))
504 * Scan the index and find paths that have conflicts that rerere can
505 * handle, i.e. the ones that has both stages #2 and #3.
507 * NEEDSWORK: we do not record or replay a previous "resolve by
508 * deletion" for a delete-modify conflict, as that is inherently risky
509 * without knowing what modification is being discarded. The only
510 * safe case, i.e. both side doing the deletion and modification that
511 * are identical to the previous round, might want to be handled,
514 static int find_conflict(struct string_list
*conflict
)
517 if (read_cache() < 0)
518 return error("Could not read index");
520 for (i
= 0; i
< active_nr
;) {
522 const struct cache_entry
*e
= active_cache
[i
];
523 i
= check_one_conflict(i
, &conflict_type
);
524 if (conflict_type
== THREE_STAGED
)
525 string_list_insert(conflict
, (const char *)e
->name
);
531 * The merge_rr list is meant to hold outstanding conflicted paths
532 * that rerere could handle. Abuse the list by adding other types of
533 * entries to allow the caller to show "rerere remaining".
535 * - Conflicted paths that rerere does not handle are added
536 * - Conflicted paths that have been resolved are marked as such
537 * by storing RERERE_RESOLVED to .util field (where conflict ID
538 * is expected to be stored).
540 * Do *not* write MERGE_RR file out after calling this function.
542 * NEEDSWORK: we may want to fix the caller that implements "rerere
543 * remaining" to do this without abusing merge_rr.
545 int rerere_remaining(struct string_list
*merge_rr
)
548 if (setup_rerere(merge_rr
, RERERE_READONLY
))
550 if (read_cache() < 0)
551 return error("Could not read index");
553 for (i
= 0; i
< active_nr
;) {
555 const struct cache_entry
*e
= active_cache
[i
];
556 i
= check_one_conflict(i
, &conflict_type
);
557 if (conflict_type
== PUNTED
)
558 string_list_insert(merge_rr
, (const char *)e
->name
);
559 else if (conflict_type
== RESOLVED
) {
560 struct string_list_item
*it
;
561 it
= string_list_lookup(merge_rr
, (const char *)e
->name
);
564 it
->util
= RERERE_RESOLVED
;
572 * Find the conflict identified by "id"; the change between its
573 * "preimage" (i.e. a previous contents with conflict markers) and its
574 * "postimage" (i.e. the corresponding contents with conflicts
575 * resolved) may apply cleanly to the contents stored in "path", i.e.
576 * the conflict this time around.
578 * Returns 0 for successful replay of recorded resolution, or non-zero
581 static int merge(const struct rerere_id
*id
, const char *path
)
585 mmfile_t cur
= {NULL
, 0}, base
= {NULL
, 0}, other
= {NULL
, 0};
586 mmbuffer_t result
= {NULL
, 0};
589 * Normalize the conflicts in path and write it out to
590 * "thisimage" temporary file.
592 if (handle_file(path
, NULL
, rerere_path(id
, "thisimage")) < 0) {
597 if (read_mmfile(&cur
, rerere_path(id
, "thisimage")) ||
598 read_mmfile(&base
, rerere_path(id
, "preimage")) ||
599 read_mmfile(&other
, rerere_path(id
, "postimage"))) {
605 * A three-way merge. Note that this honors user-customizable
606 * low-level merge driver settings.
608 ret
= ll_merge(&result
, path
, &base
, NULL
, &cur
, "", &other
, "", NULL
);
613 * A successful replay of recorded resolution.
614 * Mark that "postimage" was used to help gc.
616 if (utime(rerere_path(id
, "postimage"), NULL
) < 0)
617 warning("failed utime() on %s: %s",
618 rerere_path(id
, "postimage"),
621 /* Update "path" with the resolution */
622 f
= fopen(path
, "w");
624 return error("Could not open %s: %s", path
,
626 if (fwrite(result
.ptr
, result
.size
, 1, f
) != 1)
627 error("Could not write %s: %s", path
, strerror(errno
));
629 return error("Writing %s failed: %s", path
,
641 static struct lock_file index_lock
;
643 static void update_paths(struct string_list
*update
)
647 hold_locked_index(&index_lock
, 1);
649 for (i
= 0; i
< update
->nr
; i
++) {
650 struct string_list_item
*item
= &update
->items
[i
];
651 if (add_file_to_cache(item
->string
, 0))
653 fprintf(stderr
, "Staged '%s' using previous resolution.\n",
657 if (active_cache_changed
) {
658 if (write_locked_index(&the_index
, &index_lock
, COMMIT_LOCK
))
659 die("Unable to write new index file");
661 rollback_lock_file(&index_lock
);
665 * The path indicated by rr_item may still have conflict for which we
666 * have a recorded resolution, in which case replay it and optionally
667 * update it. Or it may have been resolved by the user and we may
668 * only have the preimage for that conflict, in which case the result
669 * needs to be recorded as a resolution in a postimage file.
671 static void do_rerere_one_path(struct string_list_item
*rr_item
,
672 struct string_list
*update
)
674 const char *path
= rr_item
->string
;
675 const struct rerere_id
*id
= rr_item
->util
;
677 /* Is there a recorded resolution we could attempt to apply? */
678 if (has_rerere_resolution(id
)) {
680 return; /* failed to replay */
682 if (rerere_autoupdate
)
683 string_list_insert(update
, path
);
686 "Resolved '%s' using previous resolution.\n",
688 } else if (!handle_file(path
, NULL
, NULL
)) {
689 /* The user has resolved it. */
690 copy_file(rerere_path(id
, "postimage"), path
, 0666);
691 fprintf(stderr
, "Recorded resolution for '%s'.\n", path
);
695 free_rerere_id(rr_item
);
696 rr_item
->util
= NULL
;
699 static int do_plain_rerere(struct string_list
*rr
, int fd
)
701 struct string_list conflict
= STRING_LIST_INIT_DUP
;
702 struct string_list update
= STRING_LIST_INIT_DUP
;
705 find_conflict(&conflict
);
708 * MERGE_RR records paths with conflicts immediately after
709 * merge failed. Some of the conflicted paths might have been
710 * hand resolved in the working tree since then, but the
711 * initial run would catch all and register their preimages.
713 for (i
= 0; i
< conflict
.nr
; i
++) {
714 struct rerere_id
*id
;
715 unsigned char sha1
[20];
716 const char *path
= conflict
.items
[i
].string
;
719 if (string_list_has_string(rr
, path
))
723 * Ask handle_file() to scan and assign a
724 * conflict ID. No need to write anything out
727 ret
= handle_file(path
, sha1
, NULL
);
731 id
= new_rerere_id(sha1
);
732 string_list_insert(rr
, path
)->util
= id
;
735 * If the directory does not exist, create
736 * it. mkdir_in_gitdir() will fail with
737 * EEXIST if there already is one.
739 * NEEDSWORK: make sure "gc" does not remove
740 * preimage without removing the directory.
742 if (mkdir_in_gitdir(rerere_path(id
, NULL
)))
746 * We are the first to encounter this
747 * conflict. Ask handle_file() to write the
748 * normalized contents to the "preimage" file.
750 handle_file(path
, NULL
, rerere_path(id
, "preimage"));
751 fprintf(stderr
, "Recorded preimage for '%s'\n", path
);
754 for (i
= 0; i
< rr
->nr
; i
++)
755 do_rerere_one_path(&rr
->items
[i
], &update
);
758 update_paths(&update
);
760 return write_rr(rr
, fd
);
763 static void git_rerere_config(void)
765 git_config_get_bool("rerere.enabled", &rerere_enabled
);
766 git_config_get_bool("rerere.autoupdate", &rerere_autoupdate
);
767 git_config(git_default_config
, NULL
);
770 static GIT_PATH_FUNC(git_path_rr_cache
, "rr-cache")
772 static int is_rerere_enabled(void)
779 rr_cache_exists
= is_directory(git_path_rr_cache());
780 if (rerere_enabled
< 0)
781 return rr_cache_exists
;
783 if (!rr_cache_exists
&& mkdir_in_gitdir(git_path_rr_cache()))
784 die("Could not create directory %s", git_path_rr_cache());
788 int setup_rerere(struct string_list
*merge_rr
, int flags
)
793 if (!is_rerere_enabled())
796 if (flags
& (RERERE_AUTOUPDATE
|RERERE_NOAUTOUPDATE
))
797 rerere_autoupdate
= !!(flags
& RERERE_AUTOUPDATE
);
798 if (flags
& RERERE_READONLY
)
801 fd
= hold_lock_file_for_update(&write_lock
, git_path_merge_rr(),
808 * The main entry point that is called internally from codepaths that
809 * perform mergy operations, possibly leaving conflicted index entries
810 * and working tree files.
812 int rerere(int flags
)
814 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
817 fd
= setup_rerere(&merge_rr
, flags
);
820 return do_plain_rerere(&merge_rr
, fd
);
823 static int rerere_forget_one_path(const char *path
, struct string_list
*rr
)
825 const char *filename
;
826 struct rerere_id
*id
;
827 unsigned char sha1
[20];
829 struct string_list_item
*item
;
832 * Recreate the original conflict from the stages in the
833 * index and compute the conflict ID
835 ret
= handle_cache(path
, sha1
, NULL
);
837 return error("Could not parse conflict hunks in '%s'", path
);
839 /* Nuke the recorded resolution for the conflict */
840 id
= new_rerere_id(sha1
);
841 filename
= rerere_path(id
, "postimage");
842 if (unlink(filename
))
843 return (errno
== ENOENT
844 ? error("no remembered resolution for %s", path
)
845 : error("cannot unlink %s: %s", filename
, strerror(errno
)));
848 * Update the preimage so that the user can resolve the
849 * conflict in the working tree, run us again to record
852 handle_cache(path
, sha1
, rerere_path(id
, "preimage"));
853 fprintf(stderr
, "Updated preimage for '%s'\n", path
);
856 * And remember that we can record resolution for this
857 * conflict when the user is done.
859 item
= string_list_insert(rr
, path
);
860 free_rerere_id(item
);
862 fprintf(stderr
, "Forgot resolution for %s\n", path
);
866 int rerere_forget(struct pathspec
*pathspec
)
869 struct string_list conflict
= STRING_LIST_INIT_DUP
;
870 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
872 if (read_cache() < 0)
873 return error("Could not read index");
875 fd
= setup_rerere(&merge_rr
, RERERE_NOAUTOUPDATE
);
880 * The paths may have been resolved (incorrectly);
881 * recover the original conflicted state and then
882 * find the conflicted paths.
884 unmerge_cache(pathspec
);
885 find_conflict(&conflict
);
886 for (i
= 0; i
< conflict
.nr
; i
++) {
887 struct string_list_item
*it
= &conflict
.items
[i
];
888 if (!match_pathspec(pathspec
, it
->string
,
889 strlen(it
->string
), 0, NULL
, 0))
891 rerere_forget_one_path(it
->string
, &merge_rr
);
893 return write_rr(&merge_rr
, fd
);
897 * Garbage collection support
901 * Note that this is not reentrant but is used only one-at-a-time
902 * so it does not matter right now.
904 static struct rerere_id
*dirname_to_id(const char *name
)
906 static struct rerere_id id
;
907 xsnprintf(id
.hex
, sizeof(id
.hex
), "%s", name
);
911 static time_t rerere_created_at(const char *dir_name
)
914 struct rerere_id
*id
= dirname_to_id(dir_name
);
916 return stat(rerere_path(id
, "preimage"), &st
) ? (time_t) 0 : st
.st_mtime
;
919 static time_t rerere_last_used_at(const char *dir_name
)
922 struct rerere_id
*id
= dirname_to_id(dir_name
);
924 return stat(rerere_path(id
, "postimage"), &st
) ? (time_t) 0 : st
.st_mtime
;
928 * Remove the recorded resolution for a given conflict ID
930 static void unlink_rr_item(struct rerere_id
*id
)
932 unlink(rerere_path(id
, "thisimage"));
933 unlink(rerere_path(id
, "preimage"));
934 unlink(rerere_path(id
, "postimage"));
936 * NEEDSWORK: what if this rmdir() fails? Wouldn't we then
937 * assume that we already have preimage recorded in
940 rmdir(rerere_path(id
, NULL
));
943 void rerere_gc(struct string_list
*rr
)
945 struct string_list to_remove
= STRING_LIST_INIT_DUP
;
949 time_t now
= time(NULL
), then
;
950 int cutoff_noresolve
= 15;
951 int cutoff_resolve
= 60;
953 if (setup_rerere(rr
, 0) < 0)
956 git_config_get_int("gc.rerereresolved", &cutoff_resolve
);
957 git_config_get_int("gc.rerereunresolved", &cutoff_noresolve
);
958 git_config(git_default_config
, NULL
);
959 dir
= opendir(git_path("rr-cache"));
961 die_errno("unable to open rr-cache directory");
962 /* Collect stale conflict IDs ... */
963 while ((e
= readdir(dir
))) {
964 if (is_dot_or_dotdot(e
->d_name
))
967 then
= rerere_last_used_at(e
->d_name
);
969 cutoff
= cutoff_resolve
;
971 then
= rerere_created_at(e
->d_name
);
974 cutoff
= cutoff_noresolve
;
976 if (then
< now
- cutoff
* 86400)
977 string_list_append(&to_remove
, e
->d_name
);
980 /* ... and then remove them one-by-one */
981 for (i
= 0; i
< to_remove
.nr
; i
++)
982 unlink_rr_item(dirname_to_id(to_remove
.items
[i
].string
));
983 string_list_clear(&to_remove
, 0);
984 rollback_lock_file(&write_lock
);
988 * During a conflict resolution, after "rerere" recorded the
989 * preimages, abandon them if the user did not resolve them or
990 * record their resolutions. And drop $GIT_DIR/MERGE_RR.
992 * NEEDSWORK: shouldn't we be calling this from "reset --hard"?
994 void rerere_clear(struct string_list
*merge_rr
)
998 if (setup_rerere(merge_rr
, 0) < 0)
1001 for (i
= 0; i
< merge_rr
->nr
; i
++) {
1002 struct rerere_id
*id
= merge_rr
->items
[i
].util
;
1003 if (!has_rerere_resolution(id
))
1006 unlink_or_warn(git_path_merge_rr());
1007 rollback_lock_file(&write_lock
);