alternates: encapsulate alt->base munging

The alternate_object_database struct holds a path to the alternate objects, but we also use that buffer as scratch space for forming loose object filenames. Let's pull that logic into a helper function so that we can more easily modify it. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Oct 3, 2016 at 16:35 UTC 29ec6af2b81894d3236f2aec100323138023ef4d
1 file changed +13 -6
sha1_file.c
+13 -6
@@ -204,6 +204,13 @@ const char *sha1_file_name(const unsigned char *sha1)
204 return buf;
205 }
206
207 +static const char *alt_sha1_path(struct alternate_object_database *alt,
208 + const unsigned char *sha1)
209 +{
210 + fill_sha1_path(alt->name, sha1);
211 + return alt->base;
212 +}
213 +
214 /*
215 * Return the name of the pack or index file with the specified sha1
216 * in its filename. *base and *name are scratch space that must be
@@ -601,8 +608,8 @@ static int check_and_freshen_nonlocal(const unsigned char *sha1, int freshen)
608 struct alternate_object_database *alt;
609 prepare_alt_odb();
610 for (alt = alt_odb_list; alt; alt = alt->next) {
604 - fill_sha1_path(alt->name, sha1);
605 - if (check_and_freshen_file(alt->base, freshen))
611 + const char *path = alt_sha1_path(alt, sha1);
612 + if (check_and_freshen_file(path, freshen))
613 return 1;
614 }
615 return 0;
@@ -1600,8 +1607,8 @@ static int stat_sha1_file(const unsigned char *sha1, struct stat *st)
1607 prepare_alt_odb();
1608 errno = ENOENT;
1609 for (alt = alt_odb_list; alt; alt = alt->next) {
1603 - fill_sha1_path(alt->name, sha1);
1604 - if (!lstat(alt->base, st))
1610 + const char *path = alt_sha1_path(alt, sha1);
1611 + if (!lstat(path, st))
1612 return 0;
1613 }
1614
@@ -1621,8 +1628,8 @@ static int open_sha1_file(const unsigned char *sha1)
1628
1629 prepare_alt_odb();
1630 for (alt = alt_odb_list; alt; alt = alt->next) {
1624 - fill_sha1_path(alt->name, sha1);
1625 - fd = git_open_noatime(alt->base);
1631 + const char *path = alt_sha1_path(alt, sha1);
1632 + fd = git_open_noatime(path);
1633 if (fd >= 0)
1634 return fd;
1635 if (most_interesting_errno == ENOENT)