1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
4 #include "git-compat-util.h"
11 #include "string-list.h"
12 #include "read-cache-ll.h"
14 #include "xdiff-interface.h"
16 #include "resolve-undo.h"
20 #include "object-file.h"
21 #include "object-store-ll.h"
26 #define THREE_STAGED 2
27 void *RERERE_RESOLVED
= &RERERE_RESOLVED
;
29 /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
30 static int rerere_enabled
= -1;
32 /* automatically update cleanly resolved paths to the index */
33 static int rerere_autoupdate
;
35 #define RR_HAS_POSTIMAGE 1
36 #define RR_HAS_PREIMAGE 2
38 int status_alloc
, status_nr
;
39 unsigned char *status
;
40 char name
[FLEX_ARRAY
];
43 static struct strmap rerere_dirs
= STRMAP_INIT
;
45 static void free_rerere_dirs(void)
47 struct hashmap_iter iter
;
48 struct strmap_entry
*ent
;
50 strmap_for_each_entry(&rerere_dirs
, &iter
, ent
) {
51 struct rerere_dir
*rr_dir
= ent
->value
;
55 strmap_clear(&rerere_dirs
, 0);
58 static void free_rerere_id(struct string_list_item
*item
)
63 static const char *rerere_id_hex(const struct rerere_id
*id
)
65 return id
->collection
->name
;
68 static void fit_variant(struct rerere_dir
*rr_dir
, int variant
)
71 ALLOC_GROW(rr_dir
->status
, variant
, rr_dir
->status_alloc
);
72 if (rr_dir
->status_nr
< variant
) {
73 memset(rr_dir
->status
+ rr_dir
->status_nr
,
74 '\0', variant
- rr_dir
->status_nr
);
75 rr_dir
->status_nr
= variant
;
79 static void assign_variant(struct rerere_id
*id
)
82 struct rerere_dir
*rr_dir
= id
->collection
;
84 variant
= id
->variant
;
86 for (variant
= 0; variant
< rr_dir
->status_nr
; variant
++)
87 if (!rr_dir
->status
[variant
])
90 fit_variant(rr_dir
, variant
);
91 id
->variant
= variant
;
94 const char *rerere_path(const struct rerere_id
*id
, const char *file
)
97 return git_path("rr-cache/%s", rerere_id_hex(id
));
100 return git_path("rr-cache/%s/%s", rerere_id_hex(id
), file
);
102 return git_path("rr-cache/%s/%s.%d",
103 rerere_id_hex(id
), file
, id
->variant
);
106 static int is_rr_file(const char *name
, const char *filename
, int *variant
)
111 if (!strcmp(name
, filename
)) {
115 if (!skip_prefix(name
, filename
, &suffix
) || *suffix
!= '.')
119 *variant
= strtol(suffix
+ 1, &ep
, 10);
125 static void scan_rerere_dir(struct rerere_dir
*rr_dir
)
128 DIR *dir
= opendir(git_path("rr-cache/%s", rr_dir
->name
));
132 while ((de
= readdir(dir
)) != NULL
) {
135 if (is_rr_file(de
->d_name
, "postimage", &variant
)) {
136 fit_variant(rr_dir
, variant
);
137 rr_dir
->status
[variant
] |= RR_HAS_POSTIMAGE
;
138 } else if (is_rr_file(de
->d_name
, "preimage", &variant
)) {
139 fit_variant(rr_dir
, variant
);
140 rr_dir
->status
[variant
] |= RR_HAS_PREIMAGE
;
146 static struct rerere_dir
*find_rerere_dir(const char *hex
)
148 struct rerere_dir
*rr_dir
;
150 rr_dir
= strmap_get(&rerere_dirs
, hex
);
152 FLEX_ALLOC_STR(rr_dir
, name
, hex
);
153 rr_dir
->status
= NULL
;
154 rr_dir
->status_nr
= 0;
155 rr_dir
->status_alloc
= 0;
156 strmap_put(&rerere_dirs
, hex
, rr_dir
);
158 scan_rerere_dir(rr_dir
);
163 static int has_rerere_resolution(const struct rerere_id
*id
)
165 const int both
= RR_HAS_POSTIMAGE
|RR_HAS_PREIMAGE
;
166 int variant
= id
->variant
;
170 return ((id
->collection
->status
[variant
] & both
) == both
);
173 static struct rerere_id
*new_rerere_id_hex(char *hex
)
175 struct rerere_id
*id
= xmalloc(sizeof(*id
));
176 id
->collection
= find_rerere_dir(hex
);
177 id
->variant
= -1; /* not known yet */
181 static struct rerere_id
*new_rerere_id(unsigned char *hash
)
183 return new_rerere_id_hex(hash_to_hex(hash
));
187 * $GIT_DIR/MERGE_RR file is a collection of records, each of which is
188 * "conflict ID", a HT and pathname, terminated with a NUL, and is
189 * used to keep track of the set of paths that "rerere" may need to
190 * work on (i.e. what is left by the previous invocation of "git
191 * rerere" during the current conflict resolution session).
193 static void read_rr(struct repository
*r
, struct string_list
*rr
)
195 struct strbuf buf
= STRBUF_INIT
;
196 FILE *in
= fopen_or_warn(git_path_merge_rr(r
), "r");
200 while (!strbuf_getwholeline(&buf
, in
, '\0')) {
202 unsigned char hash
[GIT_MAX_RAWSZ
];
203 struct rerere_id
*id
;
205 const unsigned hexsz
= the_hash_algo
->hexsz
;
207 /* There has to be the hash, tab, path and then NUL */
208 if (buf
.len
< hexsz
+ 2 || get_hash_hex(buf
.buf
, hash
))
209 die(_("corrupt MERGE_RR"));
211 if (buf
.buf
[hexsz
] != '.') {
213 path
= buf
.buf
+ hexsz
;
216 variant
= strtol(buf
.buf
+ hexsz
+ 1, &path
, 10);
218 die(_("corrupt MERGE_RR"));
220 if (*(path
++) != '\t')
221 die(_("corrupt MERGE_RR"));
222 buf
.buf
[hexsz
] = '\0';
223 id
= new_rerere_id_hex(buf
.buf
);
224 id
->variant
= variant
;
226 * make sure id->collection->status has enough space
227 * for the variant we are interested in
229 fit_variant(id
->collection
, variant
);
230 string_list_insert(rr
, path
)->util
= id
;
232 strbuf_release(&buf
);
236 static struct lock_file write_lock
;
238 static int write_rr(struct string_list
*rr
, int out_fd
)
241 for (i
= 0; i
< rr
->nr
; i
++) {
242 struct strbuf buf
= STRBUF_INIT
;
243 struct rerere_id
*id
;
245 assert(rr
->items
[i
].util
!= RERERE_RESOLVED
);
247 id
= rr
->items
[i
].util
;
250 assert(id
->variant
>= 0);
252 strbuf_addf(&buf
, "%s.%d\t%s%c",
253 rerere_id_hex(id
), id
->variant
,
254 rr
->items
[i
].string
, 0);
256 strbuf_addf(&buf
, "%s\t%s%c",
258 rr
->items
[i
].string
, 0);
260 if (write_in_full(out_fd
, buf
.buf
, buf
.len
) < 0)
261 die(_("unable to write rerere record"));
263 strbuf_release(&buf
);
265 if (commit_lock_file(&write_lock
) != 0)
266 die(_("unable to write rerere record"));
271 * "rerere" interacts with conflicted file contents using this I/O
272 * abstraction. It reads a conflicted contents from one place via
273 * "getline()" method, and optionally can write it out after
274 * normalizing the conflicted hunks to the "output". Subclasses of
275 * rerere_io embed this structure at the beginning of their own
279 int (*getline
)(struct strbuf
*, struct rerere_io
*);
282 /* some more stuff */
285 static void ferr_write(const void *p
, size_t count
, FILE *fp
, int *err
)
289 if (fwrite(p
, count
, 1, fp
) != 1)
293 static inline void ferr_puts(const char *s
, FILE *fp
, int *err
)
295 ferr_write(s
, strlen(s
), fp
, err
);
298 static void rerere_io_putstr(const char *str
, struct rerere_io
*io
)
301 ferr_puts(str
, io
->output
, &io
->wrerror
);
304 static void rerere_io_putmem(const char *mem
, size_t sz
, struct rerere_io
*io
)
307 ferr_write(mem
, sz
, io
->output
, &io
->wrerror
);
311 * Subclass of rerere_io that reads from an on-disk file
313 struct rerere_io_file
{
319 * ... and its getline() method implementation
321 static int rerere_file_getline(struct strbuf
*sb
, struct rerere_io
*io_
)
323 struct rerere_io_file
*io
= (struct rerere_io_file
*)io_
;
324 return strbuf_getwholeline(sb
, io
->input
, '\n');
328 * Require the exact number of conflict marker letters, no more, no
329 * less, followed by SP or any whitespace
332 static int is_cmarker(char *buf
, int marker_char
, int marker_size
)
337 * The beginning of our version and the end of their version
338 * always are labeled like "<<<<< ours" or ">>>>> theirs",
339 * hence we set want_sp for them. Note that the version from
340 * the common ancestor in diff3-style output is not always
341 * labelled (e.g. "||||| common" is often seen but "|||||"
342 * alone is also valid), so we do not set want_sp.
344 want_sp
= (marker_char
== '<') || (marker_char
== '>');
346 while (marker_size
--)
347 if (*buf
++ != marker_char
)
349 if (want_sp
&& *buf
!= ' ')
351 return isspace(*buf
);
354 static void rerere_strbuf_putconflict(struct strbuf
*buf
, int ch
, size_t size
)
356 strbuf_addchars(buf
, ch
, size
);
357 strbuf_addch(buf
, '\n');
360 static int handle_conflict(struct strbuf
*out
, struct rerere_io
*io
,
361 int marker_size
, git_hash_ctx
*ctx
)
364 RR_SIDE_1
= 0, RR_SIDE_2
, RR_ORIGINAL
366 struct strbuf one
= STRBUF_INIT
, two
= STRBUF_INIT
;
367 struct strbuf buf
= STRBUF_INIT
, conflict
= STRBUF_INIT
;
368 int has_conflicts
= -1;
370 while (!io
->getline(&buf
, io
)) {
371 if (is_cmarker(buf
.buf
, '<', marker_size
)) {
372 if (handle_conflict(&conflict
, io
, marker_size
, NULL
) < 0)
374 if (hunk
== RR_SIDE_1
)
375 strbuf_addbuf(&one
, &conflict
);
377 strbuf_addbuf(&two
, &conflict
);
378 strbuf_release(&conflict
);
379 } else if (is_cmarker(buf
.buf
, '|', marker_size
)) {
380 if (hunk
!= RR_SIDE_1
)
383 } else if (is_cmarker(buf
.buf
, '=', marker_size
)) {
384 if (hunk
!= RR_SIDE_1
&& hunk
!= RR_ORIGINAL
)
387 } else if (is_cmarker(buf
.buf
, '>', marker_size
)) {
388 if (hunk
!= RR_SIDE_2
)
390 if (strbuf_cmp(&one
, &two
) > 0)
391 strbuf_swap(&one
, &two
);
393 rerere_strbuf_putconflict(out
, '<', marker_size
);
394 strbuf_addbuf(out
, &one
);
395 rerere_strbuf_putconflict(out
, '=', marker_size
);
396 strbuf_addbuf(out
, &two
);
397 rerere_strbuf_putconflict(out
, '>', marker_size
);
399 the_hash_algo
->update_fn(ctx
, one
.buf
?
402 the_hash_algo
->update_fn(ctx
, two
.buf
?
407 } else if (hunk
== RR_SIDE_1
)
408 strbuf_addbuf(&one
, &buf
);
409 else if (hunk
== RR_ORIGINAL
)
411 else if (hunk
== RR_SIDE_2
)
412 strbuf_addbuf(&two
, &buf
);
414 strbuf_release(&one
);
415 strbuf_release(&two
);
416 strbuf_release(&buf
);
418 return has_conflicts
;
422 * Read contents a file with conflicts, normalize the conflicts
423 * by (1) discarding the common ancestor version in diff3-style,
424 * (2) reordering our side and their side so that whichever sorts
425 * alphabetically earlier comes before the other one, while
426 * computing the "conflict ID", which is just an SHA-1 hash of
427 * one side of the conflict, NUL, the other side of the conflict,
428 * and NUL concatenated together.
430 * Return 1 if conflict hunks are found, 0 if there are no conflict
431 * hunks and -1 if an error occurred.
433 static int handle_path(unsigned char *hash
, struct rerere_io
*io
, int marker_size
)
436 struct strbuf buf
= STRBUF_INIT
, out
= STRBUF_INIT
;
437 int has_conflicts
= 0;
439 the_hash_algo
->init_fn(&ctx
);
441 while (!io
->getline(&buf
, io
)) {
442 if (is_cmarker(buf
.buf
, '<', marker_size
)) {
443 has_conflicts
= handle_conflict(&out
, io
, marker_size
,
445 if (has_conflicts
< 0)
447 rerere_io_putmem(out
.buf
, out
.len
, io
);
450 rerere_io_putstr(buf
.buf
, io
);
452 strbuf_release(&buf
);
453 strbuf_release(&out
);
456 the_hash_algo
->final_fn(hash
, &ctx
);
458 return has_conflicts
;
462 * Scan the path for conflicts, do the "handle_path()" thing above, and
463 * return the number of conflict hunks found.
465 static int handle_file(struct index_state
*istate
,
466 const char *path
, unsigned char *hash
, const char *output
)
468 int has_conflicts
= 0;
469 struct rerere_io_file io
;
470 int marker_size
= ll_merge_marker_size(istate
, path
);
472 memset(&io
, 0, sizeof(io
));
473 io
.io
.getline
= rerere_file_getline
;
474 io
.input
= fopen(path
, "r");
477 return error_errno(_("could not open '%s'"), path
);
480 io
.io
.output
= fopen(output
, "w");
482 error_errno(_("could not write '%s'"), output
);
488 has_conflicts
= handle_path(hash
, (struct rerere_io
*)&io
, marker_size
);
492 error(_("there were errors while writing '%s' (%s)"),
493 path
, strerror(io
.io
.wrerror
));
494 if (io
.io
.output
&& fclose(io
.io
.output
))
495 io
.io
.wrerror
= error_errno(_("failed to flush '%s'"), path
);
497 if (has_conflicts
< 0) {
499 unlink_or_warn(output
);
500 return error(_("could not parse conflict hunks in '%s'"), path
);
504 return has_conflicts
;
508 * Look at a cache entry at "i" and see if it is not conflicting,
509 * conflicting and we are willing to handle, or conflicting and
510 * we are unable to handle, and return the determination in *type.
511 * Return the cache index to be looked at next, by skipping the
512 * stages we have already looked at in this invocation of this
515 static int check_one_conflict(struct index_state
*istate
, int i
, int *type
)
517 const struct cache_entry
*e
= istate
->cache
[i
];
525 while (i
< istate
->cache_nr
&& ce_stage(istate
->cache
[i
]) == 1)
528 /* Only handle regular files with both stages #2 and #3 */
529 if (i
+ 1 < istate
->cache_nr
) {
530 const struct cache_entry
*e2
= istate
->cache
[i
];
531 const struct cache_entry
*e3
= istate
->cache
[i
+ 1];
532 if (ce_stage(e2
) == 2 &&
534 ce_same_name(e
, e3
) &&
535 S_ISREG(e2
->ce_mode
) &&
536 S_ISREG(e3
->ce_mode
))
537 *type
= THREE_STAGED
;
540 /* Skip the entries with the same name */
541 while (i
< istate
->cache_nr
&& ce_same_name(e
, istate
->cache
[i
]))
547 * Scan the index and find paths that have conflicts that rerere can
548 * handle, i.e. the ones that has both stages #2 and #3.
550 * NEEDSWORK: we do not record or replay a previous "resolve by
551 * deletion" for a delete-modify conflict, as that is inherently risky
552 * without knowing what modification is being discarded. The only
553 * safe case, i.e. both side doing the deletion and modification that
554 * are identical to the previous round, might want to be handled,
557 static int find_conflict(struct repository
*r
, struct string_list
*conflict
)
561 if (repo_read_index(r
) < 0)
562 return error(_("index file corrupt"));
564 for (i
= 0; i
< r
->index
->cache_nr
;) {
566 const struct cache_entry
*e
= r
->index
->cache
[i
];
567 i
= check_one_conflict(r
->index
, i
, &conflict_type
);
568 if (conflict_type
== THREE_STAGED
)
569 string_list_insert(conflict
, (const char *)e
->name
);
575 * The merge_rr list is meant to hold outstanding conflicted paths
576 * that rerere could handle. Abuse the list by adding other types of
577 * entries to allow the caller to show "rerere remaining".
579 * - Conflicted paths that rerere does not handle are added
580 * - Conflicted paths that have been resolved are marked as such
581 * by storing RERERE_RESOLVED to .util field (where conflict ID
582 * is expected to be stored).
584 * Do *not* write MERGE_RR file out after calling this function.
586 * NEEDSWORK: we may want to fix the caller that implements "rerere
587 * remaining" to do this without abusing merge_rr.
589 int rerere_remaining(struct repository
*r
, struct string_list
*merge_rr
)
593 if (setup_rerere(r
, merge_rr
, RERERE_READONLY
))
595 if (repo_read_index(r
) < 0)
596 return error(_("index file corrupt"));
598 for (i
= 0; i
< r
->index
->cache_nr
;) {
600 const struct cache_entry
*e
= r
->index
->cache
[i
];
601 i
= check_one_conflict(r
->index
, i
, &conflict_type
);
602 if (conflict_type
== PUNTED
)
603 string_list_insert(merge_rr
, (const char *)e
->name
);
604 else if (conflict_type
== RESOLVED
) {
605 struct string_list_item
*it
;
606 it
= string_list_lookup(merge_rr
, (const char *)e
->name
);
609 it
->util
= RERERE_RESOLVED
;
617 * Try using the given conflict resolution "ID" to see
618 * if that recorded conflict resolves cleanly what we
621 static int try_merge(struct index_state
*istate
,
622 const struct rerere_id
*id
, const char *path
,
623 mmfile_t
*cur
, mmbuffer_t
*result
)
625 enum ll_merge_result ret
;
626 mmfile_t base
= {NULL
, 0}, other
= {NULL
, 0};
628 if (read_mmfile(&base
, rerere_path(id
, "preimage")) ||
629 read_mmfile(&other
, rerere_path(id
, "postimage"))) {
630 ret
= LL_MERGE_CONFLICT
;
633 * A three-way merge. Note that this honors user-customizable
634 * low-level merge driver settings.
636 ret
= ll_merge(result
, path
, &base
, NULL
, cur
, "", &other
, "",
647 * Find the conflict identified by "id"; the change between its
648 * "preimage" (i.e. a previous contents with conflict markers) and its
649 * "postimage" (i.e. the corresponding contents with conflicts
650 * resolved) may apply cleanly to the contents stored in "path", i.e.
651 * the conflict this time around.
653 * Returns 0 for successful replay of recorded resolution, or non-zero
656 static int merge(struct index_state
*istate
, const struct rerere_id
*id
, const char *path
)
660 mmfile_t cur
= {NULL
, 0};
661 mmbuffer_t result
= {NULL
, 0};
664 * Normalize the conflicts in path and write it out to
665 * "thisimage" temporary file.
667 if ((handle_file(istate
, path
, NULL
, rerere_path(id
, "thisimage")) < 0) ||
668 read_mmfile(&cur
, rerere_path(id
, "thisimage"))) {
673 ret
= try_merge(istate
, id
, path
, &cur
, &result
);
678 * A successful replay of recorded resolution.
679 * Mark that "postimage" was used to help gc.
681 if (utime(rerere_path(id
, "postimage"), NULL
) < 0)
682 warning_errno(_("failed utime() on '%s'"),
683 rerere_path(id
, "postimage"));
685 /* Update "path" with the resolution */
686 f
= fopen(path
, "w");
688 return error_errno(_("could not open '%s'"), path
);
689 if (fwrite(result
.ptr
, result
.size
, 1, f
) != 1)
690 error_errno(_("could not write '%s'"), path
);
692 return error_errno(_("writing '%s' failed"), path
);
701 static void update_paths(struct repository
*r
, struct string_list
*update
)
703 struct lock_file index_lock
= LOCK_INIT
;
706 repo_hold_locked_index(r
, &index_lock
, LOCK_DIE_ON_ERROR
);
708 for (i
= 0; i
< update
->nr
; i
++) {
709 struct string_list_item
*item
= &update
->items
[i
];
710 if (add_file_to_index(r
->index
, item
->string
, 0))
712 fprintf_ln(stderr
, _("Staged '%s' using previous resolution."),
716 if (write_locked_index(r
->index
, &index_lock
,
717 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
718 die(_("unable to write new index file"));
721 static void remove_variant(struct rerere_id
*id
)
723 unlink_or_warn(rerere_path(id
, "postimage"));
724 unlink_or_warn(rerere_path(id
, "preimage"));
725 id
->collection
->status
[id
->variant
] = 0;
729 * The path indicated by rr_item may still have conflict for which we
730 * have a recorded resolution, in which case replay it and optionally
731 * update it. Or it may have been resolved by the user and we may
732 * only have the preimage for that conflict, in which case the result
733 * needs to be recorded as a resolution in a postimage file.
735 static void do_rerere_one_path(struct index_state
*istate
,
736 struct string_list_item
*rr_item
,
737 struct string_list
*update
)
739 const char *path
= rr_item
->string
;
740 struct rerere_id
*id
= rr_item
->util
;
741 struct rerere_dir
*rr_dir
= id
->collection
;
744 variant
= id
->variant
;
746 /* Has the user resolved it already? */
748 if (!handle_file(istate
, path
, NULL
, NULL
)) {
749 copy_file(rerere_path(id
, "postimage"), path
, 0666);
750 id
->collection
->status
[variant
] |= RR_HAS_POSTIMAGE
;
751 fprintf_ln(stderr
, _("Recorded resolution for '%s'."), path
);
752 free_rerere_id(rr_item
);
753 rr_item
->util
= NULL
;
757 * There may be other variants that can cleanly
758 * replay. Try them and update the variant number for
763 /* Does any existing resolution apply cleanly? */
764 for (variant
= 0; variant
< rr_dir
->status_nr
; variant
++) {
765 const int both
= RR_HAS_PREIMAGE
| RR_HAS_POSTIMAGE
;
766 struct rerere_id vid
= *id
;
768 if ((rr_dir
->status
[variant
] & both
) != both
)
771 vid
.variant
= variant
;
772 if (merge(istate
, &vid
, path
))
773 continue; /* failed to replay */
776 * If there already is a different variant that applies
777 * cleanly, there is no point maintaining our own variant.
779 if (0 <= id
->variant
&& id
->variant
!= variant
)
782 if (rerere_autoupdate
)
783 string_list_insert(update
, path
);
786 _("Resolved '%s' using previous resolution."),
788 free_rerere_id(rr_item
);
789 rr_item
->util
= NULL
;
793 /* None of the existing one applies; we need a new variant */
796 variant
= id
->variant
;
797 handle_file(istate
, path
, NULL
, rerere_path(id
, "preimage"));
798 if (id
->collection
->status
[variant
] & RR_HAS_POSTIMAGE
) {
799 const char *path
= rerere_path(id
, "postimage");
801 die_errno(_("cannot unlink stray '%s'"), path
);
802 id
->collection
->status
[variant
] &= ~RR_HAS_POSTIMAGE
;
804 id
->collection
->status
[variant
] |= RR_HAS_PREIMAGE
;
805 fprintf_ln(stderr
, _("Recorded preimage for '%s'"), path
);
808 static int do_plain_rerere(struct repository
*r
,
809 struct string_list
*rr
, int fd
)
811 struct string_list conflict
= STRING_LIST_INIT_DUP
;
812 struct string_list update
= STRING_LIST_INIT_DUP
;
815 find_conflict(r
, &conflict
);
818 * MERGE_RR records paths with conflicts immediately after
819 * merge failed. Some of the conflicted paths might have been
820 * hand resolved in the working tree since then, but the
821 * initial run would catch all and register their preimages.
823 for (i
= 0; i
< conflict
.nr
; i
++) {
824 struct rerere_id
*id
;
825 unsigned char hash
[GIT_MAX_RAWSZ
];
826 const char *path
= conflict
.items
[i
].string
;
830 * Ask handle_file() to scan and assign a
831 * conflict ID. No need to write anything out
834 ret
= handle_file(r
->index
, path
, hash
, NULL
);
835 if (ret
!= 0 && string_list_has_string(rr
, path
)) {
836 remove_variant(string_list_lookup(rr
, path
)->util
);
837 string_list_remove(rr
, path
, 1);
842 id
= new_rerere_id(hash
);
843 string_list_insert(rr
, path
)->util
= id
;
845 /* Ensure that the directory exists. */
846 mkdir_in_gitdir(rerere_path(id
, NULL
));
849 for (i
= 0; i
< rr
->nr
; i
++)
850 do_rerere_one_path(r
->index
, &rr
->items
[i
], &update
);
853 update_paths(r
, &update
);
855 string_list_clear(&conflict
, 0);
856 string_list_clear(&update
, 0);
857 return write_rr(rr
, fd
);
860 static void git_rerere_config(void)
862 git_config_get_bool("rerere.enabled", &rerere_enabled
);
863 git_config_get_bool("rerere.autoupdate", &rerere_autoupdate
);
864 git_config(git_default_config
, NULL
);
867 static GIT_PATH_FUNC(git_path_rr_cache
, "rr-cache")
869 static int is_rerere_enabled(void)
876 rr_cache_exists
= is_directory(git_path_rr_cache());
877 if (rerere_enabled
< 0)
878 return rr_cache_exists
;
880 if (!rr_cache_exists
&& mkdir_in_gitdir(git_path_rr_cache()))
881 die(_("could not create directory '%s'"), git_path_rr_cache());
885 int setup_rerere(struct repository
*r
, struct string_list
*merge_rr
, int flags
)
890 if (!is_rerere_enabled())
893 if (flags
& (RERERE_AUTOUPDATE
|RERERE_NOAUTOUPDATE
))
894 rerere_autoupdate
= !!(flags
& RERERE_AUTOUPDATE
);
895 if (flags
& RERERE_READONLY
)
898 fd
= hold_lock_file_for_update(&write_lock
,
899 git_path_merge_rr(r
),
901 read_rr(r
, merge_rr
);
906 * The main entry point that is called internally from codepaths that
907 * perform mergy operations, possibly leaving conflicted index entries
908 * and working tree files.
910 int repo_rerere(struct repository
*r
, int flags
)
912 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
915 fd
= setup_rerere(r
, &merge_rr
, flags
);
918 status
= do_plain_rerere(r
, &merge_rr
, fd
);
920 string_list_clear(&merge_rr
, 1);
925 * Subclass of rerere_io that reads from an in-core buffer that is a
928 struct rerere_io_mem
{
934 * ... and its getline() method implementation
936 static int rerere_mem_getline(struct strbuf
*sb
, struct rerere_io
*io_
)
938 struct rerere_io_mem
*io
= (struct rerere_io_mem
*)io_
;
945 ep
= memchr(io
->input
.buf
, '\n', io
->input
.len
);
947 ep
= io
->input
.buf
+ io
->input
.len
;
948 else if (*ep
== '\n')
950 len
= ep
- io
->input
.buf
;
951 strbuf_add(sb
, io
->input
.buf
, len
);
952 strbuf_remove(&io
->input
, 0, len
);
956 static int handle_cache(struct index_state
*istate
,
957 const char *path
, unsigned char *hash
, const char *output
)
959 mmfile_t mmfile
[3] = {{NULL
}};
960 mmbuffer_t result
= {NULL
, 0};
961 const struct cache_entry
*ce
;
962 int pos
, len
, i
, has_conflicts
;
963 struct rerere_io_mem io
;
964 int marker_size
= ll_merge_marker_size(istate
, path
);
967 * Reproduce the conflicted merge in-core
970 pos
= index_name_pos(istate
, path
, len
);
975 while (pos
< istate
->cache_nr
) {
976 enum object_type type
;
979 ce
= istate
->cache
[pos
++];
980 if (ce_namelen(ce
) != len
|| memcmp(ce
->name
, path
, len
))
982 i
= ce_stage(ce
) - 1;
983 if (!mmfile
[i
].ptr
) {
984 mmfile
[i
].ptr
= repo_read_object_file(the_repository
,
988 die(_("unable to read %s"),
989 oid_to_hex(&ce
->oid
));
990 mmfile
[i
].size
= size
;
993 for (i
= 0; i
< 3; i
++)
994 if (!mmfile
[i
].ptr
&& !mmfile
[i
].size
)
995 mmfile
[i
].ptr
= xstrdup("");
998 * NEEDSWORK: handle conflicts from merges with
999 * merge.renormalize set, too?
1001 ll_merge(&result
, path
, &mmfile
[0], NULL
,
1003 &mmfile
[2], "theirs",
1005 for (i
= 0; i
< 3; i
++)
1006 free(mmfile
[i
].ptr
);
1008 memset(&io
, 0, sizeof(io
));
1009 io
.io
.getline
= rerere_mem_getline
;
1011 io
.io
.output
= fopen(output
, "w");
1013 io
.io
.output
= NULL
;
1014 strbuf_init(&io
.input
, 0);
1015 strbuf_attach(&io
.input
, result
.ptr
, result
.size
, result
.size
);
1018 * Grab the conflict ID and optionally write the original
1019 * contents with conflict markers out.
1021 has_conflicts
= handle_path(hash
, (struct rerere_io
*)&io
, marker_size
);
1022 strbuf_release(&io
.input
);
1024 fclose(io
.io
.output
);
1025 return has_conflicts
;
1028 static int rerere_forget_one_path(struct index_state
*istate
,
1030 struct string_list
*rr
)
1032 const char *filename
;
1033 struct rerere_id
*id
;
1034 unsigned char hash
[GIT_MAX_RAWSZ
];
1036 struct string_list_item
*item
;
1039 * Recreate the original conflict from the stages in the
1040 * index and compute the conflict ID
1042 ret
= handle_cache(istate
, path
, hash
, NULL
);
1044 return error(_("could not parse conflict hunks in '%s'"), path
);
1046 /* Nuke the recorded resolution for the conflict */
1047 id
= new_rerere_id(hash
);
1049 for (id
->variant
= 0;
1050 id
->variant
< id
->collection
->status_nr
;
1052 mmfile_t cur
= { NULL
, 0 };
1053 mmbuffer_t result
= {NULL
, 0};
1054 int cleanly_resolved
;
1056 if (!has_rerere_resolution(id
))
1059 handle_cache(istate
, path
, hash
, rerere_path(id
, "thisimage"));
1060 if (read_mmfile(&cur
, rerere_path(id
, "thisimage"))) {
1062 error(_("failed to update conflicted state in '%s'"), path
);
1065 cleanly_resolved
= !try_merge(istate
, id
, path
, &cur
, &result
);
1068 if (cleanly_resolved
)
1072 if (id
->collection
->status_nr
<= id
->variant
) {
1073 error(_("no remembered resolution for '%s'"), path
);
1077 filename
= rerere_path(id
, "postimage");
1078 if (unlink(filename
)) {
1079 if (errno
== ENOENT
)
1080 error(_("no remembered resolution for '%s'"), path
);
1082 error_errno(_("cannot unlink '%s'"), filename
);
1087 * Update the preimage so that the user can resolve the
1088 * conflict in the working tree, run us again to record
1091 handle_cache(istate
, path
, hash
, rerere_path(id
, "preimage"));
1092 fprintf_ln(stderr
, _("Updated preimage for '%s'"), path
);
1095 * And remember that we can record resolution for this
1096 * conflict when the user is done.
1098 item
= string_list_insert(rr
, path
);
1099 free_rerere_id(item
);
1101 fprintf(stderr
, _("Forgot resolution for '%s'\n"), path
);
1109 int rerere_forget(struct repository
*r
, struct pathspec
*pathspec
)
1112 struct string_list conflict
= STRING_LIST_INIT_DUP
;
1113 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
1115 if (repo_read_index(r
) < 0)
1116 return error(_("index file corrupt"));
1118 fd
= setup_rerere(r
, &merge_rr
, RERERE_NOAUTOUPDATE
);
1123 * The paths may have been resolved (incorrectly);
1124 * recover the original conflicted state and then
1125 * find the conflicted paths.
1127 unmerge_index(r
->index
, pathspec
, 0);
1128 find_conflict(r
, &conflict
);
1129 for (i
= 0; i
< conflict
.nr
; i
++) {
1130 struct string_list_item
*it
= &conflict
.items
[i
];
1131 if (!match_pathspec(r
->index
, pathspec
, it
->string
,
1132 strlen(it
->string
), 0, NULL
, 0))
1134 rerere_forget_one_path(r
->index
, it
->string
, &merge_rr
);
1137 ret
= write_rr(&merge_rr
, fd
);
1139 string_list_clear(&conflict
, 0);
1140 string_list_clear(&merge_rr
, 1);
1145 * Garbage collection support
1148 static timestamp_t
rerere_created_at(struct rerere_id
*id
)
1152 return stat(rerere_path(id
, "preimage"), &st
) ? (time_t) 0 : st
.st_mtime
;
1155 static timestamp_t
rerere_last_used_at(struct rerere_id
*id
)
1159 return stat(rerere_path(id
, "postimage"), &st
) ? (time_t) 0 : st
.st_mtime
;
1163 * Remove the recorded resolution for a given conflict ID
1165 static void unlink_rr_item(struct rerere_id
*id
)
1167 unlink_or_warn(rerere_path(id
, "thisimage"));
1169 id
->collection
->status
[id
->variant
] = 0;
1172 static void prune_one(struct rerere_id
*id
,
1173 timestamp_t cutoff_resolve
, timestamp_t cutoff_noresolve
)
1178 then
= rerere_last_used_at(id
);
1180 cutoff
= cutoff_resolve
;
1182 then
= rerere_created_at(id
);
1185 cutoff
= cutoff_noresolve
;
1191 /* Does the basename in "path" look plausibly like an rr-cache entry? */
1192 static int is_rr_cache_dirname(const char *path
)
1194 struct object_id oid
;
1196 return !parse_oid_hex(path
, &oid
, &end
) && !*end
;
1199 void rerere_gc(struct repository
*r
, struct string_list
*rr
)
1201 struct string_list to_remove
= STRING_LIST_INIT_DUP
;
1205 timestamp_t now
= time(NULL
);
1206 timestamp_t cutoff_noresolve
= now
- 15 * 86400;
1207 timestamp_t cutoff_resolve
= now
- 60 * 86400;
1209 if (setup_rerere(r
, rr
, 0) < 0)
1212 repo_config_get_expiry_in_days(the_repository
, "gc.rerereresolved",
1213 &cutoff_resolve
, now
);
1214 repo_config_get_expiry_in_days(the_repository
, "gc.rerereunresolved",
1215 &cutoff_noresolve
, now
);
1216 git_config(git_default_config
, NULL
);
1217 dir
= opendir(git_path("rr-cache"));
1219 die_errno(_("unable to open rr-cache directory"));
1220 /* Collect stale conflict IDs ... */
1221 while ((e
= readdir_skip_dot_and_dotdot(dir
))) {
1222 struct rerere_dir
*rr_dir
;
1223 struct rerere_id id
;
1226 if (!is_rr_cache_dirname(e
->d_name
))
1227 continue; /* or should we remove e->d_name? */
1229 rr_dir
= find_rerere_dir(e
->d_name
);
1232 for (id
.variant
= 0, id
.collection
= rr_dir
;
1233 id
.variant
< id
.collection
->status_nr
;
1235 prune_one(&id
, cutoff_resolve
, cutoff_noresolve
);
1236 if (id
.collection
->status
[id
.variant
])
1240 string_list_append(&to_remove
, e
->d_name
);
1244 /* ... and then remove the empty directories */
1245 for (i
= 0; i
< to_remove
.nr
; i
++)
1246 rmdir(git_path("rr-cache/%s", to_remove
.items
[i
].string
));
1247 string_list_clear(&to_remove
, 0);
1248 rollback_lock_file(&write_lock
);
1252 * During a conflict resolution, after "rerere" recorded the
1253 * preimages, abandon them if the user did not resolve them or
1254 * record their resolutions. And drop $GIT_DIR/MERGE_RR.
1256 * NEEDSWORK: shouldn't we be calling this from "reset --hard"?
1258 void rerere_clear(struct repository
*r
, struct string_list
*merge_rr
)
1262 if (setup_rerere(r
, merge_rr
, 0) < 0)
1265 for (i
= 0; i
< merge_rr
->nr
; i
++) {
1266 struct rerere_id
*id
= merge_rr
->items
[i
].util
;
1267 if (!has_rerere_resolution(id
)) {
1269 rmdir(rerere_path(id
, NULL
));
1272 unlink_or_warn(git_path_merge_rr(r
));
1273 rollback_lock_file(&write_lock
);