Ruby Productivity Scripts : Clone a Git Project

Objective


To write a simple script to automate git clone and browsing the source code.

Steps


Step 1

As developers, we are so busy that we don't have time to think about the amount of repetitive tasks we do on a daily basis. The following steps:

  1. Change directory to ~/temp
  2. git clone git_url
  3. mate downloaded-project-directory

are repeated for every project that I want to read the source code to learn about how to use and to learn some new tricks. So, why not automate this task and learn Ruby at the same time?

Step 2

#!/usr/bin/env ruby -w

require 'uri'
require 'fileutils'

if ARGV[0].nil?
  raise 'specify git url to clone'
else
  git_url = ARGV[0]
end

u = URI(git_url)

project_name = u.path.split('/').last
directory_name = project_name.split('.').first

Dir.chdir("/Users/bparanj/temp")

unless File.directory?("./#{directory_name}")  
  system("git clone #{git_url}")
end

system("mate #{directory_name}")

I have hardcoded the directory, since I usually use the temp directory for downloading projects. You can modify the script to read the project directory to use from the command line.

Step 3

Make the script executable:

$chmod 0755

Run it:

$./giter.rb https://github.com/br/hello.git

Summary


In this article, we wrote a simple pure ruby script to automate a frequent task of cloning a git repo and browsing the code.

References


Ruby Docs Dir
Ruby Docs Dir#exists
Ruby Docs URI


Related Articles

git


Create your own user feedback survey