threads / patch / 27712

patch, 3 partsfsck: minor typofix

Subject: [PATCH 1/3] fsck: minor typofix

## tl;dr

3 messages between Jun 27, 2011 and Jun 27, 2011. Diffs are folded; open one to read it.

replies: 2people: 1as markdown or json

Junio C Hamano· Jun 27, 2011, 17:50 UTC · lore

There are two places that diagnose the same kind of error in mark_object(), that receives an object, the type the object is expected to be, and the object that refers to it (IOW, the reason why we want to make sure that the first object is in a good order). When the object that is referred to is missing, we report "broken link from X; broken link to Y".

However, one of the two places, that is used much less often, said "broken link from X; broken link from Y".

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 This rarely used codepath is triggered only when fsck_walk() calls this
 function that is set as a callback in traverse_one_object() from one of
 the type specific scanners, e.g. fsck_walk_tree(), and an object that is
 referred to is of a wrong type, e.g. a tree entry with mode 100644 is
 looked up with lookup_blob(), and the type of the named object is already
 known to us to be something different. The look-up code issues an unrelated
 error message in such a case, but then this codepath does not know what the
 object name of the offending object was.
---
 builtin/fsck.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
Show changes to builtin/fsck.c +1 −1
diff --git a/builtin/fsck.c b/builtin/fsck.c
index 5ae0366..64f30d4 100644
--- a/builtin/fsck.c
+++ b/builtin/fsck.c
@@ -83,7 +83,7 @@ static int mark_object(struct object *obj, int type, void *data)
 		/* ... these references to parent->fld are safe here */
 		printf("broken link from %7s %s\n",
 			   typename(parent->type), sha1_to_hex(parent->sha1));
-		printf("broken link from %7s %s\n",
+		printf("broken link to %7s %s\n",
 			   (type == OBJ_ANY ? "unknown" : typename(type)), "unknown");
 		errors_found |= ERROR_REACHABLE;
 		return 1;
-- 
1.7.6
Junio C Hamano· Jun 27, 2011, 17:50 UTC · re: Junio C Hamano · lore

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

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(-)
Show changes to builtin/fsck.c +8 −25
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
Junio C Hamano· Jun 27, 2011, 17:50 UTC · re: Junio C Hamano · lore

[PATCH 3/3] object: reclaim one bit in the flags field

The object layer has "used" bit in the structure that is only used while running fsck. Remove it, and instead use one bit from the flags to record the fact that an object is referenced from some other object (only during fsck).

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin/fsck.c |    7 ++++---
 object.c       |    1 -
 object.h       |    3 +--
 3 files changed, 5 insertions(+), 6 deletions(-)
Show changes to 3 files +5 −6

builtin/fsck.c, object.c, object.h

diff --git a/builtin/fsck.c b/builtin/fsck.c
index 90f5c2c..dde0907 100644
--- a/builtin/fsck.c
+++ b/builtin/fsck.c
@@ -14,6 +14,7 @@
 
 #define REACHABLE 0x0001
 #define SEEN      0x0002
+#define USED	  0x0004
 
 static int show_root;
 static int show_tags;
@@ -114,7 +115,7 @@ static int mark_object(struct object *obj, int type, void *data)
 static void mark_object_reachable(struct object *obj)
 {
 	if (obj) {
-		obj->used = 1;
+		obj->flags |= USED;
 		mark_object(obj, OBJ_ANY, NULL);
 	}
 }
@@ -156,7 +157,7 @@ static int mark_used(struct object *obj, int type, void *data)
 {
 	if (!obj)
 		return 1;
-	obj->used = 1;
+	obj->flags |= USED;
 	return 0;
 }
 
@@ -214,7 +215,7 @@ 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) {
+	if (!(obj->flags & USED)) {
 		printf("dangling %s %s\n", typename(obj->type),
 		       sha1_to_hex(obj->sha1));
 		if (write_lost_and_found) {
diff --git a/object.c b/object.c
index 31976b5..4a9d77f 100644
--- a/object.c
+++ b/object.c
@@ -111,7 +111,6 @@ void *create_object(const unsigned char *sha1, int type, void *o)
 	struct object *obj = o;
 
 	obj->parsed = 0;
-	obj->used = 0;
 	obj->type = type;
 	obj->flags = 0;
 	hashcpy(obj->sha1, sha1);
diff --git a/object.h b/object.h
index b6618d9..dbfc88f 100644
--- a/object.h
+++ b/object.h
@@ -19,14 +19,13 @@ struct object_array {
 #define OBJECT_ARRAY_INIT { 0, 0, NULL }
 
 #define TYPE_BITS   3
-#define FLAG_BITS  27
+#define FLAG_BITS  (31-TYPE_BITS)
 
 /*
  * The object type is stored in 3 bits.
  */
 struct object {
 	unsigned parsed : 1;
-	unsigned used : 1;
 	unsigned type : TYPE_BITS;
 	unsigned flags : FLAG_BITS;
 	unsigned char sha1[20];
-- 
1.7.6

← back to recent threads