Skip to content

Commit c0a5423

Browse files
committed
rerere: delay the recording of preimage
We record the preimage only when there is no directory to record the conflict we encountered, i.e. when $GIT_DIR/rr-cache/$ID does not exist. As the plan is to allow multiple <preimage,postimage> pairs as variants for the same conflict ID eventually, this logic needs to go. As the first step in that direction, stop the "did we create the directory? Then we record the preimage" logic. Instead, we record if a preimage does not exist when we saw a conflict in a path. Also make sure that we remove a stale postimage, which most likely is totally unrelated to the resolution of this new conflict, when we create a new preimage under $ID when $GIT_DIR/rr-cache/$ID already exists. In later patches, we will further update this logic to be "do we have <preimage,postimage> pair that cleanly resolve the current conflicts? If not, record a new preimage as a new variant", but that does not happen at this stage yet. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 05dd9f1 commit c0a5423

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed

rerere.c

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ static int has_rerere_resolution(const struct rerere_id *id)
122122
return ((id->collection->status & both) == both);
123123
}
124124

125+
static int has_rerere_preimage(const struct rerere_id *id)
126+
{
127+
return (id->collection->status & RR_HAS_PREIMAGE);
128+
}
129+
125130
static struct rerere_id *new_rerere_id_hex(char *hex)
126131
{
127132
struct rerere_id *id = xmalloc(sizeof(*id));
@@ -749,8 +754,24 @@ static void do_rerere_one_path(struct string_list_item *rr_item,
749754
const char *path = rr_item->string;
750755
const struct rerere_id *id = rr_item->util;
751756

752-
/* Is there a recorded resolution we could attempt to apply? */
753-
if (has_rerere_resolution(id)) {
757+
if (!has_rerere_preimage(id)) {
758+
/*
759+
* We are the first to encounter this conflict. Ask
760+
* handle_file() to write the normalized contents to
761+
* the "preimage" file.
762+
*/
763+
handle_file(path, NULL, rerere_path(id, "preimage"));
764+
if (id->collection->status & RR_HAS_POSTIMAGE) {
765+
const char *path = rerere_path(id, "postimage");
766+
if (unlink(path))
767+
die_errno("cannot unlink stray '%s'", path);
768+
id->collection->status &= ~RR_HAS_POSTIMAGE;
769+
}
770+
id->collection->status |= RR_HAS_PREIMAGE;
771+
fprintf(stderr, "Recorded preimage for '%s'\n", path);
772+
return;
773+
} else if (has_rerere_resolution(id)) {
774+
/* Is there a recorded resolution we could attempt to apply? */
754775
if (merge(id, path))
755776
return; /* failed to replay */
756777

@@ -807,31 +828,8 @@ static int do_plain_rerere(struct string_list *rr, int fd)
807828
id = new_rerere_id(sha1);
808829
string_list_insert(rr, path)->util = id;
809830

810-
/*
811-
* Ensure that the directory exists.
812-
* mkdir_in_gitdir() will fail with EEXIST if there
813-
* already is one.
814-
*/
815-
if (mkdir_in_gitdir(rerere_path(id, NULL)) &&
816-
errno != EEXIST)
817-
continue; /* NEEDSWORK: perhaps we should die? */
818-
819-
if (id->collection->status & RR_HAS_PREIMAGE) {
820-
;
821-
} else {
822-
/*
823-
* We are the first to encounter this
824-
* conflict. Ask handle_file() to write the
825-
* normalized contents to the "preimage" file.
826-
*
827-
* NEEDSWORK: what should happen if we had a
828-
* leftover postimage that is totally
829-
* unrelated? Perhaps we should unlink it?
830-
*/
831-
handle_file(path, NULL, rerere_path(id, "preimage"));
832-
id->collection->status |= RR_HAS_PREIMAGE;
833-
fprintf(stderr, "Recorded preimage for '%s'\n", path);
834-
}
831+
/* Ensure that the directory exists. */
832+
mkdir_in_gitdir(rerere_path(id, NULL));
835833
}
836834

837835
for (i = 0; i < rr->nr; i++)

0 commit comments

Comments
 (0)