# Multi-line git-grep(1): -M

1 messages from 2026-11-17 to 2026-11-17. Participants: Alejandro Colomar.
Thread: https://gitlist.dev/t/66865

## Alejandro Colomar, 2026-11-17 17:17

Subject: Multi-line git-grep(1): -M
Message-ID: <avyInNCE9QlbcV5e@devuan>
URL: https://gitlist.dev/e/avyInNCE9QlbcV5e%40devuan

```
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,;[\]*\?:+-]|(?&params))*(?:\.\.\.)?\))(?:[\w\s\(,\)[\]]|::)*;
	(?s)^[\w[](?:[\w\s\(,\)[\]*]|::)+[\w\s\)*\]]\s+\**\(?ARRAY_END\)?\s*(?<params>\((?:[\w\s,;[\]*\?:+-]|(?&params))*(?:\.\.\.)?\))[ \t]*\n*(?<space>[ \t]*){.*?^\k<space>}
	(?s)^[\w[](?:[\w\s\(,\)[\]*]|::)+[\w\s\)*\]]\s+\**\(?(?:compat_)?sys_ARRAY_END\)?\s*(?<params>\((?:[\w\s,;[\]*\?:+-]|(?&params))*(?:\.\.\.)?\))(?:[\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,;[\]*\?:+-]|(?&params))*(?:\.\.\.)?\))(?:[\w\s\(,\)[\]]|::)*;
	(?s)^[\w[](?:[\w\s\(,\)[\]*]|::)+[\w\s\)*\]]\s+\**\(?M_DECL_FUNC \(__ARRAY_END\)\)?\s*(?<params>\((?:[\w\s,;[\]*\?:+-]|(?&params))*(?:\.\.\.)?\))[ \t]*\n*(?<space>[ \t]*){.*?^\k<space>}
	(?s)^[\w[](?:[\w\s\(,\)[\]*]|::)+[\w\s\)*\]]\s+\**\(?_IO_ARRAY_END\)?\s*(?<params>\((?:[\w\s,;[\]*\?:+-]|(?&params))*(?:\.\.\.)?\))[ \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>

```
