Skip to content

Commit cc899ec

Browse files
committed
rerere: explain the primary codepath
Explain the internals of rerere as in-code comments, while sprinkling "NEEDSWORK" comment to highlight iffy bits and questionable assumptions. This one covers the codepath reached from rerere(), the primary interface to the subsystem. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4b68c2a commit cc899ec

File tree

1 file changed

+82
-13
lines changed

1 file changed

+82
-13
lines changed

rerere.c

Lines changed: 82 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,21 @@ static int is_cmarker(char *buf, int marker_char, int marker_size)
206206
return isspace(*buf);
207207
}
208208

209+
/*
210+
* Read contents a file with conflicts, normalize the conflicts
211+
* by (1) discarding the common ancestor version in diff3-style,
212+
* (2) reordering our side and their side so that whichever sorts
213+
* alphabetically earlier comes before the other one, while
214+
* computing the "conflict ID", which is just an SHA-1 hash of
215+
* one side of the conflict, NUL, the other side of the conflict,
216+
* and NUL concatenated together.
217+
*
218+
* Return the number of conflict hunks found.
219+
*
220+
* NEEDSWORK: the logic and theory of operation behind this conflict
221+
* normalization may deserve to be documented somewhere, perhaps in
222+
* Documentation/technical/rerere.txt.
223+
*/
209224
static int handle_path(unsigned char *sha1, struct rerere_io *io, int marker_size)
210225
{
211226
git_SHA_CTX ctx;
@@ -276,6 +291,10 @@ static int handle_path(unsigned char *sha1, struct rerere_io *io, int marker_siz
276291
return hunk_no;
277292
}
278293

294+
/*
295+
* Scan the path for conflicts, do the "handle_path()" thing above, and
296+
* return the number of conflict hunks found.
297+
*/
279298
static int handle_file(const char *path, unsigned char *sha1, const char *output)
280299
{
281300
int hunk_no = 0;
@@ -515,29 +534,54 @@ int rerere_remaining(struct string_list *merge_rr)
515534
return 0;
516535
}
517536

537+
/*
538+
* Find the conflict identified by "name"; the change between its
539+
* "preimage" (i.e. a previous contents with conflict markers) and its
540+
* "postimage" (i.e. the corresponding contents with conflicts
541+
* resolved) may apply cleanly to the contents stored in "path", i.e.
542+
* the conflict this time around.
543+
*
544+
* Returns 0 for successful replay of recorded resolution, or non-zero
545+
* for failure.
546+
*/
518547
static int merge(const char *name, const char *path)
519548
{
520549
int ret;
521550
mmfile_t cur = {NULL, 0}, base = {NULL, 0}, other = {NULL, 0};
522551
mmbuffer_t result = {NULL, 0};
523552

553+
/*
554+
* Normalize the conflicts in path and write it out to
555+
* "thisimage" temporary file.
556+
*/
524557
if (handle_file(path, NULL, rerere_path(name, "thisimage")) < 0)
525558
return 1;
526559

527560
if (read_mmfile(&cur, rerere_path(name, "thisimage")) ||
528-
read_mmfile(&base, rerere_path(name, "preimage")) ||
529-
read_mmfile(&other, rerere_path(name, "postimage"))) {
561+
read_mmfile(&base, rerere_path(name, "preimage")) ||
562+
read_mmfile(&other, rerere_path(name, "postimage"))) {
530563
ret = 1;
531564
goto out;
532565
}
566+
567+
/*
568+
* A three-way merge. Note that this honors user-customizable
569+
* low-level merge driver settings.
570+
*/
533571
ret = ll_merge(&result, path, &base, NULL, &cur, "", &other, "", NULL);
534572
if (!ret) {
535573
FILE *f;
536574

575+
/*
576+
* A successful replay of recorded resolution.
577+
* Mark that "postimage" was used to help gc.
578+
*/
537579
if (utime(rerere_path(name, "postimage"), NULL) < 0)
538580
warning("failed utime() on %s: %s",
539581
rerere_path(name, "postimage"),
540582
strerror(errno));
583+
584+
/* Update "path" with the resolution */
541585
f = fopen(path, "w");
542586
if (!f)
543587
return error("Could not open %s: %s", path,
@@ -590,41 +634,61 @@ static int do_plain_rerere(struct string_list *rr, int fd)
590634
find_conflict(&conflict);
591635

592636
/*
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.
637+
* MERGE_RR records paths with conflicts immediately after
638+
* merge failed. Some of the conflicted paths might have been
639+
* hand resolved in the working tree since then, but the
640+
* initial run would catch all and register their preimages.
597641
*/
598-
599642
for (i = 0; i < conflict.nr; i++) {
600643
const char *path = conflict.items[i].string;
601644
if (!string_list_has_string(rr, path)) {
602645
unsigned char sha1[20];
603646
char *hex;
604647
int ret;
648+
649+
/*
650+
* Ask handle_file() to scan and assign a
651+
* conflict ID. No need to write anything out
652+
* yet.
653+
*/
605654
ret = handle_file(path, sha1, NULL);
606655
if (ret < 1)
607656
continue;
608657
hex = xstrdup(sha1_to_hex(sha1));
609658
string_list_insert(rr, path)->util = hex;
659+
660+
/*
661+
* If the directory does not exist, create
662+
* it. mkdir_in_gitdir() will fail with
663+
* EEXIST if there already is one.
664+
*
665+
* NEEDSWORK: make sure "gc" does not remove
666+
* preimage without removing the directory.
667+
*/
610668
if (mkdir_in_gitdir(git_path("rr-cache/%s", hex)))
611669
continue;
670+
671+
/*
672+
* We are the first to encounter this
673+
* conflict. Ask handle_file() to write the
674+
* normalized contents to the "preimage" file.
675+
*/
612676
handle_file(path, NULL, rerere_path(hex, "preimage"));
613677
fprintf(stderr, "Recorded preimage for '%s'\n", path);
614678
}
615679
}
616680

617681
/*
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.
682+
* Some of the paths that had conflicts earlier might have
683+
* been resolved by the user. Others may be similar to a
684+
* conflict already that was resolved before.
621685
*/
622-
623686
for (i = 0; i < rr->nr; i++) {
624687
int ret;
625688
const char *path = rr->items[i].string;
626689
const char *name = (const char *)rr->items[i].util;
627690

691+
/* Is there a recorded resolution we could attempt to apply? */
628692
if (has_rerere_resolution(name)) {
629693
if (merge(name, path))
630694
continue;
@@ -638,13 +702,13 @@ static int do_plain_rerere(struct string_list *rr, int fd)
638702
goto mark_resolved;
639703
}
640704

641-
/* Let's see if we have resolved it. */
705+
/* Let's see if the user has resolved it. */
642706
ret = handle_file(path, NULL, NULL);
643707
if (ret)
644708
continue;
645709

646-
fprintf(stderr, "Recorded resolution for '%s'.\n", path);
647710
copy_file(rerere_path(name, "postimage"), path, 0666);
711+
fprintf(stderr, "Recorded resolution for '%s'.\n", path);
648712
mark_resolved:
649713
free(rr->items[i].util);
650714
rr->items[i].util = NULL;
@@ -698,6 +762,11 @@ int setup_rerere(struct string_list *merge_rr, int flags)
698762
return fd;
699763
}
700764

765+
/*
766+
* The main entry point that is called internally from codepaths that
767+
* perform mergy operations, possibly leaving conflicted index entries
768+
* and working tree files.
769+
*/
701770
int rerere(int flags)
702771
{
703772
struct string_list merge_rr = STRING_LIST_INIT_DUP;

0 commit comments

Comments
 (0)