From: Alejandro Colomar <alx+git@kernel.org>
To: git@vger.kernel.org
Cc: Steffen Nurpmeso <steffen@sdaoden.eu>
Subject: Multi-line git-grep(1): -M
Date: Tue, 17 Nov 2026 18:17:32 +0100 [thread overview]
Message-ID: <avyInNCE9QlbcV5e@devuan> (raw)
[-- Attachment #1: Type: text/plain, Size: 4469 bytes --]
Hi!
pcre2grep(1) is mostly similar to `grep -P`, except that it has a -M
flag for multi-line searches. It'd be nice to see such a flag in
git-grep(1).
Use case:
grepc(1) allows one to search for C code. A couple of examples in the
Linux kernel repository:
$ grepc ARRAY_END include/
include/linux/array_size.h:#define ARRAY_END(arr) (&(arr)[ARRAY_SIZE(arr)])
$ grepc -tfld open fs/
fs/open.c:SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
{
if (force_o_largefile())
flags |= O_LARGEFILE;
return do_sys_open(AT_FDCWD, filename, flags, mode);
}
fs/open.c:COMPAT_SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, umode_t, mode)
{
return do_sys_open(AT_FDCWD, filename, flags, mode);
}
grepc(1) uses a helper program to produce the regexes: grepc_c(1).
Here are the regexes used internally for the above:
$ grepc_c ARRAY_END
(?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>}[^;]*;
(?s)^[\w[](?:[\w\s\(,\)[\]*]|::)+[\w\s\)*\]]\s+\**\(?ARRAY_END\)?\s*(?<params>\((?:[\w\s,;[\]*\?:+-]|(?¶ms))*(?:\.\.\.)?\))(?:[\w\s\(,\)[\]]|::)*;
(?s)^[\w[](?:[\w\s\(,\)[\]*]|::)+[\w\s\)*\]]\s+\**\(?ARRAY_END\)?\s*(?<params>\((?:[\w\s,;[\]*\?:+-]|(?¶ms))*(?:\.\.\.)?\))[ \t]*\n*(?<space>[ \t]*){.*?^\k<space>}
(?s)^[\w[](?:[\w\s\(,\)[\]*]|::)+[\w\s\)*\]]\s+\**\(?(?:compat_)?sys_ARRAY_END\)?\s*(?<params>\((?:[\w\s,;[\]*\?:+-]|(?¶ms))*(?:\.\.\.)?\))(?:[\w\s\(,\)[\]]|::)*;
(?s)^(?:COMPAT_)?SYSCALL_DEFINE.\(ARRAY_END\b[\w\s,;[\]*\?:+-]*\)[ \t]*\n*(?<space>[ \t]*){.*?^\k<space>}
(?s)^[\w[](?:[\w\s\(,\)[\]*]|::)+[\w\s\)*\]]\s+\**\(?_IO_ARRAY_END\)?\s*(?<params>\((?:[\w\s,;[\]*\?:+-]|(?¶ms))*(?:\.\.\.)?\))(?:[\w\s\(,\)[\]]|::)*;
(?s)^[\w[](?:[\w\s\(,\)[\]*]|::)+[\w\s\)*\]]\s+\**\(?M_DECL_FUNC \(__ARRAY_END\)\)?\s*(?<params>\((?:[\w\s,;[\]*\?:+-]|(?¶ms))*(?:\.\.\.)?\))[ \t]*\n*(?<space>[ \t]*){.*?^\k<space>}
(?s)^[\w[](?:[\w\s\(,\)[\]*]|::)+[\w\s\)*\]]\s+\**\(?_IO_ARRAY_END\)?\s*(?<params>\((?:[\w\s,;[\]*\?:+-]|(?¶ms))*(?:\.\.\.)?\))[ \t]*\n*(?<space>[ \t]*){.*?^\k<space>}
(?s)^[ \t]*#\s*define\s[\s\\]*ARRAY_END\b\([^\(]*\)(?:(?![^\\]$).)*.*?(?<!\\)$
(?s)^[ \t]*#\s*define\s[\s\\]*ARRAY_END\b(?!\()(?:(?![^\\]$).)*.*?(?<!\\)$
(?s)^(?:[\w[](?:[\w\s\(,\)[\]*]|::)*[\w\s\)*\]]\s+)?\b(?:struct|union|enum)\b[\w \t[\]:]*(?<!\w)ARRAY_END(?!\w)[ \t]*\n*(?<space>[ \t]*){.*?^\k<space>}[^;]*;
(?s)^[ \t]*typedef\s+[^{};]+\bARRAY_END;
(?s)^[ \t]*typedef\s+(?:struct|union|enum)\b[\w \t[\]:]*[ \t]*\n*(?<space>[ \t]*){.*?^\k<space>}\s*ARRAY_END(?:\[[\w\(,\)]\])*;
(?s)^[ \t]*typedef\s+[^{};]+\(\**ARRAY_END\)\s*\([^{};]+;
$ grepc_c -tfld open
(?s)^(?:COMPAT_)?SYSCALL_DEFINE.\(open\b[\w\s,;[\]*\?:+-]*\)[ \t]*\n*(?<space>[ \t]*){.*?^\k<space>}
And those regexes can actually be used by git-grep(1) with -P (and -f):
$ grepc_c -tm ARRAY_END
(?s)^[ \t]*#\s*define\s[\s\\]*ARRAY_END\b\([^\(]*\)(?:(?![^\\]$).)*.*?(?<!\\)$
(?s)^[ \t]*#\s*define\s[\s\\]*ARRAY_END\b(?!\()(?:(?![^\\]$).)*.*?(?<!\\)$
$ git grep -P -f <(grepc_c -tm ARRAY_END)
include/linux/array_size.h:#define ARRAY_END(arr) (&(arr)[ARRAY_SIZE(arr)])
This is interesting to be able to search for code definitions in the
past:
$ git grep -P -f <(grepc_c -tm ARRAY_SIZE) v4.0 -- include/
v4.0:include/linux/kernel.h:#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
$ git grep -P -f <(grepc_c -tm ARRAY_SIZE) v7.0 -- include/
v7.0:include/linux/array_size.h:#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
But it only works for single-line matches. It'd be great to be able to
use these multi-line patterns for searching.
BTW, one can already search in the past, but one needs to know the file
in which to search (which is possible, if you know the file in the
present, and use git-blame(1) to investigate the history of it).
$ git show v6.0:include/linux/overflow.h | grepc -tm struct_size
(standard input):#define struct_size(p, member, count) \
__builtin_choose_expr(__is_constexpr(count), \
sizeof(*(p)) + flex_array_size(p, member, count), \
size_add(sizeof(*(p)), flex_array_size(p, member, count)))
Does this feature make sense to you?
Have a lovely day!
Alex
--
<https://www.alejandro-colomar.es>
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
reply other threads:[~2026-11-17 17:17 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=avyInNCE9QlbcV5e@devuan \
--to=alx+git@kernel.org \
--cc=git@vger.kernel.org \
--cc=steffen@sdaoden.eu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox