How To Get Only Certain Object Types Back From Git LS-Files

35 views Asked by At

I'm trying to see what opportunities I might have to refactor some code. So I'm looking to see how to search my repository for the longest files to consider. But how can you use git ls-files's to return back just a certain filename like java-files *.java?

I'm not understanding the documentation yet. And, I could use some rephrasing to understand this. I've tried running variations of --format='*.java', --format=objecttype *.java *.java, grep java, and others. I have the rest of the code git ls-files | grep java | xargs ls -l | sort -nrk5 | head -n 10 from "How can I find the N largest files in a Git repository?" SO Q&A.

1

There are 1 answers

0
joeljpa On

This seems to work just fine.

git ls-files  | grep ".*java" | xargs ls -alh | sort -rhk5 | head -n 10

I've replaced instances of -n with -h which gives you the filesize in a nice human-readable format.

-rw-r--r--  1 joel  staff    65K Nov 20 15:18 src/com/crawlmb/keyboard/CrawlKeyboardView.java
-rw-r--r--  1 joel  staff    17K Nov 20 15:18 src/com/crawlmb/ConfigEditor.java
-rw-r--r--  1 joel  staff    16K Nov 20 15:18 src/com/crawlmb/activity/PreferencesActivity.java
(skipped remaining entries)

I noticed that if you're searching for smaller patterns like for .c files, replace ".*c" with ".*\.c" for more precision. Otherwise, I got results having entries like "doc/Guidebook.txt" because of the 'c' occurrence.