Created
June 23, 2017 01:31
-
-
Save pcbeard/c4669960a2d3dfee601dd8f930079c60 to your computer and use it in GitHub Desktop.
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/ruby | |
# validate that we're in an actual git repository. | |
git_dir = "#{Dir.pwd}/.git" | |
exit unless File.exist?(git_dir) | |
paths = ["config", "info/refs", "logs/HEAD", "packed-refs"].map {|path| "#{git_dir}/#{path}" } | |
patterns = ['refs/remotes/origin', 'refs/remotes/svn'] | |
ARGV.each do|arg| | |
if arg == "--reverse" then | |
patterns.reverse! | |
end | |
end | |
# validate that all necessary files exist in the .git directory | |
paths.each do|path| | |
if !File.exist?(path) then | |
puts "missing file #{path}, exiting" | |
exit | |
end | |
end | |
# validate that all files we'll be renaming exist. | |
renamed_paths = ["logs/#{patterns[0]}", patterns[0], "svn/#{patterns[0]}"].map {|path| "#{git_dir}/#{path}" } | |
renamed_paths.each do|path| | |
if !File.exist?(path) then | |
puts "missing file #{path}, exiting" | |
exit | |
end | |
path = path.sub(patterns[0], patterns[1]) | |
if File.exist?(path) then | |
puts "file #{path} already exists, exiting" | |
exit | |
end | |
end | |
paths.each do|path| | |
contents = File.read(path) | |
new_contents = contents.gsub(patterns[0], patterns[1]) | |
# update the project if any substitutions were made | |
if (new_contents != contents) then | |
puts "Renaming '#{patterns[0]}' to '#{patterns[1]}' in file #{path}" | |
# puts new_contents | |
File.open(path, 'w') { |file| file.write(new_contents) } | |
end | |
end | |
# do the rename if all substitutions have been performed. | |
renamed_paths.each do|path| | |
new_path = path.sub(patterns[0], patterns[1]) | |
puts "Renaming '#{path}' to '#{new_path}'" | |
File.rename(path, new_path) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment