1 #include "git-compat-util.h"
8 #include "string-list.h"
9 #include "read-cache-ll.h"
11 #include "xdiff-interface.h"
13 #include "resolve-undo.h"
17 #include "object-file.h"
18 #include "object-store-ll.h"
23 #define THREE_STAGED 2
24 void *RERERE_RESOLVED
= &RERERE_RESOLVED
;
26 /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
27 static int rerere_enabled
= -1;
29 /* automatically update cleanly resolved paths to the index */
30 static int rerere_autoupdate
;
32 #define RR_HAS_POSTIMAGE 1
33 #define RR_HAS_PREIMAGE 2
35 int status_alloc
, status_nr
;
36 unsigned char *status
;
37 char name
[FLEX_ARRAY
];
40 static struct strmap rerere_dirs
= STRMAP_INIT
;
42 static void free_rerere_dirs(void)
44 struct hashmap_iter iter
;
45 struct strmap_entry
*ent
;
47 strmap_for_each_entry(&rerere_dirs
, &iter
, ent
) {
48 struct rerere_dir
*rr_dir
= ent
->value
;
52 strmap_clear(&rerere_dirs
, 0);
55 static void free_rerere_id(struct string_list_item
*item
)
60 static const char *rerere_id_hex(const struct rerere_id
*id
)
62 return id
->collection
->name
;
65 static void fit_variant(struct rerere_dir
*rr_dir
, int variant
)
68 ALLOC_GROW(rr_dir
->status
, variant
, rr_dir
->status_alloc
);
69 if (rr_dir
->status_nr
< variant
) {
70 memset(rr_dir
->status
+ rr_dir
->status_nr
,
71 '\0', variant
- rr_dir
->status_nr
);
72 rr_dir
->status_nr
= variant
;
76 static void assign_variant(struct rerere_id
*id
)
79 struct rerere_dir
*rr_dir
= id
->collection
;
81 variant
= id
->variant
;
83 for (variant
= 0; variant
< rr_dir
->status_nr
; variant
++)
84 if (!rr_dir
->status
[variant
])
87 fit_variant(rr_dir
, variant
);
88 id
->variant
= variant
;
91 const char *rerere_path(const struct rerere_id
*id
, const char *file
)
94 return git_path("rr-cache/%s", rerere_id_hex(id
));
97 return git_path("rr-cache/%s/%s", rerere_id_hex(id
), file
);
99 return git_path("rr-cache/%s/%s.%d",
100 rerere_id_hex(id
), file
, id
->variant
);
103 static int is_rr_file(const char *name
, const char *filename
, int *variant
)
108 if (!strcmp(name
, filename
)) {
112 if (!skip_prefix(name
, filename
, &suffix
) || *suffix
!= '.')
116 *variant
= strtol(suffix
+ 1, &ep
, 10);
122 static void scan_rerere_dir(struct rerere_dir
*rr_dir
)
125 DIR *dir
= opendir(git_path("rr-cache/%s", rr_dir
->name
));
129 while ((de
= readdir(dir
)) != NULL
) {
132 if (is_rr_file(de
->d_name
, "postimage", &variant
)) {
133 fit_variant(rr_dir
, variant
);
134 rr_dir
->status
[variant
] |= RR_HAS_POSTIMAGE
;
135 } else if (is_rr_file(de
->d_name
, "preimage", &variant
)) {
136 fit_variant(rr_dir
, variant
);
137 rr_dir
->status
[variant
] |= RR_HAS_PREIMAGE
;
143 static struct rerere_dir
*find_rerere_dir(const char *hex
)
145 struct rerere_dir
*rr_dir
;
147 rr_dir
= strmap_get(&rerere_dirs
, hex
);
149 FLEX_ALLOC_STR(rr_dir
, name
, hex
);
150 rr_dir
->status
= NULL
;
151 rr_dir
->status_nr
= 0;
152 rr_dir
->status_alloc
= 0;
153 strmap_put(&rerere_dirs
, hex
, rr_dir
);
155 scan_rerere_dir(rr_dir
);
160 static int has_rerere_resolution(const struct rerere_id
*id
)
162 const int both
= RR_HAS_POSTIMAGE
|RR_HAS_PREIMAGE
;
163 int variant
= id
->variant
;
167 return ((id
->collection
->status
[variant
] & both
) == both
);
170 static struct rerere_id
*new_rerere_id_hex(char *hex
)
172 struct rerere_id
*id
= xmalloc(sizeof(*id
));
173 id
->collection
= find_rerere_dir(hex
);
174 id
->variant
= -1; /* not known yet */
178 static struct rerere_id
*new_rerere_id(unsigned char *hash
)
180 return new_rerere_id_hex(hash_to_hex(hash
));
184 * $GIT_DIR/MERGE_RR file is a collection of records, each of which is
185 * "conflict ID", a HT and pathname, terminated with a NUL, and is
186 * used to keep track of the set of paths that "rerere" may need to
187 * work on (i.e. what is left by the previous invocation of "git
188 * rerere" during the current conflict resolution session).
190 static void read_rr(struct repository
*r
, struct string_list
*rr
)
192 struct strbuf buf
= STRBUF_INIT
;
193 FILE *in
= fopen_or_warn(git_path_merge_rr(r
), "r");
197 while (!strbuf_getwholeline(&buf
, in
, '\0')) {
199 unsigned char hash
[GIT_MAX_RAWSZ
];
200 struct rerere_id
*id
;
202 const unsigned hexsz
= the_hash_algo
->hexsz
;
204 /* There has to be the hash, tab, path and then NUL */
205 if (buf
.len
< hexsz
+ 2 || get_hash_hex(buf
.buf
, hash
))
206 die(_("corrupt MERGE_RR"));
208 if (buf
.buf
[hexsz
] != '.') {
210 path
= buf
.buf
+ hexsz
;
213 variant
= strtol(buf
.buf
+ hexsz
+ 1, &path
, 10);
215 die(_("corrupt MERGE_RR"));
217 if (*(path
++) != '\t')
218 die(_("corrupt MERGE_RR"));
219 buf
.buf
[hexsz
] = '\0';
220 id
= new_rerere_id_hex(buf
.buf
);
221 id
->variant
= variant
;
223 * make sure id->collection->status has enough space
224 * for the variant we are interested in
226 fit_variant(id
->collection
, variant
);
227 string_list_insert(rr
, path
)->util
= id
;
229 strbuf_release(&buf
);
233 static struct lock_file write_lock
;
235 static int write_rr(struct string_list
*rr
, int out_fd
)
238 for (i
= 0; i
< rr
->nr
; i
++) {
239 struct strbuf buf
= STRBUF_INIT
;
240 struct rerere_id
*id
;
242 assert(rr
->items
[i
].util
!= RERERE_RESOLVED
);
244 id
= rr
->items
[i
].util
;
247 assert(id
->variant
>= 0);
249 strbuf_addf(&buf
, "%s.%d\t%s%c",
250 rerere_id_hex(id
), id
->variant
,
251 rr
->items
[i
].string
, 0);
253 strbuf_addf(&buf
, "%s\t%s%c",
255 rr
->items
[i
].string
, 0);
257 if (write_in_full(out_fd
, buf
.buf
, buf
.len
) < 0)
258 die(_("unable to write rerere record"));
260 strbuf_release(&buf
);
262 if (commit_lock_file(&write_lock
) != 0)
263 die(_("unable to write rerere record"));
268 * "rerere" interacts with conflicted file contents using this I/O
269 * abstraction. It reads a conflicted contents from one place via
270 * "getline()" method, and optionally can write it out after
271 * normalizing the conflicted hunks to the "output". Subclasses of
272 * rerere_io embed this structure at the beginning of their own
276 int (*getline
)(struct strbuf
*, struct rerere_io
*);
279 /* some more stuff */
282 static void ferr_write(const void *p
, size_t count
, FILE *fp
, int *err
)
286 if (fwrite(p
, count
, 1, fp
) != 1)
290 static inline void ferr_puts(const char *s
, FILE *fp
, int *err
)
292 ferr_write(s
, strlen(s
), fp
, err
);
295 static void rerere_io_putstr(const char *str
, struct rerere_io
*io
)
298 ferr_puts(str
, io
->output
, &io
->wrerror
);
301 static void rerere_io_putmem(const char *mem
, size_t sz
, struct rerere_io
*io
)
304 ferr_write(mem
, sz
, io
->output
, &io
->wrerror
);
308 * Subclass of rerere_io that reads from an on-disk file
310 struct rerere_io_file
{
316 * ... and its getline() method implementation
318 static int rerere_file_getline(struct strbuf
*sb
, struct rerere_io
*io_
)
320 struct rerere_io_file
*io
= (struct rerere_io_file
*)io_
;
321 return strbuf_getwholeline(sb
, io
->input
, '\n');
325 * Require the exact number of conflict marker letters, no more, no
326 * less, followed by SP or any whitespace
329 static int is_cmarker(char *buf
, int marker_char
, int marker_size
)
334 * The beginning of our version and the end of their version
335 * always are labeled like "<<<<< ours" or ">>>>> theirs",
336 * hence we set want_sp for them. Note that the version from
337 * the common ancestor in diff3-style output is not always
338 * labelled (e.g. "||||| common" is often seen but "|||||"
339 * alone is also valid), so we do not set want_sp.
341 want_sp
= (marker_char
== '<') || (marker_char
== '>');
343 while (marker_size
--)
344 if (*buf
++ != marker_char
)
346 if (want_sp
&& *buf
!= ' ')
348 return isspace(*buf
);
351 static void rerere_strbuf_putconflict(struct strbuf
*buf
, int ch
, size_t size
)
353 strbuf_addchars(buf
, ch
, size
);
354 strbuf_addch(buf
, '\n');
357 static int handle_conflict(struct strbuf
*out
, struct rerere_io
*io
,
358 int marker_size
, git_hash_ctx
*ctx
)
361 RR_SIDE_1
= 0, RR_SIDE_2
, RR_ORIGINAL
363 struct strbuf one
= STRBUF_INIT
, two
= STRBUF_INIT
;
364 struct strbuf buf
= STRBUF_INIT
, conflict
= STRBUF_INIT
;
365 int has_conflicts
= -1;
367 while (!io
->getline(&buf
, io
)) {
368 if (is_cmarker(buf
.buf
, '<', marker_size
)) {
369 if (handle_conflict(&conflict
, io
, marker_size
, NULL
) < 0)
371 if (hunk
== RR_SIDE_1
)
372 strbuf_addbuf(&one
, &conflict
);
374 strbuf_addbuf(&two
, &conflict
);
375 strbuf_release(&conflict
);
376 } else if (is_cmarker(buf
.buf
, '|', marker_size
)) {
377 if (hunk
!= RR_SIDE_1
)
380 } else if (is_cmarker(buf
.buf
, '=', marker_size
)) {
381 if (hunk
!= RR_SIDE_1
&& hunk
!= RR_ORIGINAL
)
384 } else if (is_cmarker(buf
.buf
, '>', marker_size
)) {
385 if (hunk
!= RR_SIDE_2
)
387 if (strbuf_cmp(&one
, &two
) > 0)
388 strbuf_swap(&one
, &two
);
390 rerere_strbuf_putconflict(out
, '<', marker_size
);
391 strbuf_addbuf(out
, &one
);
392 rerere_strbuf_putconflict(out
, '=', marker_size
);
393 strbuf_addbuf(out
, &two
);
394 rerere_strbuf_putconflict(out
, '>', marker_size
);
396 the_hash_algo
->update_fn(ctx
, one
.buf
?
399 the_hash_algo
->update_fn(ctx
, two
.buf
?
404 } else if (hunk
== RR_SIDE_1
)
405 strbuf_addbuf(&one
, &buf
);
406 else if (hunk
== RR_ORIGINAL
)
408 else if (hunk
== RR_SIDE_2
)
409 strbuf_addbuf(&two
, &buf
);
411 strbuf_release(&one
);
412 strbuf_release(&two
);
413 strbuf_release(&buf
);
415 return has_conflicts
;
419 * Read contents a file with conflicts, normalize the conflicts
420 * by (1) discarding the common ancestor version in diff3-style,
421 * (2) reordering our side and their side so that whichever sorts
422 * alphabetically earlier comes before the other one, while
423 * computing the "conflict ID", which is just an SHA-1 hash of
424 * one side of the conflict, NUL, the other side of the conflict,
425 * and NUL concatenated together.
427 * Return 1 if conflict hunks are found, 0 if there are no conflict
428 * hunks and -1 if an error occurred.
430 static int handle_path(unsigned char *hash
, struct rerere_io
*io
, int marker_size
)
433 struct strbuf buf
= STRBUF_INIT
, out
= STRBUF_INIT
;
434 int has_conflicts
= 0;
436 the_hash_algo
->init_fn(&ctx
);
438 while (!io
->getline(&buf
, io
)) {
439 if (is_cmarker(buf
.buf
, '<', marker_size
)) {
440 has_conflicts
= handle_conflict(&out
, io
, marker_size
,
442 if (has_conflicts
< 0)
444 rerere_io_putmem(out
.buf
, out
.len
, io
);
447 rerere_io_putstr(buf
.buf
, io
);
449 strbuf_release(&buf
);
450 strbuf_release(&out
);
453 the_hash_algo
->final_fn(hash
, &ctx
);
455 return has_conflicts
;
459 * Scan the path for conflicts, do the "handle_path()" thing above, and
460 * return the number of conflict hunks found.
462 static int handle_file(struct index_state
*istate
,
463 const char *path
, unsigned char *hash
, const char *output
)
465 int has_conflicts
= 0;
466 struct rerere_io_file io
;
467 int marker_size
= ll_merge_marker_size(istate
, path
);
469 memset(&io
, 0, sizeof(io
));
470 io
.io
.getline
= rerere_file_getline
;
471 io
.input
= fopen(path
, "r");
474 return error_errno(_("could not open '%s'"), path
);
477 io
.io
.output
= fopen(output
, "w");
479 error_errno(_("could not write '%s'"), output
);
485 has_conflicts
= handle_path(hash
, (struct rerere_io
*)&io
, marker_size
);
489 error(_("there were errors while writing '%s' (%s)"),
490 path
, strerror(io
.io
.wrerror
));
491 if (io
.io
.output
&& fclose(io
.io
.output
))
492 io
.io
.wrerror
= error_errno(_("failed to flush '%s'"), path
);
494 if (has_conflicts
< 0) {
496 unlink_or_warn(output
);
497 return error(_("could not parse conflict hunks in '%s'"), path
);
501 return has_conflicts
;
505 * Look at a cache entry at "i" and see if it is not conflicting,
506 * conflicting and we are willing to handle, or conflicting and
507 * we are unable to handle, and return the determination in *type.
508 * Return the cache index to be looked at next, by skipping the
509 * stages we have already looked at in this invocation of this
512 static int check_one_conflict(struct index_state
*istate
, int i
, int *type
)
514 const struct cache_entry
*e
= istate
->cache
[i
];
522 while (i
< istate
->cache_nr
&& ce_stage(istate
->cache
[i
]) == 1)
525 /* Only handle regular files with both stages #2 and #3 */
526 if (i
+ 1 < istate
->cache_nr
) {
527 const struct cache_entry
*e2
= istate
->cache
[i
];
528 const struct cache_entry
*e3
= istate
->cache
[i
+ 1];
529 if (ce_stage(e2
) == 2 &&
531 ce_same_name(e
, e3
) &&
532 S_ISREG(e2
->ce_mode
) &&
533 S_ISREG(e3
->ce_mode
))
534 *type
= THREE_STAGED
;
537 /* Skip the entries with the same name */
538 while (i
< istate
->cache_nr
&& ce_same_name(e
, istate
->cache
[i
]))
544 * Scan the index and find paths that have conflicts that rerere can
545 * handle, i.e. the ones that has both stages #2 and #3.
547 * NEEDSWORK: we do not record or replay a previous "resolve by
548 * deletion" for a delete-modify conflict, as that is inherently risky
549 * without knowing what modification is being discarded. The only
550 * safe case, i.e. both side doing the deletion and modification that
551 * are identical to the previous round, might want to be handled,
554 static int find_conflict(struct repository
*r
, struct string_list
*conflict
)
558 if (repo_read_index(r
) < 0)
559 return error(_("index file corrupt"));
561 for (i
= 0; i
< r
->index
->cache_nr
;) {
563 const struct cache_entry
*e
= r
->index
->cache
[i
];
564 i
= check_one_conflict(r
->index
, i
, &conflict_type
);
565 if (conflict_type
== THREE_STAGED
)
566 string_list_insert(conflict
, (const char *)e
->name
);
572 * The merge_rr list is meant to hold outstanding conflicted paths
573 * that rerere could handle. Abuse the list by adding other types of
574 * entries to allow the caller to show "rerere remaining".
576 * - Conflicted paths that rerere does not handle are added
577 * - Conflicted paths that have been resolved are marked as such
578 * by storing RERERE_RESOLVED to .util field (where conflict ID
579 * is expected to be stored).
581 * Do *not* write MERGE_RR file out after calling this function.
583 * NEEDSWORK: we may want to fix the caller that implements "rerere
584 * remaining" to do this without abusing merge_rr.
586 int rerere_remaining(struct repository
*r
, struct string_list
*merge_rr
)
590 if (setup_rerere(r
, merge_rr
, RERERE_READONLY
))
592 if (repo_read_index(r
) < 0)
593 return error(_("index file corrupt"));
595 for (i
= 0; i
< r
->index
->cache_nr
;) {
597 const struct cache_entry
*e
= r
->index
->cache
[i
];
598 i
= check_one_conflict(r
->index
, i
, &conflict_type
);
599 if (conflict_type
== PUNTED
)
600 string_list_insert(merge_rr
, (const char *)e
->name
);
601 else if (conflict_type
== RESOLVED
) {
602 struct string_list_item
*it
;
603 it
= string_list_lookup(merge_rr
, (const char *)e
->name
);
606 it
->util
= RERERE_RESOLVED
;
614 * Try using the given conflict resolution "ID" to see
615 * if that recorded conflict resolves cleanly what we
618 static int try_merge(struct index_state
*istate
,
619 const struct rerere_id
*id
, const char *path
,
620 mmfile_t
*cur
, mmbuffer_t
*result
)
622 enum ll_merge_result ret
;
623 mmfile_t base
= {NULL
, 0}, other
= {NULL
, 0};
625 if (read_mmfile(&base
, rerere_path(id
, "preimage")) ||
626 read_mmfile(&other
, rerere_path(id
, "postimage"))) {
627 ret
= LL_MERGE_CONFLICT
;
630 * A three-way merge. Note that this honors user-customizable
631 * low-level merge driver settings.
633 ret
= ll_merge(result
, path
, &base
, NULL
, cur
, "", &other
, "",
644 * Find the conflict identified by "id"; the change between its
645 * "preimage" (i.e. a previous contents with conflict markers) and its
646 * "postimage" (i.e. the corresponding contents with conflicts
647 * resolved) may apply cleanly to the contents stored in "path", i.e.
648 * the conflict this time around.
650 * Returns 0 for successful replay of recorded resolution, or non-zero
653 static int merge(struct index_state
*istate
, const struct rerere_id
*id
, const char *path
)
657 mmfile_t cur
= {NULL
, 0};
658 mmbuffer_t result
= {NULL
, 0};
661 * Normalize the conflicts in path and write it out to
662 * "thisimage" temporary file.
664 if ((handle_file(istate
, path
, NULL
, rerere_path(id
, "thisimage")) < 0) ||
665 read_mmfile(&cur
, rerere_path(id
, "thisimage"))) {
670 ret
= try_merge(istate
, id
, path
, &cur
, &result
);
675 * A successful replay of recorded resolution.
676 * Mark that "postimage" was used to help gc.
678 if (utime(rerere_path(id
, "postimage"), NULL
) < 0)
679 warning_errno(_("failed utime() on '%s'"),
680 rerere_path(id
, "postimage"));
682 /* Update "path" with the resolution */
683 f
= fopen(path
, "w");
685 return error_errno(_("could not open '%s'"), path
);
686 if (fwrite(result
.ptr
, result
.size
, 1, f
) != 1)
687 error_errno(_("could not write '%s'"), path
);
689 return error_errno(_("writing '%s' failed"), path
);
698 static void update_paths(struct repository
*r
, struct string_list
*update
)
700 struct lock_file index_lock
= LOCK_INIT
;
703 repo_hold_locked_index(r
, &index_lock
, LOCK_DIE_ON_ERROR
);
705 for (i
= 0; i
< update
->nr
; i
++) {
706 struct string_list_item
*item
= &update
->items
[i
];
707 if (add_file_to_index(r
->index
, item
->string
, 0))
709 fprintf_ln(stderr
, _("Staged '%s' using previous resolution."),
713 if (write_locked_index(r
->index
, &index_lock
,
714 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
715 die(_("unable to write new index file"));
718 static void remove_variant(struct rerere_id
*id
)
720 unlink_or_warn(rerere_path(id
, "postimage"));
721 unlink_or_warn(rerere_path(id
, "preimage"));
722 id
->collection
->status
[id
->variant
] = 0;
726 * The path indicated by rr_item may still have conflict for which we
727 * have a recorded resolution, in which case replay it and optionally
728 * update it. Or it may have been resolved by the user and we may
729 * only have the preimage for that conflict, in which case the result
730 * needs to be recorded as a resolution in a postimage file.
732 static void do_rerere_one_path(struct index_state
*istate
,
733 struct string_list_item
*rr_item
,
734 struct string_list
*update
)
736 const char *path
= rr_item
->string
;
737 struct rerere_id
*id
= rr_item
->util
;
738 struct rerere_dir
*rr_dir
= id
->collection
;
741 variant
= id
->variant
;
743 /* Has the user resolved it already? */
745 if (!handle_file(istate
, path
, NULL
, NULL
)) {
746 copy_file(rerere_path(id
, "postimage"), path
, 0666);
747 id
->collection
->status
[variant
] |= RR_HAS_POSTIMAGE
;
748 fprintf_ln(stderr
, _("Recorded resolution for '%s'."), path
);
749 free_rerere_id(rr_item
);
750 rr_item
->util
= NULL
;
754 * There may be other variants that can cleanly
755 * replay. Try them and update the variant number for
760 /* Does any existing resolution apply cleanly? */
761 for (variant
= 0; variant
< rr_dir
->status_nr
; variant
++) {
762 const int both
= RR_HAS_PREIMAGE
| RR_HAS_POSTIMAGE
;
763 struct rerere_id vid
= *id
;
765 if ((rr_dir
->status
[variant
] & both
) != both
)
768 vid
.variant
= variant
;
769 if (merge(istate
, &vid
, path
))
770 continue; /* failed to replay */
773 * If there already is a different variant that applies
774 * cleanly, there is no point maintaining our own variant.
776 if (0 <= id
->variant
&& id
->variant
!= variant
)
779 if (rerere_autoupdate
)
780 string_list_insert(update
, path
);
783 _("Resolved '%s' using previous resolution."),
785 free_rerere_id(rr_item
);
786 rr_item
->util
= NULL
;
790 /* None of the existing one applies; we need a new variant */
793 variant
= id
->variant
;
794 handle_file(istate
, path
, NULL
, rerere_path(id
, "preimage"));
795 if (id
->collection
->status
[variant
] & RR_HAS_POSTIMAGE
) {
796 const char *path
= rerere_path(id
, "postimage");
798 die_errno(_("cannot unlink stray '%s'"), path
);
799 id
->collection
->status
[variant
] &= ~RR_HAS_POSTIMAGE
;
801 id
->collection
->status
[variant
] |= RR_HAS_PREIMAGE
;
802 fprintf_ln(stderr
, _("Recorded preimage for '%s'"), path
);
805 static int do_plain_rerere(struct repository
*r
,
806 struct string_list
*rr
, int fd
)
808 struct string_list conflict
= STRING_LIST_INIT_DUP
;
809 struct string_list update
= STRING_LIST_INIT_DUP
;
812 find_conflict(r
, &conflict
);
815 * MERGE_RR records paths with conflicts immediately after
816 * merge failed. Some of the conflicted paths might have been
817 * hand resolved in the working tree since then, but the
818 * initial run would catch all and register their preimages.
820 for (i
= 0; i
< conflict
.nr
; i
++) {
821 struct rerere_id
*id
;
822 unsigned char hash
[GIT_MAX_RAWSZ
];
823 const char *path
= conflict
.items
[i
].string
;
827 * Ask handle_file() to scan and assign a
828 * conflict ID. No need to write anything out
831 ret
= handle_file(r
->index
, path
, hash
, NULL
);
832 if (ret
!= 0 && string_list_has_string(rr
, path
)) {
833 remove_variant(string_list_lookup(rr
, path
)->util
);
834 string_list_remove(rr
, path
, 1);
839 id
= new_rerere_id(hash
);
840 string_list_insert(rr
, path
)->util
= id
;
842 /* Ensure that the directory exists. */
843 mkdir_in_gitdir(rerere_path(id
, NULL
));
846 for (i
= 0; i
< rr
->nr
; i
++)
847 do_rerere_one_path(r
->index
, &rr
->items
[i
], &update
);
850 update_paths(r
, &update
);
852 return write_rr(rr
, fd
);
855 static void git_rerere_config(void)
857 git_config_get_bool("rerere.enabled", &rerere_enabled
);
858 git_config_get_bool("rerere.autoupdate", &rerere_autoupdate
);
859 git_config(git_default_config
, NULL
);
862 static GIT_PATH_FUNC(git_path_rr_cache
, "rr-cache")
864 static int is_rerere_enabled(void)
871 rr_cache_exists
= is_directory(git_path_rr_cache());
872 if (rerere_enabled
< 0)
873 return rr_cache_exists
;
875 if (!rr_cache_exists
&& mkdir_in_gitdir(git_path_rr_cache()))
876 die(_("could not create directory '%s'"), git_path_rr_cache());
880 int setup_rerere(struct repository
*r
, struct string_list
*merge_rr
, int flags
)
885 if (!is_rerere_enabled())
888 if (flags
& (RERERE_AUTOUPDATE
|RERERE_NOAUTOUPDATE
))
889 rerere_autoupdate
= !!(flags
& RERERE_AUTOUPDATE
);
890 if (flags
& RERERE_READONLY
)
893 fd
= hold_lock_file_for_update(&write_lock
,
894 git_path_merge_rr(r
),
896 read_rr(r
, merge_rr
);
901 * The main entry point that is called internally from codepaths that
902 * perform mergy operations, possibly leaving conflicted index entries
903 * and working tree files.
905 int repo_rerere(struct repository
*r
, int flags
)
907 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
910 fd
= setup_rerere(r
, &merge_rr
, flags
);
913 status
= do_plain_rerere(r
, &merge_rr
, fd
);
919 * Subclass of rerere_io that reads from an in-core buffer that is a
922 struct rerere_io_mem
{
928 * ... and its getline() method implementation
930 static int rerere_mem_getline(struct strbuf
*sb
, struct rerere_io
*io_
)
932 struct rerere_io_mem
*io
= (struct rerere_io_mem
*)io_
;
939 ep
= memchr(io
->input
.buf
, '\n', io
->input
.len
);
941 ep
= io
->input
.buf
+ io
->input
.len
;
942 else if (*ep
== '\n')
944 len
= ep
- io
->input
.buf
;
945 strbuf_add(sb
, io
->input
.buf
, len
);
946 strbuf_remove(&io
->input
, 0, len
);
950 static int handle_cache(struct index_state
*istate
,
951 const char *path
, unsigned char *hash
, const char *output
)
953 mmfile_t mmfile
[3] = {{NULL
}};
954 mmbuffer_t result
= {NULL
, 0};
955 const struct cache_entry
*ce
;
956 int pos
, len
, i
, has_conflicts
;
957 struct rerere_io_mem io
;
958 int marker_size
= ll_merge_marker_size(istate
, path
);
961 * Reproduce the conflicted merge in-core
964 pos
= index_name_pos(istate
, path
, len
);
969 while (pos
< istate
->cache_nr
) {
970 enum object_type type
;
973 ce
= istate
->cache
[pos
++];
974 if (ce_namelen(ce
) != len
|| memcmp(ce
->name
, path
, len
))
976 i
= ce_stage(ce
) - 1;
977 if (!mmfile
[i
].ptr
) {
978 mmfile
[i
].ptr
= repo_read_object_file(the_repository
,
982 die(_("unable to read %s"),
983 oid_to_hex(&ce
->oid
));
984 mmfile
[i
].size
= size
;
987 for (i
= 0; i
< 3; i
++)
988 if (!mmfile
[i
].ptr
&& !mmfile
[i
].size
)
989 mmfile
[i
].ptr
= xstrdup("");
992 * NEEDSWORK: handle conflicts from merges with
993 * merge.renormalize set, too?
995 ll_merge(&result
, path
, &mmfile
[0], NULL
,
997 &mmfile
[2], "theirs",
999 for (i
= 0; i
< 3; i
++)
1000 free(mmfile
[i
].ptr
);
1002 memset(&io
, 0, sizeof(io
));
1003 io
.io
.getline
= rerere_mem_getline
;
1005 io
.io
.output
= fopen(output
, "w");
1007 io
.io
.output
= NULL
;
1008 strbuf_init(&io
.input
, 0);
1009 strbuf_attach(&io
.input
, result
.ptr
, result
.size
, result
.size
);
1012 * Grab the conflict ID and optionally write the original
1013 * contents with conflict markers out.
1015 has_conflicts
= handle_path(hash
, (struct rerere_io
*)&io
, marker_size
);
1016 strbuf_release(&io
.input
);
1018 fclose(io
.io
.output
);
1019 return has_conflicts
;
1022 static int rerere_forget_one_path(struct index_state
*istate
,
1024 struct string_list
*rr
)
1026 const char *filename
;
1027 struct rerere_id
*id
;
1028 unsigned char hash
[GIT_MAX_RAWSZ
];
1030 struct string_list_item
*item
;
1033 * Recreate the original conflict from the stages in the
1034 * index and compute the conflict ID
1036 ret
= handle_cache(istate
, path
, hash
, NULL
);
1038 return error(_("could not parse conflict hunks in '%s'"), path
);
1040 /* Nuke the recorded resolution for the conflict */
1041 id
= new_rerere_id(hash
);
1043 for (id
->variant
= 0;
1044 id
->variant
< id
->collection
->status_nr
;
1046 mmfile_t cur
= { NULL
, 0 };
1047 mmbuffer_t result
= {NULL
, 0};
1048 int cleanly_resolved
;
1050 if (!has_rerere_resolution(id
))
1053 handle_cache(istate
, path
, hash
, rerere_path(id
, "thisimage"));
1054 if (read_mmfile(&cur
, rerere_path(id
, "thisimage"))) {
1056 error(_("failed to update conflicted state in '%s'"), path
);
1059 cleanly_resolved
= !try_merge(istate
, id
, path
, &cur
, &result
);
1062 if (cleanly_resolved
)
1066 if (id
->collection
->status_nr
<= id
->variant
) {
1067 error(_("no remembered resolution for '%s'"), path
);
1071 filename
= rerere_path(id
, "postimage");
1072 if (unlink(filename
)) {
1073 if (errno
== ENOENT
)
1074 error(_("no remembered resolution for '%s'"), path
);
1076 error_errno(_("cannot unlink '%s'"), filename
);
1081 * Update the preimage so that the user can resolve the
1082 * conflict in the working tree, run us again to record
1085 handle_cache(istate
, path
, hash
, rerere_path(id
, "preimage"));
1086 fprintf_ln(stderr
, _("Updated preimage for '%s'"), path
);
1089 * And remember that we can record resolution for this
1090 * conflict when the user is done.
1092 item
= string_list_insert(rr
, path
);
1093 free_rerere_id(item
);
1095 fprintf(stderr
, _("Forgot resolution for '%s'\n"), path
);
1103 int rerere_forget(struct repository
*r
, struct pathspec
*pathspec
)
1106 struct string_list conflict
= STRING_LIST_INIT_DUP
;
1107 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
1109 if (repo_read_index(r
) < 0)
1110 return error(_("index file corrupt"));
1112 fd
= setup_rerere(r
, &merge_rr
, RERERE_NOAUTOUPDATE
);
1117 * The paths may have been resolved (incorrectly);
1118 * recover the original conflicted state and then
1119 * find the conflicted paths.
1121 unmerge_index(r
->index
, pathspec
, 0);
1122 find_conflict(r
, &conflict
);
1123 for (i
= 0; i
< conflict
.nr
; i
++) {
1124 struct string_list_item
*it
= &conflict
.items
[i
];
1125 if (!match_pathspec(r
->index
, pathspec
, it
->string
,
1126 strlen(it
->string
), 0, NULL
, 0))
1128 rerere_forget_one_path(r
->index
, it
->string
, &merge_rr
);
1130 return write_rr(&merge_rr
, fd
);
1134 * Garbage collection support
1137 static timestamp_t
rerere_created_at(struct rerere_id
*id
)
1141 return stat(rerere_path(id
, "preimage"), &st
) ? (time_t) 0 : st
.st_mtime
;
1144 static timestamp_t
rerere_last_used_at(struct rerere_id
*id
)
1148 return stat(rerere_path(id
, "postimage"), &st
) ? (time_t) 0 : st
.st_mtime
;
1152 * Remove the recorded resolution for a given conflict ID
1154 static void unlink_rr_item(struct rerere_id
*id
)
1156 unlink_or_warn(rerere_path(id
, "thisimage"));
1158 id
->collection
->status
[id
->variant
] = 0;
1161 static void prune_one(struct rerere_id
*id
,
1162 timestamp_t cutoff_resolve
, timestamp_t cutoff_noresolve
)
1167 then
= rerere_last_used_at(id
);
1169 cutoff
= cutoff_resolve
;
1171 then
= rerere_created_at(id
);
1174 cutoff
= cutoff_noresolve
;
1180 /* Does the basename in "path" look plausibly like an rr-cache entry? */
1181 static int is_rr_cache_dirname(const char *path
)
1183 struct object_id oid
;
1185 return !parse_oid_hex(path
, &oid
, &end
) && !*end
;
1188 void rerere_gc(struct repository
*r
, struct string_list
*rr
)
1190 struct string_list to_remove
= STRING_LIST_INIT_DUP
;
1194 timestamp_t now
= time(NULL
);
1195 timestamp_t cutoff_noresolve
= now
- 15 * 86400;
1196 timestamp_t cutoff_resolve
= now
- 60 * 86400;
1198 if (setup_rerere(r
, rr
, 0) < 0)
1201 git_config_get_expiry_in_days("gc.rerereresolved", &cutoff_resolve
, now
);
1202 git_config_get_expiry_in_days("gc.rerereunresolved", &cutoff_noresolve
, now
);
1203 git_config(git_default_config
, NULL
);
1204 dir
= opendir(git_path("rr-cache"));
1206 die_errno(_("unable to open rr-cache directory"));
1207 /* Collect stale conflict IDs ... */
1208 while ((e
= readdir_skip_dot_and_dotdot(dir
))) {
1209 struct rerere_dir
*rr_dir
;
1210 struct rerere_id id
;
1213 if (!is_rr_cache_dirname(e
->d_name
))
1214 continue; /* or should we remove e->d_name? */
1216 rr_dir
= find_rerere_dir(e
->d_name
);
1219 for (id
.variant
= 0, id
.collection
= rr_dir
;
1220 id
.variant
< id
.collection
->status_nr
;
1222 prune_one(&id
, cutoff_resolve
, cutoff_noresolve
);
1223 if (id
.collection
->status
[id
.variant
])
1227 string_list_append(&to_remove
, e
->d_name
);
1231 /* ... and then remove the empty directories */
1232 for (i
= 0; i
< to_remove
.nr
; i
++)
1233 rmdir(git_path("rr-cache/%s", to_remove
.items
[i
].string
));
1234 string_list_clear(&to_remove
, 0);
1235 rollback_lock_file(&write_lock
);
1239 * During a conflict resolution, after "rerere" recorded the
1240 * preimages, abandon them if the user did not resolve them or
1241 * record their resolutions. And drop $GIT_DIR/MERGE_RR.
1243 * NEEDSWORK: shouldn't we be calling this from "reset --hard"?
1245 void rerere_clear(struct repository
*r
, struct string_list
*merge_rr
)
1249 if (setup_rerere(r
, merge_rr
, 0) < 0)
1252 for (i
= 0; i
< merge_rr
->nr
; i
++) {
1253 struct rerere_id
*id
= merge_rr
->items
[i
].util
;
1254 if (!has_rerere_resolution(id
)) {
1256 rmdir(rerere_path(id
, NULL
));
1259 unlink_or_warn(git_path_merge_rr(r
));
1260 rollback_lock_file(&write_lock
);