GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Fork of chad/gitjour
Description: Serve git and advertise with bonjour
Homepage: http://rubyforge.org/projects/gitjour
Clone URL: git://github.com/wilson/gitjour.git
Refactor, reformat, reunfuck, 6.2.0 is in the bag!

Remove cooloptions, it's not necessary.
evanphx (author)
Sat May 31 22:59:08 -0700 2008
commit  952d7594a305c2bbd119b49f0a792dc98d7a2c63
tree    2cb084b6d9b0dfad18a0cab02cb7aca74560274a
parent  aeba310d792840faeb27bdcac6a4ef446636ac8f
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 
...
1
2
 
3
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
6
7
8
 
9
0
@@ -1,41 +1,9 @@
0
 #!/usr/bin/env ruby
0
 require 'rubygems'
0
-require 'cooloptions'
0
 require 'gitjour'
0
 
0
-COMMANDS = {
0
- 'list' => 'Lists available repositories.',
0
- 'clone' => 'Clone a gitjour served repository.',
0
- 'serve' => 'Serve up the current directory via gitjour.'
0
-}
0
-
0
-options = CoolOptions.parse!("[options] command [name]") do |o|
0
- o.desc 'Serve up and use git repositories via Bonjour/DNSSD.'
0
- o.desc "\n"
0
-
0
- o.desc "Commands:"
0
- COMMANDS.each do |c, help|
0
- o.desc " #{c.ljust(5)} #{help}"
0
- end
0
-
0
- o.desc "\n"
0
-
0
- o.desc "Options:"
0
- o.on '(P)port PORT', "Pass to serve to serve on a different port.", 9418
0
- o.on 'path PATH', "Path to serve."
0
- o.on 'verbose', "Verbose output.", false
0
-
0
- o.after do |result|
0
- (result.command = ARGV.shift) || o.error("Command is required.")
0
- o.error("Unknown command.") if !COMMANDS.keys.include?(result.command)
0
- result.name = ARGV.shift
0
- o.error("Name is required.") if result.command == "clone" && !result.name
0
- result.port = result.port.to_i
0
- end
0
-end
0
-
0
 trap "INT" do
0
   exit!
0
 end
0
 
0
-Gitjour::Application.run(options)
0
+Gitjour::Application.run(*ARGV)
...
56
57
58
59
 
60
61
62
...
68
69
70
71
72
 
...
56
57
58
 
59
60
61
62
...
68
69
70
 
71
72
0
@@ -56,7 +56,7 @@ hoe = Hoe.new(GEM_NAME, VERS) do |p|
0
   p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
0
   p.test_globs = ["test/**/test_*.rb"]
0
   p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
0
- p.extra_deps = ['dnssd', 'cooloptions']
0
+ p.extra_deps = ['dnssd']
0
   # == Optional
0
   p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
0
   #p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
0
@@ -68,4 +68,4 @@ end
0
 CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
0
 PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
0
 hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
0
-hoe.rsync_args = '-av --delete --ignore-errors'
0
\ No newline at end of file
0
+hoe.rsync_args = '-av --delete --ignore-errors'
...
1
2
3
 
 
4
5
6
...
9
10
11
12
13
14
 
 
15
16
17
18
 
19
20
 
21
22
 
23
24
25
...
28
29
30
31
 
32
33
 
 
 
34
35
36
37
38
39
40
41
42
43
 
 
 
 
 
44
45
46
47
 
 
 
 
 
 
48
49
50
51
52
 
 
 
 
53
54
55
56
 
57
58
59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
61
62
...
64
65
66
67
68
 
69
70
71
 
 
72
73
 
74
75
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
...
1
2
3
4
5
6
7
8
...
11
12
13
 
 
 
14
15
16
17
18
 
19
20
 
21
22
 
23
24
25
26
...
29
30
31
 
32
33
 
34
35
36
37
38
39
40
 
 
 
 
 
 
41
42
43
44
45
46
 
 
 
47
48
49
50
51
52
53
54
 
 
 
55
56
57
58
59
60
 
 
61
62
 
 
63
64
65
66
67
68
69
70
71
72
73
74
75
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
...
103
104
105
 
 
106
107
 
 
108
109
110
 
111
112
 
 
 
