fsck-objects: refactor checking for connectivity
This separates the connectivity check into separate codepaths, one for reachable objects and the other for unreachable ones, while adding a lot of comments to explain what is going on. When checking an unreachable object, unlike a reachable one, we do not have to complain if it does not exist (we used to complain about a missing blob even when the only thing that references it is a tree that is dangling). Also we do not have to check and complain about objects that are referenced by an unreachable object. This makes the messages from fsck-objects a lot less noisy and more useful. Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Linus Torvalds committed
Jan 21, 2007 at 22:26 UTC
18af29f247f79b6fb61335f98a21daea67f5632f
1 file changed
+95
-38
fsck-objects.c
+95
-38
@@ -54,6 +54,99 @@ static int objwarning(struct object *obj, const char *err, ...)
54
return -1;
55
}
56
57
+/*
58
+ * Check a single reachable object
59
+ */
60
+static void check_reachable_object(struct object *obj)
61
+{
62
+ const struct object_refs *refs;
63
+
64
+ /*
65
+ * We obviously want the object to be parsed,
66
+ * except if it was in a pack-file and we didn't
67
+ * do a full fsck
68
+ */
69
+ if (!obj->parsed) {
70
+ if (has_sha1_file(obj->sha1))
71
+ return; /* it is in pack - forget about it */
72
+ printf("missing %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
73
+ return;
74
+ }
75
+
76
+ /*
77
+ * Check that everything that we try to reference is also good.
78
+ */
79
+ refs = lookup_object_refs(obj);
80
+ if (refs) {
81
+ unsigned j;
82
+ for (j = 0; j < refs->count; j++) {
83
+ struct object *ref = refs->ref[j];
84
+ if (ref->parsed ||
85
+ (has_sha1_file(ref->sha1)))
86
+ continue;
87
+ printf("broken link from %7s %s\n",
88
+ typename(obj->type), sha1_to_hex(obj->sha1));
89
+ printf(" to %7s %s\n",
90
+ typename(ref->type), sha1_to_hex(ref->sha1));
91
+ }
92
+ }
93
+}
94
+
95
+/*
96
+ * Check a single unreachable object
97
+ */
98
+static void check_unreachable_object(struct object *obj)
99
+{
100
+ /*
101
+ * Missing unreachable object? Ignore it. It's not like
102
+ * we miss it (since it can't be reached), nor do we want
103
+ * to complain about it being unreachable (since it does
104
+ * not exist).
105
+ */
106
+ if (!obj->parsed)
107
+ return;
108
+
109
+ /*
110
+ * Unreachable object that exists? Show it if asked to,
111
+ * since this is something that is prunable.
112
+ */
113
+ if (show_unreachable) {
114
+ printf("unreachable %s %s\n", typename(obj->type), sha1_to_hex(obj->sha1));
115
+ return;
116
+ }
117
+
118
+ /*
119
+ * "!used" means that nothing at all points to it, including
120
+ * other unreacahble objects. In other words, it's the "tip"
121
+ * of some set of unreachable objects, usually a commit that
122
+ * got dropped.
123
+ *
124
+ * Such starting points are more interesting than some random
125
+ * set of unreachable objects, so we show them even if the user
126
+ * hasn't asked for _all_ unreachable objects. If you have
127
+ * deleted a branch by mistake, this is a prime candidate to
128
+ * start looking at, for example.
129
+ */
130
+ if (!obj->used) {
131
+ printf("dangling %s %s\n", typename(obj->type),
132
+ sha1_to_hex(obj->sha1));
133
+ return;
134
+ }
135
+
136
+ /*
137
+ * Otherwise? It's there, it's unreachable, and some other unreachable
138
+ * object points to it. Ignore it - it's not interesting, and we showed
139
+ * all the interesting cases above.
140
+ */
141
+}
142
+
143
+static void check_object(struct object *obj)
144
+{
145
+ if (obj->flags & REACHABLE)
146
+ check_reachable_object(obj);
147
+ else
148
+ check_unreachable_object(obj);
149
+}
150
151
static void check_connectivity(void)
152
{
@@ -62,46 +155,10 @@ static void check_connectivity(void)
155
/* Look up all the requirements, warn about missing objects.. */
156
max = get_max_object_index();
157
for (i = 0; i < max; i++) {
65
- const struct object_refs *refs;
158
struct object *obj = get_indexed_object(i);
159
68
- if (!obj)
69
- continue;
70
-
71
- if (!obj->parsed) {
72
- if (has_sha1_file(obj->sha1))
73
- ; /* it is in pack */
74
- else
75
- printf("missing %s %s\n",
76
- typename(obj->type), sha1_to_hex(obj->sha1));
77
- continue;
78
- }
79
-
80
- refs = lookup_object_refs(obj);
81
- if (refs) {
82
- unsigned j;
83
- for (j = 0; j < refs->count; j++) {
84
- struct object *ref = refs->ref[j];
85
- if (ref->parsed ||
86
- (has_sha1_file(ref->sha1)))
87
- continue;
88
- printf("broken link from %7s %s\n",
89
- typename(obj->type), sha1_to_hex(obj->sha1));
90
- printf(" to %7s %s\n",
91
- typename(ref->type), sha1_to_hex(ref->sha1));
92
- }
93
- }
94
-
95
- if (show_unreachable && !(obj->flags & REACHABLE)) {
96
- printf("unreachable %s %s\n",
97
- typename(obj->type), sha1_to_hex(obj->sha1));
98
- continue;
99
- }
100
-
101
- if (!obj->used) {
102
- printf("dangling %s %s\n", typename(obj->type),
103
- sha1_to_hex(obj->sha1));
104
- }
160
+ if (obj)
161
+ check_object(obj);
162
}
163
}
164