76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/ping_pong_pear.rb', line 76
def self.start args
require 'webrick'
require 'securerandom'
require 'shellwords'
project_name = args.first || File.basename(Dir.pwd)
post_commit_hook = '.git/hooks/post-commit'
uuid = SecureRandom.uuid
system 'git update-server-info'
server = WEBrick::HTTPServer.new Port: 0, DocumentRoot: '.git'
http_port = server.listeners.map { |x| x.addr[1] }.first
my_address = Broadcaster.my_public_address.ip_address
File.open(post_commit_hook, 'w') { |f|
f.puts "#!/usr/bin/env ruby"
f.puts "ARGV[0] = 'update'"
f.puts "ARGV[1] = '#{project_name}'"
f.puts "ARGV[2] = '#{uuid}'"
f.puts "ARGV[3] = '#{my_address}'"
f.puts "ARGV[4] = '#{http_port}'"
f.write File.read __FILE__
}
File.chmod 0755, post_commit_hook
Thread.new {
listener = PingPongPear::Listener.new
listener.start { |cmd, name, *rest|
next unless name == project_name
case cmd
when 'locate' then Broadcaster.send_location project_name, my_address, http_port
when 'commit'
unless rest.first == uuid
url = "http://#{rest.drop(1).join(':')}"
system "git pull #{Shellwords.escape(url)}"
end
end
}
}
trap('INT') {
File.unlink post_commit_hook
server.shutdown
}
server.start
end
|