fsck: refactor how to describe objects
In many places, we refer to objects via their SHA-1s. Let's abstract that into a function. For the moment, it does nothing else than what we did previously: print out the 40-digit hex string. But that will change over the course of the next patches. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin committed
Jul 17, 2016 at 12:59 UTC
993a21b0a05bf2e2063c58e5722c29f5747e39d4
1 file changed
+23
-14
builtin/fsck.c
+23
-14
@@ -40,6 +40,11 @@ static int show_dangling = 1;
40
#define ERROR_PACK 04
41
#define ERROR_REFS 010
42
43
+static const char *describe_object(struct object *obj)
44
+{
45
+ return oid_to_hex(&obj->oid);
46
+}
47
+
48
static int fsck_config(const char *var, const char *value, void *cb)
49
{
50
if (strcmp(var, "fsck.skiplist") == 0) {
@@ -67,7 +72,7 @@ static void objreport(struct object *obj, const char *msg_type,
72
const char *err)
73
{
74
fprintf(stderr, "%s in %s %s: %s\n",
70
- msg_type, typename(obj->type), oid_to_hex(&obj->oid), err);
75
+ msg_type, typename(obj->type), describe_object(obj), err);
76
}
77
78
static int objerror(struct object *obj, const char *err)
@@ -97,7 +102,7 @@ static int mark_object(struct object *obj, int type, void *data, struct fsck_opt
102
if (!obj) {
103
/* ... these references to parent->fld are safe here */
104
printf("broken link from %7s %s\n",
100
- typename(parent->type), oid_to_hex(&parent->oid));
105
+ typename(parent->type), describe_object(parent));
106
printf("broken link from %7s %s\n",
107
(type == OBJ_ANY ? "unknown" : typename(type)), "unknown");
108
errors_found |= ERROR_REACHABLE;
@@ -114,9 +119,9 @@ static int mark_object(struct object *obj, int type, void *data, struct fsck_opt
119
if (!(obj->flags & HAS_OBJ)) {
120
if (parent && !has_object_file(&obj->oid)) {
121
printf("broken link from %7s %s\n",
117
- typename(parent->type), oid_to_hex(&parent->oid));
122
+ typename(parent->type), describe_object(parent));
123
printf(" to %7s %s\n",
119
- typename(obj->type), oid_to_hex(&obj->oid));
124
+ typename(obj->type), describe_object(obj));
125
errors_found |= ERROR_REACHABLE;
126
}
127
return 1;
@@ -190,7 +195,8 @@ static void check_reachable_object(struct object *obj)
195
return; /* it is in pack - forget about it */
196
if (connectivity_only && has_object_file(&obj->oid))
197
return;
193
- printf("missing %s %s\n", typename(obj->type), oid_to_hex(&obj->oid));
198
+ printf("missing %s %s\n", typename(obj->type),
199
+ describe_object(obj));
200
errors_found |= ERROR_REACHABLE;
201
return;
202
}
@@ -215,7 +221,8 @@ static void check_unreachable_object(struct object *obj)
221
* since this is something that is prunable.
222
*/
223
if (show_unreachable) {
218
- printf("unreachable %s %s\n", typename(obj->type), oid_to_hex(&obj->oid));
224
+ printf("unreachable %s %s\n", typename(obj->type),
225
+ describe_object(obj));
226
return;
227
}
228
@@ -234,11 +241,11 @@ static void check_unreachable_object(struct object *obj)
241
if (!obj->used) {
242
if (show_dangling)
243
printf("dangling %s %s\n", typename(obj->type),
237
- oid_to_hex(&obj->oid));
244
+ describe_object(obj));
245
if (write_lost_and_found) {
246
char *filename = git_pathdup("lost-found/%s/%s",
247
obj->type == OBJ_COMMIT ? "commit" : "other",
241
- oid_to_hex(&obj->oid));
248
+ describe_object(obj));
249
FILE *f;
250
251
if (safe_create_leading_directories_const(filename)) {
@@ -252,7 +259,7 @@ static void check_unreachable_object(struct object *obj)
259
if (stream_blob_to_fd(fileno(f), obj->oid.hash, NULL, 1))
260
die_errno("Could not write '%s'", filename);
261
} else
255
- fprintf(f, "%s\n", oid_to_hex(&obj->oid));
262
+ fprintf(f, "%s\n", describe_object(obj));
263
if (fclose(f))
264
die_errno("Could not finish '%s'",
265
filename);
@@ -271,7 +278,7 @@ static void check_unreachable_object(struct object *obj)
278
static void check_object(struct object *obj)
279
{
280
if (verbose)
274
- fprintf(stderr, "Checking %s\n", oid_to_hex(&obj->oid));
281
+ fprintf(stderr, "Checking %s\n", describe_object(obj));
282
283
if (obj->flags & REACHABLE)
284
check_reachable_object(obj);
@@ -307,7 +314,7 @@ static int fsck_obj(struct object *obj)
314
315
if (verbose)
316
fprintf(stderr, "Checking %s %s\n",
310
- typename(obj->type), oid_to_hex(&obj->oid));
317
+ typename(obj->type), describe_object(obj));
318
319
if (fsck_walk(obj, NULL, &fsck_obj_options))
320
objerror(obj, "broken links");
@@ -326,15 +333,17 @@ static int fsck_obj(struct object *obj)
333
free_commit_buffer(commit);
334
335
if (!commit->parents && show_root)
329
- printf("root %s\n", oid_to_hex(&commit->object.oid));
336
+ printf("root %s\n", describe_object(&commit->object));
337
}
338
339
if (obj->type == OBJ_TAG) {
340
struct tag *tag = (struct tag *) obj;
341
342
if (show_tags && tag->tagged) {
336
- printf("tagged %s %s", typename(tag->tagged->type), oid_to_hex(&tag->tagged->oid));
337
- printf(" (%s) in %s\n", tag->tag, oid_to_hex(&tag->object.oid));
343
+ printf("tagged %s %s", typename(tag->tagged->type),
344
+ describe_object(tag->tagged));
345
+ printf(" (%s) in %s\n", tag->tag,
346
+ describe_object(&tag->object));
347
}
348
}
349