Created
January 31, 2011 13:11
-
-
Save tlberglund/804003 to your computer and use it in GitHub Desktop.
A Git script to list objects by size in an unpacked repo.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sizes are in bytes (as given by
git cat-file -s
in line 36).