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

[PATCH 2/3] fsck: clarify semantics of mark_object_reachable()

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 27, 2011, 17:50 UTC
Message-ID
<1309197059-7177-2-git-send-email-gitster@pobox.com>
In-Reply-To
<1309197059-7177-1-git-send-email-gitster@pobox.com>

Any object that is directly reachable from refs, in the index or in the cache tree, are obviously "used", and indeed all the existing callers of mark_object_reachable() function sets this bit. Move the assignment from the callers to this function.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin/fsck.c |   33 ++++++++-------------------------
 1 files changed, 8 insertions(+), 25 deletions(-)
diff --git a/builtin/fsck.c b/builtin/fsck.c
index 64f30d4..90f5c2c 100644
--- a/builtin/fsck.c
+++ b/builtin/fsck.c
@@ -113,7 +113,10 @@ static int mark_object(struct object *obj, int type, void *data)
 
 static void mark_object_reachable(struct object *obj)
 {
-	mark_object(obj, OBJ_ANY, NULL);
+	if (obj) {
+		obj->used = 1;
+		mark_object(obj, OBJ_ANY, NULL);
+	}
 }
 
 static int traverse_one_object(struct object *obj)
@@ -435,24 +438,13 @@ static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
 		const char *email, unsigned long timestamp, int tz,
 		const char *message, void *cb_data)
 {
-	struct object *obj;
-
 	if (verbose)
 		fprintf(stderr, "Checking reflog %s->%s\n",
 			sha1_to_hex(osha1), sha1_to_hex(nsha1));
 
-	if (!is_null_sha1(osha1)) {
-		obj = lookup_object(osha1);
-		if (obj) {
-			obj->used = 1;
-			mark_object_reachable(obj);
-		}
-	}
-	obj = lookup_object(nsha1);
-	if (obj) {
-		obj->used = 1;
-		mark_object_reachable(obj);
-	}
+	if (!is_null_sha1(osha1))
+		mark_object_reachable(lookup_object(osha1));
+	mark_object_reachable(lookup_object(nsha1));
 	return 0;
 }
 
@@ -480,7 +472,6 @@ static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int f
 	if (obj->type != OBJ_COMMIT && is_branch(refname))
 		error("%s: not a commit", refname);
 	default_refs++;
-	obj->used = 1;
 	mark_object_reachable(obj);
 
 	return 0;
@@ -568,7 +559,6 @@ static int fsck_cache_tree(struct cache_tree *it)
 			      sha1_to_hex(it->sha1));
 			return 1;
 		}
-		obj->used = 1;
 		mark_object_reachable(obj);
 		if (obj->type != OBJ_TREE)
 			err |= objerror(obj, "non-tree in cache-tree");
@@ -647,12 +637,8 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
 		unsigned char sha1[20];
 		if (!get_sha1(arg, sha1)) {
 			struct object *obj = lookup_object(sha1);
-
-			/* Error is printed by lookup_object(). */
 			if (!obj)
 				continue;
-
-			obj->used = 1;
 			mark_object_reachable(obj);
 			heads++;
 			continue;
@@ -675,7 +661,6 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
 		for (i = 0; i < active_nr; i++) {
 			unsigned int mode;
 			struct blob *blob;
-			struct object *obj;
 
 			mode = active_cache[i]->ce_mode;
 			if (S_ISGITLINK(mode))
@@ -683,9 +668,7 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
 			blob = lookup_blob(active_cache[i]->sha1);
 			if (!blob)
 				continue;
-			obj = &blob->object;
-			obj->used = 1;
-			mark_object_reachable(obj);
+			mark_object_reachable(&blob->object);
 		}
 		if (active_cache_tree)
 			fsck_cache_tree(active_cache_tree);
-- 
1.7.6
Previous: Junio C HamanoNext: Junio C Hamano
Message 2 of 3 in “fsck: minor typofix”
  1. 1/3 fsck: minor typofixJunio C Hamano, Jun 27, 2011
  2. 2/3 fsck: clarify semantics of mark_object_reachable()Junio C Hamano, Jun 27, 2011
  3. 3/3 object: reclaim one bit in the flags fieldJunio C Hamano, Jun 27, 2011

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.