Created
March 13, 2015 12:31
-
-
Save DannyBen/d416f9cd1323115f84ed to your computer and use it in GitHub Desktop.
Find and delete big files from git history
This file contains 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 | |
def usage | |
puts """ | |
gitclean - List and remove big files from git history | |
Usage: gitclean list [<rev> <threshold>] | |
gitclean del <file> | |
Example: gitclean list master 10 | |
gitclean del that-huge-logfile.log | |
""" | |
exit | |
end | |
command = ARGV.shift | |
['list', 'del'].include? command or usage | |
if command == 'del' | |
file = ARGV.shift | |
file.to_s.empty? and usage | |
exec "git filter-branch -f --index-filter 'git rm -r --cached --ignore-unmatch #{file}' HEAD" | |
exit | |
end | |
if command == 'list' | |
head, treshold = ARGV | |
head ||= 'HEAD' | |
Megabyte = 1000 ** 2 | |
treshold = (treshold || 0.1).to_f * Megabyte | |
big_files = {} | |
IO.popen("git rev-list #{head}", 'r') do |rev_list| | |
rev_list.each_line do |commit| | |
commit.chomp! | |
for object in `git ls-tree -zrl #{commit}`.split("\0") | |
bits, type, sha, size, path = object.split(/\s+/, 5) | |
size = size.to_i | |
big_files[sha] = [path, size, commit] if size >= treshold | |
end | |
end | |
end | |
big_files.each do |sha, (path, size, commit)| | |
where = `git show -s #{commit} --format='%h: %cr'`.chomp | |
puts "%4.1fM\t%s\t(%s)" % [size.to_f / Megabyte, path, where] | |
end | |
exit | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment