From 5d821843a21a88ac0f22c909025127ee37dc45ca Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Tue, 25 Mar 2008 23:45:47 -0700 Subject: [PATCH] fsck --lost-found: refactor handling of dangling objects This moves a deeply nested part of the function check_unreachable_object() into a separate function. Signed-off-by: Junio C Hamano --- builtin-fsck.c | 71 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 37 insertions(+), 34 deletions(-) diff --git a/builtin-fsck.c b/builtin-fsck.c index 0929c7f245..5480d8d79a 100644 --- a/builtin-fsck.c +++ b/builtin-fsck.c @@ -170,6 +170,41 @@ static void check_reachable_object(struct object *obj) } } +static void dangling_object(struct object *obj) +{ + char *filename; + FILE *f; + printf("dangling %s %s\n", typename(obj->type), + sha1_to_hex(obj->sha1)); + if (!write_lost_and_found) + return; + + filename = git_path("lost-found/%s/%s", + obj->type == OBJ_COMMIT ? "commit" : "other", + sha1_to_hex(obj->sha1)); + + if (safe_create_leading_directories(filename)) { + error("Could not create lost-found"); + return; + } + if (!(f = fopen(filename, "w"))) + die_errno("Could not open '%s'", filename); + if (obj->type == OBJ_BLOB) { + enum object_type type; + unsigned long size; + char *buf = read_sha1_file(obj->sha1, + &type, &size); + if (buf) { + if (fwrite(buf, size, 1, f) != 1) + die_errno("Could not write '%s'", filename); + free(buf); + } + } else + fprintf(f, "%s\n", sha1_to_hex(obj->sha1)); + if (fclose(f)) + die_errno("Could not finish '%s'", filename); +} + /* * Check a single unreachable object */ @@ -205,40 +240,8 @@ 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) { - printf("dangling %s %s\n", typename(obj->type), - sha1_to_hex(obj->sha1)); - if (write_lost_and_found) { - char *filename = git_path("lost-found/%s/%s", - obj->type == OBJ_COMMIT ? "commit" : "other", - sha1_to_hex(obj->sha1)); - FILE *f; - - if (safe_create_leading_directories(filename)) { - error("Could not create lost-found"); - return; - } - if (!(f = fopen(filename, "w"))) - die_errno("Could not open '%s'", filename); - if (obj->type == OBJ_BLOB) { - enum object_type type; - unsigned long size; - char *buf = read_sha1_file(obj->sha1, - &type, &size); - if (buf) { - if (fwrite(buf, size, 1, f) != 1) - die_errno("Could not write '%s'", - filename); - free(buf); - } - } else - fprintf(f, "%s\n", sha1_to_hex(obj->sha1)); - if (fclose(f)) - die_errno("Could not finish '%s'", - filename); - } - return; - } + if (!obj->used) + dangling_object(obj); /* * Otherwise? It's there, it's unreachable, and some other unreachable -- 2.11.4.GIT