server-info: drop objdirlen pointer arithmetic
authorJeff King <peff@peff.net>
Fri, 5 Apr 2019 18:14:04 +0000 (14:14 -0400)
committerJunio C Hamano <gitster@pobox.com>
Tue, 16 Apr 2019 07:58:21 +0000 (16:58 +0900)
When writing objects/info/packs, we use the basename of each pack
(i.e., just the "pack-1234abcd.pack" part). We compute that manually by
adding "objdirlen + 6" to the name.

This _should_ work consistently, as we do not include non-local packs,
meaning everything should be in $objdir/pack/. Before f13d7db4af
(server-info.c: use pack_local like everybody else., 2005-12-05), this
was definitely true, since we computed "local" based on comparing the
objdir string. Since then, we're relying on the code on packfile.c to
match our expectations of p->pack_name and p->local.

I think our expectations do still hold today, but we can be a bit more
defensive by just using pack_basename() to get the base. That
future-proofs us, and should hopefully be more obviously safe to
somebody reading the code.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
server-info.c
index c9fbfd3a510959b5feaf9c39dc8c23e260245cfe..ab03c1b3c225ea2d1596d5b9b8fd1b33ffde8bcc 100644 (file)
@@ -93,16 +93,13 @@ static struct pack_info {
        int new_num;
 } **info;
 static int num_pack;
-static const char *objdir;
-static int objdirlen;
 
 static struct pack_info *find_pack_by_name(const char *name)
 {
        int i;
        for (i = 0; i < num_pack; i++) {
                struct packed_git *p = info[i]->p;
-               /* skip "/pack/" after ".git/objects" */
-               if (!strcmp(p->pack_name + objdirlen + 6, name))
+               if (!strcmp(pack_basename(p), name))
                        return info[i];
        }
        return NULL;
@@ -196,9 +193,6 @@ static void init_pack_info(const char *infofile, int force)
        int stale;
        int i = 0;
 
-       objdir = get_object_directory();
-       objdirlen = strlen(objdir);
-
        for (p = get_all_packs(the_repository); p; p = p->next) {
                /* we ignore things on alternate path since they are
                 * not available to the pullers in general.
@@ -246,7 +240,7 @@ static int write_pack_info_file(FILE *fp)
 {
        int i;
        for (i = 0; i < num_pack; i++) {
-               if (fprintf(fp, "P %s\n", info[i]->p->pack_name + objdirlen + 6) < 0)
+               if (fprintf(fp, "P %s\n", pack_basename(info[i]->p)) < 0)
                        return -1;
        }
        if (fputc('\n', fp) == EOF)