Skip to content

Commit

Permalink
Merged latest.
Browse files Browse the repository at this point in the history
  • Loading branch information
ntalbott committed May 31, 2008
1 parent 261a4d7 commit e196aba
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 55 deletions.
2 changes: 1 addition & 1 deletion Manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ Rakefile
config/hoe.rb
config/requirements.rb
lib/gitjour.rb
lib/gitjour/application.rb
lib/gitjour/version.rb
log/debug.log
script/destroy
script/generate
script/txt2html
Expand Down
9 changes: 8 additions & 1 deletion README.txt
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
README
README

DEVELOPMENT

How to test from the console:

irb -r 'lib/gitjour/application'
> Gitjour::Application.run "list"
53 changes: 6 additions & 47 deletions bin/gitjour
Original file line number Diff line number Diff line change
@@ -1,38 +1,14 @@
#!/usr/bin/env ruby
require 'rubygems'
require 'dnssd'
require 'set'
require 'cooloptions'

Thread.abort_on_exception = true

GitService = Struct.new(:name, :host, :port, :description)

def service_list(looking_for = nil)
wait_seconds = 5

service_list = Set.new
waiting_thread = Thread.new { sleep wait_seconds }

service = DNSSD.browse "_git._tcp" do |reply|
DNSSD.resolve reply.name, reply.type, reply.domain do |resolve_reply|
service_list << GitService.new(reply.name, resolve_reply.target, resolve_reply.port, resolve_reply.text_record['description'])
if looking_for && reply.name == looking_for
waiting_thread.kill
end
end
end
puts "Gathering for up to #{wait_seconds} seconds..."
waiting_thread.join
service.stop
service_list
end
require 'gitjour'

COMMANDS = {
'list' => 'Lists available repositories.',
'clone' => 'Clone a gitjour served repository.',
'serve' => 'Serve up the current directory via gitjour.'
}

options = CoolOptions.parse!("[options] command [name]") do |o|
o.desc 'Serve up and use git repositories via Bonjour/DNSSD.'
o.desc "\n"
Expand All @@ -45,33 +21,16 @@ options = CoolOptions.parse!("[options] command [name]") do |o|
o.desc "\n"

o.desc "Options:"
o.on 'port PORT', "Pass to serve to serve on a different port.", 9148
o.on 'port PORT', "Pass to serve to serve on a different port.", 9418
o.on 'verbose', "Verbose output.", false

o.after do |result|
(result.command = ARGV.shift) || o.error("Command is required.")
o.error("Unknown command.") if !COMMANDS.keys.include?(result.command)
result.name = ARGV.shift
o.error("Name is required.") if result.command == "clone" && !result.name
result.port = result.port.to_i
end
end

case options.command
when "list"
service_list.each do |service|
puts "#{service.name} on #{service.host}: gitjour clone #{service.name}"
end
when "clone"
name_of_share = options.name
service = service_list(options.name).detect{|service| service.name == options.name}
system("git clone git://#{service.host}:#{service.port}/ #{options.name}")
when "serve"
name = options.name || File.basename(Dir.pwd)
tr = DNSSD::TextRecord.new
tr['description'] = File.read(".git/description") rescue "a git project"
DNSSD.register(name, "_git._tcp", 'local', options.port.to_i, tr.encode) do |register_reply|
p register_reply
end
`git-daemon --verbose --export-all --base-path=#{Dir.pwd} --port=#{options.port}`
end


Gitjour::Application.run(options)
6 changes: 1 addition & 5 deletions lib/gitjour.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
$:.unshift File.dirname(__FILE__)

module Gitjour

end
require 'gitjour/application'
92 changes: 92 additions & 0 deletions lib/gitjour/application.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
require 'rubygems'
require 'dnssd'
require 'set'
Thread.abort_on_exception = true

module Gitjour
GitService = Struct.new(:name, :host, :port, :description)
class Application

class << self
def run(options)
@@verbose = options.verbose
case options.command
when "list"
service_list.each do |service|
puts "=== #{service.name} on #{service.host} ==="
puts " gitjour clone #{service.name}"
puts " #{service.description}" if service.description && service.description != '' && service.description !~ /^Unnamed repository/
puts
end
when "clone"
clone(options.name)
when "serve"
serve(options.name, options.port)
else
help
end
end

private
def clone(name)
service = service_list(name).detect{|service| service.name == name} rescue exit_with!("Couldn't find #{name}")
cl("git clone git://#{service.host}:#{service.port}/ #{name}/")
end

def serve(path, port)
path ||= Dir.pwd
path = File.expand_path(path)
File.exists?("#{path}/.git") ? announce_repo(path, port) : Dir["#{path}/*"].each_with_index{|dir,i| announce_repo(dir, port+i) if File.directory?(dir)}
cl("git-daemon --verbose --export-all --port=#{port} --base-path=#{path} --base-path-relaxed")
end

def exit_with!(message)
STDERR.puts message
exit!
end

def service_list(looking_for = nil)
wait_seconds = 5

service_list = Set.new
waiting_thread = Thread.new { sleep wait_seconds }

service = DNSSD.browse "_git._tcp" do |reply|
DNSSD.resolve reply.name, reply.type, reply.domain do |resolve_reply|
service_list << GitService.new(reply.name, resolve_reply.target, resolve_reply.port, resolve_reply.text_record['description'])
if looking_for && reply.name == looking_for
waiting_thread.kill
end
end
end
puts "Gathering for up to #{wait_seconds} seconds..."
waiting_thread.join
service.stop
service_list
end

def announce_repo(path, port)
return unless File.exists?("#{path}/.git")
name = "#{File.basename(path)}"
tr = DNSSD::TextRecord.new
tr['description'] = File.read(".git/description") rescue "a git project"
DNSSD.register(name, "_git._tcp", 'local', port, tr.encode) do |register_reply|
puts "Registered #{name}. Starting service."
end
end

def cl(command)
output = `#{command}`
if @@verbose
puts command
puts output
end
end
end


end
end



2 changes: 1 addition & 1 deletion lib/gitjour/version.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Gitjour #:nodoc:
module VERSION #:nodoc:
MAJOR = 3
MAJOR = 6
MINOR = 0
TINY = 0

Expand Down

0 comments on commit e196aba

Please sign in to comment.