简体   繁体   English

如何列出 git 存储库中跟踪文件的所有不同扩展名?

[英]How to list all distinct extensions of tracked files in a git repository?

I'd like to know all distinct extensions of files tracked by git in a given repo, in order to create appropriate .gitattributes file.我想知道 git 在给定存储库中跟踪的文件的所有不同扩展名,以便创建适当.gitattributes文件。

Example output expected:示例 output 预期:

bat
gitignore
gradle
html
jar
java
js
json
md
png
properties
py
svg
webp
xml
yml

What command can I use for that?我可以使用什么命令?

git ls-tree -r HEAD --name-only | perl -ne 'print $1 if m/\.([^.\/]+)$/' | sort -u 

When you declare it as an alias, you have to escape $1 : 当您将其声明为别名时,您必须转义$1

alias gitFileExtensions="git ls-tree -r HEAD --name-only | perl -ne 'print \$1 if m/\.([^.\/]+)$/' | sort -u"

This is better than naive find , because: 这比天真的find要好,因为:

  • it excludes untracked (gitignored) files 它排除了未跟踪(gitignored)文件
  • it excludes .git directory which contains usually hundreds/thousands of files and hence slows down the search 它排除了.git目录,其中通常包含数百/数千个文件,因此减慢了搜索速度

(inspired by How can I find all of the distinct file extensions in a folder hierarchy? ) (受如何在文件夹层次结构中找到所有不同文件扩展名的启发

If you have access to PowerShell, here is a nice one-liner which also gives you the count of how many files exist of each type:如果您有权访问 PowerShell,这是一个很好的单行代码,它还可以计算每种类型存在的文件数量:

$ext = @{}; git ls-tree -r HEAD --name-only | Get-Item | %{ $ext[$_.Extension]++ }; $ext

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM