{"thread":{"id":"66865","subject":"Multi-line git-grep(1): -M","startedAt":"2026-11-17T17:17:32Z","lastAt":"2026-11-17T17:17:32Z","messageCount":1,"participants":["Alejandro Colomar"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"558632","messageId":"avyInNCE9QlbcV5e@devuan","threadId":"66865","inReplyTo":null,"subject":"Multi-line git-grep(1): -M","fromName":"Alejandro Colomar","fromEmail":"alx+git@kernel.org","sentAt":"2026-11-17T17:17:32Z","receivedAt":"2026-11-17T17:17:32Z","isPatch":false,"sender":{"key":"alx+git@kernel.org","avatar":null},"body":"Hi!\n\npcre2grep(1) is mostly similar to `grep -P`, except that it has a -M\nflag for multi-line searches.  It'd be nice to see such a flag in\ngit-grep(1).\n\nUse case:\n\ngrepc(1) allows one to search for C code.  A couple of examples in the\nLinux kernel repository:\n\n\t$ grepc ARRAY_END include/\n\tinclude/linux/array_size.h:#define ARRAY_END(arr)  (&(arr)[ARRAY_SIZE(arr)])\n\n\t$ grepc -tfld open fs/\n\tfs/open.c:SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)\n\t{\n\t\tif (force_o_largefile())\n\t\t\tflags |= O_LARGEFILE;\n\t\treturn do_sys_open(AT_FDCWD, filename, flags, mode);\n\t}\n\tfs/open.c:COMPAT_SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)\n\t{\n\t\treturn do_sys_open(AT_FDCWD, filename, flags, mode);\n\t}\n\ngrepc(1) uses a helper program to produce the regexes: grepc_c(1).\nHere are the regexes used internally for the above:\n\n\t$ grepc_c ARRAY_END\n\t(?s)^(?:[\\w[](?:[\\w\\s\\(,\\)[\\]*]|::)*[\\w\\s\\)*\\]]\\s+)?\\b(?:enum)\\b[\\w \\t[\\]:]*(?<!\\w)(?!\\w)[ \\t]*\\n*(?<space>[ \\t]*){(?:(?!^\\k<space>?}).)*^[ \\t]*ARRAY_END\\b\\s*[=,].*?^\\k<space>}[^;]*;\n\t(?s)^[\\w[](?:[\\w\\s\\(,\\)[\\]*]|::)+[\\w\\s\\)*\\]]\\s+\\**\\(?ARRAY_END\\)?\\s*(?<params>\\((?:[\\w\\s,;[\\]*\\?:+-]|(?&params))*(?:\\.\\.\\.)?\\))(?:[\\w\\s\\(,\\)[\\]]|::)*;\n\t(?s)^[\\w[](?:[\\w\\s\\(,\\)[\\]*]|::)+[\\w\\s\\)*\\]]\\s+\\**\\(?ARRAY_END\\)?\\s*(?<params>\\((?:[\\w\\s,;[\\]*\\?:+-]|(?&params))*(?:\\.\\.\\.)?\\))[ \\t]*\\n*(?<space>[ \\t]*){.*?^\\k<space>}\n\t(?s)^[\\w[](?:[\\w\\s\\(,\\)[\\]*]|::)+[\\w\\s\\)*\\]]\\s+\\**\\(?(?:compat_)?sys_ARRAY_END\\)?\\s*(?<params>\\((?:[\\w\\s,;[\\]*\\?:+-]|(?&params))*(?:\\.\\.\\.)?\\))(?:[\\w\\s\\(,\\)[\\]]|::)*;\n\t(?s)^(?:COMPAT_)?SYSCALL_DEFINE.\\(ARRAY_END\\b[\\w\\s,;[\\]*\\?:+-]*\\)[ \\t]*\\n*(?<space>[ \\t]*){.*?^\\k<space>}\n\t(?s)^[\\w[](?:[\\w\\s\\(,\\)[\\]*]|::)+[\\w\\s\\)*\\]]\\s+\\**\\(?_IO_ARRAY_END\\)?\\s*(?<params>\\((?:[\\w\\s,;[\\]*\\?:+-]|(?&params))*(?:\\.\\.\\.)?\\))(?:[\\w\\s\\(,\\)[\\]]|::)*;\n\t(?s)^[\\w[](?:[\\w\\s\\(,\\)[\\]*]|::)+[\\w\\s\\)*\\]]\\s+\\**\\(?M_DECL_FUNC \\(__ARRAY_END\\)\\)?\\s*(?<params>\\((?:[\\w\\s,;[\\]*\\?:+-]|(?&params))*(?:\\.\\.\\.)?\\))[ \\t]*\\n*(?<space>[ \\t]*){.*?^\\k<space>}\n\t(?s)^[\\w[](?:[\\w\\s\\(,\\)[\\]*]|::)+[\\w\\s\\)*\\]]\\s+\\**\\(?_IO_ARRAY_END\\)?\\s*(?<params>\\((?:[\\w\\s,;[\\]*\\?:+-]|(?&params))*(?:\\.\\.\\.)?\\))[ \\t]*\\n*(?<space>[ \\t]*){.*?^\\k<space>}\n\t(?s)^[ \\t]*#\\s*define\\s[\\s\\\\]*ARRAY_END\\b\\([^\\(]*\\)(?:(?![^\\\\]$).)*.*?(?<!\\\\)$\n\t(?s)^[ \\t]*#\\s*define\\s[\\s\\\\]*ARRAY_END\\b(?!\\()(?:(?![^\\\\]$).)*.*?(?<!\\\\)$\n\t(?s)^(?:[\\w[](?:[\\w\\s\\(,\\)[\\]*]|::)*[\\w\\s\\)*\\]]\\s+)?\\b(?:struct|union|enum)\\b[\\w \\t[\\]:]*(?<!\\w)ARRAY_END(?!\\w)[ \\t]*\\n*(?<space>[ \\t]*){.*?^\\k<space>}[^;]*;\n\t(?s)^[ \\t]*typedef\\s+[^{};]+\\bARRAY_END;\n\t(?s)^[ \\t]*typedef\\s+(?:struct|union|enum)\\b[\\w \\t[\\]:]*[ \\t]*\\n*(?<space>[ \\t]*){.*?^\\k<space>}\\s*ARRAY_END(?:\\[[\\w\\(,\\)]\\])*;\n\t(?s)^[ \\t]*typedef\\s+[^{};]+\\(\\**ARRAY_END\\)\\s*\\([^{};]+;\n\n\t$ grepc_c -tfld open\n\t(?s)^(?:COMPAT_)?SYSCALL_DEFINE.\\(open\\b[\\w\\s,;[\\]*\\?:+-]*\\)[ \\t]*\\n*(?<space>[ \\t]*){.*?^\\k<space>}\n\nAnd those regexes can actually be used by git-grep(1) with -P (and -f):\n\n\t$ grepc_c -tm ARRAY_END\n\t(?s)^[ \\t]*#\\s*define\\s[\\s\\\\]*ARRAY_END\\b\\([^\\(]*\\)(?:(?![^\\\\]$).)*.*?(?<!\\\\)$\n\t(?s)^[ \\t]*#\\s*define\\s[\\s\\\\]*ARRAY_END\\b(?!\\()(?:(?![^\\\\]$).)*.*?(?<!\\\\)$\n\t$ git grep -P -f <(grepc_c -tm ARRAY_END)\n\tinclude/linux/array_size.h:#define ARRAY_END(arr)  (&(arr)[ARRAY_SIZE(arr)])\n\nThis is interesting to be able to search for code definitions in the\npast:\n\n\t$ git grep -P -f <(grepc_c -tm ARRAY_SIZE) v4.0 -- include/\n\tv4.0:include/linux/kernel.h:#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))\n\t$ git grep -P -f <(grepc_c -tm ARRAY_SIZE) v7.0 -- include/\n\tv7.0:include/linux/array_size.h:#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))\n\nBut it only works for single-line matches.  It'd be great to be able to\nuse these multi-line patterns for searching.\n\nBTW, one can already search in the past, but one needs to know the file\nin which to search (which is possible, if you know the file in the\npresent, and use git-blame(1) to investigate the history of it).\n\n\t$ git show v6.0:include/linux/overflow.h | grepc -tm struct_size\n\t(standard input):#define struct_size(p, member, count)\t\t\t\t\\\n\t\t__builtin_choose_expr(__is_constexpr(count),\t\t\t\\\n\t\t\tsizeof(*(p)) + flex_array_size(p, member, count),\t\\\n\t\t\tsize_add(sizeof(*(p)), flex_array_size(p, member, count)))\n\nDoes this feature make sense to you?\n\n\nHave a lovely day!\nAlex\n\n-- \n<https://www.alejandro-colomar.es>\n"}]}