Skip to content

Instantly share code, notes, and snippets.

@rarajabs
Forked from nas/Automate git pulling
Created February 23, 2012 01:49
Show Gist options
  • Save rarajabs/1889129 to your computer and use it in GitHub Desktop.
Save rarajabs/1889129 to your computer and use it in GitHub Desktop.
Automate git pulling
#!/usr/local/bin/ruby -swI .
#
# AUTOMATE UPDATING ALL PROJECTS
#
# Problem: going to each project and then pulling changes.
# Solution: Automate git pulling for all my projects
#
# 1. Create a pull_projects file and add the code from this gist
# 2. Move the file into a directory that is in your path, i.e. $PATH
# 3. Make the file executable, eg. chmod +x /usr/local/bin/pull_projects
# 4. Run e.g. pull_projects dir1_with_projects dir2_with_projects
# 5. Optional: run this on terminal startup so you dont even have to do it manually
if ARGV.length == 0
raise "You must specify atleast one project directory to pull changes from remote repository"
end
ARGV.each do |projects_dir|
puts "Starting to pull projects in #{projects_dir}\n"
dirs = Dir.entries projects_dir
dirs.reject!{|entry| entry.match(/^\./)}
dirs.each do |entry|
full_dir_path = "#{projects_dir}/#{entry}"
if File.directory? full_dir_path
Dir.chdir(full_dir_path)
check_diff = `git diff`
# Don't pull if I have local changes
unless check_diff.match(/^$/)
puts "You have pending changes in #{entry}"
else
puts "PULLING #{entry}....\n"
system "git pull"
end
Dir.chdir('../')
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment