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

[PATCH 1/2] fsck --lost-found: refactor handling of dangling objects

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Mar 28, 2008, 14:05 UTC
Message-ID
<alpine.LSU.1.00.0803281505330.18259@racer.site>
In-Reply-To
<alpine.LSU.1.00.0803281504591.18259@racer.site>
From: Junio C Hamano <gitster@pobox.com>

This moves a deeply nested part of the function check_unreachable_object() into a separate function.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 builtin-fsck.c |   65 ++++++++++++++++++++++++++++++-------------------------
 1 files changed, 35 insertions(+), 30 deletions(-)
diff --git a/builtin-fsck.c b/builtin-fsck.c
index 78a6e1f..f01263a 100644
--- a/builtin-fsck.c
+++ b/builtin-fsck.c
@@ -145,6 +145,39 @@ static void check_reachable_object(struct object *obj)
 	}
 }
 
+static void dangling_object(struct object *obj)
+{
+	char *filename;
+	FILE *f;
+	printf("dangling %s %s\n", typename(obj->type),
+			sha1_to_hex(obj->sha1));
+	if (!write_lost_and_found)
+		return;
+
+	filename = git_path("lost-found/%s/%s",
+			    obj->type == OBJ_COMMIT ? "commit" : "other",
+			    sha1_to_hex(obj->sha1));
+
+	if (safe_create_leading_directories(filename)) {
+		error("Could not create lost-found");
+		return;
+	}
+	if (!(f = fopen(filename, "w")))
+		die("Could not open %s", filename);
+	if (obj->type == OBJ_BLOB) {
+		enum object_type type;
+		unsigned long size;
+		char *buf = read_sha1_file(obj->sha1,
+				&type, &size);
+		if (buf) {
+			fwrite(buf, size, 1, f);
+			free(buf);
+		}
+	} else
+		fprintf(f, "%s\n", sha1_to_hex(obj->sha1));
+	fclose(f);
+}
+
 /*
  * Check a single unreachable object
  */
@@ -180,36 +213,8 @@ static void check_unreachable_object(struct object *obj)
 	 * deleted a branch by mistake, this is a prime candidate to
 	 * start looking at, for example.
 	 */
-	if (!obj->used) {
-		printf("dangling %s %s\n", typename(obj->type),
-		       sha1_to_hex(obj->sha1));
-		if (write_lost_and_found) {
-			char *filename = git_path("lost-found/%s/%s",
-				obj->type == OBJ_COMMIT ? "commit" : "other",
-				sha1_to_hex(obj->sha1));
-			FILE *f;
-
-			if (safe_create_leading_directories(filename)) {
-				error("Could not create lost-found");
-				return;
-			}
-			if (!(f = fopen(filename, "w")))
-				die("Could not open %s", filename);
-			if (obj->type == OBJ_BLOB) {
-				enum object_type type;
-				unsigned long size;
-				char *buf = read_sha1_file(obj->sha1,
-						&type, &size);
-				if (buf) {
-					fwrite(buf, size, 1, f);
-					free(buf);
-				}
-			} else
-				fprintf(f, "%s\n", sha1_to_hex(obj->sha1));
-			fclose(f);
-		}
-		return;
-	}
+	if (!obj->used)
+		dangling_object(obj);
 
 	/*
 	 * Otherwise? It's there, it's unreachable, and some other unreachable
-- 
1.5.5.rc2.186.gbac51
Previous: Johannes SchindelinNext: Johannes Schindelin
Message 3 of 4 in “fsck --lost-found: show found commits human readably”
  1. fsck --lost-found: show found commits human readablyJunio C Hamano, Mar 26, 2008
  2. Johannes SchindelinMar 28, 2008
  3. 1/2 fsck --lost-found: refactor handling of dangling objectsJohannes Schindelin, Mar 28, 2008
  4. 2/2 fsck --lost-found: show found commits human readablyJohannes Schindelin, Mar 28, 2008

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.