@matheusbsbs / matheus-bagliano / commits / f1f0d0889e
Fork 0

[PATCH] Make the git-fsck-objects diagnostics more useful

Actually report what exactly is wrong with the object, instead of an ambiguous 'bad sha1 file' or such. In places where we already do, unify the format and clean the messages up. Signed-off-by: Petr Baudis <pasky@suse.cz> Signed-off-by: Junio C Hamano <junkio@cox.net>

Petr Baudis committed Sep 20, 2005 at 20:56 UTC f1f0d0889e5557bc467490fdbd53f7b912503a33
1 file changed +48 -34
fsck-objects.c
+48 -34
@@ -20,6 +20,35 @@ static int check_strict = 0;
20 static int keep_cache_objects = 0;
21 static unsigned char head_sha1[20];
22
23 +
24 +static void objreport(struct object *obj, const char *severity,
25 + const char *err, va_list params)
26 +{
27 + fprintf(stderr, "%s in %s %s: ",
28 + severity, obj->type, sha1_to_hex(obj->sha1));
29 + vfprintf(stderr, err, params);
30 + fputs("\n", stderr);
31 +}
32 +
33 +int objerror(struct object *obj, const char *err, ...)
34 +{
35 + va_list params;
36 + va_start(params, err);
37 + objreport(obj, "error", err, params);
38 + va_end(params);
39 + return -1;
40 +}
41 +
42 +int objwarning(struct object *obj, const char *err, ...)
43 +{
44 + va_list params;
45 + va_start(params, err);
46 + objreport(obj, "warning", err, params);
47 + va_end(params);
48 + return -1;
49 +}
50 +
51 +
52 static void check_connectivity(void)
53 {
54 int i;
@@ -162,31 +191,19 @@ static int fsck_tree(struct tree *item)
191
192 retval = 0;
193 if (has_full_path) {
165 - fprintf(stderr, "warning: git-fsck-objects: tree %s "
166 - "has full pathnames in it\n",
167 - sha1_to_hex(item->object.sha1));
194 + objwarning(&item->object, "contains full pathnames");
195 }
196 if (has_zero_pad) {
170 - fprintf(stderr, "warning: git-fsck-objects: tree %s "
171 - "has zero-padded file modes in it\n",
172 - sha1_to_hex(item->object.sha1));
197 + objwarning(&item->object, "contains zero-padded file modes");
198 }
199 if (has_bad_modes) {
175 - fprintf(stderr, "warning: git-fsck-objects: tree %s "
176 - "has bad file modes in it\n",
177 - sha1_to_hex(item->object.sha1));
200 + objwarning(&item->object, "contains bad file modes");
201 }
202 if (has_dup_entries) {
180 - fprintf(stderr, "error: git-fsck-objects: tree %s "
181 - "has duplicate file entries\n",
182 - sha1_to_hex(item->object.sha1));
183 - retval = -1;
203 + retval = objerror(&item->object, "contains duplicate file entries");
204 }
205 if (not_properly_sorted) {
186 - fprintf(stderr, "error: git-fsck-objects: tree %s "
187 - "is not properly sorted\n",
188 - sha1_to_hex(item->object.sha1));
189 - retval = -1;
206 + retval = objerror(&item->object, "not properly sorted");
207 }
208 return retval;
209 }
@@ -194,24 +211,24 @@ static int fsck_tree(struct tree *item)
211 static int fsck_commit(struct commit *commit)
212 {
213 char *buffer = commit->buffer;
197 - unsigned char sha1[20];
214 + unsigned char tree_sha1[20], sha1[20];
215
216 if (memcmp(buffer, "tree ", 5))
200 - return -1;
201 - if (get_sha1_hex(buffer+5, sha1) || buffer[45] != '\n')
202 - return -1;
217 + return objerror(&commit->object, "invalid format - expected 'tree' line");
218 + if (get_sha1_hex(buffer+5, tree_sha1) || buffer[45] != '\n')
219 + return objerror(&commit->object, "invalid 'tree' line format - bad sha1");
220 buffer += 46;
221 while (!memcmp(buffer, "parent ", 7)) {
222 if (get_sha1_hex(buffer+7, sha1) || buffer[47] != '\n')
206 - return -1;
223 + return objerror(&commit->object, "invalid 'parent' line format - bad sha1");
224 buffer += 48;
225 }
226 if (memcmp(buffer, "author ", 7))
210 - return -1;
227 + return objerror(&commit->object, "invalid format - expected 'author' line");
228 free(commit->buffer);
229 commit->buffer = NULL;
230 if (!commit->tree)
214 - return -1;
231 + return objerror(&commit->object, "could not load commit's tree %s", tree_sha1);
232 if (!commit->parents && show_root)
233 printf("root %s\n", sha1_to_hex(commit->object.sha1));
234 if (!commit->date)
@@ -225,8 +242,7 @@ static int fsck_tag(struct tag *tag)
242 struct object *tagged = tag->tagged;
243
244 if (!tagged) {
228 - printf("bad object in tag %s\n", sha1_to_hex(tag->object.sha1));
229 - return -1;
245 + return objerror(&tag->object, "could not load tagged object");
246 }
247 if (!show_tags)
248 return 0;
@@ -240,7 +256,7 @@ static int fsck_sha1(unsigned char *sha1)
256 {
257 struct object *obj = parse_object(sha1);
258 if (!obj)
243 - return -1;
259 + return error("%s: object not found", sha1_to_hex(sha1));
260 if (obj->type == blob_type)
261 return 0;
262 if (obj->type == tree_type)
@@ -249,7 +265,8 @@ static int fsck_sha1(unsigned char *sha1)
265 return fsck_commit((struct commit *) obj);
266 if (obj->type == tag_type)
267 return fsck_tag((struct tag *) obj);
252 - return -1;
268 + /* By now, parse_object() would've returned NULL instead. */
269 + return objerror(obj, "unknown type '%s' (internal fsck error)", obj->type);
270 }
271
272 /*
@@ -285,8 +302,7 @@ static void fsck_sha1_list(void)
302 unsigned char *sha1 = entry->sha1;
303
304 sha1_list.entry[i] = NULL;
288 - if (fsck_sha1(sha1) < 0)
289 - fprintf(stderr, "bad sha1 entry '%s'\n", sha1_to_hex(sha1));
305 + fsck_sha1(sha1);
306 free(entry);
307 }
308 sha1_list.nr = 0;
@@ -479,9 +495,7 @@ int main(int argc, char **argv)
495 for (i = 0; i < num; i++) {
496 unsigned char sha1[20];
497 nth_packed_object_sha1(p, i, sha1);
482 - if (fsck_sha1(sha1) < 0)
483 - fprintf(stderr, "bad sha1 entry '%s'\n", sha1_to_hex(sha1));
484 -
498 + fsck_sha1(sha1);
499 }
500 }
501 }
@@ -505,7 +519,7 @@ int main(int argc, char **argv)
519 heads++;
520 continue;
521 }
508 - error("expected sha1, got %s", arg);
522 + error("invalid parameter: expected sha1, got '%s'", arg);
523 }
524
525 /*