Skip to content

Instantly share code, notes, and snippets.

@kplawver
Created April 16, 2014 16:21
Show Gist options
  • Save kplawver/10901980 to your computer and use it in GitHub Desktop.
Save kplawver/10901980 to your computer and use it in GitHub Desktop.
Have a lot of git repos around? This script will go clean them up for you.
# Running: ruby gitgc.rb and it'll do its thing.
#
# There's potentially a lot of output if you have a lot of directories
# and if you've never run git gc on them, it can take a while.
#
# Put the full paths of the directories where you keep git repos in the $STARTING_DIRS array:
# Example:
# $STARTING_DIRS = ["/Users/me/Documents/Projects"]
#
$STARTING_DIRS = []
def gitgc(dir)
puts "Dir #{dir} is gitted, so gc'ing it."
`cd #{dir} && git gc`
end
def crawl(dir)
entries = Dir.entries(dir)
if entries.include?(".git")
gitgc(dir)
end
# Looking for subdirectories to crawl
entries.each do |entry|
full_path = File.join(dir,entry)
next if entry == ".rvm" || entry == ".." || entry == "." || !File.directory?(full_path)
if File.directory?(full_path)
crawl(full_path.to_s)
end
end
end
$STARTING_DIRS.each do |starting_dir|
crawl(starting_dir)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment