Skip to content

Instantly share code, notes, and snippets.

@mad-p
Created July 22, 2019 02:30
Show Gist options
  • Save mad-p/04e58c1076d2a19799e76f10507fc161 to your computer and use it in GitHub Desktop.
Save mad-p/04e58c1076d2a19799e76f10507fc161 to your computer and use it in GitHub Desktop.
Find suitable git clone for repositories
#!/usr/bin/env ruby
# Find suitable git clone for repositories
# Usage: ruby resolve_repos.rb | sh -x
dirs = Dir.glob("*")
dirs.each do |dir|
next unless File.directory?(dir)
git_remote = `cd #{dir}; git remote -v 2>&1 | egrep '\\(fetch\\)$|^fatal'`
next if /^fatal/ =~ git_remote
remotes = {}
git_remote.split(/\n/).each do |line|
name, url, = line.split
remotes[name] = url
end
if remotes['origin']
mirror = /\.git$/ =~ dir ? "--mirror" : ""
puts "git clone #{mirror} #{remotes['origin']} #{dir}"
end
remotes.each do |k, v|
next if k == 'origin'
puts "(cd #{dir}; git remote add #{k} #{v})"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment