Script to clone git repo or update git repo

Yesterday there was question on Stackoverflow.com how to clone a git repo using a script or update a git repo if it already exists in a local machine. Here is my quick solution that i wrote using ruby. There are many things that can be improved in this script but I am not going to improved them now. We can add a rescue block when we are trying to communicate with github.

REPO_PATH = '/Users/full_path/to_repo'
REPO_NAME = 'xxx'
GITHUB_URL = 'git@github.com:xxx/xxx.git'


def change_dir_to_repo
  Dir.chdir(REPO_PATH)
  puts system('pwd')
end

def git_repo_exists?
  if Dir.exists?(REPO_NAME)
    puts "Git repo #{REPO_NAME} exists."
    update_git_repo
  else
    puts "Git repo #{REPO_NAME} does not exists."
    clone_git_repo
  end
end

def clone_git_repo
  system("git clone #{GITHUB_URL}")
  puts "Done"
end

def update_git_repo
  puts "Changing directory to #{REPO_NAME}"
  Dir.chdir(REPO_NAME)
  puts "Changing branch to master"
  system('git checkout master')
  puts "updating git repo"
  system('git pull')
  puts "Done"
end

change_dir_to_repo
git_repo_exists?
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s