{"thread":{"id":"7774","subject":"[PATCH 3/3] Teach cat-file a --quiet option","startedAt":"2007-04-22T01:14:47Z","lastAt":"2007-04-22T09:20:47Z","messageCount":6,"participants":["Shawn O. Pearce","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":3},"messages":[{"id":"40080","messageId":"20070422011447.GC2910@spearce.org","threadId":"7774","inReplyTo":null,"subject":"[PATCH 3/3] Teach cat-file a --quiet option","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2007-04-22T01:14:47Z","receivedAt":"2007-04-22T01:14:47Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Sometimes when you get the content of a file in a script you don't\nwant an error message for missing files; instead its OK to treat\na missing file the same as one whose content was empty.\n\nThis is especially true if the script is using something like\n`cat-file blob HEAD:path/to/file` to look at an optional file's\ncontent.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n Documentation/git-cat-file.txt |    4 ++++\n builtin-cat-file.c             |   26 ++++++++++++++++++++------\n 2 files changed, 24 insertions(+), 6 deletions(-)\n\ndiff --git a/Documentation/git-cat-file.txt b/Documentation/git-cat-file.txt\nindex 075c0d0..f6edb1a 100644\n--- a/Documentation/git-cat-file.txt\n+++ b/Documentation/git-cat-file.txt\n@@ -46,6 +46,10 @@ OPTIONS\n \tor to ask for a \"blob\" with <object> being a tag object that\n \tpoints at it.\n \n+--quiet::\n+\tDon't print an error message if the object doesn't exist;\n+\tinstead exit with non-zero status like -e.\n+\n OUTPUT\n ------\n If '-t' is specified, one of the <type>.\ndiff --git a/builtin-cat-file.c b/builtin-cat-file.c\nindex b2437fe..8807a61 100644\n--- a/builtin-cat-file.c\n+++ b/builtin-cat-file.c\n@@ -82,13 +82,15 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)\n \tenum object_type type;\n \tvoid *buf;\n \tunsigned long size;\n-\tint opt = 0, i;\n+\tint opt = 0, quiet = 0, i;\n \tconst char *exp_type = NULL, *obj_name = NULL;\n \n \tgit_config(git_default_config);\n \tfor (i = 1; i < argc; i++) {\n \t\tconst char *a = argv[i];\n-\t\tif (!opt && a[0] == '-' && a[1])\n+\t\tif (!strcmp(a, \"--quiet\"))\n+\t\t\tquiet = 1;\n+\t\telse if (!opt && a[0] == '-' && a[1])\n \t\t\topt = a[1];\n \t\telse if (!opt && !exp_type)\n \t\t\texp_type = a;\n@@ -99,8 +101,11 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)\n \t}\n \tif (!obj_name)\n \t\tusage(\"git-cat-file [-t|-s|-e|-p|<type>] <sha1>\");\n-\tif (get_sha1(obj_name, sha1))\n+\tif (get_sha1(obj_name, sha1)) {\n+\t\tif (quiet || opt == 'e')\n+\t\t\treturn 1;\n \t\tdie(\"Not a valid object name %s\", obj_name);\n+\t}\n \n \tbuf = NULL;\n \tswitch (opt) {\n@@ -125,8 +130,11 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)\n \n \tcase 'p':\n \t\ttype = sha1_object_info(sha1, NULL);\n-\t\tif (type < 0)\n+\t\tif (type < 0) {\n+\t\t\tif (quiet)\n+\t\t\t\treturn 1;\n \t\t\tdie(\"Not a valid object name %s\", obj_name);\n+\t\t}\n \n \t\t/* custom pretty-print here */\n \t\tif (type == OBJ_TREE) {\n@@ -135,8 +143,11 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)\n \t\t}\n \n \t\tbuf = read_sha1_file(sha1, &type, &size);\n-\t\tif (!buf)\n+\t\tif (!buf) {\n+\t\t\tif (quiet)\n+\t\t\t\treturn 1;\n \t\t\tdie(\"Cannot read object %s\", obj_name);\n+\t\t}\n \t\tif (type == OBJ_TAG) {\n \t\t\tpprint_tag(sha1, buf, size);\n \t\t\treturn 0;\n@@ -152,8 +163,11 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)\n \t\tdie(\"git-cat-file: unknown option: %s\\n\", exp_type);\n \t}\n \n-\tif (!buf)\n+\tif (!buf) {\n+\t\tif (quiet)\n+\t\t\treturn 1;\n \t\tdie(\"git-cat-file %s: bad file\", obj_name);\n+\t}\n \n \twrite_or_die(1, buf, size);\n \treturn 0;\n-- \n1.5.1.1.135.gf948\n"},{"id":"40102","messageId":"7virboq1tx.fsf@assigned-by-dhcp.cox.net","threadId":"7774","inReplyTo":"20070422011447.GC2910@spearce.org","subject":"Re: [PATCH 3/3] Teach cat-file a --quiet option","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2007-04-22T07:56:42Z","receivedAt":"2007-04-22T07:56:42Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Shawn O. Pearce\" <spearce@spearce.org> writes:\n\n> Sometimes when you get the content of a file in a script you don't\n> want an error message for missing files; instead its OK to treat\n> a missing file the same as one whose content was empty.\n>\n> This is especially true if the script is using something like\n> `cat-file blob HEAD:path/to/file` to look at an optional file's\n> content.\n\nI am not sure if I agree with this logic.  How is this different\nfrom discarding stderr to /dev/null?\n"},{"id":"40103","messageId":"20070422081106.GI17480@spearce.org","threadId":"7774","inReplyTo":"7virboq1tx.fsf@assigned-by-dhcp.cox.net","subject":"Re: [PATCH 3/3] Teach cat-file a --quiet option","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2007-04-22T08:11:06Z","receivedAt":"2007-04-22T08:11:06Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Junio C Hamano <junkio@cox.net> wrote:\n> \"Shawn O. Pearce\" <spearce@spearce.org> writes:\n> \n> > Sometimes when you get the content of a file in a script you don't\n> > want an error message for missing files; instead its OK to treat\n> > a missing file the same as one whose content was empty.\n...\n> I am not sure if I agree with this logic.  How is this different\n> from discarding stderr to /dev/null?\n \nIts not any different.  But it means I can do:\n\n\topen(I,'-|','git','cat-file','--quiet','blob',\"HEAD:users/$who\");\n\nand not worry about redirection to silence the case of when $who\nis not in the users subtree of HEAD.  Sure, I could redirect that,\nbut then that's something more like:\n\n\tif (open(I,'-|')) {\n\t\topen STDERR, \">/dev/null\";\n\t\texec 'git','cat-file','--quiet','blob',\"HEAD:users/$who\";\n\t\texit 1;\n\t}\n\nand uh, why, that's really annoying.  And my Perl is rusty enough\nthat I'm not even sure I did that right, I'd have to go look it\nup danngit.  And didn't we just add a --quiet to git-diff?  This is\ndifferent, but not that much different..\n\nWhat's also annoying is cat-file today prints an error with -e if\nyou use the \"branch:path\" syntax, but not if you supply the 40 byte\nhex SHA-1 of the blob in question.  Again, you have to redirect\nthe one syntax, but not the other, even for just a simple -e.\nWhich makes -e slightly less useful.  So I also fixed that...\n\nAnyway... if you really don't like it, drop it, I'll just have to\ngo digging through the Perl manual...  ;-)\n\n-- \nShawn.\n"},{"id":"40104","messageId":"20070422081556.GJ17480@spearce.org","threadId":"7774","inReplyTo":"7virboq1tx.fsf@assigned-by-dhcp.cox.net","subject":"Re: [PATCH 3/3] Teach cat-file a --quiet option","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2007-04-22T08:15:56Z","receivedAt":"2007-04-22T08:15:56Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Junio C Hamano <junkio@cox.net> wrote:\n> \"Shawn O. Pearce\" <spearce@spearce.org> writes:\n> > This is especially true if the script is using something like\n> > `cat-file blob HEAD:path/to/file` to look at an optional file's\n> > content.\n> \n> I am not sure if I agree with this logic.  How is this different\n> from discarding stderr to /dev/null?\n\nAlso, a redirect of stderr to /dev/null kills other error messages\nthat might be interesting to see on stderr of the caller, like if\na packfile isn't readable...  while my --quiet patch doesn't.j The\n--quiet flag only kills errors that occurred because the requested\nobject doesn't exist.\n\n-- \nShawn.\n"},{"id":"40105","messageId":"7vd51wpxz2.fsf@assigned-by-dhcp.cox.net","threadId":"7774","inReplyTo":"20070422081106.GI17480@spearce.org","subject":"Re: [PATCH 3/3] Teach cat-file a --quiet option","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2007-04-22T09:20:01Z","receivedAt":"2007-04-22T09:20:01Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Shawn O. Pearce\" <spearce@spearce.org> writes:\n\n> What's also annoying is cat-file today prints an error with -e if\n> you use the \"branch:path\" syntax, but not if you supply the 40 byte\n> hex SHA-1 of the blob in question.\n\nI _think_ the intent of -e or any other \"silently check\" option\nis to signal \"status\" with exit code, while still indicating an\nerror with the message.\n\nFor the particular example of \"cat-file -e\", whose purpose is to\naccept an object name string and report if such an object exists\nin the repository:\n\n\t$ git cat-file -e 45354a57ee7e3e42c7137db6c94fa968c6babe8d; echo $?\n        0\n\t$ git cat-file -e 45354a57ee7e3e42c7137db6c94fa968c6babe8e; echo $?\n        1\n\t$ git cat-file -e HEAD:NO-SUCH-PATH\n\tfatal: Not a valid object name HEAD:NO-SUCH-PATH\n\nThe input of the above two _look_ like valid object names and\nthey return Ok/Bad because the user is asking \"is there such an\nobject?\".\n\nTechnically speaking, with the last case, the user did not give\na correctly formatted object name (treeish colon needs to be\nfollowed by a path that exists in the treeish to be considered a\nvalid object name), so it can be argued that complaining is the\nright thing to do, but that is too literal to the law and I\nwould agree that it probably is on the wrong side of the\nborderline.\n"},{"id":"40106","messageId":"7v8xckpxxs.fsf@assigned-by-dhcp.cox.net","threadId":"7774","inReplyTo":"20070422081556.GJ17480@spearce.org","subject":"Re: [PATCH 3/3] Teach cat-file a --quiet option","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2007-04-22T09:20:47Z","receivedAt":"2007-04-22T09:20:47Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Shawn O. Pearce\" <spearce@spearce.org> writes:\n\n> Junio C Hamano <junkio@cox.net> wrote:\n>> \"Shawn O. Pearce\" <spearce@spearce.org> writes:\n>> > This is especially true if the script is using something like\n>> > `cat-file blob HEAD:path/to/file` to look at an optional file's\n>> > content.\n>> \n>> I am not sure if I agree with this logic.  How is this different\n>> from discarding stderr to /dev/null?\n>\n> Also, a redirect of stderr to /dev/null kills other error messages\n> that might be interesting to see on stderr of the caller,...\n\nI think that quite depends on what your definition of \"other\"\nis; see my other message on this subject.\n"}]}