How to list all distinct extensions of tracked files in a git repository?
Asked Answered
E

2

6

I'd like to know all distinct extensions of files tracked by git in a given repo, in order to create appropriate .gitattributes file.

Example output expected:

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

What command can I use for that?

Ernest answered 4/12, 2015 at 12:42 Comment(0)
E
9
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:

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

This is better than naive find, because:

  • it excludes untracked (gitignored) files
  • it excludes .git directory which contains usually hundreds/thousands of files and hence slows down the search

(inspired by How can I find all of the distinct file extensions in a folder hierarchy?)

Ernest answered 4/12, 2015 at 12:42 Comment(4)
Any reason you used double quotes around the perl part? Also maybe it'd be worth using a hash rather than piping to sort - you could add && !$a{$1}++ (with single quotes around the whole command) to only print the first occurrence of each result.Eal
If I use single quotes, it prints SCALAR(0xa031e3c)SCALAR(0xa031e3c).... I have to remove the escape before $1 for it to work back. But then when I declare an alias, I have to add the escape back. Updated.Ernest
Yeah, $ needs escaping from the shell inside double quotes. Better to just use a function in my opinion.Eal
ad !$a{$1}++ I tested on a huge git repo (Chrome's blink) and the speed difference is negligible in practice (0.8s vs 1.0s on my machine). I think I'll leave sort for readability :)Ernest
S
1

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:

$ext = @{}; git ls-tree -r HEAD --name-only | Get-Item | %{ $ext[$_.Extension]++ }; $ext
Soler answered 28/8, 2022 at 20:59 Comment(1)
This just gives me an error. Get-Ittem : Illegal characters in path.Maganmagana

© 2022 - 2024 — McMap. All rights reserved.