Class: GitMirror

Inherits:
Object
  • Object
show all
Defined in:
lib/git_mirror.rb

Instance Method Summary collapse

Constructor Details

#initializeGitMirror

Returns a new instance of GitMirror.



4
5
6
# File 'lib/git_mirror.rb', line 4

def initialize
  @root = Pathname.new(ENV['GIT_MIRROR_ROOT'] || '/var/git_mirror')
end

Instance Method Details

#execute(command) ⇒ Object



40
41
42
43
44
# File 'lib/git_mirror.rb', line 40

def execute(command)
  prepare_root
  puts command
  system(command)
end

#has_mirror?(repo_url) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/git_mirror.rb', line 22

def has_mirror?(repo_url)
  local_path_for(repo_url).directory?
end

#local_path_for(repo_url) ⇒ Object



26
27
28
# File 'lib/git_mirror.rb', line 26

def local_path_for(repo_url)
  @root + repo_url.split(/github.com./, 2).last
end

#mirror(repo_url) ⇒ Object



17
18
19
20
# File 'lib/git_mirror.rb', line 17

def mirror(repo_url)
  return if has_mirror?(repo_url)
  execute "git clone --mirror --quiet #{repo_url} #{local_path_for(repo_url)}"
end

#prepare_rootObject



8
9
10
# File 'lib/git_mirror.rb', line 8

def prepare_root
  @root.mkpath
end

#update(repo_url) ⇒ Object



12
13
14
15
# File 'lib/git_mirror.rb', line 12

def update(repo_url)
  mirror(repo_url) unless has_mirror?(repo_url)
  update_local(local_path_for(repo_url))
end

#update_allObject



30
31
32
33
34
# File 'lib/git_mirror.rb', line 30

def update_all
  Pathname.glob(@root + '**/HEAD').each do |head|
    update_local(head.parent)
  end
end

#update_local(path) ⇒ Object



36
37
38
# File 'lib/git_mirror.rb', line 36

def update_local(path)
  execute "git fetch --prune --quiet #{path}"
end