Skip to content

Instantly share code, notes, and snippets.

@xorvo
Created February 17, 2024 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xorvo/7878669c086c65b4decc03469510a476 to your computer and use it in GitHub Desktop.
Save xorvo/7878669c086c65b4decc03469510a476 to your computer and use it in GitHub Desktop.
Clone git repositories to dedicated path
#!/usr/bin/env ruby
# For pulling git repo into a destination directory based on global path of the repo
# example: git@github.com:rabbitmq/rabbitmq-server.git => ~/projects/github.com/rabbitmq/rabbitmq-server
require 'fileutils'
PROJECTS_DIR="#{Dir.home}/projects"
url = ARGV[0]
regex = /git@(?<host>[\w-]+\.\w+):(?<user>[\w-]+)\/(?<repo>[\w\.-]+)\.git/
matchdata = regex.match(url)
raise "Invalid input #{url}" unless matchdata
parent_dir = File.join(PROJECTS_DIR, matchdata['host'], matchdata['user'])
dir = File.join(parent_dir, matchdata['repo'])
FileUtils.mkdir_p(parent_dir)
system("git clone #{url} #{dir} && cd #{dir}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment