git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] ls-files: add an --exclude-links option

From
Jeff King <peff@peff.net>
Date
Jun 21, 2023, 08:17 UTC
Message-ID
<20230621081754.GA803861@coredump.intra.peff.net>
In-Reply-To
<pull.1549.git.1687327684909.gitgitgadget@gmail.com>
On Wed, Jun 21, 2023 at 06:08:04AM +0000, Guido Martínez via GitGitGadget wrote:
Show 13 quoted lines
> From: =?UTF-8?q?Guido=20Mart=C3=ADnez?= <mtzguido@gmail.com>
> 
> Add an option to exclude symlinks from the listed files. This is useful
> in case we are listing the files in order to process the contents,
> for instance to do some text replacement with `sed -i`. In that case,
> there is no point in processing the links, and it could even be
> counterproductive as some tools (like sed) will replace the link with a
> fresh regular file.
> 
> This option enables a straightforward implementation of a `git sed`:
> 
>     #!/bin/bash
>     git ls-files --exclude-links -z | xargs -0 -P $(nproc) -- sed -i -e "$@"

This invocation would likewise have a problem with gitlink entries (for submodules). I think what you really want is not "exclude symlinks" but "show only regular files". There is no option for that, but you can do it by grepping modes from "-s", like:

  git ls-files -s | grep '^100[67]' | ...

You do unfortunately have to then pull the filename out of the rest of the line, and since we didn't use "-z", it will be quoted (and using "-z" makes it hard to use tools like grep and sed). A mild application of perl works, though:

  git ls-files -s |
  perl -0ne 'print if s/^100(644|755).*?\t//' |
  xargs -0 ...

So I dunno. That is not exactly pretty, but if you were hiding it in a "git sed" alias or script, it's not so bad.

If we were to add an option to ls-files, it would make more sense to me to allow the user to include/exclude by mode or possibly naming the type (e.g., "f" for regular files, "l" for symbolic links, etc, which matches "find"). And then allow inclusion/exclusion similar to the way that git-diff's --diff-filter option works.

-Peff
Previous: Guido Martínez via GitGitGadgetNext: Guido Martínez
Message 2 of 7 in “ls-files: add an --exclude-links option”
  1. ls-files: add an --exclude-links optionGuido Martínez via GitGitGadget, Jun 21, 2023
  2. Jeff KingJun 21, 2023
  3. Guido MartínezJun 21, 2023
  4. Junio C HamanoJun 21, 2023
  5. Guido MartínezJun 21, 2023
  6. Junio C HamanoJun 21, 2023
  7. Guido MartínezJun 21, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.