Skip to content

Instantly share code, notes, and snippets.

@andyh
Forked from topfunky/caprc.rb
Created December 7, 2008 22:31
Show Gist options
  • Save andyh/33269 to your computer and use it in GitHub Desktop.
Save andyh/33269 to your computer and use it in GitHub Desktop.
# DESCRIPTION: Cap task to setup the current directory as git repository and then create and push to a remote repository via ssh.
#
# INSTALL: Copy to home directory as ~/.caprc
# RUN: cap git:setup scm=t
# If it's a Rails app run
# RUN: cap git:setup scm=t railsapp=t
#
# AUTHOR: Andy Henson (http://www.elaptics.co.uk) based on code by Geoffrey Grosenbach http://peepcode.com
#
if ENV['scm']
role :scm, "git.example.com"
set :user, "deploy"
set :scm_hostname, "git.example.com"
set :remote_git_repo_dir, "/home/deploy/git-repos" # where you want to store your git repositories
end
namespace :git do
desc "Setup Git repo in the current directory and create remotely"
task :setup, :roles => :scm do
repo = File.basename(FileUtils.pwd)
remote_repo_dir = "#{remote_git_repo_dir}/#{repo}.git"
unless Capistrano::CLI.ui.agree("Are you sure you want to setup '#{repo}'? (yes/no): ")
puts "Git setup aborted."
exit
end
unless File.exists?(".git")
system "git init"
if ENV['railsapp']
system %(echo ".DS_Store\ndb/*.sqlite3\ndoc/api\ndoc/app\nlog/*.log\ntmp/**/*" >> .gitignore)
system "touch tmp/.gitignore"
system "touch log/.gitignore"
end
system "git add ."
system "git commit -m \"Initial Commit\""
end
run [
"mkdir -p #{remote_repo_dir}",
"cd #{remote_repo_dir}",
"git --bare init",
].join(" && ")
system "git remote add origin ssh://#{user}@#{scm_hostname}/#{remote_repo_dir}"
system "git push origin master"
# Configure new remote repo as the origin
system %(echo "[branch \\"master\\"]\n\tremote = origin\n\tmerge = refs/heads/master" >> .git/config)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment