Skip to content

Instantly share code, notes, and snippets.

@brainopia
Created October 25, 2015 22:53
Show Gist options
  • Save brainopia/794611951dc733611768 to your computer and use it in GitHub Desktop.
Save brainopia/794611951dc733611768 to your computer and use it in GitHub Desktop.
git mirrors
require 'yaml'
require 'json'
require 'bundler/inline'
gemfile _install=true do
source 'https://rubygems.org'
gem 'pry'
gem 'puma'
gem 'sinatra', require: 'sinatra/base'
end
class WebHook < Sinatra::Application
set :bind, '0.0.0.0'
set :port, 3000
set :queue, Queue.new
post '/payload' do
push = JSON request.body.read
settings.queue << push['repository']['ssh_url']
end
end
Thread.abort_on_exception = true
Thread.new { WebHook.run! }
class Mirrors
# repos: { from_repo => to_repo, ... }
def initialize(repos)
@repos = repos
end
def sync_all
@repos.each_key {|repo| sync repo }
end
def sync_webhooks_forever
loop do
repo = WebHook.queue.pop
if @repos[repo]
sync repo
else
puts "Missing repo: #{repo}"
end
end
end
private
def sync(from)
to = @repos[from]
unless File.exists? to
system <<-SH
git clone --mirror #{from} #{to} &&
cd #{to} &&
git remote set-url --push origin #{to}
SH
end
system <<-SH
cd #{to} &&
git fetch -p origin &&
git push --mirror
SH
end
def system(*args)
puts *args
super
end
end
mirrors = Mirrors.new YAML.load_file 'mirrors.yml'
mirrors.sync_all
mirrors.sync_webhooks_forever
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment