Skip to content

Instantly share code, notes, and snippets.

@matthijsgroen
Created March 3, 2011 13:28
Show Gist options
  • Save matthijsgroen/852760 to your computer and use it in GitHub Desktop.
Save matthijsgroen/852760 to your computer and use it in GitHub Desktop.
Move repositories from one git repo to another
#!/usr/bin/ruby
require "yaml"
module Git
class RepoMover
CONFIG_ROOT = "repo_movement"
def initialize(config_file)
STDOUT.sync = true
@configuration = YAML::load_file config_file
@source = @configuration[CONFIG_ROOT]["source"]
@destination = @configuration[CONFIG_ROOT]["destination"]
end
def move_repositories
repo_list = @configuration[CONFIG_ROOT]["repositories"]
puts "moving #{repo_list.length} repositories from #{@source} => #{@destination}"
repo_list[0..1].each_with_index do |repo, index|
print "(#{index + 1}/#{repo_list.length}) "
move_repo repo
end
end
private
def move_repo repo_data
git = "cd repository && git"
source = ""
destination = ""
if repo_data.is_a? String
source = repo_data
destination = repo_data
elsif repo_data.is_a? Hash
source = repo_data["source"]
destination = repo_data["destination"]
end
print "moving: #{source} => #{destination}"
print " checkout.."
`git clone #{@source}:#{source} repository -q`
output = `#{git} branch -a`
remote_branches = parse_remote_branches(output, "origin")
#puts remote_branches.inspect
`#{git} remote add destination #{@destination}:#{destination}`
print "moving #{remote_branches.length} branches..."
remote_branches.each do |branch|
`#{git} checkout #{branch} -q`
`#{git} push destination #{branch} -q`
end
puts "done"
`rm -rf repository`
end
def parse_remote_branches(text, remote)
text.split("\n").collect do |line|
if line.include?("remotes/#{remote}/") and !line.include?("remotes/#{remote}/HEAD")
line.split("/").last
end
end.compact
end
end
end
mover = Git::RepoMover.new("repo_move.yml")
mover.move_repositories
repo_movement:
source: git@server.com
destination: git@other-server.com
repositories:
- repo1
- repo2
-
source: source-name
destination: destination-name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment