{"thread":{"id":"7772","subject":"[PATCH 1/3] Cleanup variables in cat-file","startedAt":"2007-04-22T01:14:39Z","lastAt":"2007-04-22T01:14:39Z","messageCount":1,"participants":["Shawn O. Pearce"],"isPatch":true,"patchVersion":1,"patchTotal":3},"messages":[{"id":"40078","messageId":"20070422011439.GA2910@spearce.org","threadId":"7772","inReplyTo":null,"subject":"[PATCH 1/3] Cleanup variables in cat-file","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2007-04-22T01:14:39Z","receivedAt":"2007-04-22T01:14:39Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"I want to add new command line options to cat-file, but\nto do that we need to change how we handle argv[] first.\nThis is a simple cleanup that assigns names to the two\narguments we currently care about.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n builtin-cat-file.c |   30 ++++++++++++++++++------------\n 1 files changed, 18 insertions(+), 12 deletions(-)\n\ndiff --git a/builtin-cat-file.c b/builtin-cat-file.c\nindex d61d3d5..f132d58 100644\n--- a/builtin-cat-file.c\n+++ b/builtin-cat-file.c\n@@ -83,17 +83,21 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)\n \tvoid *buf;\n \tunsigned long size;\n \tint opt;\n+\tconst char *exp_type, *obj_name;\n \n \tgit_config(git_default_config);\n \tif (argc != 3)\n \t\tusage(\"git-cat-file [-t|-s|-e|-p|<type>] <sha1>\");\n-\tif (get_sha1(argv[2], sha1))\n-\t\tdie(\"Not a valid object name %s\", argv[2]);\n+\texp_type = argv[1];\n+\tobj_name = argv[2];\n+\n+\tif (get_sha1(obj_name, sha1))\n+\t\tdie(\"Not a valid object name %s\", obj_name);\n \n \topt = 0;\n-\tif ( argv[1][0] == '-' ) {\n-\t\topt = argv[1][1];\n-\t\tif ( !opt || argv[1][2] )\n+\tif ( exp_type[0] == '-' ) {\n+\t\topt = exp_type[1];\n+\t\tif ( !opt || exp_type[2] )\n \t\t\topt = -1; /* Not a single character option */\n \t}\n \n@@ -121,15 +125,17 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)\n \tcase 'p':\n \t\ttype = sha1_object_info(sha1, NULL);\n \t\tif (type < 0)\n-\t\t\tdie(\"Not a valid object name %s\", argv[2]);\n+\t\t\tdie(\"Not a valid object name %s\", obj_name);\n \n \t\t/* custom pretty-print here */\n-\t\tif (type == OBJ_TREE)\n-\t\t\treturn cmd_ls_tree(2, argv + 1, NULL);\n+\t\tif (type == OBJ_TREE) {\n+\t\t\tconst char *ls_args[3] = {\"ls-tree\", obj_name, NULL};\n+\t\t\treturn cmd_ls_tree(2, ls_args, NULL);\n+\t\t}\n \n \t\tbuf = read_sha1_file(sha1, &type, &size);\n \t\tif (!buf)\n-\t\t\tdie(\"Cannot read object %s\", argv[2]);\n+\t\t\tdie(\"Cannot read object %s\", obj_name);\n \t\tif (type == OBJ_TAG) {\n \t\t\tpprint_tag(sha1, buf, size);\n \t\t\treturn 0;\n@@ -138,15 +144,15 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)\n \t\t/* otherwise just spit out the data */\n \t\tbreak;\n \tcase 0:\n-\t\tbuf = read_object_with_reference(sha1, argv[1], &size, NULL);\n+\t\tbuf = read_object_with_reference(sha1, exp_type, &size, NULL);\n \t\tbreak;\n \n \tdefault:\n-\t\tdie(\"git-cat-file: unknown option: %s\\n\", argv[1]);\n+\t\tdie(\"git-cat-file: unknown option: %s\\n\", exp_type);\n \t}\n \n \tif (!buf)\n-\t\tdie(\"git-cat-file %s: bad file\", argv[2]);\n+\t\tdie(\"git-cat-file %s: bad file\", obj_name);\n \n \twrite_or_die(1, buf, size);\n \treturn 0;\n-- \n1.5.1.1.135.gf948\n"}]}