1 #include "git-compat-util.h"
8 #include "string-list.h"
10 #include "xdiff-interface.h"
12 #include "resolve-undo.h"
16 #include "object-store.h"
17 #include "hash-lookup.h"
22 #define THREE_STAGED 2
23 void *RERERE_RESOLVED
= &RERERE_RESOLVED
;
25 /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
26 static int rerere_enabled
= -1;
28 /* automatically update cleanly resolved paths to the index */
29 static int rerere_autoupdate
;
31 #define RR_HAS_POSTIMAGE 1
32 #define RR_HAS_PREIMAGE 2
34 int status_alloc
, status_nr
;
35 unsigned char *status
;
36 char name
[FLEX_ARRAY
];
39 static struct strmap rerere_dirs
= STRMAP_INIT
;
41 static void free_rerere_dirs(void)
43 struct hashmap_iter iter
;
44 struct strmap_entry
*ent
;
46 strmap_for_each_entry(&rerere_dirs
, &iter
, ent
) {
47 struct rerere_dir
*rr_dir
= ent
->value
;
51 strmap_clear(&rerere_dirs
, 0);
54 static void free_rerere_id(struct string_list_item
*item
)
59 static const char *rerere_id_hex(const struct rerere_id
*id
)
61 return id
->collection
->name
;
64 static void fit_variant(struct rerere_dir
*rr_dir
, int variant
)
67 ALLOC_GROW(rr_dir
->status
, variant
, rr_dir
->status_alloc
);
68 if (rr_dir
->status_nr
< variant
) {
69 memset(rr_dir
->status
+ rr_dir
->status_nr
,
70 '\0', variant
- rr_dir
->status_nr
);
71 rr_dir
->status_nr
= variant
;
75 static void assign_variant(struct rerere_id
*id
)
78 struct rerere_dir
*rr_dir
= id
->collection
;
80 variant
= id
->variant
;
82 for (variant
= 0; variant
< rr_dir
->status_nr
; variant
++)
83 if (!rr_dir
->status
[variant
])
86 fit_variant(rr_dir
, variant
);
87 id
->variant
= variant
;
90 const char *rerere_path(const struct rerere_id
*id
, const char *file
)
93 return git_path("rr-cache/%s", rerere_id_hex(id
));
96 return git_path("rr-cache/%s/%s", rerere_id_hex(id
), file
);
98 return git_path("rr-cache/%s/%s.%d",
99 rerere_id_hex(id
), file
, id
->variant
);
102 static int is_rr_file(const char *name
, const char *filename
, int *variant
)
107 if (!strcmp(name
, filename
)) {
111 if (!skip_prefix(name
, filename
, &suffix
) || *suffix
!= '.')
115 *variant
= strtol(suffix
+ 1, &ep
, 10);
121 static void scan_rerere_dir(struct rerere_dir
*rr_dir
)
124 DIR *dir
= opendir(git_path("rr-cache/%s", rr_dir
->name
));
128 while ((de
= readdir(dir
)) != NULL
) {
131 if (is_rr_file(de
->d_name
, "postimage", &variant
)) {
132 fit_variant(rr_dir
, variant
);
133 rr_dir
->status
[variant
] |= RR_HAS_POSTIMAGE
;
134 } else if (is_rr_file(de
->d_name
, "preimage", &variant
)) {
135 fit_variant(rr_dir
, variant
);
136 rr_dir
->status
[variant
] |= RR_HAS_PREIMAGE
;
142 static struct rerere_dir
*find_rerere_dir(const char *hex
)
144 struct rerere_dir
*rr_dir
;
146 rr_dir
= strmap_get(&rerere_dirs
, hex
);
148 FLEX_ALLOC_STR(rr_dir
, name
, hex
);
149 rr_dir
->status
= NULL
;
150 rr_dir
->status_nr
= 0;
151 rr_dir
->status_alloc
= 0;
152 strmap_put(&rerere_dirs
, hex
, rr_dir
);
154 scan_rerere_dir(rr_dir
);
159 static int has_rerere_resolution(const struct rerere_id
*id
)
161 const int both
= RR_HAS_POSTIMAGE
|RR_HAS_PREIMAGE
;
162 int variant
= id
->variant
;
166 return ((id
->collection
->status
[variant
] & both
) == both
);
169 static struct rerere_id
*new_rerere_id_hex(char *hex
)
171 struct rerere_id
*id
= xmalloc(sizeof(*id
));
172 id
->collection
= find_rerere_dir(hex
);
173 id
->variant
= -1; /* not known yet */
177 static struct rerere_id
*new_rerere_id(unsigned char *hash
)
179 return new_rerere_id_hex(hash_to_hex(hash
));
183 * $GIT_DIR/MERGE_RR file is a collection of records, each of which is
184 * "conflict ID", a HT and pathname, terminated with a NUL, and is
185 * used to keep track of the set of paths that "rerere" may need to
186 * work on (i.e. what is left by the previous invocation of "git
187 * rerere" during the current conflict resolution session).
189 static void read_rr(struct repository
*r
, struct string_list
*rr
)
191 struct strbuf buf
= STRBUF_INIT
;
192 FILE *in
= fopen_or_warn(git_path_merge_rr(r
), "r");
196 while (!strbuf_getwholeline(&buf
, in
, '\0')) {
198 unsigned char hash
[GIT_MAX_RAWSZ
];
199 struct rerere_id
*id
;
201 const unsigned hexsz
= the_hash_algo
->hexsz
;
203 /* There has to be the hash, tab, path and then NUL */
204 if (buf
.len
< hexsz
+ 2 || get_sha1_hex(buf
.buf
, hash
))
205 die(_("corrupt MERGE_RR"));
207 if (buf
.buf
[hexsz
] != '.') {
209 path
= buf
.buf
+ hexsz
;
212 variant
= strtol(buf
.buf
+ hexsz
+ 1, &path
, 10);
214 die(_("corrupt MERGE_RR"));
216 if (*(path
++) != '\t')
217 die(_("corrupt MERGE_RR"));
218 buf
.buf
[hexsz
] = '\0';
219 id
= new_rerere_id_hex(buf
.buf
);
220 id
->variant
= variant
;
221 string_list_insert(rr
, path
)->util
= id
;
223 strbuf_release(&buf
);
227 static struct lock_file write_lock
;
229 static int write_rr(struct string_list
*rr
, int out_fd
)
232 for (i
= 0; i
< rr
->nr
; i
++) {
233 struct strbuf buf
= STRBUF_INIT
;
234 struct rerere_id
*id
;
236 assert(rr
->items
[i
].util
!= RERERE_RESOLVED
);
238 id
= rr
->items
[i
].util
;
241 assert(id
->variant
>= 0);
243 strbuf_addf(&buf
, "%s.%d\t%s%c",
244 rerere_id_hex(id
), id
->variant
,
245 rr
->items
[i
].string
, 0);
247 strbuf_addf(&buf
, "%s\t%s%c",
249 rr
->items
[i
].string
, 0);
251 if (write_in_full(out_fd
, buf
.buf
, buf
.len
) < 0)
252 die(_("unable to write rerere record"));
254 strbuf_release(&buf
);
256 if (commit_lock_file(&write_lock
) != 0)
257 die(_("unable to write rerere record"));
262 * "rerere" interacts with conflicted file contents using this I/O
263 * abstraction. It reads a conflicted contents from one place via
264 * "getline()" method, and optionally can write it out after
265 * normalizing the conflicted hunks to the "output". Subclasses of
266 * rerere_io embed this structure at the beginning of their own
270 int (*getline
)(struct strbuf
*, struct rerere_io
*);
273 /* some more stuff */
276 static void ferr_write(const void *p
, size_t count
, FILE *fp
, int *err
)
280 if (fwrite(p
, count
, 1, fp
) != 1)
284 static inline void ferr_puts(const char *s
, FILE *fp
, int *err
)
286 ferr_write(s
, strlen(s
), fp
, err
);
289 static void rerere_io_putstr(const char *str
, struct rerere_io
*io
)
292 ferr_puts(str
, io
->output
, &io
->wrerror
);
295 static void rerere_io_putmem(const char *mem
, size_t sz
, struct rerere_io
*io
)
298 ferr_write(mem
, sz
, io
->output
, &io
->wrerror
);
302 * Subclass of rerere_io that reads from an on-disk file
304 struct rerere_io_file
{
310 * ... and its getline() method implementation
312 static int rerere_file_getline(struct strbuf
*sb
, struct rerere_io
*io_
)
314 struct rerere_io_file
*io
= (struct rerere_io_file
*)io_
;
315 return strbuf_getwholeline(sb
, io
->input
, '\n');
319 * Require the exact number of conflict marker letters, no more, no
320 * less, followed by SP or any whitespace
323 static int is_cmarker(char *buf
, int marker_char
, int marker_size
)
328 * The beginning of our version and the end of their version
329 * always are labeled like "<<<<< ours" or ">>>>> theirs",
330 * hence we set want_sp for them. Note that the version from
331 * the common ancestor in diff3-style output is not always
332 * labelled (e.g. "||||| common" is often seen but "|||||"
333 * alone is also valid), so we do not set want_sp.
335 want_sp
= (marker_char
== '<') || (marker_char
== '>');
337 while (marker_size
--)
338 if (*buf
++ != marker_char
)
340 if (want_sp
&& *buf
!= ' ')
342 return isspace(*buf
);
345 static void rerere_strbuf_putconflict(struct strbuf
*buf
, int ch
, size_t size
)
347 strbuf_addchars(buf
, ch
, size
);
348 strbuf_addch(buf
, '\n');
351 static int handle_conflict(struct strbuf
*out
, struct rerere_io
*io
,
352 int marker_size
, git_hash_ctx
*ctx
)
355 RR_SIDE_1
= 0, RR_SIDE_2
, RR_ORIGINAL
357 struct strbuf one
= STRBUF_INIT
, two
= STRBUF_INIT
;
358 struct strbuf buf
= STRBUF_INIT
, conflict
= STRBUF_INIT
;
359 int has_conflicts
= -1;
361 while (!io
->getline(&buf
, io
)) {
362 if (is_cmarker(buf
.buf
, '<', marker_size
)) {
363 if (handle_conflict(&conflict
, io
, marker_size
, NULL
) < 0)
365 if (hunk
== RR_SIDE_1
)
366 strbuf_addbuf(&one
, &conflict
);
368 strbuf_addbuf(&two
, &conflict
);
369 strbuf_release(&conflict
);
370 } else if (is_cmarker(buf
.buf
, '|', marker_size
)) {
371 if (hunk
!= RR_SIDE_1
)
374 } else if (is_cmarker(buf
.buf
, '=', marker_size
)) {
375 if (hunk
!= RR_SIDE_1
&& hunk
!= RR_ORIGINAL
)
378 } else if (is_cmarker(buf
.buf
, '>', marker_size
)) {
379 if (hunk
!= RR_SIDE_2
)
381 if (strbuf_cmp(&one
, &two
) > 0)
382 strbuf_swap(&one
, &two
);
384 rerere_strbuf_putconflict(out
, '<', marker_size
);
385 strbuf_addbuf(out
, &one
);
386 rerere_strbuf_putconflict(out
, '=', marker_size
);
387 strbuf_addbuf(out
, &two
);
388 rerere_strbuf_putconflict(out
, '>', marker_size
);
390 the_hash_algo
->update_fn(ctx
, one
.buf
?
393 the_hash_algo
->update_fn(ctx
, two
.buf
?
398 } else if (hunk
== RR_SIDE_1
)
399 strbuf_addbuf(&one
, &buf
);
400 else if (hunk
== RR_ORIGINAL
)
402 else if (hunk
== RR_SIDE_2
)
403 strbuf_addbuf(&two
, &buf
);
405 strbuf_release(&one
);
406 strbuf_release(&two
);
407 strbuf_release(&buf
);
409 return has_conflicts
;
413 * Read contents a file with conflicts, normalize the conflicts
414 * by (1) discarding the common ancestor version in diff3-style,
415 * (2) reordering our side and their side so that whichever sorts
416 * alphabetically earlier comes before the other one, while
417 * computing the "conflict ID", which is just an SHA-1 hash of
418 * one side of the conflict, NUL, the other side of the conflict,
419 * and NUL concatenated together.
421 * Return 1 if conflict hunks are found, 0 if there are no conflict
422 * hunks and -1 if an error occurred.
424 static int handle_path(unsigned char *hash
, struct rerere_io
*io
, int marker_size
)
427 struct strbuf buf
= STRBUF_INIT
, out
= STRBUF_INIT
;
428 int has_conflicts
= 0;
430 the_hash_algo
->init_fn(&ctx
);
432 while (!io
->getline(&buf
, io
)) {
433 if (is_cmarker(buf
.buf
, '<', marker_size
)) {
434 has_conflicts
= handle_conflict(&out
, io
, marker_size
,
436 if (has_conflicts
< 0)
438 rerere_io_putmem(out
.buf
, out
.len
, io
);
441 rerere_io_putstr(buf
.buf
, io
);
443 strbuf_release(&buf
);
444 strbuf_release(&out
);
447 the_hash_algo
->final_fn(hash
, &ctx
);
449 return has_conflicts
;
453 * Scan the path for conflicts, do the "handle_path()" thing above, and
454 * return the number of conflict hunks found.
456 static int handle_file(struct index_state
*istate
,
457 const char *path
, unsigned char *hash
, const char *output
)
459 int has_conflicts
= 0;
460 struct rerere_io_file io
;
461 int marker_size
= ll_merge_marker_size(istate
, path
);
463 memset(&io
, 0, sizeof(io
));
464 io
.io
.getline
= rerere_file_getline
;
465 io
.input
= fopen(path
, "r");
468 return error_errno(_("could not open '%s'"), path
);
471 io
.io
.output
= fopen(output
, "w");
473 error_errno(_("could not write '%s'"), output
);
479 has_conflicts
= handle_path(hash
, (struct rerere_io
*)&io
, marker_size
);
483 error(_("there were errors while writing '%s' (%s)"),
484 path
, strerror(io
.io
.wrerror
));
485 if (io
.io
.output
&& fclose(io
.io
.output
))
486 io
.io
.wrerror
= error_errno(_("failed to flush '%s'"), path
);
488 if (has_conflicts
< 0) {
490 unlink_or_warn(output
);
491 return error(_("could not parse conflict hunks in '%s'"), path
);
495 return has_conflicts
;
499 * Look at a cache entry at "i" and see if it is not conflicting,
500 * conflicting and we are willing to handle, or conflicting and
501 * we are unable to handle, and return the determination in *type.
502 * Return the cache index to be looked at next, by skipping the
503 * stages we have already looked at in this invocation of this
506 static int check_one_conflict(struct index_state
*istate
, int i
, int *type
)
508 const struct cache_entry
*e
= istate
->cache
[i
];
516 while (i
< istate
->cache_nr
&& ce_stage(istate
->cache
[i
]) == 1)
519 /* Only handle regular files with both stages #2 and #3 */
520 if (i
+ 1 < istate
->cache_nr
) {
521 const struct cache_entry
*e2
= istate
->cache
[i
];
522 const struct cache_entry
*e3
= istate
->cache
[i
+ 1];
523 if (ce_stage(e2
) == 2 &&
525 ce_same_name(e
, e3
) &&
526 S_ISREG(e2
->ce_mode
) &&
527 S_ISREG(e3
->ce_mode
))
528 *type
= THREE_STAGED
;
531 /* Skip the entries with the same name */
532 while (i
< istate
->cache_nr
&& ce_same_name(e
, istate
->cache
[i
]))
538 * Scan the index and find paths that have conflicts that rerere can
539 * handle, i.e. the ones that has both stages #2 and #3.
541 * NEEDSWORK: we do not record or replay a previous "resolve by
542 * deletion" for a delete-modify conflict, as that is inherently risky
543 * without knowing what modification is being discarded. The only
544 * safe case, i.e. both side doing the deletion and modification that
545 * are identical to the previous round, might want to be handled,
548 static int find_conflict(struct repository
*r
, struct string_list
*conflict
)
552 if (repo_read_index(r
) < 0)
553 return error(_("index file corrupt"));
555 for (i
= 0; i
< r
->index
->cache_nr
;) {
557 const struct cache_entry
*e
= r
->index
->cache
[i
];
558 i
= check_one_conflict(r
->index
, i
, &conflict_type
);
559 if (conflict_type
== THREE_STAGED
)
560 string_list_insert(conflict
, (const char *)e
->name
);
566 * The merge_rr list is meant to hold outstanding conflicted paths
567 * that rerere could handle. Abuse the list by adding other types of
568 * entries to allow the caller to show "rerere remaining".
570 * - Conflicted paths that rerere does not handle are added
571 * - Conflicted paths that have been resolved are marked as such
572 * by storing RERERE_RESOLVED to .util field (where conflict ID
573 * is expected to be stored).
575 * Do *not* write MERGE_RR file out after calling this function.
577 * NEEDSWORK: we may want to fix the caller that implements "rerere
578 * remaining" to do this without abusing merge_rr.
580 int rerere_remaining(struct repository
*r
, struct string_list
*merge_rr
)
584 if (setup_rerere(r
, merge_rr
, RERERE_READONLY
))
586 if (repo_read_index(r
) < 0)
587 return error(_("index file corrupt"));
589 for (i
= 0; i
< r
->index
->cache_nr
;) {
591 const struct cache_entry
*e
= r
->index
->cache
[i
];
592 i
= check_one_conflict(r
->index
, i
, &conflict_type
);
593 if (conflict_type
== PUNTED
)
594 string_list_insert(merge_rr
, (const char *)e
->name
);
595 else if (conflict_type
== RESOLVED
) {
596 struct string_list_item
*it
;
597 it
= string_list_lookup(merge_rr
, (const char *)e
->name
);
600 it
->util
= RERERE_RESOLVED
;
608 * Try using the given conflict resolution "ID" to see
609 * if that recorded conflict resolves cleanly what we
612 static int try_merge(struct index_state
*istate
,
613 const struct rerere_id
*id
, const char *path
,
614 mmfile_t
*cur
, mmbuffer_t
*result
)
616 enum ll_merge_result ret
;
617 mmfile_t base
= {NULL
, 0}, other
= {NULL
, 0};
619 if (read_mmfile(&base
, rerere_path(id
, "preimage")) ||
620 read_mmfile(&other
, rerere_path(id
, "postimage"))) {
621 ret
= LL_MERGE_CONFLICT
;
624 * A three-way merge. Note that this honors user-customizable
625 * low-level merge driver settings.
627 ret
= ll_merge(result
, path
, &base
, NULL
, cur
, "", &other
, "",
638 * Find the conflict identified by "id"; the change between its
639 * "preimage" (i.e. a previous contents with conflict markers) and its
640 * "postimage" (i.e. the corresponding contents with conflicts
641 * resolved) may apply cleanly to the contents stored in "path", i.e.
642 * the conflict this time around.
644 * Returns 0 for successful replay of recorded resolution, or non-zero
647 static int merge(struct index_state
*istate
, const struct rerere_id
*id
, const char *path
)
651 mmfile_t cur
= {NULL
, 0};
652 mmbuffer_t result
= {NULL
, 0};
655 * Normalize the conflicts in path and write it out to
656 * "thisimage" temporary file.
658 if ((handle_file(istate
, path
, NULL
, rerere_path(id
, "thisimage")) < 0) ||
659 read_mmfile(&cur
, rerere_path(id
, "thisimage"))) {
664 ret
= try_merge(istate
, id
, path
, &cur
, &result
);
669 * A successful replay of recorded resolution.
670 * Mark that "postimage" was used to help gc.
672 if (utime(rerere_path(id
, "postimage"), NULL
) < 0)
673 warning_errno(_("failed utime() on '%s'"),
674 rerere_path(id
, "postimage"));
676 /* Update "path" with the resolution */
677 f
= fopen(path
, "w");
679 return error_errno(_("could not open '%s'"), path
);
680 if (fwrite(result
.ptr
, result
.size
, 1, f
) != 1)
681 error_errno(_("could not write '%s'"), path
);
683 return error_errno(_("writing '%s' failed"), path
);
692 static void update_paths(struct repository
*r
, struct string_list
*update
)
694 struct lock_file index_lock
= LOCK_INIT
;
697 repo_hold_locked_index(r
, &index_lock
, LOCK_DIE_ON_ERROR
);
699 for (i
= 0; i
< update
->nr
; i
++) {
700 struct string_list_item
*item
= &update
->items
[i
];
701 if (add_file_to_index(r
->index
, item
->string
, 0))
703 fprintf_ln(stderr
, _("Staged '%s' using previous resolution."),
707 if (write_locked_index(r
->index
, &index_lock
,
708 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
709 die(_("unable to write new index file"));
712 static void remove_variant(struct rerere_id
*id
)
714 unlink_or_warn(rerere_path(id
, "postimage"));
715 unlink_or_warn(rerere_path(id
, "preimage"));
716 id
->collection
->status
[id
->variant
] = 0;
720 * The path indicated by rr_item may still have conflict for which we
721 * have a recorded resolution, in which case replay it and optionally
722 * update it. Or it may have been resolved by the user and we may
723 * only have the preimage for that conflict, in which case the result
724 * needs to be recorded as a resolution in a postimage file.
726 static void do_rerere_one_path(struct index_state
*istate
,
727 struct string_list_item
*rr_item
,
728 struct string_list
*update
)
730 const char *path
= rr_item
->string
;
731 struct rerere_id
*id
= rr_item
->util
;
732 struct rerere_dir
*rr_dir
= id
->collection
;
735 variant
= id
->variant
;
737 /* Has the user resolved it already? */
739 if (!handle_file(istate
, path
, NULL
, NULL
)) {
740 copy_file(rerere_path(id
, "postimage"), path
, 0666);
741 id
->collection
->status
[variant
] |= RR_HAS_POSTIMAGE
;
742 fprintf_ln(stderr
, _("Recorded resolution for '%s'."), path
);
743 free_rerere_id(rr_item
);
744 rr_item
->util
= NULL
;
748 * There may be other variants that can cleanly
749 * replay. Try them and update the variant number for
754 /* Does any existing resolution apply cleanly? */
755 for (variant
= 0; variant
< rr_dir
->status_nr
; variant
++) {
756 const int both
= RR_HAS_PREIMAGE
| RR_HAS_POSTIMAGE
;
757 struct rerere_id vid
= *id
;
759 if ((rr_dir
->status
[variant
] & both
) != both
)
762 vid
.variant
= variant
;
763 if (merge(istate
, &vid
, path
))
764 continue; /* failed to replay */
767 * If there already is a different variant that applies
768 * cleanly, there is no point maintaining our own variant.
770 if (0 <= id
->variant
&& id
->variant
!= variant
)
773 if (rerere_autoupdate
)
774 string_list_insert(update
, path
);
777 _("Resolved '%s' using previous resolution."),
779 free_rerere_id(rr_item
);
780 rr_item
->util
= NULL
;
784 /* None of the existing one applies; we need a new variant */
787 variant
= id
->variant
;
788 handle_file(istate
, path
, NULL
, rerere_path(id
, "preimage"));
789 if (id
->collection
->status
[variant
] & RR_HAS_POSTIMAGE
) {
790 const char *path
= rerere_path(id
, "postimage");
792 die_errno(_("cannot unlink stray '%s'"), path
);
793 id
->collection
->status
[variant
] &= ~RR_HAS_POSTIMAGE
;
795 id
->collection
->status
[variant
] |= RR_HAS_PREIMAGE
;
796 fprintf_ln(stderr
, _("Recorded preimage for '%s'"), path
);
799 static int do_plain_rerere(struct repository
*r
,
800 struct string_list
*rr
, int fd
)
802 struct string_list conflict
= STRING_LIST_INIT_DUP
;
803 struct string_list update
= STRING_LIST_INIT_DUP
;
806 find_conflict(r
, &conflict
);
809 * MERGE_RR records paths with conflicts immediately after
810 * merge failed. Some of the conflicted paths might have been
811 * hand resolved in the working tree since then, but the
812 * initial run would catch all and register their preimages.
814 for (i
= 0; i
< conflict
.nr
; i
++) {
815 struct rerere_id
*id
;
816 unsigned char hash
[GIT_MAX_RAWSZ
];
817 const char *path
= conflict
.items
[i
].string
;
821 * Ask handle_file() to scan and assign a
822 * conflict ID. No need to write anything out
825 ret
= handle_file(r
->index
, path
, hash
, NULL
);
826 if (ret
!= 0 && string_list_has_string(rr
, path
)) {
827 remove_variant(string_list_lookup(rr
, path
)->util
);
828 string_list_remove(rr
, path
, 1);
833 id
= new_rerere_id(hash
);
834 string_list_insert(rr
, path
)->util
= id
;
836 /* Ensure that the directory exists. */
837 mkdir_in_gitdir(rerere_path(id
, NULL
));
840 for (i
= 0; i
< rr
->nr
; i
++)
841 do_rerere_one_path(r
->index
, &rr
->items
[i
], &update
);
844 update_paths(r
, &update
);
846 return write_rr(rr
, fd
);
849 static void git_rerere_config(void)
851 git_config_get_bool("rerere.enabled", &rerere_enabled
);
852 git_config_get_bool("rerere.autoupdate", &rerere_autoupdate
);
853 git_config(git_default_config
, NULL
);
856 static GIT_PATH_FUNC(git_path_rr_cache
, "rr-cache")
858 static int is_rerere_enabled(void)
865 rr_cache_exists
= is_directory(git_path_rr_cache());
866 if (rerere_enabled
< 0)
867 return rr_cache_exists
;
869 if (!rr_cache_exists
&& mkdir_in_gitdir(git_path_rr_cache()))
870 die(_("could not create directory '%s'"), git_path_rr_cache());
874 int setup_rerere(struct repository
*r
, struct string_list
*merge_rr
, int flags
)
879 if (!is_rerere_enabled())
882 if (flags
& (RERERE_AUTOUPDATE
|RERERE_NOAUTOUPDATE
))
883 rerere_autoupdate
= !!(flags
& RERERE_AUTOUPDATE
);
884 if (flags
& RERERE_READONLY
)
887 fd
= hold_lock_file_for_update(&write_lock
,
888 git_path_merge_rr(r
),
890 read_rr(r
, merge_rr
);
895 * The main entry point that is called internally from codepaths that
896 * perform mergy operations, possibly leaving conflicted index entries
897 * and working tree files.
899 int repo_rerere(struct repository
*r
, int flags
)
901 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
904 fd
= setup_rerere(r
, &merge_rr
, flags
);
907 status
= do_plain_rerere(r
, &merge_rr
, fd
);
913 * Subclass of rerere_io that reads from an in-core buffer that is a
916 struct rerere_io_mem
{
922 * ... and its getline() method implementation
924 static int rerere_mem_getline(struct strbuf
*sb
, struct rerere_io
*io_
)
926 struct rerere_io_mem
*io
= (struct rerere_io_mem
*)io_
;
933 ep
= memchr(io
->input
.buf
, '\n', io
->input
.len
);
935 ep
= io
->input
.buf
+ io
->input
.len
;
936 else if (*ep
== '\n')
938 len
= ep
- io
->input
.buf
;
939 strbuf_add(sb
, io
->input
.buf
, len
);
940 strbuf_remove(&io
->input
, 0, len
);
944 static int handle_cache(struct index_state
*istate
,
945 const char *path
, unsigned char *hash
, const char *output
)
947 mmfile_t mmfile
[3] = {{NULL
}};
948 mmbuffer_t result
= {NULL
, 0};
949 const struct cache_entry
*ce
;
950 int pos
, len
, i
, has_conflicts
;
951 struct rerere_io_mem io
;
952 int marker_size
= ll_merge_marker_size(istate
, path
);
955 * Reproduce the conflicted merge in-core
958 pos
= index_name_pos(istate
, path
, len
);
963 while (pos
< istate
->cache_nr
) {
964 enum object_type type
;
967 ce
= istate
->cache
[pos
++];
968 if (ce_namelen(ce
) != len
|| memcmp(ce
->name
, path
, len
))
970 i
= ce_stage(ce
) - 1;
971 if (!mmfile
[i
].ptr
) {
972 mmfile
[i
].ptr
= read_object_file(&ce
->oid
, &type
,
974 mmfile
[i
].size
= size
;
977 for (i
= 0; i
< 3; i
++)
978 if (!mmfile
[i
].ptr
&& !mmfile
[i
].size
)
979 mmfile
[i
].ptr
= xstrdup("");
982 * NEEDSWORK: handle conflicts from merges with
983 * merge.renormalize set, too?
985 ll_merge(&result
, path
, &mmfile
[0], NULL
,
987 &mmfile
[2], "theirs",
989 for (i
= 0; i
< 3; i
++)
992 memset(&io
, 0, sizeof(io
));
993 io
.io
.getline
= rerere_mem_getline
;
995 io
.io
.output
= fopen(output
, "w");
998 strbuf_init(&io
.input
, 0);
999 strbuf_attach(&io
.input
, result
.ptr
, result
.size
, result
.size
);
1002 * Grab the conflict ID and optionally write the original
1003 * contents with conflict markers out.
1005 has_conflicts
= handle_path(hash
, (struct rerere_io
*)&io
, marker_size
);
1006 strbuf_release(&io
.input
);
1008 fclose(io
.io
.output
);
1009 return has_conflicts
;
1012 static int rerere_forget_one_path(struct index_state
*istate
,
1014 struct string_list
*rr
)
1016 const char *filename
;
1017 struct rerere_id
*id
;
1018 unsigned char hash
[GIT_MAX_RAWSZ
];
1020 struct string_list_item
*item
;
1023 * Recreate the original conflict from the stages in the
1024 * index and compute the conflict ID
1026 ret
= handle_cache(istate
, path
, hash
, NULL
);
1028 return error(_("could not parse conflict hunks in '%s'"), path
);
1030 /* Nuke the recorded resolution for the conflict */
1031 id
= new_rerere_id(hash
);
1033 for (id
->variant
= 0;
1034 id
->variant
< id
->collection
->status_nr
;
1036 mmfile_t cur
= { NULL
, 0 };
1037 mmbuffer_t result
= {NULL
, 0};
1038 int cleanly_resolved
;
1040 if (!has_rerere_resolution(id
))
1043 handle_cache(istate
, path
, hash
, rerere_path(id
, "thisimage"));
1044 if (read_mmfile(&cur
, rerere_path(id
, "thisimage"))) {
1046 error(_("failed to update conflicted state in '%s'"), path
);
1049 cleanly_resolved
= !try_merge(istate
, id
, path
, &cur
, &result
);
1052 if (cleanly_resolved
)
1056 if (id
->collection
->status_nr
<= id
->variant
) {
1057 error(_("no remembered resolution for '%s'"), path
);
1061 filename
= rerere_path(id
, "postimage");
1062 if (unlink(filename
)) {
1063 if (errno
== ENOENT
)
1064 error(_("no remembered resolution for '%s'"), path
);
1066 error_errno(_("cannot unlink '%s'"), filename
);
1071 * Update the preimage so that the user can resolve the
1072 * conflict in the working tree, run us again to record
1075 handle_cache(istate
, path
, hash
, rerere_path(id
, "preimage"));
1076 fprintf_ln(stderr
, _("Updated preimage for '%s'"), path
);
1079 * And remember that we can record resolution for this
1080 * conflict when the user is done.
1082 item
= string_list_insert(rr
, path
);
1083 free_rerere_id(item
);
1085 fprintf(stderr
, _("Forgot resolution for '%s'\n"), path
);
1093 int rerere_forget(struct repository
*r
, struct pathspec
*pathspec
)
1096 struct string_list conflict
= STRING_LIST_INIT_DUP
;
1097 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
1099 if (repo_read_index(r
) < 0)
1100 return error(_("index file corrupt"));
1102 fd
= setup_rerere(r
, &merge_rr
, RERERE_NOAUTOUPDATE
);
1107 * The paths may have been resolved (incorrectly);
1108 * recover the original conflicted state and then
1109 * find the conflicted paths.
1111 unmerge_index(r
->index
, pathspec
);
1112 find_conflict(r
, &conflict
);
1113 for (i
= 0; i
< conflict
.nr
; i
++) {
1114 struct string_list_item
*it
= &conflict
.items
[i
];
1115 if (!match_pathspec(r
->index
, pathspec
, it
->string
,
1116 strlen(it
->string
), 0, NULL
, 0))
1118 rerere_forget_one_path(r
->index
, it
->string
, &merge_rr
);
1120 return write_rr(&merge_rr
, fd
);
1124 * Garbage collection support
1127 static timestamp_t
rerere_created_at(struct rerere_id
*id
)
1131 return stat(rerere_path(id
, "preimage"), &st
) ? (time_t) 0 : st
.st_mtime
;
1134 static timestamp_t
rerere_last_used_at(struct rerere_id
*id
)
1138 return stat(rerere_path(id
, "postimage"), &st
) ? (time_t) 0 : st
.st_mtime
;
1142 * Remove the recorded resolution for a given conflict ID
1144 static void unlink_rr_item(struct rerere_id
*id
)
1146 unlink_or_warn(rerere_path(id
, "thisimage"));
1148 id
->collection
->status
[id
->variant
] = 0;
1151 static void prune_one(struct rerere_id
*id
,
1152 timestamp_t cutoff_resolve
, timestamp_t cutoff_noresolve
)
1157 then
= rerere_last_used_at(id
);
1159 cutoff
= cutoff_resolve
;
1161 then
= rerere_created_at(id
);
1164 cutoff
= cutoff_noresolve
;
1170 /* Does the basename in "path" look plausibly like an rr-cache entry? */
1171 static int is_rr_cache_dirname(const char *path
)
1173 struct object_id oid
;
1175 return !parse_oid_hex(path
, &oid
, &end
) && !*end
;
1178 void rerere_gc(struct repository
*r
, struct string_list
*rr
)
1180 struct string_list to_remove
= STRING_LIST_INIT_DUP
;
1184 timestamp_t now
= time(NULL
);
1185 timestamp_t cutoff_noresolve
= now
- 15 * 86400;
1186 timestamp_t cutoff_resolve
= now
- 60 * 86400;
1188 if (setup_rerere(r
, rr
, 0) < 0)
1191 git_config_get_expiry_in_days("gc.rerereresolved", &cutoff_resolve
, now
);
1192 git_config_get_expiry_in_days("gc.rerereunresolved", &cutoff_noresolve
, now
);
1193 git_config(git_default_config
, NULL
);
1194 dir
= opendir(git_path("rr-cache"));
1196 die_errno(_("unable to open rr-cache directory"));
1197 /* Collect stale conflict IDs ... */
1198 while ((e
= readdir_skip_dot_and_dotdot(dir
))) {
1199 struct rerere_dir
*rr_dir
;
1200 struct rerere_id id
;
1203 if (!is_rr_cache_dirname(e
->d_name
))
1204 continue; /* or should we remove e->d_name? */
1206 rr_dir
= find_rerere_dir(e
->d_name
);
1209 for (id
.variant
= 0, id
.collection
= rr_dir
;
1210 id
.variant
< id
.collection
->status_nr
;
1212 prune_one(&id
, cutoff_resolve
, cutoff_noresolve
);
1213 if (id
.collection
->status
[id
.variant
])
1217 string_list_append(&to_remove
, e
->d_name
);
1221 /* ... and then remove the empty directories */
1222 for (i
= 0; i
< to_remove
.nr
; i
++)
1223 rmdir(git_path("rr-cache/%s", to_remove
.items
[i
].string
));
1224 string_list_clear(&to_remove
, 0);
1225 rollback_lock_file(&write_lock
);
1229 * During a conflict resolution, after "rerere" recorded the
1230 * preimages, abandon them if the user did not resolve them or
1231 * record their resolutions. And drop $GIT_DIR/MERGE_RR.
1233 * NEEDSWORK: shouldn't we be calling this from "reset --hard"?
1235 void rerere_clear(struct repository
*r
, struct string_list
*merge_rr
)
1239 if (setup_rerere(r
, merge_rr
, 0) < 0)
1242 for (i
= 0; i
< merge_rr
->nr
; i
++) {
1243 struct rerere_id
*id
= merge_rr
->items
[i
].util
;
1244 if (!has_rerere_resolution(id
)) {
1246 rmdir(rerere_path(id
, NULL
));
1249 unlink_or_warn(git_path_merge_rr(r
));
1250 rollback_lock_file(&write_lock
);