113
114
115
116
117
118
119
120
121
122
123
 
 
 
 
124
125
126
127
128
129
 
 
 
 
 
 
 
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
 
 
 
 
 
 
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
0
@@ -1,6 +1,8 @@
0
 require 'rubygems'
0
 require 'dnssd'
0
 require 'set'
0
+require 'gitjour/version'
0
+
0
 Thread.abort_on_exception = true
0
 
0
 module Gitjour
0
@@ -9,17 +11,16 @@ module Gitjour
0
   class Application
0
 
0
     class << self
0
- def run(options)
0
- @@verbose = options.verbose
0
- case options.command
0
+ def run(*args)
0
+ case args.shift
0
           when "list"
0
             list
0
           when "clone"
0
- clone(options.name)
0
+ clone(*args)
0
           when "serve"
0
- serve(options.name, options.path, options.port)
0
+ serve(*args)
0
           when "remote"
0
- remote(options.name)
0
+ remote(*args)
0
           else
0
             help
0
         end
0
@@ -28,35 +29,73 @@ module Gitjour
0
       private
0
       def list
0
         service_list.each do |service|
0
- puts "=== #{service.name} on #{service.host} ==="
0
+ puts "=== #{service.name} on #{service.host}:#{service.port} ==="
0
           puts " gitjour clone #{service.name}"
0
- puts " #{service.description}" if service.description && service.description != '' && service.description !~ /^Unnamed repository/
0
+ if service.description != '' && service.description !~ /^Unnamed repository/
0
+ puts " #{service.description}"
0
+ end
0
           puts
0
         end
0
       end
0
 
0
- def get_host_and_share(repository_name)
0
- name_of_share = repository_name || fail("You have to pass in a name")
0
- host = service_list(name_of_share).detect{|service| service.name == name_of_share}.host rescue exit_with!("Couldn't find #{name_of_share}")
0
- system("git clone git://#{host}/ #{name_of_share}/")
0
- [host, name_of_share]
0
- end
0
+ def clone(repository_name, *rest)
0
+ dir = rest.shift || repository_name
0
+ if File.exists?(dir)
0
+ exit_with! "ERROR: Clone directory '#{dir}' already exists."
0
+ end
0
 
0
- def clone(name)
0
- service = service_list(name).detect{|service| service.name == name} rescue exit_with!("Couldn't find #{name}")
0
- cl("git clone git://#{service.host}:#{service.port}/ #{name}/")
0
+ puts "Cloning '#{repository_name}' into directory '#{dir}'..."
0
+
0
+ service = locate_repo repository_name
0
+ puts "Connecting to #{service.host}:#{service.port}"
0
+
0
+ system "git clone git://#{service.host}:#{service.port}/ #{dir}/"
0
       end
0
 
0
- def remote(repository_name,*rest)
0
- host, name_of_share = get_host_and_share(repository_name)
0
- system("git remote add #{name_of_share} git://#{host}/")
0
+ def remote(repository_name, *rest)
0
+ dir = rest.shift || repository_name
0
+ service = locate_repo repository_name
0
+ system "git remote add #{dir} git://#{service.host}:#{service.port}/"
0
       end
0
 
0
- def serve(path, port)
0
- path ||= Dir.pwd
0
+ def serve(path=Dir.pwd, *rest)
0
         path = File.expand_path(path)
0
- File.exists?("#{path}/.git") ? announce_repo(path, port) : Dir["#{path}/*"].each_with_index{|dir,i| announce_repo(dir, port+i) if File.directory?(dir)}
0
- cl("git-daemon --verbose --export-all --port=#{port} --base-path=#{path} --base-path-relaxed")
0
+ name = rest.shift || File.basename(path)
0
+ port = rest.shift || 9418
0
+
0
+ if File.exists?("#{path}/.git")
0
+ announce_repo(path, name, port.to_i)
0
+ else
0
+ Dir["#{path}/*"].each do |dir|
0
+ if File.directory?(dir)
0
+ name = File.basename(dir)
0
+ announce_repo(dir, name, 9418)
0
+ end
0
+ end
0
+ end
0
+
0
+ `git-daemon --verbose --export-all --port=#{port} --base-path=#{path} --base-path-relaxed`
0
+ end
0
+
0
+ def help
0
+ puts "Gitjour #{Gitjour::VERSION::STRING}"
0
+ puts "Serve up and use git repositories via Bonjour/DNSSD."
0
+ puts "\nUsage: gitjour <command> [args]"
0
+ puts
0
+ puts " list"
0
+ puts " Lists available repositories."
0
+ puts
0
+ puts " clone <project> [<directory>]"
0
+ puts " Clone a gitjour served repository."
0
+ puts
0
+ puts " serve <path_to_project> [<name_of_project>] [<port>] or"
0
+ puts " <path_to_projects>"
0
+ puts " Serve up the current directory or projects via gitjour."
0
+ puts
0
+ puts " remote <project> [<name>]"
0
+ puts " Add a Bonjour remote into your current repository."
0
+ puts " Optionally pass name to not use pwd."
0
+ puts
0
       end
