{"thread":{"id":"26921","subject":"[PATCH] unpack-objects: migrate to parse-options","startedAt":"2011-03-30T08:37:26Z","lastAt":"2011-03-30T08:37:26Z","messageCount":1,"participants":["Stephen Boyd"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"164668","messageId":"1301474246-7515-1-git-send-email-bebarino@gmail.com","threadId":"26921","inReplyTo":null,"subject":"[PATCH] unpack-objects: migrate to parse-options","fromName":"Stephen Boyd","fromEmail":"bebarino@gmail.com","sentAt":"2011-03-30T08:37:26Z","receivedAt":"2011-03-30T08:37:26Z","isPatch":true,"sender":{"key":"bebarino@gmail.com","avatar":"https://avatars.githubusercontent.com/u/38832?v=4"},"body":"Signed-off-by: Stephen Boyd <bebarino@gmail.com>\n---\n builtin/unpack-objects.c |   80 ++++++++++++++++++++++------------------------\n 1 files changed, 38 insertions(+), 42 deletions(-)\n\ndiff --git a/builtin/unpack-objects.c b/builtin/unpack-objects.c\nindex f63973c..183a967 100644\n--- a/builtin/unpack-objects.c\n+++ b/builtin/unpack-objects.c\n@@ -11,9 +11,13 @@\n #include \"progress.h\"\n #include \"decorate.h\"\n #include \"fsck.h\"\n+#include \"parse-options.h\"\n \n static int dry_run, quiet, recover, has_errors, strict;\n-static const char unpack_usage[] = \"git unpack-objects [-n] [-q] [-r] [--strict] < pack-file\";\n+static const char * const unpack_usage[] = {\n+\t\"git unpack-objects [options] < pack-file\",\n+\tNULL\n+};\n \n /* We always read in 4kB chunks. */\n static unsigned char buffer[4096];\n@@ -492,10 +496,38 @@ static void unpack_all(void)\n \t\tdie(\"unresolved deltas left after unpacking\");\n }\n \n+static int pack_header_cb(const struct option *opt, const char *arg, int unset)\n+{\n+\tstruct pack_header *hdr;\n+\tchar *c;\n+\n+\thdr = (struct pack_header *)buffer;\n+\thdr->hdr_signature = htonl(PACK_SIGNATURE);\n+\thdr->hdr_version = htonl(strtoul(arg, &c, 10));\n+\tif (*c != ',')\n+\t\tdie(\"bad %s\", arg);\n+\thdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));\n+\tif (*c)\n+\t\tdie(\"bad %s\", arg);\n+\tlen = sizeof(*hdr);\n+\treturn 0;\n+}\n+\n int cmd_unpack_objects(int argc, const char **argv, const char *prefix)\n {\n-\tint i;\n \tunsigned char sha1[20];\n+\tconst struct option unpack_options[] = {\n+\t\tOPT__DRY_RUN(&dry_run,\n+\t\t\t    \"check the pack file but don't actually unpack it\"),\n+\t\tOPT__QUIET(&quiet, \"suppress percentage progress\"),\n+\t\tOPT_BOOLEAN('r', NULL, &recover, \"ignore corrupt packfiles\"),\n+\t\tOPT_BOOLEAN(0, \"strict\", &strict,\n+\t\t\t    \"don't write objects with broken content or links\"),\n+\t\t{ OPTION_CALLBACK, 0, \"pack_header\", NULL, \"<ver>,<entries>\",\n+\t\t  \"internal option\", PARSE_OPT_HIDDEN | PARSE_OPT_NONEG |\n+\t\t  PARSE_OPT_LITERAL_ARGHELP, pack_header_cb },\n+\t\tOPT_END()\n+\t};\n \n \tread_replace_refs = 0;\n \n@@ -503,47 +535,11 @@ int cmd_unpack_objects(int argc, const char **argv, const char *prefix)\n \n \tquiet = !isatty(2);\n \n-\tfor (i = 1 ; i < argc; i++) {\n-\t\tconst char *arg = argv[i];\n-\n-\t\tif (*arg == '-') {\n-\t\t\tif (!strcmp(arg, \"-n\")) {\n-\t\t\t\tdry_run = 1;\n-\t\t\t\tcontinue;\n-\t\t\t}\n-\t\t\tif (!strcmp(arg, \"-q\")) {\n-\t\t\t\tquiet = 1;\n-\t\t\t\tcontinue;\n-\t\t\t}\n-\t\t\tif (!strcmp(arg, \"-r\")) {\n-\t\t\t\trecover = 1;\n-\t\t\t\tcontinue;\n-\t\t\t}\n-\t\t\tif (!strcmp(arg, \"--strict\")) {\n-\t\t\t\tstrict = 1;\n-\t\t\t\tcontinue;\n-\t\t\t}\n-\t\t\tif (!prefixcmp(arg, \"--pack_header=\")) {\n-\t\t\t\tstruct pack_header *hdr;\n-\t\t\t\tchar *c;\n-\n-\t\t\t\thdr = (struct pack_header *)buffer;\n-\t\t\t\thdr->hdr_signature = htonl(PACK_SIGNATURE);\n-\t\t\t\thdr->hdr_version = htonl(strtoul(arg + 14, &c, 10));\n-\t\t\t\tif (*c != ',')\n-\t\t\t\t\tdie(\"bad %s\", arg);\n-\t\t\t\thdr->hdr_entries = htonl(strtoul(c + 1, &c, 10));\n-\t\t\t\tif (*c)\n-\t\t\t\t\tdie(\"bad %s\", arg);\n-\t\t\t\tlen = sizeof(*hdr);\n-\t\t\t\tcontinue;\n-\t\t\t}\n-\t\t\tusage(unpack_usage);\n-\t\t}\n-\n+\targc = parse_options(argc, argv, prefix, unpack_options,\n+\t\t\t     unpack_usage, 0);\n+\tif (argc)\n \t\t/* We don't take any non-flag arguments now.. Maybe some day */\n-\t\tusage(unpack_usage);\n-\t}\n+\t\tusage_with_options(unpack_usage, unpack_options);\n \tgit_SHA1_Init(&ctx);\n \tunpack_all();\n \tgit_SHA1_Update(&ctx, buffer, offset);\n-- \n1.7.4.2.422.g537d99\n"}]}