🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.git
1 messages
1 total messages Started by Alejandro Colomar Tue, 17 Nov 2026 17:17
Multi-line git-grep(1): -M
#3249
Author: Alejandro Colomar
Date: Tue, 17 Nov 2026 17:17
146 lines
13148 bytes
--r2qljlfwmz2fslvw
Content-Type: text/plain; protected-headers=v1; charset=utf-8
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
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
Message-ID: <avyInNCE9QlbcV5e@devuan>
MIME-Version: 1.0

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 |=3D 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*[=3D,].*?^\k<space>}[^;]*;
	(?s)^[\w[](?:[\w\s\(,\)[\]*]|::)+[\w\s\)*\]]\s+\**\(?ARRAY_END\)?\s*(?<par=
ams>\((?:[\w\s,;[\]*\?:+-]|(?&params))*(?:\.\.\.)?\))(?:[\w\s\(,\)[\]]|::)*;
	(?s)^[\w[](?:[\w\s\(,\)[\]*]|::)+[\w\s\)*\]]\s+\**\(?ARRAY_END\)?\s*(?<par=
ams>\((?:[\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*(?<sp=
ace>[ \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) / siz=
eof((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

--=20
<https://www.alejandro-colomar.es>

--r2qljlfwmz2fslvw
Content-Type: application/pgp-signature; name="signature.asc"

-----BEGIN PGP SIGNATURE-----

iQIzBAABCgAdFiEES7Jt9u9GbmlWADAi64mZXMKQwqkFAmr8jCcACgkQ64mZXMKQ
wqlStA/7Bz+Bg3ebQRkDhmY2wYfcUX+fVy9wrPjO0+vAk1Xa+7qz1cvPSROLD4iS
1+TNkCdSHIuBaOU1Sa7guRy9D1YcFuKXFaTb0Cp4IY7PQ2ljCFmQEf9QsFjuJ+yO
Z3ML6sAaun6CnE2PvrOT/DN6krj0eRKEQNFLDXqjS9gxwS9y2v13jVTGCJTwZA9h
BzysDKcp6LUlhRuh1iKG6Eu4QthN3fhyL8DiLCPAWKDU4d6wnYFxftSko6HY4bco
JtsiRrfSySZlTIQQ03Oo89RqwfCsL0gNEnaXhbP7G230yW2bj1q4FrnTcFKF0qJo
mTxodrTHXrKlEtxM1l7JEv3t9RcS6oEkNuanXVjIcrYe14uiShiM9dUHPRgzq7ZY
x/NZ4e6CehZubj1EOgxigg2XRiMOHksZA+3EAZ2rd/nDcXWa9/nsmgfM0pK1tz6l
bSrAbpznxRYEq/XLEGge9G2QspfTmZ7LGGTDTvFknGB0rtW2hl8XE/wNljaS7Fth
2vctIE7DKbTyzCw/vIk2PqvJOgq2HWrHCRkl0T4oJFSgA9GAzi/odi0eBkDyP83a
SXO9j1YsxtnbOlQBUokL0ZLLJcd6R6Sra+MZzP7oYpIg8Rj3webbkWGskDnp4y94
22Q3tnQh4oJLnsc+35R4CQztzCe2xCmJ+O/tASRHPf/dRXfbOVs=
=iOyH
-----END PGP SIGNATURE-----

--r2qljlfwmz2fslvw--
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads