1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
10 #include "string-list.h"
11 #include "read-cache-ll.h"
13 #include "xdiff-interface.h"
15 #include "resolve-undo.h"
19 #include "object-file.h"
20 #include "object-store-ll.h"
25 #define THREE_STAGED 2
26 void *RERERE_RESOLVED
= &RERERE_RESOLVED
;
28 /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
29 static int rerere_enabled
= -1;
31 /* automatically update cleanly resolved paths to the index */
32 static int rerere_autoupdate
;
34 #define RR_HAS_POSTIMAGE 1
35 #define RR_HAS_PREIMAGE 2
37 int status_alloc
, status_nr
;
38 unsigned char *status
;
39 char name
[FLEX_ARRAY
];
42 static struct strmap rerere_dirs
= STRMAP_INIT
;
44 static void free_rerere_dirs(void)
46 struct hashmap_iter iter
;
47 struct strmap_entry
*ent
;
49 strmap_for_each_entry(&rerere_dirs
, &iter
, ent
) {
50 struct rerere_dir
*rr_dir
= ent
->value
;
54 strmap_clear(&rerere_dirs
, 0);
57 static void free_rerere_id(struct string_list_item
*item
)
62 static const char *rerere_id_hex(const struct rerere_id
*id
)
64 return id
->collection
->name
;
67 static void fit_variant(struct rerere_dir
*rr_dir
, int variant
)
70 ALLOC_GROW(rr_dir
->status
, variant
, rr_dir
->status_alloc
);
71 if (rr_dir
->status_nr
< variant
) {
72 memset(rr_dir
->status
+ rr_dir
->status_nr
,
73 '\0', variant
- rr_dir
->status_nr
);
74 rr_dir
->status_nr
= variant
;
78 static void assign_variant(struct rerere_id
*id
)
81 struct rerere_dir
*rr_dir
= id
->collection
;
83 variant
= id
->variant
;
85 for (variant
= 0; variant
< rr_dir
->status_nr
; variant
++)
86 if (!rr_dir
->status
[variant
])
89 fit_variant(rr_dir
, variant
);
90 id
->variant
= variant
;
93 const char *rerere_path(const struct rerere_id
*id
, const char *file
)
96 return git_path("rr-cache/%s", rerere_id_hex(id
));
99 return git_path("rr-cache/%s/%s", rerere_id_hex(id
), file
);
101 return git_path("rr-cache/%s/%s.%d",
102 rerere_id_hex(id
), file
, id
->variant
);
105 static int is_rr_file(const char *name
, const char *filename
, int *variant
)
110 if (!strcmp(name
, filename
)) {
114 if (!skip_prefix(name
, filename
, &suffix
) || *suffix
!= '.')
118 *variant
= strtol(suffix
+ 1, &ep
, 10);
124 static void scan_rerere_dir(struct rerere_dir
*rr_dir
)
127 DIR *dir
= opendir(git_path("rr-cache/%s", rr_dir
->name
));
131 while ((de
= readdir(dir
)) != NULL
) {
134 if (is_rr_file(de
->d_name
, "postimage", &variant
)) {
135 fit_variant(rr_dir
, variant
);
136 rr_dir
->status
[variant
] |= RR_HAS_POSTIMAGE
;
137 } else if (is_rr_file(de
->d_name
, "preimage", &variant
)) {
138 fit_variant(rr_dir
, variant
);
139 rr_dir
->status
[variant
] |= RR_HAS_PREIMAGE
;
145 static struct rerere_dir
*find_rerere_dir(const char *hex
)
147 struct rerere_dir
*rr_dir
;
149 rr_dir
= strmap_get(&rerere_dirs
, hex
);
151 FLEX_ALLOC_STR(rr_dir
, name
, hex
);
152 rr_dir
->status
= NULL
;
153 rr_dir
->status_nr
= 0;
154 rr_dir
->status_alloc
= 0;
155 strmap_put(&rerere_dirs
, hex
, rr_dir
);
157 scan_rerere_dir(rr_dir
);
162 static int has_rerere_resolution(const struct rerere_id
*id
)
164 const int both
= RR_HAS_POSTIMAGE
|RR_HAS_PREIMAGE
;
165 int variant
= id
->variant
;
169 return ((id
->collection
->status
[variant
] & both
) == both
);
172 static struct rerere_id
*new_rerere_id_hex(char *hex
)
174 struct rerere_id
*id
= xmalloc(sizeof(*id
));
175 id
->collection
= find_rerere_dir(hex
);
176 id
->variant
= -1; /* not known yet */
180 static struct rerere_id
*new_rerere_id(unsigned char *hash
)
182 return new_rerere_id_hex(hash_to_hex(hash
));
186 * $GIT_DIR/MERGE_RR file is a collection of records, each of which is
187 * "conflict ID", a HT and pathname, terminated with a NUL, and is
188 * used to keep track of the set of paths that "rerere" may need to
189 * work on (i.e. what is left by the previous invocation of "git
190 * rerere" during the current conflict resolution session).
192 static void read_rr(struct repository
*r
, struct string_list
*rr
)
194 struct strbuf buf
= STRBUF_INIT
;
195 FILE *in
= fopen_or_warn(git_path_merge_rr(r
), "r");
199 while (!strbuf_getwholeline(&buf
, in
, '\0')) {
201 unsigned char hash
[GIT_MAX_RAWSZ
];
202 struct rerere_id
*id
;
204 const unsigned hexsz
= the_hash_algo
->hexsz
;
206 /* There has to be the hash, tab, path and then NUL */
207 if (buf
.len
< hexsz
+ 2 || get_hash_hex(buf
.buf
, hash
))
208 die(_("corrupt MERGE_RR"));
210 if (buf
.buf
[hexsz
] != '.') {
212 path
= buf
.buf
+ hexsz
;
215 variant
= strtol(buf
.buf
+ hexsz
+ 1, &path
, 10);
217 die(_("corrupt MERGE_RR"));
219 if (*(path
++) != '\t')
220 die(_("corrupt MERGE_RR"));
221 buf
.buf
[hexsz
] = '\0';
222 id
= new_rerere_id_hex(buf
.buf
);
223 id
->variant
= variant
;
225 * make sure id->collection->status has enough space
226 * for the variant we are interested in
228 fit_variant(id
->collection
, variant
);
229 string_list_insert(rr
, path
)->util
= id
;
231 strbuf_release(&buf
);
235 static struct lock_file write_lock
;
237 static int write_rr(struct string_list
*rr
, int out_fd
)
240 for (i
= 0; i
< rr
->nr
; i
++) {
241 struct strbuf buf
= STRBUF_INIT
;
242 struct rerere_id
*id
;
244 assert(rr
->items
[i
].util
!= RERERE_RESOLVED
);
246 id
= rr
->items
[i
].util
;
249 assert(id
->variant
>= 0);
251 strbuf_addf(&buf
, "%s.%d\t%s%c",
252 rerere_id_hex(id
), id
->variant
,
253 rr
->items
[i
].string
, 0);
255 strbuf_addf(&buf
, "%s\t%s%c",
257 rr
->items
[i
].string
, 0);
259 if (write_in_full(out_fd
, buf
.buf
, buf
.len
) < 0)
260 die(_("unable to write rerere record"));
262 strbuf_release(&buf
);
264 if (commit_lock_file(&write_lock
) != 0)
265 die(_("unable to write rerere record"));
270 * "rerere" interacts with conflicted file contents using this I/O
271 * abstraction. It reads a conflicted contents from one place via
272 * "getline()" method, and optionally can write it out after
273 * normalizing the conflicted hunks to the "output". Subclasses of
274 * rerere_io embed this structure at the beginning of their own
278 int (*getline
)(struct strbuf
*, struct rerere_io
*);
281 /* some more stuff */
284 static void ferr_write(const void *p
, size_t count
, FILE *fp
, int *err
)
288 if (fwrite(p
, count
, 1, fp
) != 1)
292 static inline void ferr_puts(const char *s
, FILE *fp
, int *err
)
294 ferr_write(s
, strlen(s
), fp
, err
);
297 static void rerere_io_putstr(const char *str
, struct rerere_io
*io
)
300 ferr_puts(str
, io
->output
, &io
->wrerror
);
303 static void rerere_io_putmem(const char *mem
, size_t sz
, struct rerere_io
*io
)
306 ferr_write(mem
, sz
, io
->output
, &io
->wrerror
);
310 * Subclass of rerere_io that reads from an on-disk file
312 struct rerere_io_file
{
318 * ... and its getline() method implementation
320 static int rerere_file_getline(struct strbuf
*sb
, struct rerere_io
*io_
)
322 struct rerere_io_file
*io
= (struct rerere_io_file
*)io_
;
323 return strbuf_getwholeline(sb
, io
->input
, '\n');
327 * Require the exact number of conflict marker letters, no more, no
328 * less, followed by SP or any whitespace
331 static int is_cmarker(char *buf
, int marker_char
, int marker_size
)
336 * The beginning of our version and the end of their version
337 * always are labeled like "<<<<< ours" or ">>>>> theirs",
338 * hence we set want_sp for them. Note that the version from
339 * the common ancestor in diff3-style output is not always
340 * labelled (e.g. "||||| common" is often seen but "|||||"
341 * alone is also valid), so we do not set want_sp.
343 want_sp
= (marker_char
== '<') || (marker_char
== '>');
345 while (marker_size
--)
346 if (*buf
++ != marker_char
)
348 if (want_sp
&& *buf
!= ' ')
350 return isspace(*buf
);
353 static void rerere_strbuf_putconflict(struct strbuf
*buf
, int ch
, size_t size
)
355 strbuf_addchars(buf
, ch
, size
);
356 strbuf_addch(buf
, '\n');
359 static int handle_conflict(struct strbuf
*out
, struct rerere_io
*io
,
360 int marker_size
, git_hash_ctx
*ctx
)
363 RR_SIDE_1
= 0, RR_SIDE_2
, RR_ORIGINAL
365 struct strbuf one
= STRBUF_INIT
, two
= STRBUF_INIT
;
366 struct strbuf buf
= STRBUF_INIT
, conflict
= STRBUF_INIT
;
367 int has_conflicts
= -1;
369 while (!io
->getline(&buf
, io
)) {
370 if (is_cmarker(buf
.buf
, '<', marker_size
)) {
371 if (handle_conflict(&conflict
, io
, marker_size
, NULL
) < 0)
373 if (hunk
== RR_SIDE_1
)
374 strbuf_addbuf(&one
, &conflict
);
376 strbuf_addbuf(&two
, &conflict
);
377 strbuf_release(&conflict
);
378 } else if (is_cmarker(buf
.buf
, '|', marker_size
)) {
379 if (hunk
!= RR_SIDE_1
)
382 } else if (is_cmarker(buf
.buf
, '=', marker_size
)) {
383 if (hunk
!= RR_SIDE_1
&& hunk
!= RR_ORIGINAL
)
386 } else if (is_cmarker(buf
.buf
, '>', marker_size
)) {
387 if (hunk
!= RR_SIDE_2
)
389 if (strbuf_cmp(&one
, &two
) > 0)
390 strbuf_swap(&one
, &two
);
392 rerere_strbuf_putconflict(out
, '<', marker_size
);
393 strbuf_addbuf(out
, &one
);
394 rerere_strbuf_putconflict(out
, '=', marker_size
);
395 strbuf_addbuf(out
, &two
);
396 rerere_strbuf_putconflict(out
, '>', marker_size
);
398 the_hash_algo
->update_fn(ctx
, one
.buf
?
401 the_hash_algo
->update_fn(ctx
, two
.buf
?
406 } else if (hunk
== RR_SIDE_1
)
407 strbuf_addbuf(&one
, &buf
);
408 else if (hunk
== RR_ORIGINAL
)
410 else if (hunk
== RR_SIDE_2
)
411 strbuf_addbuf(&two
, &buf
);
413 strbuf_release(&one
);
414 strbuf_release(&two
);
415 strbuf_release(&buf
);
417 return has_conflicts
;
421 * Read contents a file with conflicts, normalize the conflicts
422 * by (1) discarding the common ancestor version in diff3-style,
423 * (2) reordering our side and their side so that whichever sorts
424 * alphabetically earlier comes before the other one, while
425 * computing the "conflict ID", which is just an SHA-1 hash of
426 * one side of the conflict, NUL, the other side of the conflict,
427 * and NUL concatenated together.
429 * Return 1 if conflict hunks are found, 0 if there are no conflict
430 * hunks and -1 if an error occurred.
432 static int handle_path(unsigned char *hash
, struct rerere_io
*io
, int marker_size
)
435 struct strbuf buf
= STRBUF_INIT
, out
= STRBUF_INIT
;
436 int has_conflicts
= 0;
438 the_hash_algo
->init_fn(&ctx
);
440 while (!io
->getline(&buf
, io
)) {
441 if (is_cmarker(buf
.buf
, '<', marker_size
)) {
442 has_conflicts
= handle_conflict(&out
, io
, marker_size
,
444 if (has_conflicts
< 0)
446 rerere_io_putmem(out
.buf
, out
.len
, io
);
449 rerere_io_putstr(buf
.buf
, io
);
451 strbuf_release(&buf
);
452 strbuf_release(&out
);
455 the_hash_algo
->final_fn(hash
, &ctx
);
457 return has_conflicts
;
461 * Scan the path for conflicts, do the "handle_path()" thing above, and
462 * return the number of conflict hunks found.
464 static int handle_file(struct index_state
*istate
,
465 const char *path
, unsigned char *hash
, const char *output
)
467 int has_conflicts
= 0;
468 struct rerere_io_file io
;
469 int marker_size
= ll_merge_marker_size(istate
, path
);
471 memset(&io
, 0, sizeof(io
));
472 io
.io
.getline
= rerere_file_getline
;
473 io
.input
= fopen(path
, "r");
476 return error_errno(_("could not open '%s'"), path
);
479 io
.io
.output
= fopen(output
, "w");
481 error_errno(_("could not write '%s'"), output
);
487 has_conflicts
= handle_path(hash
, (struct rerere_io
*)&io
, marker_size
);
491 error(_("there were errors while writing '%s' (%s)"),
492 path
, strerror(io
.io
.wrerror
));
493 if (io
.io
.output
&& fclose(io
.io
.output
))
494 io
.io
.wrerror
= error_errno(_("failed to flush '%s'"), path
);
496 if (has_conflicts
< 0) {
498 unlink_or_warn(output
);
499 return error(_("could not parse conflict hunks in '%s'"), path
);
503 return has_conflicts
;
507 * Look at a cache entry at "i" and see if it is not conflicting,
508 * conflicting and we are willing to handle, or conflicting and
509 * we are unable to handle, and return the determination in *type.
510 * Return the cache index to be looked at next, by skipping the
511 * stages we have already looked at in this invocation of this
514 static int check_one_conflict(struct index_state
*istate
, int i
, int *type
)
516 const struct cache_entry
*e
= istate
->cache
[i
];
524 while (i
< istate
->cache_nr
&& ce_stage(istate
->cache
[i
]) == 1)
527 /* Only handle regular files with both stages #2 and #3 */
528 if (i
+ 1 < istate
->cache_nr
) {
529 const struct cache_entry
*e2
= istate
->cache
[i
];
530 const struct cache_entry
*e3
= istate
->cache
[i
+ 1];
531 if (ce_stage(e2
) == 2 &&
533 ce_same_name(e
, e3
) &&
534 S_ISREG(e2
->ce_mode
) &&
535 S_ISREG(e3
->ce_mode
))
536 *type
= THREE_STAGED
;
539 /* Skip the entries with the same name */
540 while (i
< istate
->cache_nr
&& ce_same_name(e
, istate
->cache
[i
]))
546 * Scan the index and find paths that have conflicts that rerere can
547 * handle, i.e. the ones that has both stages #2 and #3.
549 * NEEDSWORK: we do not record or replay a previous "resolve by
550 * deletion" for a delete-modify conflict, as that is inherently risky
551 * without knowing what modification is being discarded. The only
552 * safe case, i.e. both side doing the deletion and modification that
553 * are identical to the previous round, might want to be handled,
556 static int find_conflict(struct repository
*r
, struct string_list
*conflict
)
560 if (repo_read_index(r
) < 0)
561 return error(_("index file corrupt"));
563 for (i
= 0; i
< r
->index
->cache_nr
;) {
565 const struct cache_entry
*e
= r
->index
->cache
[i
];
566 i
= check_one_conflict(r
->index
, i
, &conflict_type
);
567 if (conflict_type
== THREE_STAGED
)
568 string_list_insert(conflict
, (const char *)e
->name
);
574 * The merge_rr list is meant to hold outstanding conflicted paths
575 * that rerere could handle. Abuse the list by adding other types of
576 * entries to allow the caller to show "rerere remaining".
578 * - Conflicted paths that rerere does not handle are added
579 * - Conflicted paths that have been resolved are marked as such
580 * by storing RERERE_RESOLVED to .util field (where conflict ID
581 * is expected to be stored).
583 * Do *not* write MERGE_RR file out after calling this function.
585 * NEEDSWORK: we may want to fix the caller that implements "rerere
586 * remaining" to do this without abusing merge_rr.
588 int rerere_remaining(struct repository
*r
, struct string_list
*merge_rr
)
592 if (setup_rerere(r
, merge_rr
, RERERE_READONLY
))
594 if (repo_read_index(r
) < 0)
595 return error(_("index file corrupt"));
597 for (i
= 0; i
< r
->index
->cache_nr
;) {
599 const struct cache_entry
*e
= r
->index
->cache
[i
];
600 i
= check_one_conflict(r
->index
, i
, &conflict_type
);
601 if (conflict_type
== PUNTED
)
602 string_list_insert(merge_rr
, (const char *)e
->name
);
603 else if (conflict_type
== RESOLVED
) {
604 struct string_list_item
*it
;
605 it
= string_list_lookup(merge_rr
, (const char *)e
->name
);
608 it
->util
= RERERE_RESOLVED
;
616 * Try using the given conflict resolution "ID" to see
617 * if that recorded conflict resolves cleanly what we
620 static int try_merge(struct index_state
*istate
,
621 const struct rerere_id
*id
, const char *path
,
622 mmfile_t
*cur
, mmbuffer_t
*result
)
624 enum ll_merge_result ret
;
625 mmfile_t base
= {NULL
, 0}, other
= {NULL
, 0};
627 if (read_mmfile(&base
, rerere_path(id
, "preimage")) ||
628 read_mmfile(&other
, rerere_path(id
, "postimage"))) {
629 ret
= LL_MERGE_CONFLICT
;
632 * A three-way merge. Note that this honors user-customizable
633 * low-level merge driver settings.
635 ret
= ll_merge(result
, path
, &base
, NULL
, cur
, "", &other
, "",
646 * Find the conflict identified by "id"; the change between its
647 * "preimage" (i.e. a previous contents with conflict markers) and its
648 * "postimage" (i.e. the corresponding contents with conflicts
649 * resolved) may apply cleanly to the contents stored in "path", i.e.
650 * the conflict this time around.
652 * Returns 0 for successful replay of recorded resolution, or non-zero
655 static int merge(struct index_state
*istate
, const struct rerere_id
*id
, const char *path
)
659 mmfile_t cur
= {NULL
, 0};
660 mmbuffer_t result
= {NULL
, 0};
663 * Normalize the conflicts in path and write it out to
664 * "thisimage" temporary file.
666 if ((handle_file(istate
, path
, NULL
, rerere_path(id
, "thisimage")) < 0) ||
667 read_mmfile(&cur
, rerere_path(id
, "thisimage"))) {
672 ret
= try_merge(istate
, id
, path
, &cur
, &result
);
677 * A successful replay of recorded resolution.
678 * Mark that "postimage" was used to help gc.
680 if (utime(rerere_path(id
, "postimage"), NULL
) < 0)
681 warning_errno(_("failed utime() on '%s'"),
682 rerere_path(id
, "postimage"));
684 /* Update "path" with the resolution */
685 f
= fopen(path
, "w");
687 return error_errno(_("could not open '%s'"), path
);
688 if (fwrite(result
.ptr
, result
.size
, 1, f
) != 1)
689 error_errno(_("could not write '%s'"), path
);
691 return error_errno(_("writing '%s' failed"), path
);
700 static void update_paths(struct repository
*r
, struct string_list
*update
)
702 struct lock_file index_lock
= LOCK_INIT
;
705 repo_hold_locked_index(r
, &index_lock
, LOCK_DIE_ON_ERROR
);
707 for (i
= 0; i
< update
->nr
; i
++) {
708 struct string_list_item
*item
= &update
->items
[i
];
709 if (add_file_to_index(r
->index
, item
->string
, 0))
711 fprintf_ln(stderr
, _("Staged '%s' using previous resolution."),
715 if (write_locked_index(r
->index
, &index_lock
,
716 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
717 die(_("unable to write new index file"));
720 static void remove_variant(struct rerere_id
*id
)
722 unlink_or_warn(rerere_path(id
, "postimage"));
723 unlink_or_warn(rerere_path(id
, "preimage"));
724 id
->collection
->status
[id
->variant
] = 0;
728 * The path indicated by rr_item may still have conflict for which we
729 * have a recorded resolution, in which case replay it and optionally
730 * update it. Or it may have been resolved by the user and we may
731 * only have the preimage for that conflict, in which case the result
732 * needs to be recorded as a resolution in a postimage file.
734 static void do_rerere_one_path(struct index_state
*istate
,
735 struct string_list_item
*rr_item
,
736 struct string_list
*update
)
738 const char *path
= rr_item
->string
;
739 struct rerere_id
*id
= rr_item
->util
;
740 struct rerere_dir
*rr_dir
= id
->collection
;
743 variant
= id
->variant
;
745 /* Has the user resolved it already? */
747 if (!handle_file(istate
, path
, NULL
, NULL
)) {
748 copy_file(rerere_path(id
, "postimage"), path
, 0666);
749 id
->collection
->status
[variant
] |= RR_HAS_POSTIMAGE
;
750 fprintf_ln(stderr
, _("Recorded resolution for '%s'."), path
);
751 free_rerere_id(rr_item
);
752 rr_item
->util
= NULL
;
756 * There may be other variants that can cleanly
757 * replay. Try them and update the variant number for
762 /* Does any existing resolution apply cleanly? */
763 for (variant
= 0; variant
< rr_dir
->status_nr
; variant
++) {
764 const int both
= RR_HAS_PREIMAGE
| RR_HAS_POSTIMAGE
;
765 struct rerere_id vid
= *id
;
767 if ((rr_dir
->status
[variant
] & both
) != both
)
770 vid
.variant
= variant
;
771 if (merge(istate
, &vid
, path
))
772 continue; /* failed to replay */
775 * If there already is a different variant that applies
776 * cleanly, there is no point maintaining our own variant.
778 if (0 <= id
->variant
&& id
->variant
!= variant
)
781 if (rerere_autoupdate
)
782 string_list_insert(update
, path
);
785 _("Resolved '%s' using previous resolution."),
787 free_rerere_id(rr_item
);
788 rr_item
->util
= NULL
;
792 /* None of the existing one applies; we need a new variant */
795 variant
= id
->variant
;
796 handle_file(istate
, path
, NULL
, rerere_path(id
, "preimage"));
797 if (id
->collection
->status
[variant
] & RR_HAS_POSTIMAGE
) {
798 const char *path
= rerere_path(id
, "postimage");
800 die_errno(_("cannot unlink stray '%s'"), path
);
801 id
->collection
->status
[variant
] &= ~RR_HAS_POSTIMAGE
;
803 id
->collection
->status
[variant
] |= RR_HAS_PREIMAGE
;
804 fprintf_ln(stderr
, _("Recorded preimage for '%s'"), path
);
807 static int do_plain_rerere(struct repository
*r
,
808 struct string_list
*rr
, int fd
)
810 struct string_list conflict
= STRING_LIST_INIT_DUP
;
811 struct string_list update
= STRING_LIST_INIT_DUP
;
814 find_conflict(r
, &conflict
);
817 * MERGE_RR records paths with conflicts immediately after
818 * merge failed. Some of the conflicted paths might have been
819 * hand resolved in the working tree since then, but the
820 * initial run would catch all and register their preimages.
822 for (i
= 0; i
< conflict
.nr
; i
++) {
823 struct rerere_id
*id
;
824 unsigned char hash
[GIT_MAX_RAWSZ
];
825 const char *path
= conflict
.items
[i
].string
;
829 * Ask handle_file() to scan and assign a
830 * conflict ID. No need to write anything out
833 ret
= handle_file(r
->index
, path
, hash
, NULL
);
834 if (ret
!= 0 && string_list_has_string(rr
, path
)) {
835 remove_variant(string_list_lookup(rr
, path
)->util
);
836 string_list_remove(rr
, path
, 1);
841 id
= new_rerere_id(hash
);
842 string_list_insert(rr
, path
)->util
= id
;
844 /* Ensure that the directory exists. */
845 mkdir_in_gitdir(rerere_path(id
, NULL
));
848 for (i
= 0; i
< rr
->nr
; i
++)
849 do_rerere_one_path(r
->index
, &rr
->items
[i
], &update
);
852 update_paths(r
, &update
);
854 string_list_clear(&conflict
, 0);
855 string_list_clear(&update
, 0);
856 return write_rr(rr
, fd
);
859 static void git_rerere_config(void)
861 git_config_get_bool("rerere.enabled", &rerere_enabled
);
862 git_config_get_bool("rerere.autoupdate", &rerere_autoupdate
);
863 git_config(git_default_config
, NULL
);
866 static GIT_PATH_FUNC(git_path_rr_cache
, "rr-cache")
868 static int is_rerere_enabled(void)
875 rr_cache_exists
= is_directory(git_path_rr_cache());
876 if (rerere_enabled
< 0)
877 return rr_cache_exists
;
879 if (!rr_cache_exists
&& mkdir_in_gitdir(git_path_rr_cache()))
880 die(_("could not create directory '%s'"), git_path_rr_cache());
884 int setup_rerere(struct repository
*r
, struct string_list
*merge_rr
, int flags
)
889 if (!is_rerere_enabled())
892 if (flags
& (RERERE_AUTOUPDATE
|RERERE_NOAUTOUPDATE
))
893 rerere_autoupdate
= !!(flags
& RERERE_AUTOUPDATE
);
894 if (flags
& RERERE_READONLY
)
897 fd
= hold_lock_file_for_update(&write_lock
,
898 git_path_merge_rr(r
),
900 read_rr(r
, merge_rr
);
905 * The main entry point that is called internally from codepaths that
906 * perform mergy operations, possibly leaving conflicted index entries
907 * and working tree files.
909 int repo_rerere(struct repository
*r
, int flags
)
911 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
914 fd
= setup_rerere(r
, &merge_rr
, flags
);
917 status
= do_plain_rerere(r
, &merge_rr
, fd
);
919 string_list_clear(&merge_rr
, 1);
924 * Subclass of rerere_io that reads from an in-core buffer that is a
927 struct rerere_io_mem
{
933 * ... and its getline() method implementation
935 static int rerere_mem_getline(struct strbuf
*sb
, struct rerere_io
*io_
)
937 struct rerere_io_mem
*io
= (struct rerere_io_mem
*)io_
;
944 ep
= memchr(io
->input
.buf
, '\n', io
->input
.len
);
946 ep
= io
->input
.buf
+ io
->input
.len
;
947 else if (*ep
== '\n')
949 len
= ep
- io
->input
.buf
;
950 strbuf_add(sb
, io
->input
.buf
, len
);
951 strbuf_remove(&io
->input
, 0, len
);
955 static int handle_cache(struct index_state
*istate
,
956 const char *path
, unsigned char *hash
, const char *output
)
958 mmfile_t mmfile
[3] = {{NULL
}};
959 mmbuffer_t result
= {NULL
, 0};
960 const struct cache_entry
*ce
;
961 int pos
, len
, i
, has_conflicts
;
962 struct rerere_io_mem io
;
963 int marker_size
= ll_merge_marker_size(istate
, path
);
966 * Reproduce the conflicted merge in-core
969 pos
= index_name_pos(istate
, path
, len
);
974 while (pos
< istate
->cache_nr
) {
975 enum object_type type
;
978 ce
= istate
->cache
[pos
++];
979 if (ce_namelen(ce
) != len
|| memcmp(ce
->name
, path
, len
))
981 i
= ce_stage(ce
) - 1;
982 if (!mmfile
[i
].ptr
) {
983 mmfile
[i
].ptr
= repo_read_object_file(the_repository
,
987 die(_("unable to read %s"),
988 oid_to_hex(&ce
->oid
));
989 mmfile
[i
].size
= size
;
992 for (i
= 0; i
< 3; i
++)
993 if (!mmfile
[i
].ptr
&& !mmfile
[i
].size
)
994 mmfile
[i
].ptr
= xstrdup("");
997 * NEEDSWORK: handle conflicts from merges with
998 * merge.renormalize set, too?
1000 ll_merge(&result
, path
, &mmfile
[0], NULL
,
1002 &mmfile
[2], "theirs",
1004 for (i
= 0; i
< 3; i
++)
1005 free(mmfile
[i
].ptr
);
1007 memset(&io
, 0, sizeof(io
));
1008 io
.io
.getline
= rerere_mem_getline
;
1010 io
.io
.output
= fopen(output
, "w");
1012 io
.io
.output
= NULL
;
1013 strbuf_init(&io
.input
, 0);
1014 strbuf_attach(&io
.input
, result
.ptr
, result
.size
, result
.size
);
1017 * Grab the conflict ID and optionally write the original
1018 * contents with conflict markers out.
1020 has_conflicts
= handle_path(hash
, (struct rerere_io
*)&io
, marker_size
);
1021 strbuf_release(&io
.input
);
1023 fclose(io
.io
.output
);
1024 return has_conflicts
;
1027 static int rerere_forget_one_path(struct index_state
*istate
,
1029 struct string_list
*rr
)
1031 const char *filename
;
1032 struct rerere_id
*id
;
1033 unsigned char hash
[GIT_MAX_RAWSZ
];
1035 struct string_list_item
*item
;
1038 * Recreate the original conflict from the stages in the
1039 * index and compute the conflict ID
1041 ret
= handle_cache(istate
, path
, hash
, NULL
);
1043 return error(_("could not parse conflict hunks in '%s'"), path
);
1045 /* Nuke the recorded resolution for the conflict */
1046 id
= new_rerere_id(hash
);
1048 for (id
->variant
= 0;
1049 id
->variant
< id
->collection
->status_nr
;
1051 mmfile_t cur
= { NULL
, 0 };
1052 mmbuffer_t result
= {NULL
, 0};
1053 int cleanly_resolved
;
1055 if (!has_rerere_resolution(id
))
1058 handle_cache(istate
, path
, hash
, rerere_path(id
, "thisimage"));
1059 if (read_mmfile(&cur
, rerere_path(id
, "thisimage"))) {
1061 error(_("failed to update conflicted state in '%s'"), path
);
1064 cleanly_resolved
= !try_merge(istate
, id
, path
, &cur
, &result
);
1067 if (cleanly_resolved
)
1071 if (id
->collection
->status_nr
<= id
->variant
) {
1072 error(_("no remembered resolution for '%s'"), path
);
1076 filename
= rerere_path(id
, "postimage");
1077 if (unlink(filename
)) {
1078 if (errno
== ENOENT
)
1079 error(_("no remembered resolution for '%s'"), path
);
1081 error_errno(_("cannot unlink '%s'"), filename
);
1086 * Update the preimage so that the user can resolve the
1087 * conflict in the working tree, run us again to record
1090 handle_cache(istate
, path
, hash
, rerere_path(id
, "preimage"));
1091 fprintf_ln(stderr
, _("Updated preimage for '%s'"), path
);
1094 * And remember that we can record resolution for this
1095 * conflict when the user is done.
1097 item
= string_list_insert(rr
, path
);
1098 free_rerere_id(item
);
1100 fprintf(stderr
, _("Forgot resolution for '%s'\n"), path
);
1108 int rerere_forget(struct repository
*r
, struct pathspec
*pathspec
)
1111 struct string_list conflict
= STRING_LIST_INIT_DUP
;
1112 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
1114 if (repo_read_index(r
) < 0)
1115 return error(_("index file corrupt"));
1117 fd
= setup_rerere(r
, &merge_rr
, RERERE_NOAUTOUPDATE
);
1122 * The paths may have been resolved (incorrectly);
1123 * recover the original conflicted state and then
1124 * find the conflicted paths.
1126 unmerge_index(r
->index
, pathspec
, 0);
1127 find_conflict(r
, &conflict
);
1128 for (i
= 0; i
< conflict
.nr
; i
++) {
1129 struct string_list_item
*it
= &conflict
.items
[i
];
1130 if (!match_pathspec(r
->index
, pathspec
, it
->string
,
1131 strlen(it
->string
), 0, NULL
, 0))
1133 rerere_forget_one_path(r
->index
, it
->string
, &merge_rr
);
1135 return write_rr(&merge_rr
, fd
);
1139 * Garbage collection support
1142 static timestamp_t
rerere_created_at(struct rerere_id
*id
)
1146 return stat(rerere_path(id
, "preimage"), &st
) ? (time_t) 0 : st
.st_mtime
;
1149 static timestamp_t
rerere_last_used_at(struct rerere_id
*id
)
1153 return stat(rerere_path(id
, "postimage"), &st
) ? (time_t) 0 : st
.st_mtime
;
1157 * Remove the recorded resolution for a given conflict ID
1159 static void unlink_rr_item(struct rerere_id
*id
)
1161 unlink_or_warn(rerere_path(id
, "thisimage"));
1163 id
->collection
->status
[id
->variant
] = 0;
1166 static void prune_one(struct rerere_id
*id
,
1167 timestamp_t cutoff_resolve
, timestamp_t cutoff_noresolve
)
1172 then
= rerere_last_used_at(id
);
1174 cutoff
= cutoff_resolve
;
1176 then
= rerere_created_at(id
);
1179 cutoff
= cutoff_noresolve
;
1185 /* Does the basename in "path" look plausibly like an rr-cache entry? */
1186 static int is_rr_cache_dirname(const char *path
)
1188 struct object_id oid
;
1190 return !parse_oid_hex(path
, &oid
, &end
) && !*end
;
1193 void rerere_gc(struct repository
*r
, struct string_list
*rr
)
1195 struct string_list to_remove
= STRING_LIST_INIT_DUP
;
1199 timestamp_t now
= time(NULL
);
1200 timestamp_t cutoff_noresolve
= now
- 15 * 86400;
1201 timestamp_t cutoff_resolve
= now
- 60 * 86400;
1203 if (setup_rerere(r
, rr
, 0) < 0)
1206 git_config_get_expiry_in_days("gc.rerereresolved", &cutoff_resolve
, now
);
1207 git_config_get_expiry_in_days("gc.rerereunresolved", &cutoff_noresolve
, now
);
1208 git_config(git_default_config
, NULL
);
1209 dir
= opendir(git_path("rr-cache"));
1211 die_errno(_("unable to open rr-cache directory"));
1212 /* Collect stale conflict IDs ... */
1213 while ((e
= readdir_skip_dot_and_dotdot(dir
))) {
1214 struct rerere_dir
*rr_dir
;
1215 struct rerere_id id
;
1218 if (!is_rr_cache_dirname(e
->d_name
))
1219 continue; /* or should we remove e->d_name? */
1221 rr_dir
= find_rerere_dir(e
->d_name
);
1224 for (id
.variant
= 0, id
.collection
= rr_dir
;
1225 id
.variant
< id
.collection
->status_nr
;
1227 prune_one(&id
, cutoff_resolve
, cutoff_noresolve
);
1228 if (id
.collection
->status
[id
.variant
])
1232 string_list_append(&to_remove
, e
->d_name
);
1236 /* ... and then remove the empty directories */
1237 for (i
= 0; i
< to_remove
.nr
; i
++)
1238 rmdir(git_path("rr-cache/%s", to_remove
.items
[i
].string
));
1239 string_list_clear(&to_remove
, 0);
1240 rollback_lock_file(&write_lock
);
1244 * During a conflict resolution, after "rerere" recorded the
1245 * preimages, abandon them if the user did not resolve them or
1246 * record their resolutions. And drop $GIT_DIR/MERGE_RR.
1248 * NEEDSWORK: shouldn't we be calling this from "reset --hard"?
1250 void rerere_clear(struct repository
*r
, struct string_list
*merge_rr
)
1254 if (setup_rerere(r
, merge_rr
, 0) < 0)
1257 for (i
= 0; i
< merge_rr
->nr
; i
++) {
1258 struct rerere_id
*id
= merge_rr
->items
[i
].util
;
1259 if (!has_rerere_resolution(id
)) {
1261 rmdir(rerere_path(id
, NULL
));
1264 unlink_or_warn(git_path_merge_rr(r
));
1265 rollback_lock_file(&write_lock
);