简体   繁体   English

Git使用脚本拉多个本地存储库(红宝石?)

[英]Git pull multiple local repositories with script (ruby?)

I have ~30 git repositories cloned from github that I use for web/ruby/javascript development. 我从github克隆了大约30个git存储库,用于Web / ruby​​ / javascript开发。 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 ? 我有一个ruby脚本来使用octokit克隆存储库,但是在GitClones下的所有存储库中,如何进行git pull(覆盖/重新本地化)有什么建议吗?

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. 通常,只要要使用该存储库,我都会进行拉动,但是我要去的地方有时只能使用Internet连接。 So I would like to update everything I can while I have internet. 因此,我想在拥有互联网的情况下尽一切可能进行更新。

Thanks! 谢谢! (Running osx 10.8.2) (正在运行osx 10.8.2)

If you must do it in Ruby, here's a quick and dirty script: 如果您必须在Ruby中执行此操作,则可以使用以下快速且肮脏的脚本:

#!/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. 不要忘记将文件复制到chmod + x并确保它位于GitClones目录中。

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 仅当包含.git子目录时才进行git pull
  2. It skips dot (.) dirs since no one has git repos which start with a dot. 由于没有人使用以点开头的git repos,所以它跳过点(。)目录。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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