@matheusbsbs / matheus-bagliano / commits / 82ef89b318
Fork 0

fsck: don't require object structs for display functions

Our printable_type() and describe_object() functions take whole object structs, but they really only care about the oid and type. Let's take those individually in order to give our callers more flexibility. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Oct 18, 2019 at 00:58 UTC 82ef89b318a3c88a3e6af21a05b75abf56d715da
1 file changed +37 -32
builtin/fsck.c
+37 -32
@@ -50,23 +50,20 @@ static int name_objects;
50 #define ERROR_REFS 010
51 #define ERROR_COMMIT_GRAPH 020
52
53 -static const char *describe_object(struct object *obj)
53 +static const char *describe_object(const struct object_id *oid)
54 {
55 - return fsck_describe_object(&fsck_walk_options, &obj->oid);
55 + return fsck_describe_object(&fsck_walk_options, oid);
56 }
57
58 -static const char *printable_type(struct object *obj)
58 +static const char *printable_type(const struct object_id *oid,
59 + enum object_type type)
60 {
61 const char *ret;
62
62 - if (obj->type == OBJ_NONE) {
63 - enum object_type type = oid_object_info(the_repository,
64 - &obj->oid, NULL);
65 - if (type > 0)
66 - object_as_type(the_repository, obj, type, 0);
67 - }
63 + if (type == OBJ_NONE)
64 + type = oid_object_info(the_repository, oid, NULL);
65
69 - ret = type_name(obj->type);
66 + ret = type_name(type);
67 if (!ret)
68 ret = _("unknown");
69
@@ -101,7 +98,8 @@ static int objerror(struct object *obj, const char *err)
98 errors_found |= ERROR_OBJECT;
99 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
100 fprintf_ln(stderr, _("error in %s %s: %s"),
104 - printable_type(obj), describe_object(obj), err);
101 + printable_type(&obj->oid, obj->type),
102 + describe_object(&obj->oid), err);
103 return -1;
104 }
105
@@ -112,12 +110,14 @@ static int fsck_error_func(struct fsck_options *o,
110 case FSCK_WARN:
111 /* TRANSLATORS: e.g. warning in tree 01bfda: <more explanation> */
112 fprintf_ln(stderr, _("warning in %s %s: %s"),
115 - printable_type(obj), describe_object(obj), message);
113 + printable_type(&obj->oid, obj->type),
114 + describe_object(&obj->oid), message);
115 return 0;
116 case FSCK_ERROR:
117 /* TRANSLATORS: e.g. error in tree 01bfda: <more explanation> */
118 fprintf_ln(stderr, _("error in %s %s: %s"),
120 - printable_type(obj), describe_object(obj), message);
119 + printable_type(&obj->oid, obj->type),
120 + describe_object(&obj->oid), message);
121 return 1;
122 default:
123 BUG("%d (FSCK_IGNORE?) should never trigger this callback", type);
@@ -138,7 +138,8 @@ static int mark_object(struct object *obj, int type, void *data, struct fsck_opt
138 if (!obj) {
139 /* ... these references to parent->fld are safe here */
140 printf_ln(_("broken link from %7s %s"),
141 - printable_type(parent), describe_object(parent));
141 + printable_type(&parent->oid, parent->type),
142 + describe_object(&parent->oid));
143 printf_ln(_("broken link from %7s %s"),
144 (type == OBJ_ANY ? _("unknown") : type_name(type)),
145 _("unknown"));
@@ -166,10 +167,10 @@ static int mark_object(struct object *obj, int type, void *data, struct fsck_opt
167 if (parent && !has_object_file(&obj->oid)) {
168 printf_ln(_("broken link from %7s %s\n"
169 " to %7s %s"),
169 - printable_type(parent),
170 - describe_object(parent),
171 - printable_type(obj),
172 - describe_object(obj));
170 + printable_type(&parent->oid, parent->type),
171 + describe_object(&parent->oid),
172 + printable_type(&obj->oid, obj->type),
173 + describe_object(&obj->oid));
174 errors_found |= ERROR_REACHABLE;
175 }
176 return 1;
@@ -275,8 +276,9 @@ static void check_reachable_object(struct object *obj)
276 return;
277 if (has_object_pack(&obj->oid))
278 return; /* it is in pack - forget about it */
278 - printf_ln(_("missing %s %s"), printable_type(obj),
279 - describe_object(obj));
279 + printf_ln(_("missing %s %s"),
280 + printable_type(&obj->oid, obj->type),
281 + describe_object(&obj->oid));
282 errors_found |= ERROR_REACHABLE;
283 return;
284 }
@@ -301,8 +303,9 @@ static void check_unreachable_object(struct object *obj)
303 * since this is something that is prunable.
304 */
305 if (show_unreachable) {
304 - printf_ln(_("unreachable %s %s"), printable_type(obj),
305 - describe_object(obj));
306 + printf_ln(_("unreachable %s %s"),
307 + printable_type(&obj->oid, obj->type),
308 + describe_object(&obj->oid));
309 return;
310 }
311
@@ -320,12 +323,13 @@ static void check_unreachable_object(struct object *obj)
323 */
324 if (!(obj->flags & USED)) {
325 if (show_dangling)
323 - printf_ln(_("dangling %s %s"), printable_type(obj),
324 - describe_object(obj));
326 + printf_ln(_("dangling %s %s"),
327 + printable_type(&obj->oid, obj->type),
328 + describe_object(&obj->oid));
329 if (write_lost_and_found) {
330 char *filename = git_pathdup("lost-found/%s/%s",
331 obj->type == OBJ_COMMIT ? "commit" : "other",
328 - describe_object(obj));
332 + describe_object(&obj->oid));
333 FILE *f;
334
335 if (safe_create_leading_directories_const(filename)) {
@@ -338,7 +342,7 @@ static void check_unreachable_object(struct object *obj)
342 if (stream_blob_to_fd(fileno(f), &obj->oid, NULL, 1))
343 die_errno(_("could not write '%s'"), filename);
344 } else
341 - fprintf(f, "%s\n", describe_object(obj));
345 + fprintf(f, "%s\n", describe_object(&obj->oid));
346 if (fclose(f))
347 die_errno(_("could not finish '%s'"),
348 filename);
@@ -357,7 +361,7 @@ static void check_unreachable_object(struct object *obj)
361 static void check_object(struct object *obj)
362 {
363 if (verbose)
360 - fprintf_ln(stderr, _("Checking %s"), describe_object(obj));
364 + fprintf_ln(stderr, _("Checking %s"), describe_object(&obj->oid));
365
366 if (obj->flags & REACHABLE)
367 check_reachable_object(obj);
@@ -415,7 +419,8 @@ static int fsck_obj(struct object *obj, void *buffer, unsigned long size)
419
420 if (verbose)
421 fprintf_ln(stderr, _("Checking %s %s"),
418 - printable_type(obj), describe_object(obj));
422 + printable_type(&obj->oid, obj->type),
423 + describe_object(&obj->oid));
424
425 if (fsck_walk(obj, NULL, &fsck_obj_options))
426 objerror(obj, _("broken links"));
@@ -428,7 +433,7 @@ static int fsck_obj(struct object *obj, void *buffer, unsigned long size)
433
434 if (!commit->parents && show_root)
435 printf_ln(_("root %s"),
431 - describe_object(&commit->object));
436 + describe_object(&commit->object.oid));
437 }
438
439 if (obj->type == OBJ_TAG) {
@@ -436,10 +441,10 @@ static int fsck_obj(struct object *obj, void *buffer, unsigned long size)
441
442 if (show_tags && tag->tagged) {
443 printf_ln(_("tagged %s %s (%s) in %s"),
439 - printable_type(tag->tagged),
440 - describe_object(tag->tagged),
444 + printable_type(&tag->tagged->oid, tag->tagged->type),
445 + describe_object(&tag->tagged->oid),
446 tag->tag,
442 - describe_object(&tag->object));
447 + describe_object(&tag->object.oid));
448 }
449 }
450