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
2 parents 4c0ed51 + a7d3c24 commit aeba310
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
5 changes: 3 additions & 2 deletions bin/gitjour
Expand Up @@ -21,8 +21,9 @@ 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.", 9418
o.on 'verbose', "Verbose output.", false
o.on '(P)port PORT', "Pass to serve to serve on a different port.", 9418
o.on 'path PATH', "Path to serve."
o.on 'verbose', "Verbose output.", false

o.after do |result|
(result.command = ARGV.shift) || o.error("Command is required.")
Expand Down
49 changes: 33 additions & 16 deletions lib/gitjour/application.rb
Expand Up @@ -5,50 +5,69 @@

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
list
when "clone"
clone(options.name)
when "serve"
serve(options.name, options.port)
serve(options.name, options.path, options.port)
when "remote"
remote(options.name)
else
help
end
end

private
def 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
end

def get_host_and_share(repository_name)
name_of_share = repository_name || fail("You have to pass in a name")
host = service_list(name_of_share).detect{|service| service.name == name_of_share}.host rescue exit_with!("Couldn't find #{name_of_share}")
system("git clone git://#{host}/ #{name_of_share}/")
[host, name_of_share]
end

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 remote(repository_name,*rest)
host, name_of_share = get_host_and_share(repository_name)
system("git remote add #{name_of_share} git://#{host}/")
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
service_list = Set.new
waiting_thread = Thread.new { sleep wait_seconds }

service = DNSSD.browse "_git._tcp" do |reply|
Expand All @@ -61,13 +80,13 @@ def service_list(looking_for = nil)
end
puts "Gathering for up to #{wait_seconds} seconds..."
waiting_thread.join
service.stop
service.stop
service_list
end

def announce_repo(path, port)
def announce_repo(name, path, port)
return unless File.exists?("#{path}/.git")
name = "#{File.basename(path)}"
name = share_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|
Expand All @@ -83,8 +102,6 @@ def cl(command)
end
end
end


end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/gitjour/version.rb
@@ -1,7 +1,7 @@
module Gitjour #:nodoc:
module VERSION #:nodoc:
MAJOR = 6
MINOR = 0
MINOR = 1
TINY = 0

STRING = [MAJOR, MINOR, TINY].join('.')
Expand Down

0 comments on commit aeba310

Please sign in to comment.