Class: GitAuth::GitHubMirror

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/gitauth/gh_mirror.rb

Defined Under Namespace

Classes: Project

Constant Summary collapse

VERSION =
[0, 0, 2, 0]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, token) ⇒ GitHubMirror

Returns a new instance of GitHubMirror.



16
17
18
# File 'lib/gitauth/gh_mirror.rb', line 16

def initialize(username, token)
  @api = GitHubApi.new(username, token)
end

Class Method Details

.run(options = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/gitauth/gh_mirror.rb', line 61

def run(options = {})
  options = GitAuth::Nash.new(options)
  options.user  = `git config --global github.user`.strip  unless options.user?
  options.token = `git config --global github.token`.strip unless options.token?
  logger.info "Preparing to run GitHub mirror for #{options.user}"
  mirror = self.new(options.user, options.token)
  mirror.mirror_all
rescue Exception => e
  logger.fatal "Got Exception: #{e.class.name} - #{e.message}"
  e.backtrace.each { |l| logger.fatal "--> #{l}" }
end

.version(include_path = false) ⇒ Object



73
74
75
# File 'lib/gitauth/gh_mirror.rb', line 73

def version(include_path = false)
  VERSION[0, (include_path ? 4 : 3)].join(".")
end

Instance Method Details

#clone!(p) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/gitauth/gh_mirror.rb', line 38

def clone!(p)
  if repository = GitAuth::Repo.create(p.name, "#{p.name}.git")
    FileUtils.rm_rf(repository.real_path)
    path = repository.real_path
    Dir.chdir(File.dirname(path)) do
      GitAuth.run "git clone --mirror #{p.github_clone_url} #{File.basename(path)}"
    end
    p.repository = repository
  else
    raise "Error creating local mirror of repository '#{p.name}'"
  end
end

#mirror!(p) ⇒ Object



27
28
29
# File 'lib/gitauth/gh_mirror.rb', line 27

def mirror!(p)
  mirrored?(p) ? update!(p) : clone!(p) 
end

#mirror_allObject



51
52
53
54
55
56
57
# File 'lib/gitauth/gh_mirror.rb', line 51

def mirror_all
  logger.info "Mirroring all projects"
  projects.each do |project|
    logger.info "Mirroring for #{project.name} (#{project.github_clone_url})"
    mirror!(project)
  end
end

#projectsObject



20
21
22
23
24
25
# File 'lib/gitauth/gh_mirror.rb', line 20

def projects
  @projects ||= @api.repositories.map do |repository|
    local_repo = GitAuth::Repo.get(repository.name)
    Project.new(repository.name, github_url_for_repo(repository), local_repo)
  end
end

#update!(p) ⇒ Object



31
32
33
34
35
36
# File 'lib/gitauth/gh_mirror.rb', line 31

def update!(p)
  return unless p.repository
  Dir.chdir(p.repository.real_path) do
    GitAuth.run "git fetch origin --force"
  end
end