class GitMirror

Attributes

root[RW]

Public Class Methods

new(root = ENV['GIT_MIRROR_ROOT'] || '/var/git_mirror') click to toggle source
# File lib/git_mirror.rb, line 7
def initialize(root = ENV['GIT_MIRROR_ROOT'] || '/var/git_mirror')
  @root = Pathname.new(root)
end

Public Instance Methods

execute(command) click to toggle source
# File lib/git_mirror.rb, line 46
def execute(command)
  prepare_root
  puts command
  system(command)
end
has_mirror?(repo_url) click to toggle source
# File lib/git_mirror.rb, line 25
def has_mirror?(repo_url)
  local_path_for(repo_url).directory?
end
local_path_for(repo_url) click to toggle source
# File lib/git_mirror.rb, line 29
def local_path_for(repo_url)
  root + repo_url.split(/github.com./, 2).last
end
mirror(repo_url) click to toggle source
# File lib/git_mirror.rb, line 20
def mirror(repo_url)
  return if has_mirror?(repo_url)
  execute "git clone --mirror --quiet #{repo_url} #{local_path_for(repo_url)}"
end
prepare_root() click to toggle source
# File lib/git_mirror.rb, line 11
def prepare_root
  root.mkpath
end
update(repo_url) click to toggle source
# File lib/git_mirror.rb, line 15
def update(repo_url)
  mirror(repo_url) unless has_mirror?(repo_url)
  update_local(local_path_for(repo_url))
end
update_all() click to toggle source
# File lib/git_mirror.rb, line 33
def update_all
  Pathname.glob(root + '**/HEAD').each do |head|
    update_local(head.parent)
  end
end
update_local(path) click to toggle source
# File lib/git_mirror.rb, line 39
def update_local(path)
  FileUtils.cd(path) do
    execute "git fetch --prune --quiet"
    execute "git fetch --tags --quiet"
  end
end