{"thread":{"id":"2740","subject":"[PATCH] A few more options for git-cat-file","startedAt":"2005-12-04T01:57:48Z","lastAt":"2005-12-04T06:22:51Z","messageCount":3,"participants":["H. Peter Anvin","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"13161","messageId":"43924D1C.8070306@zytor.com","threadId":"2740","inReplyTo":null,"subject":"[PATCH] A few more options for git-cat-file","fromName":"H. Peter Anvin","fromEmail":"hpa@zytor.com","sentAt":"2005-12-04T01:57:48Z","receivedAt":"2005-12-04T01:57:48Z","isPatch":true,"sender":{"key":"hpa@zytor.com","avatar":null},"body":"This adds the following options to git-cat-file:\n\n-n, to get the canonical name of a resource.  This is for one thing \nuseful in tagging scripts.\n\n-e, to test for the existence of a file.\n\nThis also cleans up the option-parsing in git-cat-file slightly.\n\nSigned-off-by: H. Peter Anvin <hpa@zytor.com>\n\n\n\ndiff --git a/Documentation/git-cat-file.txt b/Documentation/git-cat-file.txt\nindex ab4dcae..a59e513 100644\n--- a/Documentation/git-cat-file.txt\n+++ b/Documentation/git-cat-file.txt\n@@ -8,7 +8,7 @@ git-cat-file - Provide content or type i\n \n SYNOPSIS\n --------\n-'git-cat-file' (-t | -s | <type>) <object>\n+'git-cat-file' (-t | -s | -n | -e | <type>) <object>\n \n DESCRIPTION\n -----------\n@@ -29,6 +29,13 @@ OPTIONS\n \tInstead of the content, show the object size identified by\n \t<object>.\n \n+-n::\n+\tInstead of the content, show the canoncical name of <object>.\n+\n+-e::\n+\tSuppress all output; instead exit with zero status if <object>\n+\texists and is a valid object.\n+\n <type>::\n \tTypically this matches the real type of <object> but asking\n \tfor a type that can trivially be dereferenced from the given\n@@ -39,8 +46,14 @@ OPTIONS\n \n OUTPUT\n ------\n-If '-t' is specified, one of the <type>.  If '-s' is specified,\n-the size of the <object> in bytes.\n+If '-t' is specified, one of the <type>.\n+\n+If '-s' is specified, the size of the <object> in bytes.\n+\n+If '-n' is specified, the canoncial name (40-character SHA1\n+hexadecimal string) of the object.\n+\n+If '-e' is specified, no output.\n \n Otherwise the raw (though uncompressed) contents of the <object> will\n be returned.\ndiff --git a/cat-file.c b/cat-file.c\nindex d775a15..23fbd28 100644\n--- a/cat-file.c\n+++ b/cat-file.c\n@@ -11,27 +11,48 @@ int main(int argc, char **argv)\n \tchar type[20];\n \tvoid *buf;\n \tunsigned long size;\n+\tint opt;\n \n \tsetup_git_directory();\n \tif (argc != 3 || get_sha1(argv[2], sha1))\n-\t\tusage(\"git-cat-file [-t | -s | <type>] <sha1>\");\n+\t\tusage(\"git-cat-file [-t|-s|-n|-e|<type>] <sha1>\");\n \n-\tif (!strcmp(\"-t\", argv[1]) || !strcmp(\"-s\", argv[1])) {\n-\t\tif (!sha1_object_info(sha1, type,\n-\t\t\t\t      argv[1][1] == 's' ? &size : NULL)) {\n-\t\t\tswitch (argv[1][1]) {\n-\t\t\tcase 't':\n-\t\t\t\tprintf(\"%s\\n\", type);\n-\t\t\t\tbreak;\n-\t\t\tcase 's':\n-\t\t\t\tprintf(\"%lu\\n\", size);\n-\t\t\t\tbreak;\n-\t\t\t}\n+\topt = 0;\n+\tif ( argv[1][0] == '-' ) {\n+\t\topt = argv[1][1];\n+\t\tif ( !opt || argv[1][2] )\n+\t\t\topt = -1; /* Not a single character option */\n+\t}\n+\n+\tbuf = NULL;\n+\tswitch (opt) {\n+\tcase 'n':\n+\t\tprintf(\"%s\\n\", sha1_to_hex(sha1));\n+\t\treturn 0;\n+\n+\tcase 't':\n+\t\tif (!sha1_object_info(sha1, type, NULL)) {\n+\t\t\tprintf(\"%s\\n\", type);\n \t\t\treturn 0;\n \t\t}\n-\t\tbuf = NULL;\n-\t} else {\n+\t\tbreak;\n+\n+\tcase 's':\n+\t\tif (!sha1_object_info(sha1, type, &size)) {\n+\t\t\tprintf(\"%lu\\n\", size);\n+\t\t\treturn 0;\n+\t\t}\n+\t\tbreak;\n+\n+\tcase 'e':\n+\t\treturn !has_sha1_file(sha1);\n+\n+\tcase 0:\n \t\tbuf = read_object_with_reference(sha1, argv[1], &size, NULL);\n+\t\tbreak;\n+\n+\tdefault:\n+\t\tdie(\"git-cat-file: unknown option: %s\\n\", argv[1]);\n \t}\n \n \tif (!buf)\n"},{"id":"13167","messageId":"7vu0dptmh3.fsf@assigned-by-dhcp.cox.net","threadId":"2740","inReplyTo":"43924D1C.8070306@zytor.com","subject":"Re: [PATCH] A few more options for git-cat-file","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2005-12-04T06:17:28Z","receivedAt":"2005-12-04T06:17:28Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"H. Peter Anvin\" <hpa@zytor.com> writes:\n\n> This adds the following options to git-cat-file:\n>\n> -n, to get the canonical name of a resource.  This is for one thing \n> useful in tagging scripts.\n\nIsn't \"git-rev-parse --verify\" good enough?\n\n> -e, to test for the existence of a file.\n\nThis might be useful and cleaner than \"cat-file -s >/dev/null\"\nbut marginally so.  While reading the whole file and discarding\nthat to /dev/null is wasteful and would go against tastes of\nmany people, -t and -s flags do not need to read the whole thing\nand not so expensive.\n"},{"id":"13168","messageId":"43928B3B.90200@zytor.com","threadId":"2740","inReplyTo":"7vu0dptmh3.fsf@assigned-by-dhcp.cox.net","subject":"Re: [PATCH] A few more options for git-cat-file","fromName":"H. Peter Anvin","fromEmail":"hpa@zytor.com","sentAt":"2005-12-04T06:22:51Z","receivedAt":"2005-12-04T06:22:51Z","isPatch":true,"sender":{"key":"hpa@zytor.com","avatar":null},"body":"Junio C Hamano wrote:\n> \"H. Peter Anvin\" <hpa@zytor.com> writes:\n> \n> \n>>This adds the following options to git-cat-file:\n>>\n>>-n, to get the canonical name of a resource.  This is for one thing \n>>useful in tagging scripts.\n> \n> Isn't \"git-rev-parse --verify\" good enough?\n> \n\nAh, yes.  Except for the fact that I couldn't find it when I looked for \na command that did that.  git-rev-parse seems like an eclectic mixture \nof stuff, and the name isn't very clear as to what it does.\n\n> \n>>-e, to test for the existence of a file.\n> \n> This might be useful and cleaner than \"cat-file -s >/dev/null\"\n> but marginally so.  While reading the whole file and discarding\n> that to /dev/null is wasteful and would go against tastes of\n> many people, -t and -s flags do not need to read the whole thing\n> and not so expensive.\n\nStill, -e as implemented here is definitely cheaper.\n\n\t-hpa\n"}]}