[PATCH] Install git-verify-tag-script
[git.git] / fsck-cache.c
index 9867fa6..8e21bf1 100644 (file)
@@ -312,8 +312,10 @@ static int fsck_handle_ref(const char *refname, const unsigned char *sha1)
 
        obj = lookup_object(sha1);
        if (!obj) {
-               if (!standalone && has_sha1_file(sha1))
+               if (!standalone && has_sha1_file(sha1)) {
+                       default_refs++;
                        return 0; /* it is in a pack */
+               }
                error("%s: invalid sha1 pointer %s", refname, sha1_to_hex(sha1));
                /* We'll continue with the rest despite the error.. */
                return 0;
@@ -342,6 +344,31 @@ static void fsck_object_dir(const char *path)
        fsck_sha1_list();
 }
 
+static int fsck_head_link(void)
+{
+       int fd, count;
+       char hex[40];
+       unsigned char sha1[20];
+       static char path[PATH_MAX], link[PATH_MAX];
+       const char *git_dir = gitenv(GIT_DIR_ENVIRONMENT) ? : DEFAULT_GIT_DIR_ENVIRONMENT;
+
+       snprintf(path, sizeof(path), "%s/HEAD", git_dir);
+       if (readlink(path, link, sizeof(link)) < 0)
+               return error("HEAD is not a symlink");
+       if (strncmp("refs/heads/", link, 11))
+               return error("HEAD points to something strange (%s)", link);
+       fd = open(path, O_RDONLY);
+       if (fd < 0)
+               return error("HEAD: %s", strerror(errno));
+       count = read(fd, hex, sizeof(hex));
+       close(fd);
+       if (count < 0)
+               return error("HEAD: %s", strerror(errno));
+       if (count < 40 || get_sha1_hex(hex, sha1))
+               return error("HEAD: not a valid git pointer");
+       return 0;
+}
+
 int main(int argc, char **argv)
 {
        int i, heads;
@@ -382,15 +409,18 @@ int main(int argc, char **argv)
        if (standalone)
                unsetenv("GIT_ALTERNATE_OBJECT_DIRECTORIES");
 
+       fsck_head_link();
        fsck_object_dir(get_object_directory());
        if (check_full) {
                int j;
                struct packed_git *p;
                prepare_alt_odb();
                for (j = 0; alt_odb[j].base; j++) {
-                       alt_odb[j].name[-1] = 0; /* was slash */
-                       fsck_object_dir(alt_odb[j].base);
-                       alt_odb[j].name[-1] = '/';
+                       char namebuf[PATH_MAX];
+                       int namelen = alt_odb[j].name - alt_odb[j].base;
+                       memcpy(namebuf, alt_odb[j].base, namelen);
+                       namebuf[namelen - 1] = 0;
+                       fsck_object_dir(namebuf);
                }
                prepare_packed_git();
                for (p = packed_git; p; p = p->next)