Skip to content

Commit 2bc1a87

Browse files
peffgitster
authored andcommitted
rerere: check dirname format while iterating rr_cache directory
In rerere_gc(), we walk over the .git/rr_cache directory and create a struct for each entry we find. We feed any name we get from readdir() to find_rerere_dir(), which then calls get_sha1_hex() on it (since we use the binary hash as a lookup key). If that fails (i.e., the directory name is not what we expected), it returns NULL. But the comment in find_rerere_dir() says "BUG". It _would_ be a bug for the call from new_rerere_id_hex(), the only other code path, to fail here; it's generating the hex internally. But the call in rerere_gc() is using it say "is this a plausible directory name". Let's instead have rerere_gc() do its own "is this plausible" check. That has two benefits: - we can now reliably BUG() inside find_rerere_dir(), which would catch bugs in the other code path (and we now will never return NULL from the function, which makes it easier to see that a rerere_id struct will always have a non-NULL "collection" field). - it makes the use of the binary hash an implementation detail of find_rerere_dir(), not known by callers. That will free us up to change it in a future patch. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 98c431b commit 2bc1a87

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

rerere.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ static struct rerere_dir *find_rerere_dir(const char *hex)
146146
int pos;
147147

148148
if (get_sha1_hex(hex, hash))
149-
return NULL; /* BUG */
149+
BUG("cannot parse rerere dir hex?");
150150
pos = hash_pos(hash, rerere_dir, rerere_dir_nr, rerere_dir_hash);
151151
if (pos < 0) {
152152
rr_dir = xmalloc(sizeof(*rr_dir));
@@ -1178,6 +1178,13 @@ static void prune_one(struct rerere_id *id,
11781178
unlink_rr_item(id);
11791179
}
11801180

1181+
/* Does the basename in "path" look plausibly like an rr-cache entry? */
1182+
static int is_rr_cache_dirname(const char *path)
1183+
{
1184+
unsigned char hash[GIT_MAX_RAWSZ];
1185+
return !get_sha1_hex(path, hash);
1186+
}
1187+
11811188
void rerere_gc(struct repository *r, struct string_list *rr)
11821189
{
11831190
struct string_list to_remove = STRING_LIST_INIT_DUP;
@@ -1205,10 +1212,11 @@ void rerere_gc(struct repository *r, struct string_list *rr)
12051212

12061213
if (is_dot_or_dotdot(e->d_name))
12071214
continue;
1208-
rr_dir = find_rerere_dir(e->d_name);
1209-
if (!rr_dir)
1215+
if (!is_rr_cache_dirname(e->d_name))
12101216
continue; /* or should we remove e->d_name? */
12111217

1218+
rr_dir = find_rerere_dir(e->d_name);
1219+
12121220
now_empty = 1;
12131221
for (id.variant = 0, id.collection = rr_dir;
12141222
id.variant < id.collection->status_nr;

0 commit comments

Comments
 (0)