Thread View: linux.kernel.git
6 messages
6 total messages
Started by Alejandro Colomar
Fri, 06 Nov 2026 13:35
git-ls-files(1) avoiding symlinks
Author: Alejandro Colomar
Date: Fri, 06 Nov 2026 13:35
Date: Fri, 06 Nov 2026 13:35
88 lines
10577 bytes
10577 bytes
--7via6c44zxtslhjm
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: Andy Shevchenko <andy@kernel.org>,
Linus Torvalds <torvalds@linux-foundation.org>, Krzysztof Kozlowski <krzk@kernel.org>
Subject: git-ls-files(1) avoiding symlinks
Message-ID: <au3Tyaz_LoYDuskn@devuan>
MIME-Version: 1.0
Hi!
Yesterday, we were discussing alternative methods for grepping source
code. Here's a list of similar alternatives:
$ find . -type f | xargs grep ARRAY_END;
$ git ls-files | xargs grep ARRAY_END;
$ git grep ARRAY_END;
The version using find(1) has some problems: it is slow, especially
after having built a kernel, when the directory is full of temporary
objects.
The version using git-grep(1) without IPC is the fastest, and doesn't
have any problems, but it is limited to just grep(1). If we want to use
sed(1), or any other programs, we're out of luck.
A common program I want to use is grepc(1), which searches source code.
See for example this code for finding the creat(2) system call:
$ git ls-files | grep '\.[ch]$' | xargs grepc creat;
fs/open.c:SYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, m=
ode)
{
int flags =3D O_CREAT | O_WRONLY | O_TRUNC;
if (force_o_largefile())
flags |=3D O_LARGEFILE;
return do_sys_open(AT_FDCWD, pathname, flags, mode);
}
include/linux/syscalls.h:asmlinkage long sys_creat(const char __user *path=
name, umode_t mode);
tools/include/nolibc/fcntl.h:static __attribute__((unused))
int creat(const char *path, mode_t mode)
{
return open(path, O_CREAT | O_WRONLY | O_TRUNC, mode);
}
A problem with git-ls-files(1) is that it doesn't have a way to filter
files by type. That is, it will list symlinks, which will affect the
search. With find(1), I can control that with '-type f', which makes
sure I only search in regular files. I'd like to have some feature in
git-ls-files(1) that allows me to print files of the specified type.
Since git(1) knows the type of the files, this should be doable without
much overhead, right?
Have a lovely day!
Alex
--=20
<https://www.alejandro-colomar.es>
--7via6c44zxtslhjm
Content-Type: application/pgp-signature; name="signature.asc"
-----BEGIN PGP SIGNATURE-----
iQIzBAABCgAdFiEES7Jt9u9GbmlWADAi64mZXMKQwqkFAmrt134ACgkQ64mZXMKQ
wqmJhRAAulEzINHRrIWpLPk3xnynEzvWEEtsSH+Tyr9+B5sDT3NyAeNFG+R8KYHn
pOMWu21EGooZN3UTUTAem7XHP8n6Pye9CeHxQLVCe7aYoLgArOsW884jtvKSzV7/
HwAY4tVFku+piVbQDKWy9F0BM8davweH69AWsHqjHy2HGtliK9G++hZfMejiXnPl
lUtExjbQ8O+v5NTYnDUzI7zY/riOVXOFFnAjfCluAvZueih4mfnFaedTTTBefR0/
LPsrGDFQJRwlkBLH41OaF7UguqCIJctcY7U9zqs+5Yw3aXX7VHilVBKJm1LBMgah
wkcuBXTptqBLK9jz4xHofq0QDhBXeubPsBNRu6HllK9lxePDPKOfsEBp+EebyPa3
MbOqQb1hhWRjyE7kRSmLrCa3yLzULg7BUfERjLmA3VQQ+H2Pg7NHCKsbVZmFxQPb
qntfLFVBp5I0h7sBUkD1afqT/Wax7eVCrh6ZMHcabYabMed9Ev60eeg4VJPguCsP
1Ra0WqVZOwMhcI/tVVksF8ENplyl35+2UC5PKpHi4qXEuva2PkcNOvvlIp5HWwju
/74PjclsfkcI/uFqAm+F05qihCMwaQJMyQKfYQ75bYC/TrqIF21cRI5LYTu4ptGr
0Z+bZHEMUWIJlNTFPvW1JshORig6sQLlz25HKI1ugfiG2kvDY48=
=Dq8C
-----END PGP SIGNATURE-----
--7via6c44zxtslhjm--
Re: git-ls-files(1) avoiding symlinks
Author: "D. Ben Knoble"
Date: Fri, 06 Nov 2026 14:15
Date: Fri, 06 Nov 2026 14:15
56 lines
14007 bytes
14007 bytes
On Fri, Nov 6, 2026 at 8:35 AM Alejandro Colomar <alx+git@kernel.org> wrote:
>
> Hi!
>
> Yesterday, we were discussing alternative methods for grepping source
> code. Here's a list of similar alternatives:
>
> $ find . -type f | xargs grep ARRAY_END;
> $ git ls-files | xargs grep ARRAY_END;
> $ git grep ARRAY_END;
>
> The version using find(1) has some problems: it is slow, especially
> after having built a kernel, when the directory is full of temporary
> objects.
>
> The version using git-grep(1) without IPC is the fastest, and doesn't
> have any problems, but it is limited to just grep(1). If we want to use
> sed(1), or any other programs, we're out of luck.
>
> A common program I want to use is grepc(1), which searches source code.
> See for example this code for finding the creat(2) system call:
>
> $ git ls-files | grep '\.[ch]$' | xargs grepc creat;
> fs/open.c:SYSCALL_DEFINE2(creat, const char __user *, pathname, umode_t, mode)
> {
> int flags = O_CREAT | O_WRONLY | O_TRUNC;
>
> if (force_o_largefile())
> flags |= O_LARGEFILE;
> return do_sys_open(AT_FDCWD, pathname, flags, mode);
> }
> include/linux/syscalls.h:asmlinkage long sys_creat(const char __user *pathname, umode_t mode);
> tools/include/nolibc/fcntl.h:static __attribute__((unused))
> int creat(const char *path, mode_t mode)
> {
> return open(path, O_CREAT | O_WRONLY | O_TRUNC, mode);
> }
>
> A problem with git-ls-files(1) is that it doesn't have a way to filter
> files by type. That is, it will list symlinks, which will affect the
> search. With find(1), I can control that with '-type f', which makes
> sure I only search in regular files. I'd like to have some feature in
> git-ls-files(1) that allows me to print files of the specified type.
> Since git(1) knows the type of the files, this should be doable without
> much overhead, right?
Perhaps something with "git ls-files --format='%(objectmode) %(path)'
-z"? Of course, your examples already omit -z, so if you can assume
that it might be simpler.
--
D. Ben Knoble
Re: git-ls-files(1) avoiding symlinks
Author: Alejandro Colomar
Date: Fri, 06 Nov 2026 14:45
Date: Fri, 06 Nov 2026 14:45
88 lines
10743 bytes
10743 bytes
--laphslwwjce62gru 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: "D. Ben Knoble" <ben.knoble@gmail.com> Cc: git@vger.kernel.org, Andy Shevchenko <andy@kernel.org>, Linus Torvalds <torvalds@linux-foundation.org>, Krzysztof Kozlowski <krzk@kernel.org> Subject: Re: git-ls-files(1) avoiding symlinks Message-ID: <au3lu4jYwZ5hBZSy@devuan> References: <au3Tyaz_LoYDuskn@devuan> <CALnO6CB-b2XuvwkQ9pJp5ENuc12oGMdgbcwz1VKD_i3KWfKuHA@mail.gmail.com> MIME-Version: 1.0 In-Reply-To: <CALnO6CB-b2XuvwkQ9pJp5ENuc12oGMdgbcwz1VKD_i3KWfKuHA@mail.gmail.com> Hi Ben, > Date: 2026-11-06 09:15:22-0500 > From: "D. Ben Knoble" <ben.knoble@gmail.com> > [...] > > A problem with git-ls-files(1) is that it doesn't have a way to filter > > files by type. That is, it will list symlinks, which will affect the > > search. With find(1), I can control that with '-type f', which makes > > sure I only search in regular files. I'd like to have some feature in > > git-ls-files(1) that allows me to print files of the specified type. > > Since git(1) knows the type of the files, this should be doable without > > much overhead, right? >=20 > Perhaps something with "git ls-files --format=3D'%(objectmode) %(path)' > -z"? Of course, your examples already omit -z, so if you can assume > that it might be simpler. Hmmm, that could work. Thanks! $ git ls-files --format=3D'%(objectmode) %(path)' \ | grep -v ^120000 \ | sed 's/^.* //' \ | grep '\.[ch]$' \ | xargs grepc creat; With -z it becomes a bit tedious: $ git ls-files -z --format=3D'%(objectmode) %(path)' \ | grep -zv ^120000 \ | sed -z 's/^.* //' \ | grep -z '\.[ch]$' \ | xargs -0 grepc creat; But, indeed, I don't care about files with spaces often, because I don't create them in my stuff. I expect git-ls-files(1) could make this much simpler, though: $ git ls-files --type f \ | grep '\.[ch]$' \ | xargs grepc creat; Have a lovely day! Alex --=20 <https://www.alejandro-colomar.es> --laphslwwjce62gru Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEES7Jt9u9GbmlWADAi64mZXMKQwqkFAmrt5+oACgkQ64mZXMKQ wqnMFhAAviRiAIYgNWavnKkIHllRqEmYQ72tiJhjx71KI5UEuLquzCZms7TqJ9UW M1IlZSpNtKZU4hlOP08EiwWNe7fFty7VZk9pvkfKG3pmQtL1ZEI3gXLN7cJjr5i4 MnIAxM3cl4j5V7NTstfvj1PeiQCCgs9aF8z0ixWeWqetpeL+xhrLX73sBv65PERp mLVRZlnQ1SQczZg0lWV264opxyZwNlTayEZV4ALXPLTGDbmL7RunRW4nq4TY4je6 ZNdqVTsxv4BKbtUfT65PXnAA9HoyzcfAD2XQOc4h1poR/CMkVy6a/AxOD/hJ48go qMq1FPOfu0DTUZQsB+BCmtI4P7IPpIAGyKselAdMZ2QzPEsRP9wBVipCwUPDCKQP 6FxK2+EI7bssAjofrrhUAziATpgY67Wglg87hjsIDKXZH32OHLw8nB7xlIARQr/s O33eNfXe8Mif8k6vzIErOobB9yB0r3j3T1/O+OX9kik7OF53BcgD90da5SrFvx1t ICUYJfy6vHprIOMSo7sXeB9WxJPC8/Il3DV+7F/IXIuHRRWLaCZNzp+U4PM0XAao b9hNECzNnwR9RrLP/SXHQZxBsobCFh3hacFVD2LyuYqOOAdSmaQIzwnl3YIPdCQf rRW24EqIK+CqXl+693wOm3yZKgfi8G/wt13QTHLFNnn1iJaCiqc= =C42d -----END PGP SIGNATURE----- --laphslwwjce62gru--
Re: git-ls-files(1) avoiding symlinks
Author: Alejandro Colomar
Date: Fri, 06 Nov 2026 19:36
Date: Fri, 06 Nov 2026 19:36
88 lines
10524 bytes
10524 bytes
--wiyvbp77ygcid7vz 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: Ben Knoble <ben.knoble@gmail.com> Cc: git@vger.kernel.org, Andy Shevchenko <andy@kernel.org>, Linus Torvalds <torvalds@linux-foundation.org>, Krzysztof Kozlowski <krzk@kernel.org> Subject: Re: git-ls-files(1) avoiding symlinks Message-ID: <au4rmQRjRtIv1Oru@debian> References: <au3lu4jYwZ5hBZSy@devuan> <AE63992D-7BEA-413D-A372-78708CFA8F6F@gmail.com> MIME-Version: 1.0 In-Reply-To: <AE63992D-7BEA-413D-A372-78708CFA8F6F@gmail.com> Hi Ben, > Date: 2026-11-06 12:34:20-0500 > From: Ben Knoble <ben.knoble@gmail.com> > [...] > > Hmmm, that could work. Thanks! > >=20 > > $ git ls-files --format=3D'%(objectmode) %(path)' \ > > | grep -v ^120000 \ > > | sed 's/^.* //' \ > > | grep '\.[ch]$' \ > > | xargs grepc creat; > >=20 > > With -z it becomes a bit tedious: > >=20 > > $ git ls-files -z --format=3D'%(objectmode) %(path)' \ > > | grep -zv ^120000 \ > > | sed -z 's/^.* //' \ > > | grep -z '\.[ch]$' \ > > | xargs -0 grepc creat; >=20 > The filtering for C files can also be done by Git, I believe: >=20 > git ls-files =E2=80=A6 '*.[ch]' I prefer regexes over globs; they're more powerful, and they're hot in brain memory. :) > > But, indeed, I don't care about files with spaces often, because I don't > > create them in my stuff. > >=20 > > I expect git-ls-files(1) could make this much simpler, though: > >=20 > > $ git ls-files --type f \ > > | grep '\.[ch]$' \ > > | xargs grepc creat; >=20 > I believe the expression is =E2=80=9Cpatches [scratching your own itch ar= e] welcome=E2=80=9D ;)=20 Nice! If the idea is welcome, I can certainly work on a patch. :) Have a lovely night! Alex --=20 <https://www.alejandro-colomar.es> --wiyvbp77ygcid7vz Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEES7Jt9u9GbmlWADAi64mZXMKQwqkFAmruLD8ACgkQ64mZXMKQ wqmv4w//WOK6c9yElT/g4r7756NLSXotHAqfEafLb1jML5x0muIIgek+p1rDkbKs X/TygZDIFYIolX6JHQb6ALrgfF1l6805QjFP+TOrRgVvod0YUb+hva+EUR8ntahC XdElmjObRoc7XCMHX3b1sBgAfc+vo5Tqbi+/Ho12vtiaUZjkMYZYxG7zo0NK1l4A D4vFYlXUDzjKBm4OWyJ65q0sLC5V4o+5YsVIKOGcD+FFUmHJT2gfEMyP0Opl6qt7 DBEgf45pXV/eSBkpJhLyxS+/aSQm9fTw5PO/6GNl1r/9ZdtfsnEW6Zvr1d8/DVfr 9T1pZNDS3hbY6wgVCFd7KuqdI3i2i7I1MBCQvFLMWJhqtjGg6Ren8Oqs+Z04Qe6N QsXc0Tu+YAa8qC/Sr2ahtQAiUsqsgTeHE8VDu0k8MCBf/M2X6pUtHWp9szKeTKE4 fV3DbsysMF24jBCVbYGUNFtb1puSin7anifZbyuYamwEH3O//FeLDN3mhdbeT7AK aUEOo5kgRU4e1/qFSw2JF34Urg3n2IsbREF5TVo86Un/k+aMcuT02KIxz6MpKBHR 4kGf5a8dWJgtGlC5agWLeotesJ4NrY+KZR57xISftmBYCsnjhmmVEG4nbYtPUpnJ 5aWsfGMU47A+YRJtn96ix197WMqUy09v9M7LwdoiCfIrOhs/g7k= =4ZDg -----END PGP SIGNATURE----- --wiyvbp77ygcid7vz--
Re: git-ls-files(1) avoiding symlinks
Author: Junio C Hamano
Date: Fri, 06 Nov 2026 20:16
Date: Fri, 06 Nov 2026 20:16
42 lines
13991 bytes
13991 bytes
Alejandro Colomar <alx+git@kernel.org> writes:
>> > With -z it becomes a bit tedious:
>> >
>> > $ git ls-files -z --format='%(objectmode) %(path)' \
>> > | grep -zv ^120000 \
>> > | sed -z 's/^.* //' \
>> > | grep -z '\.[ch]$' \
>> > | xargs -0 grepc creat;
Shouldn't the grep|sed|grep be doable with a single sed invocation?
>> > I expect git-ls-files(1) could make this much simpler, though:
>> >
>> > $ git ls-files --type f \
>> > | grep '\.[ch]$' \
>> > | xargs grepc creat;
Not really. Why is "--type" have to be so special? What would the
next person ask for? --size?
The "ls-files" plumbing command is designed as a component to build
your own pipeline with. Don't throw in random unrelated features to
it. In the longer run, it is not a good substitute for improving
the skill to construct a good pipeline.
Learn the tools available to you that you already have heard of, so
that you can make better uses of them (in this case "sed"). If you
squash the grep|sed|grep pipeline into a single sed
git ls-files -z -s "*.[ch]" |
sed -z -n -e '/^120000/d' -e '/^.* 0 /s///p' |
xargs -r -0 ls -ld
the result does not look too bad.
One unrelated tip for better readability is to end, not begin, your
lines with the "|" pipe characters. That way you do not have to
sprinkle so many unsightly backslashes ;-).
HTH.
Re: git-ls-files(1) avoiding symlinks
Author: Alejandro Colomar
Date: Fri, 06 Nov 2026 21:06
Date: Fri, 06 Nov 2026 21:06
141 lines
12665 bytes
12665 bytes
--2mq5maq4vgfxettg 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: Junio C Hamano <gitster@pobox.com> Cc: Ben Knoble <ben.knoble@gmail.com>, git@vger.kernel.org, Andy Shevchenko <andy@kernel.org>, Linus Torvalds <torvalds@linux-foundation.org>, Krzysztof Kozlowski <krzk@kernel.org> Subject: Re: git-ls-files(1) avoiding symlinks Message-ID: <au49oFqexVxPUm-2@debian> References: <au3lu4jYwZ5hBZSy@devuan> <AE63992D-7BEA-413D-A372-78708CFA8F6F@gmail.com> <au4rmQRjRtIv1Oru@debian> <xmqqeccxhg0f.fsf@gitster.g> MIME-Version: 1.0 In-Reply-To: <xmqqeccxhg0f.fsf@gitster.g> Hi Junio, > Date: 2026-11-06 12:16:48-0800 > From: Junio C Hamano <gitster@pobox.com> > > Alejandro Colomar <alx+git@kernel.org> writes: >=20 > >> > With -z it becomes a bit tedious: > >> >=20 > >> > $ git ls-files -z --format=3D'%(objectmode) %(path)' \ > >> > | grep -zv ^120000 \ > >> > | sed -z 's/^.* //' \ > >> > | grep -z '\.[ch]$' \ > >> > | xargs -0 grepc creat; >=20 > Shouldn't the grep|sed|grep be doable with a single sed invocation? >=20 > >> > I expect git-ls-files(1) could make this much simpler, though: > >> >=20 > >> > $ git ls-files --type f \ > >> > | grep '\.[ch]$' \ > >> > | xargs grepc creat; >=20 > Not really. Why is "--type" have to be so special? What would the > next person ask for? --size? Indeed. While looking at the implementation of git-ls-files(1), I realized it wasn't easy to decide how to implement this, as it has other types (objecttype, which is blob/commit/...). I thought perhaps --objectmode, but then I'm not inderested in searching specific modes, but rather filtering out some. Perhaps in this case it's more appropriate to have my own local command 'git-find' that does what I want. I've ended up doing this, and now don't think a flag in git-ls-files(1) would make much sense. $ cat $(which git-find) #!/bin/bash # Copyright 2026, Alejandro Colomar <alx@kernel.org> # SPDX-License-Identifier: GPL-3.0-or-later set -Eeufo pipefail; git ls-files --format=3D'%(objectmode) %(path)' \ | grep -v '^120000' \ | sed 's/^.* //'; And will eventually add a -z flag for this command when I find I need it. > The "ls-files" plumbing command is designed as a component to build > your own pipeline with. Don't throw in random unrelated features to > it. In the longer run, it is not a good substitute for improving > the skill to construct a good pipeline. Indeed. > Learn the tools available to you that you already have heard of, so > that you can make better uses of them (in this case "sed"). If you > squash the grep|sed|grep pipeline into a single sed >=20 > git ls-files -z -s "*.[ch]" | > sed -z -n -e '/^120000/d' -e '/^.* 0 /s///p' | > xargs -r -0 ls -ld >=20 > the result does not look too bad. I find it simpler to have a simple grep -v rejecting the lines I don't want, and then sed(1) getting rid of the stuff. The command above seems interesting for performance, but compared to the tool using the file names, that's probably negligible. About git-ls-files(1)'s -s, I think it's easier to parse with the %(objectmode) in the format, as I don't need all the other information printed by -s. >=20 > One unrelated tip for better readability is to end, not begin, your > lines with the "|" pipe characters. That way you do not have to > sprinkle so many unsightly backslashes ;-). I like to have them at the front of the line because it shows more clearly whether I'm piping a command or starting a new pipeline. I got used to the backslashes. After all, the first few characters of a line are all aligned, and the most significant ones. Stuff to the right I tend to ignore it as noise. :) >=20 > HTH. Yup! Thanks! Have a lovely night! Alex --=20 <https://www.alejandro-colomar.es> --2mq5maq4vgfxettg Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEES7Jt9u9GbmlWADAi64mZXMKQwqkFAmruQVgACgkQ64mZXMKQ wqm3xQ//d/4pFbXvmYPJ/BU4a15hj16T4g7t8WAm0uimhuN5U6Mj13bna6r/UTuf UOYvH5Bq8NCyZaLJR2pCHHR6Ag6P2L/NsLrWrAaqh/HQRDsEKKRdqjZspSlSc0qg H6UZCtfPfRS4xZIzCQab4hDxj578Ygj+OZVL4QpjVUr3LZYqpiU5lZq0SThTw7KW ImtOmJ2i5OFMGBGIaBstq6qMgN4dccFLswmI+vlWgiPznzb0qsJE3AMznVbkHAcI gDLiD7YjRiFWSAA5xTK7z4YCkJ0yfrsaGHkwH2/puIFvxbpFNtY2ONMOf1KJCQcX VguahWCNQ8MdqFionX6teJ7ZNwL+7VzGVYM76hjn9SiOIZ/6C+uAzi8gEAUYw/iO PLZ0rGsbxierBNmfdxTGuvBz48eM9fXNd8wMwzz5VpS8qkXKKgk78+plpra0Fk8w C5NX911xdkF+RU00ZM5cNyxQr9eKQAPH6WWrUNCYdXOygckd6tfg1x+EFPNiERil t1Hpt7tqu+A3N5VQvhljfa00FNZGSVA2rS8viorN9CblslB2OLxNg5+SY3Y+kYn3 C+5hjgsg5KKux+iGZYBMo26QtVrue/2zlnaTgLA+oxS9OxXGQ3A86y8BvrDuwaSl f25agyJS6Obzu4c+cheHMHK0MDPYn9rYZIvhpV08EtuCPcZq36I= =njVY -----END PGP SIGNATURE----- --2mq5maq4vgfxettg--
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