How to list all text (non-binary) files in a git repository?

空扰寡人 提交于 2019-12-17 18:43:21

问题


I have a repository with a lot of autogenerated source files I've marked as "binary" in .gitattributes (they are checked in because not everyone has access to the generator tools). Additionally, the repo has a lot of source-ish files in ignored directories (again, generated as part of the build processes), and a number of actual binary files (e.g. little resource files like icons).

I'd now like to find all the non-auto-generated and non-ignored files in the repo. I thought I'd just do this with find and a bunch of exclusions, but now I have a horrendous find statement with a dozen clauses (and it still doesn't perfectly do the job). git ls-files works but shows me all the binary files without differentiation, which I have to filter out.

So, I'm wondering: is there a simple command I can run which lists every file checked into the the repo, and which git considers a "text" file?


回答1:


git grep --cached -Il ''

lists all non-empty regular (no symlinks) text files:

  • -I: don't match the pattern in binary files
  • -l: only show the matching file names, not matching lines
  • '': the empty string makes git grep match any non-empty file
  • --cached: also find files added with git add but not yet committed (optional)

Or you could use How to determine if Git handles a file as binary or as text? in a for loop with git ls-files.

TODO empty files.

Find all binary files instead: Find all binary files in git HEAD

Tested on Git 2.16.1 with this test repo.




回答2:


A clever hack to achieve this: listing all non-binary files that contains carriage returns

$ git grep --cached -I -l -e $'\r'

For my case, an empty string works better:

$ git grep --cached -I -l -e $''

Took it from git list binary and/or non-binary files?.




回答3:


The standard method for listing non-ignored files is:

git ls-files --exclude-standard --cached

But, as you seen, it lists all versioned files.

One workaround could be to define in a separate file "exclude_binaries" an exclusion pattern in order to match all binaries that you know of.

git ls-files --exclude-standard --cached \
--exclude-from=/path/to/`exclude_binaries`

That would be a less complex find, but it doesn't provide a fully automated way to list non-binary files: you still have to identify and list them in a separate pattern file.



来源:https://stackoverflow.com/questions/18973057/how-to-list-all-text-non-binary-files-in-a-git-repository

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!