]>
git.ipfire.org Git - thirdparty/git.git/blob - rerere.c
3 #include "string-list.h"
5 #include "xdiff-interface.h"
7 #include "resolve-undo.h"
11 #include "sha1-lookup.h"
15 #define THREE_STAGED 2
16 void *RERERE_RESOLVED
= &RERERE_RESOLVED
;
18 /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
19 static int rerere_enabled
= -1;
21 /* automatically update cleanly resolved paths to the index */
22 static int rerere_autoupdate
;
24 static char *merge_rr_path
;
26 static int rerere_dir_nr
;
27 static int rerere_dir_alloc
;
29 #define RR_HAS_POSTIMAGE 1
30 #define RR_HAS_PREIMAGE 2
31 static struct rerere_dir
{
32 unsigned char sha1
[20];
36 static void free_rerere_dirs(void)
39 for (i
= 0; i
< rerere_dir_nr
; i
++)
42 rerere_dir_nr
= rerere_dir_alloc
= 0;
46 static void free_rerere_id(struct string_list_item
*item
)
51 static const char *rerere_id_hex(const struct rerere_id
*id
)
53 return sha1_to_hex(id
->collection
->sha1
);
56 const char *rerere_path(const struct rerere_id
*id
, const char *file
)
59 return git_path("rr-cache/%s", rerere_id_hex(id
));
61 return git_path("rr-cache/%s/%s", rerere_id_hex(id
), file
);
64 static int is_rr_file(const char *name
, const char *filename
)
66 return !strcmp(name
, filename
);
69 static void scan_rerere_dir(struct rerere_dir
*rr_dir
)
72 DIR *dir
= opendir(git_path("rr-cache/%s", sha1_to_hex(rr_dir
->sha1
)));
76 while ((de
= readdir(dir
)) != NULL
) {
77 if (is_rr_file(de
->d_name
, "postimage"))
78 rr_dir
->status
|= RR_HAS_POSTIMAGE
;
79 else if (is_rr_file(de
->d_name
, "preimage"))
80 rr_dir
->status
|= RR_HAS_PREIMAGE
;
85 static const unsigned char *rerere_dir_sha1(size_t i
, void *table
)
87 struct rerere_dir
**rr_dir
= table
;
88 return rr_dir
[i
]->sha1
;
91 static struct rerere_dir
*find_rerere_dir(const char *hex
)
93 unsigned char sha1
[20];
94 struct rerere_dir
*rr_dir
;
97 if (get_sha1_hex(hex
, sha1
))
98 return NULL
; /* BUG */
99 pos
= sha1_pos(sha1
, rerere_dir
, rerere_dir_nr
, rerere_dir_sha1
);
101 rr_dir
= xmalloc(sizeof(*rr_dir
));
102 hashcpy(rr_dir
->sha1
, sha1
);
106 /* Make sure the array is big enough ... */
107 ALLOC_GROW(rerere_dir
, rerere_dir_nr
+ 1, rerere_dir_alloc
);
108 /* ... and add it in. */
110 memmove(rerere_dir
+ pos
+ 1, rerere_dir
+ pos
,
111 (rerere_dir_nr
- pos
- 1) * sizeof(*rerere_dir
));
112 rerere_dir
[pos
] = rr_dir
;
113 scan_rerere_dir(rr_dir
);
115 return rerere_dir
[pos
];
118 static int has_rerere_resolution(const struct rerere_id
*id
)
120 return (id
->collection
->status
& RR_HAS_POSTIMAGE
);
123 static struct rerere_id
*new_rerere_id_hex(char *hex
)
125 struct rerere_id
*id
= xmalloc(sizeof(*id
));
126 id
->collection
= find_rerere_dir(hex
);
130 static struct rerere_id
*new_rerere_id(unsigned char *sha1
)
132 return new_rerere_id_hex(sha1_to_hex(sha1
));
136 * $GIT_DIR/MERGE_RR file is a collection of records, each of which is
137 * "conflict ID", a HT and pathname, terminated with a NUL, and is
138 * used to keep track of the set of paths that "rerere" may need to
139 * work on (i.e. what is left by the previous invocation of "git
140 * rerere" during the current conflict resolution session).
142 static void read_rr(struct string_list
*rr
)
144 struct strbuf buf
= STRBUF_INIT
;
145 FILE *in
= fopen(merge_rr_path
, "r");
149 while (!strbuf_getwholeline(&buf
, in
, '\0')) {
151 unsigned char sha1
[20];
152 struct rerere_id
*id
;
154 /* There has to be the hash, tab, path and then NUL */
155 if (buf
.len
< 42 || get_sha1_hex(buf
.buf
, sha1
))
156 die("corrupt MERGE_RR");
158 if (buf
.buf
[40] != '\t')
159 die("corrupt MERGE_RR");
162 id
= new_rerere_id_hex(buf
.buf
);
163 string_list_insert(rr
, path
)->util
= id
;
165 strbuf_release(&buf
);
169 static struct lock_file write_lock
;
171 static int write_rr(struct string_list
*rr
, int out_fd
)
174 for (i
= 0; i
< rr
->nr
; i
++) {
175 struct strbuf buf
= STRBUF_INIT
;
176 struct rerere_id
*id
;
178 assert(rr
->items
[i
].util
!= RERERE_RESOLVED
);
180 id
= rr
->items
[i
].util
;
183 strbuf_addf(&buf
, "%s\t%s%c",
185 rr
->items
[i
].string
, 0);
186 if (write_in_full(out_fd
, buf
.buf
, buf
.len
) != buf
.len
)
187 die("unable to write rerere record");
189 strbuf_release(&buf
);
191 if (commit_lock_file(&write_lock
) != 0)
192 die("unable to write rerere record");
197 * "rerere" interacts with conflicted file contents using this I/O
198 * abstraction. It reads a conflicted contents from one place via
199 * "getline()" method, and optionally can write it out after
200 * normalizing the conflicted hunks to the "output". Subclasses of
201 * rerere_io embed this structure at the beginning of their own
205 int (*getline
)(struct strbuf
*, struct rerere_io
*);
208 /* some more stuff */
211 static void ferr_write(const void *p
, size_t count
, FILE *fp
, int *err
)
215 if (fwrite(p
, count
, 1, fp
) != 1)
219 static inline void ferr_puts(const char *s
, FILE *fp
, int *err
)
221 ferr_write(s
, strlen(s
), fp
, err
);
224 static void rerere_io_putstr(const char *str
, struct rerere_io
*io
)
227 ferr_puts(str
, io
->output
, &io
->wrerror
);
231 * Write a conflict marker to io->output (if defined).
233 static void rerere_io_putconflict(int ch
, int size
, struct rerere_io
*io
)
238 if (size
<= sizeof(buf
) - 2) {
239 memset(buf
, ch
, size
);
241 buf
[size
+ 1] = '\0';
244 int sz
= sizeof(buf
) - 1;
247 * Make sure we will not write everything out
248 * in this round by leaving at least 1 byte
249 * for the next round, giving the next round
250 * a chance to add the terminating LF. Yuck.
253 sz
-= (sz
- size
) + 1;
258 rerere_io_putstr(buf
, io
);
262 static void rerere_io_putmem(const char *mem
, size_t sz
, struct rerere_io
*io
)
265 ferr_write(mem
, sz
, io
->output
, &io
->wrerror
);
269 * Subclass of rerere_io that reads from an on-disk file
271 struct rerere_io_file
{
277 * ... and its getline() method implementation
279 static int rerere_file_getline(struct strbuf
*sb
, struct rerere_io
*io_
)
281 struct rerere_io_file
*io
= (struct rerere_io_file
*)io_
;
282 return strbuf_getwholeline(sb
, io
->input
, '\n');
286 * Require the exact number of conflict marker letters, no more, no
287 * less, followed by SP or any whitespace
290 static int is_cmarker(char *buf
, int marker_char
, int marker_size
)
295 * The beginning of our version and the end of their version
296 * always are labeled like "<<<<< ours" or ">>>>> theirs",
297 * hence we set want_sp for them. Note that the version from
298 * the common ancestor in diff3-style output is not always
299 * labelled (e.g. "||||| common" is often seen but "|||||"
300 * alone is also valid), so we do not set want_sp.
302 want_sp
= (marker_char
== '<') || (marker_char
== '>');
304 while (marker_size
--)
305 if (*buf
++ != marker_char
)
307 if (want_sp
&& *buf
!= ' ')
309 return isspace(*buf
);
313 * Read contents a file with conflicts, normalize the conflicts
314 * by (1) discarding the common ancestor version in diff3-style,
315 * (2) reordering our side and their side so that whichever sorts
316 * alphabetically earlier comes before the other one, while
317 * computing the "conflict ID", which is just an SHA-1 hash of
318 * one side of the conflict, NUL, the other side of the conflict,
319 * and NUL concatenated together.
321 * Return the number of conflict hunks found.
323 * NEEDSWORK: the logic and theory of operation behind this conflict
324 * normalization may deserve to be documented somewhere, perhaps in
325 * Documentation/technical/rerere.txt.
327 static int handle_path(unsigned char *sha1
, struct rerere_io
*io
, int marker_size
)
332 RR_CONTEXT
= 0, RR_SIDE_1
, RR_SIDE_2
, RR_ORIGINAL
334 struct strbuf one
= STRBUF_INIT
, two
= STRBUF_INIT
;
335 struct strbuf buf
= STRBUF_INIT
;
340 while (!io
->getline(&buf
, io
)) {
341 if (is_cmarker(buf
.buf
, '<', marker_size
)) {
342 if (hunk
!= RR_CONTEXT
)
345 } else if (is_cmarker(buf
.buf
, '|', marker_size
)) {
346 if (hunk
!= RR_SIDE_1
)
349 } else if (is_cmarker(buf
.buf
, '=', marker_size
)) {
350 if (hunk
!= RR_SIDE_1
&& hunk
!= RR_ORIGINAL
)
353 } else if (is_cmarker(buf
.buf
, '>', marker_size
)) {
354 if (hunk
!= RR_SIDE_2
)
356 if (strbuf_cmp(&one
, &two
) > 0)
357 strbuf_swap(&one
, &two
);
360 rerere_io_putconflict('<', marker_size
, io
);
361 rerere_io_putmem(one
.buf
, one
.len
, io
);
362 rerere_io_putconflict('=', marker_size
, io
);
363 rerere_io_putmem(two
.buf
, two
.len
, io
);
364 rerere_io_putconflict('>', marker_size
, io
);
366 git_SHA1_Update(&ctx
, one
.buf
? one
.buf
: "",
368 git_SHA1_Update(&ctx
, two
.buf
? two
.buf
: "",
373 } else if (hunk
== RR_SIDE_1
)
374 strbuf_addbuf(&one
, &buf
);
375 else if (hunk
== RR_ORIGINAL
)
377 else if (hunk
== RR_SIDE_2
)
378 strbuf_addbuf(&two
, &buf
);
380 rerere_io_putstr(buf
.buf
, io
);
383 hunk
= 99; /* force error exit */
386 strbuf_release(&one
);
387 strbuf_release(&two
);
388 strbuf_release(&buf
);
391 git_SHA1_Final(sha1
, &ctx
);
392 if (hunk
!= RR_CONTEXT
)
398 * Scan the path for conflicts, do the "handle_path()" thing above, and
399 * return the number of conflict hunks found.
401 static int handle_file(const char *path
, unsigned char *sha1
, const char *output
)
404 struct rerere_io_file io
;
405 int marker_size
= ll_merge_marker_size(path
);
407 memset(&io
, 0, sizeof(io
));
408 io
.io
.getline
= rerere_file_getline
;
409 io
.input
= fopen(path
, "r");
412 return error("Could not open %s", path
);
415 io
.io
.output
= fopen(output
, "w");
418 return error("Could not write %s", output
);
422 hunk_no
= handle_path(sha1
, (struct rerere_io
*)&io
, marker_size
);
426 error("There were errors while writing %s (%s)",
427 path
, strerror(io
.io
.wrerror
));
428 if (io
.io
.output
&& fclose(io
.io
.output
))
429 io
.io
.wrerror
= error("Failed to flush %s: %s",
430 path
, strerror(errno
));
434 unlink_or_warn(output
);
435 return error("Could not parse conflict hunks in %s", path
);
443 * Subclass of rerere_io that reads from an in-core buffer that is a
446 struct rerere_io_mem
{
452 * ... and its getline() method implementation
454 static int rerere_mem_getline(struct strbuf
*sb
, struct rerere_io
*io_
)
456 struct rerere_io_mem
*io
= (struct rerere_io_mem
*)io_
;
463 ep
= memchr(io
->input
.buf
, '\n', io
->input
.len
);
465 ep
= io
->input
.buf
+ io
->input
.len
;
466 else if (*ep
== '\n')
468 len
= ep
- io
->input
.buf
;
469 strbuf_add(sb
, io
->input
.buf
, len
);
470 strbuf_remove(&io
->input
, 0, len
);
474 static int handle_cache(const char *path
, unsigned char *sha1
, const char *output
)
476 mmfile_t mmfile
[3] = {{NULL
}};
477 mmbuffer_t result
= {NULL
, 0};
478 const struct cache_entry
*ce
;
479 int pos
, len
, i
, hunk_no
;
480 struct rerere_io_mem io
;
481 int marker_size
= ll_merge_marker_size(path
);
484 * Reproduce the conflicted merge in-core
487 pos
= cache_name_pos(path
, len
);
492 while (pos
< active_nr
) {
493 enum object_type type
;
496 ce
= active_cache
[pos
++];
497 if (ce_namelen(ce
) != len
|| memcmp(ce
->name
, path
, len
))
499 i
= ce_stage(ce
) - 1;
500 if (!mmfile
[i
].ptr
) {
501 mmfile
[i
].ptr
= read_sha1_file(ce
->sha1
, &type
, &size
);
502 mmfile
[i
].size
= size
;
505 for (i
= 0; i
< 3; i
++)
506 if (!mmfile
[i
].ptr
&& !mmfile
[i
].size
)
507 mmfile
[i
].ptr
= xstrdup("");
510 * NEEDSWORK: handle conflicts from merges with
511 * merge.renormalize set, too
513 ll_merge(&result
, path
, &mmfile
[0], NULL
,
515 &mmfile
[2], "theirs", NULL
);
516 for (i
= 0; i
< 3; i
++)
519 memset(&io
, 0, sizeof(io
));
520 io
.io
.getline
= rerere_mem_getline
;
522 io
.io
.output
= fopen(output
, "w");
525 strbuf_init(&io
.input
, 0);
526 strbuf_attach(&io
.input
, result
.ptr
, result
.size
, result
.size
);
529 * Grab the conflict ID and optionally write the original
530 * contents with conflict markers out.
532 hunk_no
= handle_path(sha1
, (struct rerere_io
*)&io
, marker_size
);
533 strbuf_release(&io
.input
);
535 fclose(io
.io
.output
);
540 * Look at a cache entry at "i" and see if it is not conflicting,
541 * conflicting and we are willing to handle, or conflicting and
542 * we are unable to handle, and return the determination in *type.
543 * Return the cache index to be looked at next, by skipping the
544 * stages we have already looked at in this invocation of this
547 static int check_one_conflict(int i
, int *type
)
549 const struct cache_entry
*e
= active_cache
[i
];
557 while (ce_stage(active_cache
[i
]) == 1)
560 /* Only handle regular files with both stages #2 and #3 */
561 if (i
+ 1 < active_nr
) {
562 const struct cache_entry
*e2
= active_cache
[i
];
563 const struct cache_entry
*e3
= active_cache
[i
+ 1];
564 if (ce_stage(e2
) == 2 &&
566 ce_same_name(e
, e3
) &&
567 S_ISREG(e2
->ce_mode
) &&
568 S_ISREG(e3
->ce_mode
))
569 *type
= THREE_STAGED
;
572 /* Skip the entries with the same name */
573 while (i
< active_nr
&& ce_same_name(e
, active_cache
[i
]))
579 * Scan the index and find paths that have conflicts that rerere can
580 * handle, i.e. the ones that has both stages #2 and #3.
582 * NEEDSWORK: we do not record or replay a previous "resolve by
583 * deletion" for a delete-modify conflict, as that is inherently risky
584 * without knowing what modification is being discarded. The only
585 * safe case, i.e. both side doing the deletion and modification that
586 * are identical to the previous round, might want to be handled,
589 static int find_conflict(struct string_list
*conflict
)
592 if (read_cache() < 0)
593 return error("Could not read index");
595 for (i
= 0; i
< active_nr
;) {
597 const struct cache_entry
*e
= active_cache
[i
];
598 i
= check_one_conflict(i
, &conflict_type
);
599 if (conflict_type
== THREE_STAGED
)
600 string_list_insert(conflict
, (const char *)e
->name
);
606 * The merge_rr list is meant to hold outstanding conflicted paths
607 * that rerere could handle. Abuse the list by adding other types of
608 * entries to allow the caller to show "rerere remaining".
610 * - Conflicted paths that rerere does not handle are added
611 * - Conflicted paths that have been resolved are marked as such
612 * by storing RERERE_RESOLVED to .util field (where conflict ID
613 * is expected to be stored).
615 * Do *not* write MERGE_RR file out after calling this function.
617 * NEEDSWORK: we may want to fix the caller that implements "rerere
618 * remaining" to do this without abusing merge_rr.
620 int rerere_remaining(struct string_list
*merge_rr
)
623 if (read_cache() < 0)
624 return error("Could not read index");
626 for (i
= 0; i
< active_nr
;) {
628 const struct cache_entry
*e
= active_cache
[i
];
629 i
= check_one_conflict(i
, &conflict_type
);
630 if (conflict_type
== PUNTED
)
631 string_list_insert(merge_rr
, (const char *)e
->name
);
632 else if (conflict_type
== RESOLVED
) {
633 struct string_list_item
*it
;
634 it
= string_list_lookup(merge_rr
, (const char *)e
->name
);
637 it
->util
= RERERE_RESOLVED
;
645 * Find the conflict identified by "id"; the change between its
646 * "preimage" (i.e. a previous contents with conflict markers) and its
647 * "postimage" (i.e. the corresponding contents with conflicts
648 * resolved) may apply cleanly to the contents stored in "path", i.e.
649 * the conflict this time around.
651 * Returns 0 for successful replay of recorded resolution, or non-zero
654 static int merge(const struct rerere_id
*id
, const char *path
)
658 mmfile_t cur
= {NULL
, 0}, base
= {NULL
, 0}, other
= {NULL
, 0};
659 mmbuffer_t result
= {NULL
, 0};
662 * Normalize the conflicts in path and write it out to
663 * "thisimage" temporary file.
665 if (handle_file(path
, NULL
, rerere_path(id
, "thisimage")) < 0) {
670 if (read_mmfile(&cur
, rerere_path(id
, "thisimage")) ||
671 read_mmfile(&base
, rerere_path(id
, "preimage")) ||
672 read_mmfile(&other
, rerere_path(id
, "postimage"))) {
678 * A three-way merge. Note that this honors user-customizable
679 * low-level merge driver settings.
681 ret
= ll_merge(&result
, path
, &base
, NULL
, &cur
, "", &other
, "", NULL
);
686 * A successful replay of recorded resolution.
687 * Mark that "postimage" was used to help gc.
689 if (utime(rerere_path(id
, "postimage"), NULL
) < 0)
690 warning("failed utime() on %s: %s",
691 rerere_path(id
, "postimage"),
694 /* Update "path" with the resolution */
695 f
= fopen(path
, "w");
697 return error("Could not open %s: %s", path
,
699 if (fwrite(result
.ptr
, result
.size
, 1, f
) != 1)
700 error("Could not write %s: %s", path
, strerror(errno
));
702 return error("Writing %s failed: %s", path
,
714 static struct lock_file index_lock
;
716 static void update_paths(struct string_list
*update
)
720 hold_locked_index(&index_lock
, 1);
722 for (i
= 0; i
< update
->nr
; i
++) {
723 struct string_list_item
*item
= &update
->items
[i
];
724 if (add_file_to_cache(item
->string
, 0))
726 fprintf(stderr
, "Staged '%s' using previous resolution.\n",
730 if (active_cache_changed
) {
731 if (write_locked_index(&the_index
, &index_lock
, COMMIT_LOCK
))
732 die("Unable to write new index file");
734 rollback_lock_file(&index_lock
);
738 * The path indicated by rr_item may still have conflict for which we
739 * have a recorded resolution, in which case replay it and optionally
740 * update it. Or it may have been resolved by the user and we may
741 * only have the preimage for that conflict, in which case the result
742 * needs to be recorded as a resolution in a postimage file.
744 static void do_rerere_one_path(struct string_list_item
*rr_item
,
745 struct string_list
*update
)
747 const char *path
= rr_item
->string
;
748 const struct rerere_id
*id
= rr_item
->util
;
750 /* Is there a recorded resolution we could attempt to apply? */
751 if (has_rerere_resolution(id
)) {
753 return; /* failed to replay */
755 if (rerere_autoupdate
)
756 string_list_insert(update
, path
);
759 "Resolved '%s' using previous resolution.\n",
761 } else if (!handle_file(path
, NULL
, NULL
)) {
762 /* The user has resolved it. */
763 copy_file(rerere_path(id
, "postimage"), path
, 0666);
764 id
->collection
->status
|= RR_HAS_POSTIMAGE
;
765 fprintf(stderr
, "Recorded resolution for '%s'.\n", path
);
769 free_rerere_id(rr_item
);
770 rr_item
->util
= NULL
;
773 static int do_plain_rerere(struct string_list
*rr
, int fd
)
775 struct string_list conflict
= STRING_LIST_INIT_DUP
;
776 struct string_list update
= STRING_LIST_INIT_DUP
;
779 find_conflict(&conflict
);
782 * MERGE_RR records paths with conflicts immediately after
783 * merge failed. Some of the conflicted paths might have been
784 * hand resolved in the working tree since then, but the
785 * initial run would catch all and register their preimages.
787 for (i
= 0; i
< conflict
.nr
; i
++) {
788 struct rerere_id
*id
;
789 unsigned char sha1
[20];
790 const char *path
= conflict
.items
[i
].string
;
793 if (string_list_has_string(rr
, path
))
797 * Ask handle_file() to scan and assign a
798 * conflict ID. No need to write anything out
801 ret
= handle_file(path
, sha1
, NULL
);
805 id
= new_rerere_id(sha1
);
806 string_list_insert(rr
, path
)->util
= id
;
809 * If the directory does not exist, create
810 * it. mkdir_in_gitdir() will fail with
811 * EEXIST if there already is one.
813 * NEEDSWORK: make sure "gc" does not remove
814 * preimage without removing the directory.
816 if (mkdir_in_gitdir(rerere_path(id
, NULL
)))
820 * We are the first to encounter this
821 * conflict. Ask handle_file() to write the
822 * normalized contents to the "preimage" file.
824 handle_file(path
, NULL
, rerere_path(id
, "preimage"));
825 id
->collection
->status
|= RR_HAS_PREIMAGE
;
826 fprintf(stderr
, "Recorded preimage for '%s'\n", path
);
829 for (i
= 0; i
< rr
->nr
; i
++)
830 do_rerere_one_path(&rr
->items
[i
], &update
);
833 update_paths(&update
);
835 return write_rr(rr
, fd
);
838 static void git_rerere_config(void)
840 git_config_get_bool("rerere.enabled", &rerere_enabled
);
841 git_config_get_bool("rerere.autoupdate", &rerere_autoupdate
);
842 git_config(git_default_config
, NULL
);
845 static int is_rerere_enabled(void)
847 const char *rr_cache
;
853 rr_cache
= git_path("rr-cache");
854 rr_cache_exists
= is_directory(rr_cache
);
855 if (rerere_enabled
< 0)
856 return rr_cache_exists
;
858 if (!rr_cache_exists
&& mkdir_in_gitdir(rr_cache
))
859 die("Could not create directory %s", rr_cache
);
863 int setup_rerere(struct string_list
*merge_rr
, int flags
)
868 if (!is_rerere_enabled())
871 if (flags
& (RERERE_AUTOUPDATE
|RERERE_NOAUTOUPDATE
))
872 rerere_autoupdate
= !!(flags
& RERERE_AUTOUPDATE
);
873 merge_rr_path
= git_pathdup("MERGE_RR");
874 fd
= hold_lock_file_for_update(&write_lock
, merge_rr_path
,
881 * The main entry point that is called internally from codepaths that
882 * perform mergy operations, possibly leaving conflicted index entries
883 * and working tree files.
885 int rerere(int flags
)
887 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
890 fd
= setup_rerere(&merge_rr
, flags
);
893 status
= do_plain_rerere(&merge_rr
, fd
);
898 static int rerere_forget_one_path(const char *path
, struct string_list
*rr
)
900 const char *filename
;
901 struct rerere_id
*id
;
902 unsigned char sha1
[20];
904 struct string_list_item
*item
;
907 * Recreate the original conflict from the stages in the
908 * index and compute the conflict ID
910 ret
= handle_cache(path
, sha1
, NULL
);
912 return error("Could not parse conflict hunks in '%s'", path
);
914 /* Nuke the recorded resolution for the conflict */
915 id
= new_rerere_id(sha1
);
916 filename
= rerere_path(id
, "postimage");
917 if (unlink(filename
))
918 return (errno
== ENOENT
919 ? error("no remembered resolution for %s", path
)
920 : error("cannot unlink %s: %s", filename
, strerror(errno
)));
923 * Update the preimage so that the user can resolve the
924 * conflict in the working tree, run us again to record
927 handle_cache(path
, sha1
, rerere_path(id
, "preimage"));
928 fprintf(stderr
, "Updated preimage for '%s'\n", path
);
931 * And remember that we can record resolution for this
932 * conflict when the user is done.
934 item
= string_list_insert(rr
, path
);
935 free_rerere_id(item
);
937 fprintf(stderr
, "Forgot resolution for %s\n", path
);
941 int rerere_forget(struct pathspec
*pathspec
)
944 struct string_list conflict
= STRING_LIST_INIT_DUP
;
945 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
947 if (read_cache() < 0)
948 return error("Could not read index");
950 fd
= setup_rerere(&merge_rr
, RERERE_NOAUTOUPDATE
);
953 * The paths may have been resolved (incorrectly);
954 * recover the original conflicted state and then
955 * find the conflicted paths.
957 unmerge_cache(pathspec
);
958 find_conflict(&conflict
);
959 for (i
= 0; i
< conflict
.nr
; i
++) {
960 struct string_list_item
*it
= &conflict
.items
[i
];
961 if (!match_pathspec(pathspec
, it
->string
,
962 strlen(it
->string
), 0, NULL
, 0))
964 rerere_forget_one_path(it
->string
, &merge_rr
);
966 return write_rr(&merge_rr
, fd
);
970 * Garbage collection support
974 * Note that this is not reentrant but is used only one-at-a-time
975 * so it does not matter right now.
977 static struct rerere_id
*dirname_to_id(const char *name
)
979 static struct rerere_id id
;
980 id
.collection
= find_rerere_dir(name
);
984 static time_t rerere_created_at(const char *dir_name
)
987 struct rerere_id
*id
= dirname_to_id(dir_name
);
989 return stat(rerere_path(id
, "preimage"), &st
) ? (time_t) 0 : st
.st_mtime
;
992 static time_t rerere_last_used_at(const char *dir_name
)
995 struct rerere_id
*id
= dirname_to_id(dir_name
);
997 return stat(rerere_path(id
, "postimage"), &st
) ? (time_t) 0 : st
.st_mtime
;
1001 * Remove the recorded resolution for a given conflict ID
1003 static void unlink_rr_item(struct rerere_id
*id
)
1005 unlink(rerere_path(id
, "thisimage"));
1006 unlink(rerere_path(id
, "preimage"));
1007 unlink(rerere_path(id
, "postimage"));
1009 * NEEDSWORK: what if this rmdir() fails? Wouldn't we then
1010 * assume that we already have preimage recorded in
1011 * do_plain_rerere()?
1013 rmdir(rerere_path(id
, NULL
));
1016 void rerere_gc(struct string_list
*rr
)
1018 struct string_list to_remove
= STRING_LIST_INIT_DUP
;
1022 time_t now
= time(NULL
), then
;
1023 int cutoff_noresolve
= 15;
1024 int cutoff_resolve
= 60;
1026 git_config_get_int("gc.rerereresolved", &cutoff_resolve
);
1027 git_config_get_int("gc.rerereunresolved", &cutoff_noresolve
);
1028 git_config(git_default_config
, NULL
);
1029 dir
= opendir(git_path("rr-cache"));
1031 die_errno("unable to open rr-cache directory");
1032 /* Collect stale conflict IDs ... */
1033 while ((e
= readdir(dir
))) {
1034 if (is_dot_or_dotdot(e
->d_name
))
1037 then
= rerere_last_used_at(e
->d_name
);
1039 cutoff
= cutoff_resolve
;
1041 then
= rerere_created_at(e
->d_name
);
1044 cutoff
= cutoff_noresolve
;
1046 if (then
< now
- cutoff
* 86400)
1047 string_list_append(&to_remove
, e
->d_name
);
1050 /* ... and then remove them one-by-one */
1051 for (i
= 0; i
< to_remove
.nr
; i
++)
1052 unlink_rr_item(dirname_to_id(to_remove
.items
[i
].string
));
1053 string_list_clear(&to_remove
, 0);
1057 * During a conflict resolution, after "rerere" recorded the
1058 * preimages, abandon them if the user did not resolve them or
1059 * record their resolutions. And drop $GIT_DIR/MERGE_RR.
1061 * NEEDSWORK: shouldn't we be calling this from "reset --hard"?
1063 void rerere_clear(struct string_list
*merge_rr
)
1067 for (i
= 0; i
< merge_rr
->nr
; i
++) {
1068 struct rerere_id
*id
= merge_rr
->items
[i
].util
;
1069 if (!has_rerere_resolution(id
))
1072 unlink_or_warn(git_path("MERGE_RR"));