0
 
0
       def exit_with!(message)
0
@@ -64,43 +103,61 @@ module Gitjour
0
         exit!
0
       end
0
 
0
- def service_list(looking_for = nil)
0
- wait_seconds = 5
0
+ class Done < RuntimeError; end
0
 
0
- service_list = Set.new
0
- waiting_thread = Thread.new { sleep wait_seconds }
0
+ def discover(timeout=5)
0
+ waiting_thread = Thread.current
0
 
0
- service = DNSSD.browse "_git._tcp" do |reply|
0
+ dns = DNSSD.browse "_git._tcp" do |reply|
0
           DNSSD.resolve reply.name, reply.type, reply.domain do |resolve_reply|
0
- service_list << GitService.new(reply.name, resolve_reply.target, resolve_reply.port, resolve_reply.text_record['description'])
0
- if looking_for && reply.name == looking_for
0
- waiting_thread.kill
0
+ service = GitService.new(reply.name,
0
+ resolve_reply.target,
0
+ resolve_reply.port,
0
+ resolve_reply.text_record['description'].to_s)
0
+ begin
0
+ yield service
0
+ rescue Done
0
+ waiting_thread.run
0
             end
0
           end
0
         end
0
- puts "Gathering for up to #{wait_seconds} seconds..."
0
- waiting_thread.join
0
- service.stop
0
- service_list
0
+
0
+ puts "Gathering for up to #{timeout} seconds..."
0
+ sleep timeout
0
+ dns.stop
0
       end
0
 
0
- def announce_repo(name, path, port)
0
- return unless File.exists?("#{path}/.git")
0
- name = share_name || File.basename(path)
0
- tr = DNSSD::TextRecord.new
0
- tr['description'] = File.read(".git/description") rescue "a git project"
0
- DNSSD.register(name, "_git._tcp", 'local', port, tr.encode) do |register_reply|
0
- puts "Registered #{name}. Starting service."
0
+ def locate_repo(name)
0
+ found = nil
0
+
0
+ discover do |obj|
0
+ if obj.name == name
0
+ found = obj
0
+ raise Done
0
+ end
0
         end
0
+
0
+ return found
0
+ end
0
+
0
+ def service_list
0
+ list = Set.new
0
+ discover { |obj| list << obj }
0
+
0
+ return list
0
       end
0
-
0
- def cl(command)
0
- output = `#{command}`
0
- if @@verbose
0
- puts command
0
- puts output
0
+
0
+ def announce_repo(path, name, port)
0
+ return unless File.exists?("#{path}/.git")
0
+
0
+ tr = DNSSD::TextRecord.new
0
+ tr['description'] = File.read("#{path}/.git/description") rescue "a git project"
0
+
0
+ DNSSD.register(name, "_git._tcp", 'local', port, tr.encode) do |rr|
0
+ puts "Registered #{name} on port #{port}. Starting service."
0
         end
0
       end
0
+
0
     end
0
   end
0
 end
...
1
2
3
4
 
5
6
7
...
1
2
3
 
4
5
6
7
0
@@ -1,7 +1,7 @@
0
 module Gitjour #:nodoc:
0
   module VERSION #:nodoc:
0
     MAJOR = 6
0
- MINOR = 1
0
+ MINOR = 2
0
     TINY = 0
0
 
0
     STRING = [MAJOR, MINOR, TINY].join('.')

Comments

    No one has commented yet.