Created
June 4, 2012 11:30
-
-
Save atombender/2867854 to your computer and use it in GitHub Desktop.
Git script to change a repo's push/pull URLs to point to another Github owner
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 run(s) | |
puts "[#{s}]" | |
result = `#{s}` | |
unless $?.exited? and $?.exitstatus == 0 | |
abort "Failed to run: #{s}" | |
end | |
result | |
end | |
new_owner = ARGV.shift | |
unless new_owner | |
abort "Usage: #{$0} <new-owner>" | |
end | |
unless run("git remote show -n origin") =~ /Fetch URL: (.*?)$/m | |
abort "Could not read remote" | |
end | |
old_url = $1 | |
unless old_url =~ %r{git@github\.com:([^/]+)/(.*)$} | |
abort "Remote must be Github path" | |
end | |
old_owner = $1 | |
if old_owner == new_owner | |
abort "Owner is already #{new_owner}" | |
end | |
repo_name = $2 | |
new_url = "[email protected]:#{new_owner}/#{repo_name}" | |
puts "Writing new origin #{new_url}" | |
run("git remote set-url origin #{new_url}") | |
run("git remote set-url --push origin #{new_url}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment