Class: GitPusher::Runner

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

Class Method Summary collapse

Class Method Details

.base_dirObject



67
68
69
# File 'lib/gitpusher/runner.rb', line 67

def self.base_dir
  Context.instance.config[:base_dir]
end

.destObject



63
64
65
# File 'lib/gitpusher/runner.rb', line 63

def self.dest
  Service::Factory.create(Context.instance.config[:dest])
end

.mirror(src_repo) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/gitpusher/runner.rb', line 27

def self.mirror(src_repo)
  repo_name = File.basename(src_repo.url).gsub(/.git$/, '')
  repo_path = File.join(base_dir, "#{repo_name}.git")
  unless File.exist?(repo_path)
    puts "[#{Process.pid}][#{repo_name}]Cloning #{src_repo.url} ..."
    `git clone --mirror #{src_repo.url}`
  end

  local_repo = Grit::Repo.new(repo_path)

  has_remote_mirror = false
  local_repo.remote_list.each do |remote|
    has_remote_mirror = true if remote === 'mirror'
  end

  mirror_repo = dest.repo(repo_name) || dest.create_repo(repo_name)
  unless has_remote_mirror
    local_repo.git.remote({}, 'add', 'mirror', mirror_repo.url)
  end

  Dir.chdir(repo_path) do
    puts "[#{Process.pid}][#{repo_name}]Pruning all stale branches of #{repo_name} ..."
    local_repo.git.remote({}, 'prune', 'origin')

    puts "[#{Process.pid}][#{repo_name}]Fetching from #{src_repo.url} ..."
    local_repo.git.fetch({ :timeout => 300 }, 'origin')

    puts "[#{Process.pid}][#{repo_name}]Pushing to #{mirror_repo.url} ..."
    local_repo.git.push({ :timeout => 300 }, 'mirror','--mirror')
  end
end

.runObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/gitpusher/runner.rb', line 7

def self.run
  context = Context.instance

  src_repos = src.repos
  num_per_process = src_repos.length / context.processes
  num_per_process += 1 unless src_repos.length % context.processes == 0
  FileUtils::Verbose.mkpath base_dir unless File.directory? base_dir
  Dir.chdir(base_dir) do
    src_repos.each_slice(num_per_process) do |repos|
      fork do
        repos.each do |src_repo|
          mirror src_repo
        end
      end
    end
  end

  Process.waitall
end

.srcObject



59
60
61
# File 'lib/gitpusher/runner.rb', line 59

def self.src
  Service::Factory.create(Context.instance.config[:src])
end