Created
December 21, 2015 02:47
-
-
Save aereal/8885b930ed47cdf29a4c 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/env ruby | |
require 'fileutils' | |
require 'pathname' | |
class SourceRepo | |
def initialize(path) | |
@path = path | |
@components = Pathname.new(path).each_filename.entries | |
@root = ['', @components[0...-3]].join('/') | |
end | |
def path | |
@path | |
end | |
def exist? | |
File.exist?(@path) | |
end | |
def change_root(new_root) | |
self.class.new(File.join(new_root, self.host, self.owner, self.name)) | |
end | |
def root | |
@root | |
end | |
def host | |
@components[-3] | |
end | |
def owner | |
@components[-2] | |
end | |
def name | |
@components[-1] | |
end | |
def inspect | |
"<#{self.class}:#{self.host}/#{self.owner}/#{self.name}>" | |
end | |
end | |
ghq_root = `ghq root`.strip | |
source_repos = ARGV.map {|a| SourceRepo.new(a) } | |
source_repos.each do |src_repo| | |
target_repo = src_repo.change_root(ghq_root) | |
if target_repo.exist? | |
puts "Skip #{src_repo.path}; Already exists" | |
next | |
end | |
FileUtils::Verbose.mkdir_p File.dirname(target_repo.path) | |
FileUtils::Verbose.mv src_repo.path, target_repo.path | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment