Skip to content

Instantly share code, notes, and snippets.

@dtan4
Last active August 31, 2015 14:48
Show Gist options
  • Save dtan4/e6663489af120ecc4f67 to your computer and use it in GitHub Desktop.
Save dtan4/e6663489af120ecc4f67 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require "fileutils"
require "open3"
def ghq_name_of(repository_url)
case repository_url
when %r(\Agit@)
repository_url.gsub(%r(\Agit@), "").sub(":", "/").gsub(%r(\.git\z), "")
when %r(\Agit://)
repository_url.gsub(%r(\Agit:/), "").gsub(%r(\.git\z), "")
else
""
end
end
base_dir = Dir.pwd
ghq_root = `ghq root`.chomp
Dir.glob("*").each do |dir|
next unless Dir.exist?(dir)
current_dir = File.join(base_dir, dir)
Dir.chdir(current_dir)
begin
repository_url = `git remote -v | grep origin | grep fetch`.split(" ")[1]
ghq_name = ghq_name_of(repository_url)
next if ghq_name.empty?
ghq_path = File.join(ghq_root, ghq_name)
next if Dir.exist?(ghq_path)
FileUtils.mkdir_p(File.dirname(ghq_path))
FileUtils.cp_r(current_dir, ghq_path, verbose: true)
# Dir.chdir(base_dir)
# FileUtils.rm_r(current_dir, verbose: true)
ensure
Dir.chdir(base_dir)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment