]>
git.ipfire.org Git - thirdparty/git.git/blob - rerere.c
3 #include "string-list.h"
5 #include "xdiff-interface.h"
7 #include "resolve-undo.h"
14 #define THREE_STAGED 2
15 void *RERERE_RESOLVED
= &RERERE_RESOLVED
;
17 /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
18 static int rerere_enabled
= -1;
20 /* automatically update cleanly resolved paths to the index */
21 static int rerere_autoupdate
;
23 static char *merge_rr_path
;
25 const char *rerere_path(const char *hex
, const char *file
)
27 return git_path("rr-cache/%s/%s", hex
, file
);
30 static int has_rerere_resolution(const char *hex
)
33 return !stat(rerere_path(hex
, "postimage"), &st
);
37 * $GIT_DIR/MERGE_RR file is a collection of records, each of which is
38 * "conflict ID", a HT and pathname, terminated with a NUL, and is
39 * used to keep track of the set of paths that "rerere" may need to
40 * work on (i.e. what is left by the previous invocation of "git
41 * rerere" during the current conflict resolution session).
43 static void read_rr(struct string_list
*rr
)
45 struct strbuf buf
= STRBUF_INIT
;
46 FILE *in
= fopen(merge_rr_path
, "r");
50 while (!strbuf_getwholeline(&buf
, in
, '\0')) {
52 unsigned char sha1
[20];
54 /* There has to be the hash, tab, path and then NUL */
55 if (buf
.len
< 42 || get_sha1_hex(buf
.buf
, sha1
))
56 die("corrupt MERGE_RR");
58 if (buf
.buf
[40] != '\t')
59 die("corrupt MERGE_RR");
63 string_list_insert(rr
, path
)->util
= xstrdup(buf
.buf
);
69 static struct lock_file write_lock
;
71 static int write_rr(struct string_list
*rr
, int out_fd
)
74 for (i
= 0; i
< rr
->nr
; i
++) {
75 struct strbuf buf
= STRBUF_INIT
;
77 assert(rr
->items
[i
].util
!= RERERE_RESOLVED
);
78 if (!rr
->items
[i
].util
)
80 strbuf_addf(&buf
, "%s\t%s%c",
81 (char *)rr
->items
[i
].util
,
82 rr
->items
[i
].string
, 0);
83 if (write_in_full(out_fd
, buf
.buf
, buf
.len
) != buf
.len
)
84 die("unable to write rerere record");
88 if (commit_lock_file(&write_lock
) != 0)
89 die("unable to write rerere record");
94 * "rerere" interacts with conflicted file contents using this I/O
95 * abstraction. It reads a conflicted contents from one place via
96 * "getline()" method, and optionally can write it out after
97 * normalizing the conflicted hunks to the "output". Subclasses of
98 * rerere_io embed this structure at the beginning of their own
102 int (*getline
)(struct strbuf
*, struct rerere_io
*);
105 /* some more stuff */
108 static void ferr_write(const void *p
, size_t count
, FILE *fp
, int *err
)
112 if (fwrite(p
, count
, 1, fp
) != 1)
116 static inline void ferr_puts(const char *s
, FILE *fp
, int *err
)
118 ferr_write(s
, strlen(s
), fp
, err
);
121 static void rerere_io_putstr(const char *str
, struct rerere_io
*io
)
124 ferr_puts(str
, io
->output
, &io
->wrerror
);
128 * Write a conflict marker to io->output (if defined).
130 static void rerere_io_putconflict(int ch
, int size
, struct rerere_io
*io
)
135 if (size
<= sizeof(buf
) - 2) {
136 memset(buf
, ch
, size
);
138 buf
[size
+ 1] = '\0';
141 int sz
= sizeof(buf
) - 1;
144 * Make sure we will not write everything out
145 * in this round by leaving at least 1 byte
146 * for the next round, giving the next round
147 * a chance to add the terminating LF. Yuck.
150 sz
-= (sz
- size
) + 1;
155 rerere_io_putstr(buf
, io
);
159 static void rerere_io_putmem(const char *mem
, size_t sz
, struct rerere_io
*io
)
162 ferr_write(mem
, sz
, io
->output
, &io
->wrerror
);
166 * Subclass of rerere_io that reads from an on-disk file
168 struct rerere_io_file
{
174 * ... and its getline() method implementation
176 static int rerere_file_getline(struct strbuf
*sb
, struct rerere_io
*io_
)
178 struct rerere_io_file
*io
= (struct rerere_io_file
*)io_
;
179 return strbuf_getwholeline(sb
, io
->input
, '\n');
183 * Require the exact number of conflict marker letters, no more, no
184 * less, followed by SP or any whitespace
187 static int is_cmarker(char *buf
, int marker_char
, int marker_size
)
192 * The beginning of our version and the end of their version
193 * always are labeled like "<<<<< ours" or ">>>>> theirs",
194 * hence we set want_sp for them. Note that the version from
195 * the common ancestor in diff3-style output is not always
196 * labelled (e.g. "||||| common" is often seen but "|||||"
197 * alone is also valid), so we do not set want_sp.
199 want_sp
= (marker_char
== '<') || (marker_char
== '>');
201 while (marker_size
--)
202 if (*buf
++ != marker_char
)
204 if (want_sp
&& *buf
!= ' ')
206 return isspace(*buf
);
209 static int handle_path(unsigned char *sha1
, struct rerere_io
*io
, int marker_size
)
214 RR_CONTEXT
= 0, RR_SIDE_1
, RR_SIDE_2
, RR_ORIGINAL
216 struct strbuf one
= STRBUF_INIT
, two
= STRBUF_INIT
;
217 struct strbuf buf
= STRBUF_INIT
;
222 while (!io
->getline(&buf
, io
)) {
223 if (is_cmarker(buf
.buf
, '<', marker_size
)) {
224 if (hunk
!= RR_CONTEXT
)
227 } else if (is_cmarker(buf
.buf
, '|', marker_size
)) {
228 if (hunk
!= RR_SIDE_1
)
231 } else if (is_cmarker(buf
.buf
, '=', marker_size
)) {
232 if (hunk
!= RR_SIDE_1
&& hunk
!= RR_ORIGINAL
)
235 } else if (is_cmarker(buf
.buf
, '>', marker_size
)) {
236 if (hunk
!= RR_SIDE_2
)
238 if (strbuf_cmp(&one
, &two
) > 0)
239 strbuf_swap(&one
, &two
);
242 rerere_io_putconflict('<', marker_size
, io
);
243 rerere_io_putmem(one
.buf
, one
.len
, io
);
244 rerere_io_putconflict('=', marker_size
, io
);
245 rerere_io_putmem(two
.buf
, two
.len
, io
);
246 rerere_io_putconflict('>', marker_size
, io
);
248 git_SHA1_Update(&ctx
, one
.buf
? one
.buf
: "",
250 git_SHA1_Update(&ctx
, two
.buf
? two
.buf
: "",
255 } else if (hunk
== RR_SIDE_1
)
256 strbuf_addbuf(&one
, &buf
);
257 else if (hunk
== RR_ORIGINAL
)
259 else if (hunk
== RR_SIDE_2
)
260 strbuf_addbuf(&two
, &buf
);
262 rerere_io_putstr(buf
.buf
, io
);
265 hunk
= 99; /* force error exit */
268 strbuf_release(&one
);
269 strbuf_release(&two
);
270 strbuf_release(&buf
);
273 git_SHA1_Final(sha1
, &ctx
);
274 if (hunk
!= RR_CONTEXT
)
279 static int handle_file(const char *path
, unsigned char *sha1
, const char *output
)
282 struct rerere_io_file io
;
283 int marker_size
= ll_merge_marker_size(path
);
285 memset(&io
, 0, sizeof(io
));
286 io
.io
.getline
= rerere_file_getline
;
287 io
.input
= fopen(path
, "r");
290 return error("Could not open %s", path
);
293 io
.io
.output
= fopen(output
, "w");
296 return error("Could not write %s", output
);
300 hunk_no
= handle_path(sha1
, (struct rerere_io
*)&io
, marker_size
);
304 error("There were errors while writing %s (%s)",
305 path
, strerror(io
.io
.wrerror
));
306 if (io
.io
.output
&& fclose(io
.io
.output
))
307 io
.io
.wrerror
= error("Failed to flush %s: %s",
308 path
, strerror(errno
));
312 unlink_or_warn(output
);
313 return error("Could not parse conflict hunks in %s", path
);
321 * Subclass of rerere_io that reads from an in-core buffer that is a
324 struct rerere_io_mem
{
330 * ... and its getline() method implementation
332 static int rerere_mem_getline(struct strbuf
*sb
, struct rerere_io
*io_
)
334 struct rerere_io_mem
*io
= (struct rerere_io_mem
*)io_
;
341 ep
= memchr(io
->input
.buf
, '\n', io
->input
.len
);
343 ep
= io
->input
.buf
+ io
->input
.len
;
344 else if (*ep
== '\n')
346 len
= ep
- io
->input
.buf
;
347 strbuf_add(sb
, io
->input
.buf
, len
);
348 strbuf_remove(&io
->input
, 0, len
);
352 static int handle_cache(const char *path
, unsigned char *sha1
, const char *output
)
354 mmfile_t mmfile
[3] = {{NULL
}};
355 mmbuffer_t result
= {NULL
, 0};
356 const struct cache_entry
*ce
;
357 int pos
, len
, i
, hunk_no
;
358 struct rerere_io_mem io
;
359 int marker_size
= ll_merge_marker_size(path
);
362 * Reproduce the conflicted merge in-core
365 pos
= cache_name_pos(path
, len
);
370 while (pos
< active_nr
) {
371 enum object_type type
;
374 ce
= active_cache
[pos
++];
375 if (ce_namelen(ce
) != len
|| memcmp(ce
->name
, path
, len
))
377 i
= ce_stage(ce
) - 1;
378 if (!mmfile
[i
].ptr
) {
379 mmfile
[i
].ptr
= read_sha1_file(ce
->sha1
, &type
, &size
);
380 mmfile
[i
].size
= size
;
383 for (i
= 0; i
< 3; i
++)
384 if (!mmfile
[i
].ptr
&& !mmfile
[i
].size
)
385 mmfile
[i
].ptr
= xstrdup("");
388 * NEEDSWORK: handle conflicts from merges with
389 * merge.renormalize set, too
391 ll_merge(&result
, path
, &mmfile
[0], NULL
,
393 &mmfile
[2], "theirs", NULL
);
394 for (i
= 0; i
< 3; i
++)
397 memset(&io
, 0, sizeof(io
));
398 io
.io
.getline
= rerere_mem_getline
;
400 io
.io
.output
= fopen(output
, "w");
403 strbuf_init(&io
.input
, 0);
404 strbuf_attach(&io
.input
, result
.ptr
, result
.size
, result
.size
);
406 hunk_no
= handle_path(sha1
, (struct rerere_io
*)&io
, marker_size
);
407 strbuf_release(&io
.input
);
409 fclose(io
.io
.output
);
414 * Look at a cache entry at "i" and see if it is not conflicting,
415 * conflicting and we are willing to handle, or conflicting and
416 * we are unable to handle, and return the determination in *type.
417 * Return the cache index to be looked at next, by skipping the
418 * stages we have already looked at in this invocation of this
421 static int check_one_conflict(int i
, int *type
)
423 const struct cache_entry
*e
= active_cache
[i
];
431 while (ce_stage(active_cache
[i
]) == 1)
434 /* Only handle regular files with both stages #2 and #3 */
435 if (i
+ 1 < active_nr
) {
436 const struct cache_entry
*e2
= active_cache
[i
];
437 const struct cache_entry
*e3
= active_cache
[i
+ 1];
438 if (ce_stage(e2
) == 2 &&
440 ce_same_name(e
, e3
) &&
441 S_ISREG(e2
->ce_mode
) &&
442 S_ISREG(e3
->ce_mode
))
443 *type
= THREE_STAGED
;
446 /* Skip the entries with the same name */
447 while (i
< active_nr
&& ce_same_name(e
, active_cache
[i
]))
453 * Scan the index and find paths that have conflicts that rerere can
454 * handle, i.e. the ones that has both stages #2 and #3.
456 * NEEDSWORK: we do not record or replay a previous "resolve by
457 * deletion" for a delete-modify conflict, as that is inherently risky
458 * without knowing what modification is being discarded. The only
459 * safe case, i.e. both side doing the deletion and modification that
460 * are identical to the previous round, might want to be handled,
463 static int find_conflict(struct string_list
*conflict
)
466 if (read_cache() < 0)
467 return error("Could not read index");
469 for (i
= 0; i
< active_nr
;) {
471 const struct cache_entry
*e
= active_cache
[i
];
472 i
= check_one_conflict(i
, &conflict_type
);
473 if (conflict_type
== THREE_STAGED
)
474 string_list_insert(conflict
, (const char *)e
->name
);
480 * The merge_rr list is meant to hold outstanding conflicted paths
481 * that rerere could handle. Abuse the list by adding other types of
482 * entries to allow the caller to show "rerere remaining".
484 * - Conflicted paths that rerere does not handle are added
485 * - Conflicted paths that have been resolved are marked as such
486 * by storing RERERE_RESOLVED to .util field (where conflict ID
487 * is expected to be stored).
489 * Do *not* write MERGE_RR file out after calling this function.
491 * NEEDSWORK: we may want to fix the caller that implements "rerere
492 * remaining" to do this without abusing merge_rr.
494 int rerere_remaining(struct string_list
*merge_rr
)
497 if (read_cache() < 0)
498 return error("Could not read index");
500 for (i
= 0; i
< active_nr
;) {
502 const struct cache_entry
*e
= active_cache
[i
];
503 i
= check_one_conflict(i
, &conflict_type
);
504 if (conflict_type
== PUNTED
)
505 string_list_insert(merge_rr
, (const char *)e
->name
);
506 else if (conflict_type
== RESOLVED
) {
507 struct string_list_item
*it
;
508 it
= string_list_lookup(merge_rr
, (const char *)e
->name
);
511 it
->util
= RERERE_RESOLVED
;
518 static int merge(const char *name
, const char *path
)
521 mmfile_t cur
= {NULL
, 0}, base
= {NULL
, 0}, other
= {NULL
, 0};
522 mmbuffer_t result
= {NULL
, 0};
524 if (handle_file(path
, NULL
, rerere_path(name
, "thisimage")) < 0)
527 if (read_mmfile(&cur
, rerere_path(name
, "thisimage")) ||
528 read_mmfile(&base
, rerere_path(name
, "preimage")) ||
529 read_mmfile(&other
, rerere_path(name
, "postimage"))) {
533 ret
= ll_merge(&result
, path
, &base
, NULL
, &cur
, "", &other
, "", NULL
);
537 if (utime(rerere_path(name
, "postimage"), NULL
) < 0)
538 warning("failed utime() on %s: %s",
539 rerere_path(name
, "postimage"),
541 f
= fopen(path
, "w");
543 return error("Could not open %s: %s", path
,
545 if (fwrite(result
.ptr
, result
.size
, 1, f
) != 1)
546 error("Could not write %s: %s", path
, strerror(errno
));
548 return error("Writing %s failed: %s", path
,
561 static struct lock_file index_lock
;
563 static void update_paths(struct string_list
*update
)
567 hold_locked_index(&index_lock
, 1);
569 for (i
= 0; i
< update
->nr
; i
++) {
570 struct string_list_item
*item
= &update
->items
[i
];
571 if (add_file_to_cache(item
->string
, 0))
573 fprintf(stderr
, "Staged '%s' using previous resolution.\n",
577 if (active_cache_changed
) {
578 if (write_locked_index(&the_index
, &index_lock
, COMMIT_LOCK
))
579 die("Unable to write new index file");
581 rollback_lock_file(&index_lock
);
584 static int do_plain_rerere(struct string_list
*rr
, int fd
)
586 struct string_list conflict
= STRING_LIST_INIT_DUP
;
587 struct string_list update
= STRING_LIST_INIT_DUP
;
590 find_conflict(&conflict
);
593 * MERGE_RR records paths with conflicts immediately after merge
594 * failed. Some of the conflicted paths might have been hand resolved
595 * in the working tree since then, but the initial run would catch all
596 * and register their preimages.
599 for (i
= 0; i
< conflict
.nr
; i
++) {
600 const char *path
= conflict
.items
[i
].string
;
601 if (!string_list_has_string(rr
, path
)) {
602 unsigned char sha1
[20];
605 ret
= handle_file(path
, sha1
, NULL
);
608 hex
= xstrdup(sha1_to_hex(sha1
));
609 string_list_insert(rr
, path
)->util
= hex
;
610 if (mkdir_in_gitdir(git_path("rr-cache/%s", hex
)))
612 handle_file(path
, NULL
, rerere_path(hex
, "preimage"));
613 fprintf(stderr
, "Recorded preimage for '%s'\n", path
);
618 * Now some of the paths that had conflicts earlier might have been
619 * hand resolved. Others may be similar to a conflict already that
620 * was resolved before.
623 for (i
= 0; i
< rr
->nr
; i
++) {
625 const char *path
= rr
->items
[i
].string
;
626 const char *name
= (const char *)rr
->items
[i
].util
;
628 if (has_rerere_resolution(name
)) {
629 if (merge(name
, path
))
632 if (rerere_autoupdate
)
633 string_list_insert(&update
, path
);
636 "Resolved '%s' using previous resolution.\n",
641 /* Let's see if we have resolved it. */
642 ret
= handle_file(path
, NULL
, NULL
);
646 fprintf(stderr
, "Recorded resolution for '%s'.\n", path
);
647 copy_file(rerere_path(name
, "postimage"), path
, 0666);
649 free(rr
->items
[i
].util
);
650 rr
->items
[i
].util
= NULL
;
654 update_paths(&update
);
656 return write_rr(rr
, fd
);
659 static void git_rerere_config(void)
661 git_config_get_bool("rerere.enabled", &rerere_enabled
);
662 git_config_get_bool("rerere.autoupdate", &rerere_autoupdate
);
663 git_config(git_default_config
, NULL
);
666 static int is_rerere_enabled(void)
668 const char *rr_cache
;
674 rr_cache
= git_path("rr-cache");
675 rr_cache_exists
= is_directory(rr_cache
);
676 if (rerere_enabled
< 0)
677 return rr_cache_exists
;
679 if (!rr_cache_exists
&& mkdir_in_gitdir(rr_cache
))
680 die("Could not create directory %s", rr_cache
);
684 int setup_rerere(struct string_list
*merge_rr
, int flags
)
689 if (!is_rerere_enabled())
692 if (flags
& (RERERE_AUTOUPDATE
|RERERE_NOAUTOUPDATE
))
693 rerere_autoupdate
= !!(flags
& RERERE_AUTOUPDATE
);
694 merge_rr_path
= git_pathdup("MERGE_RR");
695 fd
= hold_lock_file_for_update(&write_lock
, merge_rr_path
,
701 int rerere(int flags
)
703 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
706 fd
= setup_rerere(&merge_rr
, flags
);
709 return do_plain_rerere(&merge_rr
, fd
);
712 static int rerere_forget_one_path(const char *path
, struct string_list
*rr
)
714 const char *filename
;
716 unsigned char sha1
[20];
718 struct string_list_item
*item
;
720 ret
= handle_cache(path
, sha1
, NULL
);
722 return error("Could not parse conflict hunks in '%s'", path
);
723 hex
= xstrdup(sha1_to_hex(sha1
));
724 filename
= rerere_path(hex
, "postimage");
725 if (unlink(filename
))
726 return (errno
== ENOENT
727 ? error("no remembered resolution for %s", path
)
728 : error("cannot unlink %s: %s", filename
, strerror(errno
)));
730 handle_cache(path
, sha1
, rerere_path(hex
, "preimage"));
731 fprintf(stderr
, "Updated preimage for '%s'\n", path
);
733 item
= string_list_insert(rr
, path
);
736 fprintf(stderr
, "Forgot resolution for %s\n", path
);
740 int rerere_forget(struct pathspec
*pathspec
)
743 struct string_list conflict
= STRING_LIST_INIT_DUP
;
744 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
746 if (read_cache() < 0)
747 return error("Could not read index");
749 fd
= setup_rerere(&merge_rr
, RERERE_NOAUTOUPDATE
);
751 unmerge_cache(pathspec
);
752 find_conflict(&conflict
);
753 for (i
= 0; i
< conflict
.nr
; i
++) {
754 struct string_list_item
*it
= &conflict
.items
[i
];
755 if (!match_pathspec(pathspec
, it
->string
,
756 strlen(it
->string
), 0, NULL
, 0))
758 rerere_forget_one_path(it
->string
, &merge_rr
);
760 return write_rr(&merge_rr
, fd
);
763 static time_t rerere_created_at(const char *name
)
766 return stat(rerere_path(name
, "preimage"), &st
) ? (time_t) 0 : st
.st_mtime
;
769 static time_t rerere_last_used_at(const char *name
)
772 return stat(rerere_path(name
, "postimage"), &st
) ? (time_t) 0 : st
.st_mtime
;
775 static void unlink_rr_item(const char *name
)
777 unlink(rerere_path(name
, "thisimage"));
778 unlink(rerere_path(name
, "preimage"));
779 unlink(rerere_path(name
, "postimage"));
780 rmdir(git_path("rr-cache/%s", name
));
783 void rerere_gc(struct string_list
*rr
)
785 struct string_list to_remove
= STRING_LIST_INIT_DUP
;
789 time_t now
= time(NULL
), then
;
790 int cutoff_noresolve
= 15;
791 int cutoff_resolve
= 60;
793 git_config_get_int("gc.rerereresolved", &cutoff_resolve
);
794 git_config_get_int("gc.rerereunresolved", &cutoff_noresolve
);
795 git_config(git_default_config
, NULL
);
796 dir
= opendir(git_path("rr-cache"));
798 die_errno("unable to open rr-cache directory");
799 while ((e
= readdir(dir
))) {
800 if (is_dot_or_dotdot(e
->d_name
))
803 then
= rerere_last_used_at(e
->d_name
);
805 cutoff
= cutoff_resolve
;
807 then
= rerere_created_at(e
->d_name
);
810 cutoff
= cutoff_noresolve
;
812 if (then
< now
- cutoff
* 86400)
813 string_list_append(&to_remove
, e
->d_name
);
816 for (i
= 0; i
< to_remove
.nr
; i
++)
817 unlink_rr_item(to_remove
.items
[i
].string
);
818 string_list_clear(&to_remove
, 0);
821 void rerere_clear(struct string_list
*merge_rr
)
825 for (i
= 0; i
< merge_rr
->nr
; i
++) {
826 const char *name
= (const char *)merge_rr
->items
[i
].util
;
827 if (!has_rerere_resolution(name
))
828 unlink_rr_item(name
);
830 unlink_or_warn(git_path("MERGE_RR"));