{"thread":{"id":"7088","subject":"[PATCH 3/3] fsck: exit with non-zero status upon errors","startedAt":"2007-03-05T08:22:06Z","lastAt":"2007-03-05T08:22:06Z","messageCount":1,"participants":["Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":3},"messages":[{"id":"36293","messageId":"7vwt1w14nl.fsf@assigned-by-dhcp.cox.net","threadId":"7088","inReplyTo":null,"subject":"[PATCH 3/3] fsck: exit with non-zero status upon errors","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2007-03-05T08:22:06Z","receivedAt":"2007-03-05T08:22:06Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"git-fsck always exited with status 0, which was a bit sloppy.\nThis makes it exit with a non-zero status when errors are\nfound.  The error code is an OR'ed result of:\n\n  1 if corrupted objects are found.\n  2 if objects that are ought to be reachable are missing or corrupt.\n\nFor example, it would exit with 1 in a repository with an\nunreachable corrupt object.  If a tree object of the HEAD commit\nis corrupt, you would get 3.\n\nSigned-off-by: Junio C Hamano <junkio@cox.net>\n---\n builtin-fsck.c |   26 +++++++++++++++++++++-----\n 1 files changed, 21 insertions(+), 5 deletions(-)\n\ndiff --git a/builtin-fsck.c b/builtin-fsck.c\nindex abdd0f2..4d03378 100644\n--- a/builtin-fsck.c\n+++ b/builtin-fsck.c\n@@ -18,6 +18,9 @@ static int check_full;\n static int check_strict;\n static int keep_cache_objects;\n static unsigned char head_sha1[20];\n+static int errors_found;\n+#define ERROR_OBJECT 01\n+#define ERROR_REACHABLE 02\n \n #ifdef NO_D_INO_IN_DIRENT\n #define SORT_DIRENT 0\n@@ -40,6 +43,7 @@ static int objerror(struct object *obj, const char *err, ...)\n {\n \tva_list params;\n \tva_start(params, err);\n+\terrors_found |= ERROR_OBJECT;\n \tobjreport(obj, \"error\", err, params);\n \tva_end(params);\n \treturn -1;\n@@ -70,6 +74,7 @@ static void check_reachable_object(struct object *obj)\n \t\tif (has_sha1_pack(obj->sha1, NULL))\n \t\t\treturn; /* it is in pack - forget about it */\n \t\tprintf(\"missing %s %s\\n\", typename(obj->type), sha1_to_hex(obj->sha1));\n+\t\terrors_found |= ERROR_REACHABLE;\n \t\treturn;\n \t}\n \n@@ -88,6 +93,7 @@ static void check_reachable_object(struct object *obj)\n \t\t\t       typename(obj->type), sha1_to_hex(obj->sha1));\n \t\t\tprintf(\"              to %7s %s\\n\",\n \t\t\t       typename(ref->type), sha1_to_hex(ref->sha1));\n+\t\t\terrors_found |= ERROR_REACHABLE;\n \t\t}\n \t}\n }\n@@ -346,8 +352,11 @@ static int fsck_tag(struct tag *tag)\n static int fsck_sha1(unsigned char *sha1)\n {\n \tstruct object *obj = parse_object(sha1);\n-\tif (!obj)\n-\t\treturn error(\"%s: object corrupt or missing\", sha1_to_hex(sha1));\n+\tif (!obj) {\n+\t\terrors_found |= ERROR_OBJECT;\n+\t\treturn error(\"%s: object corrupt or missing\",\n+\t\t\t     sha1_to_hex(sha1));\n+\t}\n \tif (obj->flags & SEEN)\n \t\treturn 0;\n \tobj->flags |= SEEN;\n@@ -359,8 +368,10 @@ static int fsck_sha1(unsigned char *sha1)\n \t\treturn fsck_commit((struct commit *) obj);\n \tif (obj->type == OBJ_TAG)\n \t\treturn fsck_tag((struct tag *) obj);\n+\n \t/* By now, parse_object() would've returned NULL instead. */\n-\treturn objerror(obj, \"unknown type '%d' (internal fsck error)\", obj->type);\n+\treturn objerror(obj, \"unknown type '%d' (internal fsck error)\",\n+\t\t\tobj->type);\n }\n \n /*\n@@ -576,11 +587,16 @@ static int fsck_cache_tree(struct cache_tree *it)\n \treturn err;\n }\n \n+static const char fsck_usage[] =\n+\"git-fsck [--tags] [--root] [[--unreachable] [--cache] [--full] \"\n+\"[--strict] <head-sha1>*]\";\n+\n int cmd_fsck(int argc, char **argv, const char *prefix)\n {\n \tint i, heads;\n \n \ttrack_object_refs = 1;\n+\terrors_found = 0;\n \n \tfor (i = 1; i < argc; i++) {\n \t\tconst char *arg = argv[i];\n@@ -610,7 +626,7 @@ int cmd_fsck(int argc, char **argv, const char *prefix)\n \t\t\tcontinue;\n \t\t}\n \t\tif (*arg == '-')\n-\t\t\tusage(\"git-fsck [--tags] [--root] [[--unreachable] [--cache] [--full] [--strict] <head-sha1>*]\");\n+\t\t\tusage(fsck_usage);\n \t}\n \n \tfsck_head_link();\n@@ -690,5 +706,5 @@ int cmd_fsck(int argc, char **argv, const char *prefix)\n \t}\n \n \tcheck_connectivity();\n-\treturn 0;\n+\treturn errors_found;\n }\n-- \n1.5.0.3.855.ga3b2\n"}]}