+static int read_sha1_reference(const char *path)
+{
+ char hexname[60];
+ unsigned char sha1[20];
+ int fd = open(path, O_RDONLY), len;
+ struct object *obj;
+
+ if (fd < 0)
+ return -1;
+
+ len = read(fd, hexname, sizeof(hexname));
+ close(fd);
+ if (len < 40)
+ return -1;
+
+ if (get_sha1_hex(hexname, sha1) < 0)
+ return -1;
+
+ obj = lookup_object(sha1);
+ if (!obj) {
+ if (!standalone && has_sha1_file(sha1))
+ return 0; /* it is in pack */
+ return error("%s: invalid sha1 pointer %.40s", path, hexname);
+ }
+
+ obj->used = 1;
+ mark_reachable(obj, REACHABLE);
+ return 0;
+}
+
+static int find_file_objects(const char *base, const char *name)
+{
+ int baselen = strlen(base);
+ int namelen = strlen(name);
+ char *path = xmalloc(baselen + namelen + 2);
+ struct stat st;
+
+ memcpy(path, base, baselen);
+ path[baselen] = '/';
+ memcpy(path + baselen + 1, name, namelen+1);
+ if (stat(path, &st) < 0)
+ return 0;
+
+ /*
+ * Recurse into directories
+ */
+ if (S_ISDIR(st.st_mode)) {
+ int count = 0;
+ DIR *dir = opendir(path);
+ if (dir) {
+ struct dirent *de;
+ while ((de = readdir(dir)) != NULL) {
+ if (de->d_name[0] == '.')
+ continue;
+ count += find_file_objects(path, de->d_name);
+ }
+ closedir(dir);
+ }
+ return count;
+ }
+ if (S_ISREG(st.st_mode))
+ return read_sha1_reference(path) == 0;
+ return 0;
+}
+
+static void get_default_heads(void)
+{
+ char *git_dir = gitenv(GIT_DIR_ENVIRONMENT) ? : DEFAULT_GIT_DIR_ENVIRONMENT;
+ int count = find_file_objects(git_dir, "refs");
+ if (!count)
+ die("No default references");
+}
+
+static void fsck_object_dir(const char *path)
+{
+ int i;
+ for (i = 0; i < 256; i++) {
+ static char dir[4096];
+ sprintf(dir, "%s/%02x", path, i);
+ fsck_dir(i, dir);
+ }
+ fsck_sha1_list();
+}
+