Skip to content

Commit 2c7929b

Browse files
committed
rerere: scan $GIT_DIR/rr-cache/$ID when instantiating a rerere_id
This will help fixing bootstrap corner-case issues, e.g. having an empty $GIT_DIR/rr-cache/$ID directory would fail to record a preimage, in later changes in this series. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1869bbe commit 2c7929b

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

rerere.c

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ static char *merge_rr_path;
2626
static int rerere_dir_nr;
2727
static int rerere_dir_alloc;
2828

29+
#define RR_HAS_POSTIMAGE 1
30+
#define RR_HAS_PREIMAGE 2
2931
static struct rerere_dir {
3032
unsigned char sha1[20];
33+
unsigned char status;
3134
} **rerere_dir;
3235

3336
static void free_rerere_dirs(void)
@@ -58,6 +61,27 @@ const char *rerere_path(const struct rerere_id *id, const char *file)
5861
return git_path("rr-cache/%s/%s", rerere_id_hex(id), file);
5962
}
6063

64+
static int is_rr_file(const char *name, const char *filename)
65+
{
66+
return !strcmp(name, filename);
67+
}
68+
69+
static void scan_rerere_dir(struct rerere_dir *rr_dir)
70+
{
71+
struct dirent *de;
72+
DIR *dir = opendir(git_path("rr-cache/%s", sha1_to_hex(rr_dir->sha1)));
73+
74+
if (!dir)
75+
return;
76+
while ((de = readdir(dir)) != NULL) {
77+
if (is_rr_file(de->d_name, "postimage"))
78+
rr_dir->status |= RR_HAS_POSTIMAGE;
79+
else if (is_rr_file(de->d_name, "preimage"))
80+
rr_dir->status |= RR_HAS_PREIMAGE;
81+
}
82+
closedir(dir);
83+
}
84+
6185
static const unsigned char *rerere_dir_sha1(size_t i, void *table)
6286
{
6387
struct rerere_dir **rr_dir = table;
@@ -76,6 +100,7 @@ static struct rerere_dir *find_rerere_dir(const char *hex)
76100
if (pos < 0) {
77101
rr_dir = xmalloc(sizeof(*rr_dir));
78102
hashcpy(rr_dir->sha1, sha1);
103+
rr_dir->status = 0;
79104
pos = -1 - pos;
80105

81106
/* Make sure the array is big enough ... */
@@ -85,15 +110,14 @@ static struct rerere_dir *find_rerere_dir(const char *hex)
85110
memmove(rerere_dir + pos + 1, rerere_dir + pos,
86111
(rerere_dir_nr - pos - 1) * sizeof(*rerere_dir));
87112
rerere_dir[pos] = rr_dir;
113+
scan_rerere_dir(rr_dir);
88114
}
89115
return rerere_dir[pos];
90116
}
91117

92118
static int has_rerere_resolution(const struct rerere_id *id)
93119
{
94-
struct stat st;
95-
96-
return !stat(rerere_path(id, "postimage"), &st);
120+
return (id->collection->status & RR_HAS_POSTIMAGE);
97121
}
98122

99123
static struct rerere_id *new_rerere_id_hex(char *hex)
@@ -737,6 +761,7 @@ static void do_rerere_one_path(struct string_list_item *rr_item,
737761
} else if (!handle_file(path, NULL, NULL)) {
738762
/* The user has resolved it. */
739763
copy_file(rerere_path(id, "postimage"), path, 0666);
764+
id->collection->status |= RR_HAS_POSTIMAGE;
740765
fprintf(stderr, "Recorded resolution for '%s'.\n", path);
741766
} else {
742767
return;
@@ -797,6 +822,7 @@ static int do_plain_rerere(struct string_list *rr, int fd)
797822
* normalized contents to the "preimage" file.
798823
*/
799824
handle_file(path, NULL, rerere_path(id, "preimage"));
825+
id->collection->status |= RR_HAS_PREIMAGE;
800826
fprintf(stderr, "Recorded preimage for '%s'\n", path);
801827
}
802828

0 commit comments

Comments
 (0)