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 <gitster@pobox.com>

Junio C Hamano committed Jul 16, 2015 at 15:47 UTC 2c7929b133387f563306c0550b24a4b5f674ecb6
1 file changed +29 -3
rerere.c
+29 -3
@@ -26,8 +26,11 @@ static char *merge_rr_path;
26 static int rerere_dir_nr;
27 static int rerere_dir_alloc;
28
29 +#define RR_HAS_POSTIMAGE 1
30 +#define RR_HAS_PREIMAGE 2
31 static struct rerere_dir {
32 unsigned char sha1[20];
33 + unsigned char status;
34 } **rerere_dir;
35
36 static void free_rerere_dirs(void)
@@ -58,6 +61,27 @@ const char *rerere_path(const struct rerere_id *id, const char *file)
61 return git_path("rr-cache/%s/%s", rerere_id_hex(id), file);
62 }
63
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 +
85 static const unsigned char *rerere_dir_sha1(size_t i, void *table)
86 {
87 struct rerere_dir **rr_dir = table;
@@ -76,6 +100,7 @@ static struct rerere_dir *find_rerere_dir(const char *hex)
100 if (pos < 0) {
101 rr_dir = xmalloc(sizeof(*rr_dir));
102 hashcpy(rr_dir->sha1, sha1);
103 + rr_dir->status = 0;
104 pos = -1 - pos;
105
106 /* Make sure the array is big enough ... */
@@ -85,15 +110,14 @@ static struct rerere_dir *find_rerere_dir(const char *hex)
110 memmove(rerere_dir + pos + 1, rerere_dir + pos,
111 (rerere_dir_nr - pos - 1) * sizeof(*rerere_dir));
112 rerere_dir[pos] = rr_dir;
113 + scan_rerere_dir(rr_dir);
114 }
115 return rerere_dir[pos];
116 }
117
118 static int has_rerere_resolution(const struct rerere_id *id)
119 {
94 - struct stat st;
95 -
96 - return !stat(rerere_path(id, "postimage"), &st);
120 + return (id->collection->status & RR_HAS_POSTIMAGE);
121 }
122
123 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,
761 } else if (!handle_file(path, NULL, NULL)) {
762 /* The user has resolved it. */
763 copy_file(rerere_path(id, "postimage"), path, 0666);
764 + id->collection->status |= RR_HAS_POSTIMAGE;
765 fprintf(stderr, "Recorded resolution for '%s'.\n", path);
766 } else {
767 return;
@@ -797,6 +822,7 @@ static int do_plain_rerere(struct string_list *rr, int fd)
822 * normalized contents to the "preimage" file.
823 */
824 handle_file(path, NULL, rerere_path(id, "preimage"));
825 + id->collection->status |= RR_HAS_PREIMAGE;
826 fprintf(stderr, "Recorded preimage for '%s'\n", path);
827 }
828