git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[5/5] Make fsck-cache read heads out of .git/refs/*/*

From
Daniel Barkalow <barkalow@iabervon.org>
Date
Apr 28, 2005, 05:17 UTC
Message-ID
<Pine.LNX.4.21.0504280113560.30848-100000@iabervon.org>
In-Reply-To
<Pine.LNX.4.21.0504280055180.30848-100000@iabervon.org>

This makes fsck-cache expect the root set of the reachability graph (i.e., your heads and tags) to be .git/refs/*/*, which contain hex versions of hashs of roots (like .get/HEAD has always been).

Signed-Off-By: Daniel Barkalow <barkalow@iabervon.org>
Index: fsck-cache.c
===================================================================
--- ca8a271be1370ab0795a869c15114e566bdd15d8/fsck-cache.c  (mode:100644 sha1:280a104050b665515418c00c33af8e6b0b0e2101)
+++ c53109e78c0b0d8925a4198fdec8295620f8f349/fsck-cache.c  (mode:100644 sha1:c4ff5bf3d71c35236e769d30035b00acf08d452b)
@@ -135,6 +135,67 @@
 	return 0;
 }
 
+int read_refs_dirs(char *path)
+{
+	DIR *dir = opendir(path);
+	char filename[NAME_MAX+1];
+	struct dirent *de;
+	int total = 0;
+	char *base;
+
+	if (!dir) {
+		error("missing refs directory '%s'", path);
+		return 0;
+	}
+	strcpy(filename, path);
+	base = filename + strlen(path);
+	*(base++) = '/';
+	while ((de = readdir(dir)) != NULL) {
+		DIR *subdir;
+		char *subbase;
+		if (de->d_name[0] == '.')
+			continue;
+		strcpy(base, de->d_name);
+		subbase = base + strlen(de->d_name);
+		subdir = opendir(filename);
+		if (!subdir) {
+			fprintf(stderr, "can't open subdir %s\n", filename);
+			continue;
+		}
+		*(subbase++) = '/';
+		while ((de = readdir(subdir)) != NULL) {
+			char hex[41];
+			char sha1[20];
+			int fd;
+			struct object *obj;
+			if (de->d_name[0] == '.')
+				continue;
+			strcpy(subbase, de->d_name);
+			fd = open(filename, O_RDONLY);
+			if (fd < 0) {
+				fprintf(stderr, "Couldn't open %s\n", filename);
+				continue;
+			}
+			if ((read(fd, hex, 41) < 41) ||
+			    (hex[40] != '\n') ||
+			    get_sha1_hex(hex, sha1)) {
+				fprintf(stderr, "Couldn't read a hash from %s\n",
+					filename);
+				continue;
+			}
+			obj = parse_object(sha1);
+			obj->used = 1;
+			mark_reachable(obj, REACHABLE);
+			
+			total++;
+		}
+		closedir(subdir);
+	}
+	closedir(dir);
+
+	return total;
+}
+
 int main(int argc, char **argv)
 {
 	int i, heads;
@@ -183,6 +244,8 @@
 		error("expected sha1, got %s", arg);
 	}
 
+	heads += read_refs_dirs(".git/refs");
+
 	if (!heads) {
 		if (show_unreachable) {
 			fprintf(stderr, "unable to do reachability without a head\n");
Previous: Daniel Barkalow
Message 10 of 10 in “[0/5] Updates for library, fsck-cache”
  1. Daniel BarkalowApr 28, 2005
  2. 1/5 Consider a blob to be parsedDaniel Barkalow, Apr 28, 2005
  3. Linus TorvaldsApr 28, 2005
  4. Daniel BarkalowApr 28, 2005
  5. [1'/5] Mark blobs as parsed when they're actually parsedDaniel Barkalow, Apr 28, 2005
  6. 2/5 Add tag header/parser to libraryDaniel Barkalow, Apr 28, 2005
  7. 3/5 Add function to parse an object of unspecified typeDaniel Barkalow, Apr 28, 2005
  8. [3'/5] Add function to parse an object of unspecified type (take 2)Daniel Barkalow, Apr 28, 2005
  9. 4/5 Rework fsck-cache to use parse_object()Daniel Barkalow, Apr 28, 2005
  10. 5/5 Make fsck-cache read heads out of .git/refs/*/*Daniel Barkalow, Apr 28, 2005

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.