Skip to content

Instantly share code, notes, and snippets.

@tlberglund
Created January 31, 2011 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tlberglund/804003 to your computer and use it in GitHub Desktop.
Save tlberglund/804003 to your computer and use it in GitHub Desktop.
A Git script to list objects by size in an unpacked repo.
#!/usr/bin/env ruby
class GitObject
attr_reader :git_type, :hash, :size
def initialize(hash, git_type, size)
@hash = hash
@git_type = git_type
@size = size
end
def to_s
"#{@hash} (#{@size})"
end
end
object_dirs = []
Dir.foreach('.git/objects') do |dir|
if /^[a-f0-9A-F]{2}/.match(dir)
object_dirs << Dir.new(".git/objects/#{dir}")
end
end
hashes = []
object_dirs.each do |dir|
hash_prefix = dir.path.split('/')[-1]
Dir.foreach(dir.path) do |object_filename|
if /^[a-f0-9A-F]{38}/ =~ object_filename
hashes << "#{hash_prefix}#{object_filename}"
end
end
end
objects = {}
hashes.each do |hash|
objects[hash] = GitObject.new(hash, `git cat-file -t #{hash}`.chomp, `git cat-file -s #{hash}`.chomp)
end
objects.values.select do |object|
object.git_type == 'blob'
end.sort! do |a,b|
b.size.to_i <=> a.size.to_i
end.each do |blob|
puts "#{blob.hash}\t#{blob.size}"
end
@courtyardz
Copy link

Sizes are in bytes (as given by git cat-file -s in line 36).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment