Skip to content

Instantly share code, notes, and snippets.

@btsai
Last active April 25, 2017 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save btsai/f0a462ceec17126a5beda5705d323057 to your computer and use it in GitHub Desktop.
Save btsai/f0a462ceec17126a5beda5705d323057 to your computer and use it in GitHub Desktop.
Ruby script to parse for large files in git repo
# gist to create file size list:
# run this in irb in your git folder.
# will output a text file to the parent folder with a listing of each filepath and file size.
# NOTE: nil is added to the end of each line to prevent outputting the result of the iterator blocks.
lines = `git gc && git verify-pack -v .git/objects/pack/pack-*.idx`.split("\n"); nil
objects = lines.find_all{ |line| line.match(/^\w+ blob\W+[0-9]+ [0-9]+ [0-9]+$/) }; nil
shas = `git rev-list --objects --all | sort -k 2`.chomp.split("\n"); nil
map = shas.inject({}){ |hash, line| sha, file = line.split(' '); hash[sha] = file unless file.nil?; hash }; nil
sizes = objects.map{ |line| sha, type, size = line.gsub(/\s+/, ' ').split(' '); file = map[sha]; [size.to_i, file] if file }; nil
sizes = sizes.compact.sort.reverse.map{ |size, file| "#{size}: #{file}" }; nil
folder_name = `pwd`.chomp.split('/').last
File.open("../file_sizes_#{folder_name}.txt", 'w'){ |f| f.write(sizes.join("\n")) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment