Skip to content

Instantly share code, notes, and snippets.

@Andrew8xx8
Last active December 17, 2015 16:59
Show Gist options
  • Select an option

  • Save Andrew8xx8/5642791 to your computer and use it in GitHub Desktop.

Select an option

Save Andrew8xx8/5642791 to your computer and use it in GitHub Desktop.
Search git submodules in repositories
#!/usr/bin/env ruby
require 'fileutils'
require 'optparse'
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: #{__FILE__} [options] START_PATH"
opts.on("-r", "--hide-repository", "Do not show repository name") do |v|
options[:hide_repo] = v
end
opts.on("--search SUBMODULE_URL", String,
"Search repositories that includes submodules with specified URL") do |v|
options[:search] = v
end
end.parse!
unless ARGV[0]
puts "Usage: #{__FILE__} [options] START_PATH"
exit
end
def search_submodules(start_dir, options)
Dir.glob(File.join(start_dir, "*/")) do |dir|
if File.exists?(File.join(dir, ".git")) || File.exists?(File.join(dir, "HEAD"))
count_objects = `cd #{dir} && git count-objects -v`
unless /count:\s0.*?size:\s0.*?packs:\s0/m =~ count_objects
objects = `cd #{dir} && git ls-tree HEAD`
objects.scan(/^\d{6}\s[a-z]+\s([^.]*?)\s\.gitmodules$/m) do |gitmodules_sha|
gitmodules = `cd #{dir} && git show #{gitmodules_sha[0]}`
submodules = gitmodules.scan(/url\s=\s(.*?)$/m).map { |s| s[0] }
if !options.include?(:search) || submodules.include?(options[:search])
unless options[:hide_repo]
puts "Repository: #{dir}"
end
submodules.each { |s| puts s }
end
end
end
else
search_submodules(dir, options)
end
end
end
search_submodules(ARGV[0], options)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment