简体   繁体   中英

Git pull multiple local repositories with script (ruby?)

I have ~30 git repositories cloned from github that I use for web/ruby/javascript development. Is it possible to bulk update all of them with a script?

I have everything pretty organized (folder structure):

- Workspace
  - Android
  - Chrome
  - GitClones
    - Bootstrap
    ~ etc...30 some repositories
  - iPhone
  - osx
  - WebDev

I have a ruby script to clone repositories with octokit , but are there any suggestions on how to do git pull (overwriting/rebasing local) in all the repositories under GitClones ?

Normally I would just do a pull whenever I was about to use that repo, but I am going to a place where internet connectivity is only going to be available sometimes. So I would like to update everything I can while I have internet.

Thanks! (Running osx 10.8.2)

If you must do it in Ruby, here's a quick and dirty script:

#!/usr/bin/env ruby

Dir.entries('./').select do |entry|
  next if %w{. .. ,,}.include? entry
  if File.directory? File.join('./', entry)
    cmd = "cd #{entry} && git pull"
    `#{cmd}`
  end
end

Don't forget to chmod +x the file you copy this into and ensure it's in your GitClones directory.

Sure, but why use ruby when shell will suffice?

function update_all() {
  for dir in GitClones/*; do 
    cd "$dir" && git pull
  done
}

Change beginning of glob to taste. This does two useful things:

  1. It only git pull when it contains .git subdir
  2. It skips dot (.) dirs since no one has git repos which start with a dot.

Enjoy

# Assumes run from Workspace
Dir['GitClones/[^.]*'].select {|e| File.directory? e }.each do |e|
  Dir.chdir(e) { `git pull` } if File.exist? File.join(e, '.git')
end

Revised to provider better output and be OS agnostic. This one cleans local changes, and updates code.

#!/usr/bin/env ruby

require 'pp'

# no stdout buffering
STDOUT.sync = true

# checks for windows/unix for chaining commands
OS_COMMAND_CHAIN = RUBY_PLATFORM =~ /mswin|mingw|cygwin/ ? "&" : ";"

Dir.entries('.').select do |entry|
  next if %w{. .. ,,}.include? entry
  if File.directory? File.join('.', entry)
    if File.directory? File.join('.', entry, '.git')
      full_path = "#{Dir.pwd}/#{entry}"
      git_dir = "--git-dir=#{full_path}/.git --work-tree=#{full_path}"
      puts "\nUPDATING '#{full_path}' \n\n"
      puts `git #{git_dir} clean -f #{OS_COMMAND_CHAIN} git #{git_dir} checkout . #{OS_COMMAND_CHAIN} git #{git_dir} pull` 
    end
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM