繁体   English   中英

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

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

我从github克隆了大约30个git存储库,用于Web / ruby​​ / javascript开发。 是否可以使用脚本批量更新所有这些脚本?

我的一切都井井有条(文件夹结构):

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

我有一个ruby脚本来使用octokit克隆存储库,但是在GitClones下的所有存储库中,如何进行git pull(覆盖/重新本地化)有什么建议吗?

通常,只要要使用该存储库,我都会进行拉动,但是我要去的地方有时只能使用Internet连接。 因此,我想在拥有互联网的情况下尽一切可能进行更新。

谢谢! (正在运行osx 10.8.2)

如果您必须在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

不要忘记将文件复制到chmod + x并确保它位于GitClones目录中。

可以,但是当外壳足够时为什么要使用红宝石呢?

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

将球的开头更改为口味。 这做了两件事:

  1. 仅当包含.git子目录时才进行git pull
  2. 由于没有人使用以点开头的git repos,所以它跳过点(。)目录。

请享用

# 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

进行了修订,以提供更好的输出并与操作系统无关。 这将清除本地更改,并更新代码。

#!/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