public
Description: Gitorious aims to provide a great way of doing distributed opensource code collaboration.
Homepage: http://gitorious.org/projects/gitorious
Clone URL: git://github.com/dysinger/gitorious.git
Search Repo:
count how many times a repository has been cloned from ip/country

Signed-off-by: David A. Cuadrado <krawek@gmail.com>
David A. Cuadrado (author)
Mon Apr 14 09:16:29 -0700 2008
commit  9fea730da3f142c6cd00ed669efab9988d4338be
tree    99d6ec223a2ee02ccf308000aaf46a569e00296b
parent  1a4396413782994a5b82d581a4529a60789b4a4d
...
9
10
11
 
12
13
14
...
30
31
32
 
33
34
35
...
227
228
229
 
 
 
 
230
231
232
...
9
10
11
12
13
14
15
...
31
32
33
34
35
36
37
...
229
230
231
232
233
234
235
236
237
238
0
@@ -9,6 +9,7 @@ class Repository < ActiveRecord::Base
0
     :order => "status, id desc", :dependent => :destroy
0
   has_many :proposed_merge_requests, :foreign_key => 'source_repository_id',
0
                 :class_name => 'MergeRequest', :order => "id desc", :dependent => :destroy
0
+ has_many :cloners, :dependent => :destroy
0
   
0
   validates_presence_of :user_id, :project_id, :name
0
   validates_format_of :name, :with => /^[a-z0-9_\-]+$/i,
0
@@ -30,6 +31,7 @@ class Repository < ActiveRecord::Base
0
   
0
   def self.find_by_path(path)
0
     repo_name, project_name = (path.split('/') - GitoriousConfig['repository_base_path'].split('/')).reverse
0
+
0
     project = Project.find_by_slug!(project_name)
0
     project.repositories.find_by_name(repo_name.sub(/\.git/, ""))
0
   end
0
@@ -227,6 +229,10 @@ class Repository < ActiveRecord::Base
0
     users_by_email = users.inject({}){|hash, user| hash[user.email] = user; hash }
0
     users_by_email
0
   end
0
+
0
+ def cloned_from(ip, country_code = "--", country_name = nil)
0
+ cloners.create(:ip => ip, :date => Time.now.utc, :country_code => country_code, :country => country_name)
0
+ end
0
     
0
   protected
0
     def set_as_mainline_if_first
...
41
42
43
 
44
45
...
41
42
43
44
45
46
0
@@ -41,4 +41,5 @@ git push
0
       </div>
0
     </li>
0
   <% end -%>
0
+ <li>The repository has been cloned <%= @repository.cloners.size %> times.</li>
0
 </ul>
0
\ No newline at end of file
...
2
3
4
 
5
6
 
7
8
 
 
9
10
11
...
20
21
22
 
23
24
25
...
27
28
29
30
31
 
32
33
34
...
39
40
41
42
43
44
45
46
47
48
 
 
 
 
 
49
50
51
52
 
 
 
 
 
 
53
54
55
...
58
59
60
 
 
 
 
 
 
 
61
62
 
 
63
64
 
 
 
65
66
67
...
2
3
4
5
6
7
8
9
10
11
12
13
14
15
...
24
25
26
27
28
29
30
...
32
33
34
 
 
35
36
37
38
...
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
...
72
73
74
75
76
77
78
79
80
81
82
 
83
84
85
86
87
88
89
90
91
92
0
@@ -2,10 +2,14 @@
0
 
0
 require 'rubygems'
0
 require 'daemons'
0
+require 'geoip'
0
 require 'socket'
0
 
0
+ENV["RAILS_ENV"] ||= "production"
0
 require File.dirname(__FILE__)+'/../config/environment'
0
 
0
+Rails.configuration.log_level = :info # Disable debug
0
+
0
 BASE_PATH = File.expand_path(GitoriousConfig['repository_base_path'])
0
 
0
 module Git
0
@@ -20,6 +24,7 @@ class Daemon
0
   def initialize
0
     daemonize(File.join(RAILS_ROOT, "log", "git-daemon.log"))
0
     
0
+ @geoip = GeoIP.new(File.join(RAILS_ROOT, "data", "GeoIP.dat"))
0
     trap "CLD" do
0
       pid = Process.wait
0
       log(pid, "Disconnected. (status=#{$?.exitstatus})")
0
@@ -27,8 +32,7 @@ class Daemon
0
     
0
     port = 9418
0
     server = TCPServer.new('localhost', port)
0
-
0
- service_regexp = /(\d{4})(git-[\w-]+)\s(.+)\x0host=([\w\.\-]+)/.freeze
0
+ service_regexp = /(\w{4})(git-[\w-]+)\s(.+)\x0host=([\w\.\-]+)/.freeze
0
     while session = server.accept
0
       line = session.recv(1000)
0
       timeout = 30
0
@@ -39,17 +43,27 @@ class Daemon
0
         host = $4
0
         
0
         path = "#{BASE_PATH}/#{path}"
0
-
0
         if !File.directory?(path)
0
           log(Process.pid, "Invalid path: #{path}")
0
           session.close
0
           next
0
         end
0
         
0
+ if !File.exist?(File.join(path, "git-daemon-export-ok"))
0
+ session.close
0
+ next
0
+ end
0
+
0
         Dir.chdir(path) do
0
           cmd = "git-upload-pack --strict --timeout=#{timeout} ."
0
           
0
           fork do
0
+ repository = nil
0
+ begin
0
+ ActiveRecord::Base.allow_concurrency = true
0
+ repository = ::Repository.find_by_path(path)
0
+ rescue Exception
0
+ end
0
             pid = Process.pid
0
             domain, port, name, ip = session.addr
0
             log(pid, "Connection from #{ip}")
0
@@ -58,10 +72,21 @@ class Daemon
0
             $stdin.reopen(session)
0
             session.close
0
             
0
+ if repository
0
+ localization = @geoip.country(ip)
0
+ repository.cloned_from(ip, localization[3], localization[5])
0
+ else
0
+ log(pid, "Cannot find repository: #{path}")
0
+ end
0
+
0
             exec(cmd)
0
- exit
0
+
0
+ exit!
0
           end
0
         end
0
+ else
0
+ $stderr.puts "Invalid request: #{line}"
0
+ session.close
0
       end
0
     end
0
   end

Comments

    No one has commented yet.