Class: GitMirror

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root = ENV['GIT_MIRROR_ROOT'] || '/var/git_mirror') ⇒ GitMirror

Returns a new instance of GitMirror.



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

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

Instance Attribute Details

#rootObject

Returns the value of attribute root.



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

def root
  @root
end

Instance Method Details

#execute(command) ⇒ Object



45
46
47
48
49
# File 'lib/git_mirror.rb', line 45

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

#has_mirror?(repo_url) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#local_path_for(repo_url) ⇒ Object



29
30
31
# 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) ⇒ Object



20
21
22
23
# 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_rootObject



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

def prepare_root
  root.mkpath
end

#update(repo_url) ⇒ Object



15
16
17
18
# 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_allObject



33
34
35
36
37
# 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) ⇒ Object



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

def update_local(path)
  FileUtils.cd(path) do
    execute "git fetch --prune --quiet"
  end
end