8
git ls-files

also lists submodules.

With: List submodules in a git repository and How to remove the lines which appear on file B from another file A? I can do:

git ls-files | grep -Fxvf <(git submodule status | cut -d' ' -f3)

or the more verbose and versatile:

git ls-files | while IFS='' read -r file; do
  if [ -f "$file" ]; then
    echo "$file"
  fi
done

Is there a shorter way using some git command / flags?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
  • 2
    Probably not, since those submodules have tree entries in your parent git repository. It seems entirely reasonable that they are displayed by `git ls-files`. I encourage you to consider implement a `--no-submodules` flag for `git ls-files` and submitting the patch to `git`; they might just take it. – larsks Oct 21 '16 at 03:42

3 Answers3

9
git grep --cached -l ''

lists all non-empty files, excluding both submodules and symlinks.

You may also be interested in excluding binary files for your mass refactorings: How to list all text (non-binary) files in a git repository?

Tested on Git 2.16.1 with this test repo.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
4

git ls-files has a -s option to list staged files' mode bits and other status indicators straight from the index, you can test those

git ls-files -s | grep -v ^16 | cut -f2-
jthill
  • 55,082
  • 5
  • 77
  • 137
3

I would strongly recommend parsing the .gitmodules file directly is generally safer and much faster than git submodule status. This file can be parsed using git config to ensure that all parsing edge cases are handled appropriately -- formatting, whitespace, comments, etc:

git ls-files | grep -Fxvf  <(git config --file .gitmodules --name-only --get-regexp path | cut -d '.' -f2-)

Alternatively, the file can be parsed directly so long as the regex is designed so that it won't match submodules which have been commended out via #:

git ls-files | grep -Fxvf  <(grep "^\s*\[submodule " .gitmodules | cut -d '"' -f2)

Note that git submodule status has an output format that makes it difficult to correctly to parse via cut. The two main drawbacks are:

  • the field of the filename will change depending on if the module is initialized or not

  • the filename is not delimited by tab \t, and it isn't the last thing on the line, so filenames with spaces cannot be handled correctly.

jstine
  • 3,234
  • 20
  • 21