Module: SyncGithubForks::Ctrl

Defined in:
lib/sync_github_forks/ctrl.rb

Class Method Summary collapse

Class Method Details

.list(options) ⇒ Object



2
3
4
5
6
7
8
9
10
11
# File 'lib/sync_github_forks/ctrl.rb', line 2

def self.list(options)
  repo_list = options.config.keys

  if repo_list.empty?
    puts "No repositories found in '#{options.config_file}'"
  else
    puts "Repos in '#{options.config_file}'"
    puts %(  * #{repo_list.sort.join("\n  * ")})
  end
end

.sync(options) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/sync_github_forks/ctrl.rb', line 13

def self.sync(options)
  require 'tmpdir'
  require 'octokit'

  line_sep = %(\n#{'='*20}\n)

  Octokit.auto_paginate = true
  client = Octokit::Client.new(:access_token => options.github_token)

  if options.repo
    forked_repos = { options.repo => options.config[options.repo] }
  else
    forked_repos = options.config
  end

  forked_repos.keys.sort.each do |repo_name|
    repo = client.repo(repo_name)

    if repo[:fork]
      Dir.mktmpdir do |tmpdir|
        Dir.chdir tmpdir do
          begin

            puts line_sep

            puts "Processing #{repo_name}...\n\n"

            %x(git clone #{repo[:ssh_url]} >& /dev/null)

            Dir.chdir repo[:name] do
              remote_url = repo[:parent][:html_url]

              %x(git remote add vendor #{remote_url} >& /dev/null)
              %x(git fetch --all >& /dev/null)
              %x(git fetch --tags >& /dev/null)

              branches = forked_repos[repo_name]['branches']
              if !branches || branches.empty?
                branches = client.branches(repo_name).map{|x| x = x[:name]}
              end

              branches.each do |branch|
                %x(git checkout vendor/#{branch} >& /dev/null)
                %x(git checkout #{branch} >& /dev/null)
                %x(git pull --ff-only vendor #{branch} >& /dev/null)

                if $?.success?
                  $stdout.puts("Updated #{branch} from #{remote_url}")
                else
                  $stderr.puts("WARNING: Could not fast forward branch #{branch} from #{remote_url}")
                  next
                end

                %x(git push origin HEAD:#{branch})
              end

              if forked_repos[repo_name]['tags']
                %x(git push origin --tags >& /dev/null)
              end

              puts line_sep
            end

          rescue StandardError => e
            $stderr.puts("Error: Failure updating #{repo_name}: #{e}")
          end
        end
      end
    end
  end
end