{"thread":{"id":"5562","subject":"Add \"-h/-H\" parsing to \"git grep\"","startedAt":"2006-09-14T17:45:12Z","lastAt":"2006-09-14T17:45:12Z","messageCount":1,"participants":["Linus Torvalds"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"26905","messageId":"Pine.LNX.4.64.0609141039380.4388@g5.osdl.org","threadId":"5562","inReplyTo":null,"subject":"Add \"-h/-H\" parsing to \"git grep\"","fromName":"Linus Torvalds","fromEmail":"torvalds@osdl.org","sentAt":"2006-09-14T17:45:12Z","receivedAt":"2006-09-14T17:45:12Z","isPatch":false,"sender":{"key":"torvalds@linux-foundation.org","avatar":"https://avatars.githubusercontent.com/u/1024025?v=4"},"body":"\nIt turns out that I actually wanted to avoid the filenames (because I \ndidn't care - I just wanted to see the context in which something was \nused) when doing a grep. But since \"git grep\" didn't take the \"-h\" \nparameter, I ended up having to do \"grep -5 -h *.c\" instead.\n\nSo here's a trivial patch that adds \"-h\" (and thus has to enable -H too) \nto \"git grep\" parsing.\n\nSigned-off-by: Linus Torvalds <torvalds@osdl.org>\n---\n\nI just verified that it does the same thing as GNU grep with some of the \ncombinations like \"-l -h\" and \"-h -n\", but I didn't bother digging any \ndeeper. Regardless, it doesn't change any behaviour unless people start \nusing \"-h\", and when they do, it's at least no worse than the old \nbehaviour (which was to say \"I can't do that, Dave\" or something).\n\ndiff --git a/builtin-grep.c b/builtin-grep.c\nindex 6430f6d..ed87a55 100644\n--- a/builtin-grep.c\n+++ b/builtin-grep.c\n@@ -138,6 +138,7 @@ #define GREP_BINARY_TEXT\t2\n \tunsigned binary:2;\n \tunsigned extended:1;\n \tunsigned relative:1;\n+\tunsigned pathname:1;\n \tint regflags;\n \tunsigned pre_context;\n \tunsigned post_context;\n@@ -316,7 +317,8 @@ static int word_char(char ch)\n static void show_line(struct grep_opt *opt, const char *bol, const char *eol,\n \t\t      const char *name, unsigned lno, char sign)\n {\n-\tprintf(\"%s%c\", name, sign);\n+\tif (opt->pathname)\n+\t\tprintf(\"%s%c\", name, sign);\n \tif (opt->linenum)\n \t\tprintf(\"%d%c\", lno, sign);\n \tprintf(\"%.*s\\n\", (int)(eol-bol), bol);\n@@ -691,6 +693,8 @@ static int external_grep(struct grep_opt\n \t\tpush_arg(\"-F\");\n \tif (opt->linenum)\n \t\tpush_arg(\"-n\");\n+\tif (!opt->pathname)\n+\t\tpush_arg(\"-h\");\n \tif (opt->regflags & REG_EXTENDED)\n \t\tpush_arg(\"-E\");\n \tif (opt->regflags & REG_ICASE)\n@@ -911,6 +915,7 @@ int cmd_grep(int argc, const char **argv\n \tmemset(&opt, 0, sizeof(opt));\n \topt.prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;\n \topt.relative = 1;\n+\topt.pathname = 1;\n \topt.pattern_tail = &opt.pattern_list;\n \topt.regflags = REG_NEWLINE;\n \n@@ -970,10 +975,12 @@ int cmd_grep(int argc, const char **argv\n \t\t\topt.linenum = 1;\n \t\t\tcontinue;\n \t\t}\n+\t\tif (!strcmp(\"-h\", arg)) {\n+\t\t\topt.pathname = 0;\n+\t\t\tcontinue;\n+\t\t}\n \t\tif (!strcmp(\"-H\", arg)) {\n-\t\t\t/* We always show the pathname, so this\n-\t\t\t * is a noop.\n-\t\t\t */\n+\t\t\topt.pathname = 1;\n \t\t\tcontinue;\n \t\t}\n \t\tif (!strcmp(\"-l\", arg) ||\n"}]}