Browse files

Refactor, reformat, reunfuck, 6.2.0 is in the bag!

Remove cooloptions, it's not necessary.
  • Loading branch information...
1 parent aeba310 commit 952d7594a305c2bbd119b49f0a792dc98d7a2c63 @evanphx evanphx committed May 31, 2008
Showing with 110 additions and 85 deletions.
  1. +1 −33 bin/gitjour
  2. +2 −2 config/hoe.rb
  3. BIN lib/gitjour/.application.rb.swp
  4. +106 −49 lib/gitjour/application.rb
  5. +1 −1 lib/gitjour/version.rb
View
34 bin/gitjour
@@ -1,41 +1,9 @@
#!/usr/bin/env ruby
require 'rubygems'
-require 'cooloptions'
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"
-
- o.desc "Commands:"
- COMMANDS.each do |c, help|
- o.desc " #{c.ljust(5)} #{help}"
- end
-
- o.desc "\n"
-
- o.desc "Options:"
- 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.")
- 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
-
trap "INT" do
exit!
end
-Gitjour::Application.run(options)
+Gitjour::Application.run(*ARGV)
View
4 config/hoe.rb
@@ -56,7 +56,7 @@ def extra_deps
p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
p.test_globs = ["test/**/test_*.rb"]
p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
- p.extra_deps = ['dnssd', 'cooloptions']
+ p.extra_deps = ['dnssd']
# == Optional
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
#p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
@@ -68,4 +68,4 @@ def extra_deps
CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
-hoe.rsync_args = '-av --delete --ignore-errors'
+hoe.rsync_args = '-av --delete --ignore-errors'
View
BIN lib/gitjour/.application.rb.swp
Binary file not shown.
View
155 lib/gitjour/application.rb
@@ -1,6 +1,8 @@
require 'rubygems'
require 'dnssd'
require 'set'
+require 'gitjour/version'
+
Thread.abort_on_exception = true
module Gitjour
@@ -9,17 +11,16 @@ module Gitjour
class Application
class << self
- def run(options)
- @@verbose = options.verbose
- case options.command
+ def run(*args)
+ case args.shift
when "list"
list
when "clone"
- clone(options.name)
+ clone(*args)
when "serve"
- serve(options.name, options.path, options.port)
+ serve(*args)
when "remote"
- remote(options.name)
+ remote(*args)
else
help
end
@@ -28,79 +29,135 @@ def run(options)
private
def list
service_list.each do |service|
- puts "=== #{service.name} on #{service.host} ==="
+ puts "=== #{service.name} on #{service.host}:#{service.port} ==="
puts " gitjour clone #{service.name}"
- puts " #{service.description}" if service.description && service.description != '' && service.description !~ /^Unnamed repository/
+ if service.description != '' && service.description !~ /^Unnamed repository/
+ puts " #{service.description}"
+ end
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(repository_name, *rest)
+ dir = rest.shift || repository_name
+ if File.exists?(dir)
+ exit_with! "ERROR: Clone directory '#{dir}' already exists."
+ 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}/")
+ puts "Cloning '#{repository_name}' into directory '#{dir}'..."
+
+ service = locate_repo repository_name
+ puts "Connecting to #{service.host}:#{service.port}"
+
+ system "git clone git://#{service.host}:#{service.port}/ #{dir}/"
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}/")
+ def remote(repository_name, *rest)
+ dir = rest.shift || repository_name
+ service = locate_repo repository_name
+ system "git remote add #{dir} git://#{service.host}:#{service.port}/"
end
- def serve(path, port)
- path ||= Dir.pwd
+ def serve(path=Dir.pwd, *rest)
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")
+ name = rest.shift || File.basename(path)
+ port = rest.shift || 9418
+
+ if File.exists?("#{path}/.git")
+ announce_repo(path, name, port.to_i)
+ else
+ Dir["#{path}/*"].each do |dir|
+ if File.directory?(dir)
+ name = File.basename(dir)
+ announce_repo(dir, name, 9418)
+ end
+ end
+ end
+
+ `git-daemon --verbose --export-all --port=#{port} --base-path=#{path} --base-path-relaxed`
+ end
+
+ def help
+ puts "Gitjour #{Gitjour::VERSION::STRING}"
+ puts "Serve up and use git repositories via Bonjour/DNSSD."
+ puts "\nUsage: gitjour <command> [args]"
+ puts
+ puts " list"
+ puts " Lists available repositories."
+ puts
+ puts " clone <project> [<directory>]"
+ puts " Clone a gitjour served repository."
+ puts
+ puts " serve <path_to_project> [<name_of_project>] [<port>] or"
+ puts " <path_to_projects>"
+ puts " Serve up the current directory or projects via gitjour."
+ puts
+ puts " remote <project> [<name>]"
+ puts " Add a Bonjour remote into your current repository."
+ puts " Optionally pass name to not use pwd."
+ puts
end
def exit_with!(message)
STDERR.puts message
exit!
end
- def service_list(looking_for = nil)
- wait_seconds = 5
+ class Done < RuntimeError; end
- service_list = Set.new
- waiting_thread = Thread.new { sleep wait_seconds }
+ def discover(timeout=5)
+ waiting_thread = Thread.current
- service = DNSSD.browse "_git._tcp" do |reply|
+ dns = 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
+ service = GitService.new(reply.name,
+ resolve_reply.target,
+ resolve_reply.port,
+ resolve_reply.text_record['description'].to_s)
+ begin
+ yield service
+ rescue Done
+ waiting_thread.run
end
end
end
- puts "Gathering for up to #{wait_seconds} seconds..."
- waiting_thread.join
- service.stop
- service_list
+
+ puts "Gathering for up to #{timeout} seconds..."
+ sleep timeout
+ dns.stop
end
- def announce_repo(name, path, port)
- return unless File.exists?("#{path}/.git")
- 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|
- puts "Registered #{name}. Starting service."
+ def locate_repo(name)
+ found = nil
+
+ discover do |obj|
+ if obj.name == name
+ found = obj
+ raise Done
+ end
end
+
+ return found
+ end
+
+ def service_list
+ list = Set.new
+ discover { |obj| list << obj }
+
+ return list
end
-
- def cl(command)
- output = `#{command}`
- if @@verbose
- puts command
- puts output
+
+ def announce_repo(path, name, port)
+ return unless File.exists?("#{path}/.git")
+
+ tr = DNSSD::TextRecord.new
+ tr['description'] = File.read("#{path}/.git/description") rescue "a git project"
+
+ DNSSD.register(name, "_git._tcp", 'local', port, tr.encode) do |rr|
+ puts "Registered #{name} on port #{port}. Starting service."
end
end
+
end
end
end
View
2 lib/gitjour/version.rb
@@ -1,7 +1,7 @@
module Gitjour #:nodoc:
module VERSION #:nodoc:
MAJOR = 6
- MINOR = 1
+ MINOR = 2
TINY = 0
STRING = [MAJOR, MINOR, TINY].join('.')

1 comment on commit 952d759

@nicksieger

Thank you for re-un-fuckifying. Much better.

Please sign in to comment.