[FIX] svn.validate_server_connection was broken by head() functionality. Fixed, and...
[obnox/ohloh/ohloh_scm.git] / lib / scm / adapters / svn / validation.rb
1 module Scm::Adapters
2         class SvnAdapter < AbstractAdapter
3                 def self.url_regex
4                         /^(file|http|https|svn):\/\/(\/)?[A-Za-z0-9_\-\.]+(:\d+)?(\/[A-Za-z0-9_\-\.\/\+%^~]*)?$/
5                 end
6
7                 def self.public_url_regex
8                         /^(http|https|svn):\/\/[A-Za-z0-9_\-\.]+(:\d+)?(\/[A-Za-z0-9_\-\.\/\+%^~]*)?$/
9                 end
10
11                 def normalize
12                         super
13                         @url = path_to_file_url(@url)
14                         @url = force_https_if_sourceforge(@url)
15                         @branch_name = @branch_name[0..-2] if @branch_name && @branch_name[-1..-1] == '/'
16                         self
17                 end
18
19                 # If the URL is a simple directory path, make sure it is prefixed by file://
20                 def path_to_file_url(path)
21                         url =~ /:\/\// ? url : 'file://' + File.expand_path(path)
22                 end
23
24                 def force_https_if_sourceforge(url)
25                         # SourceForge requires https for svnsync
26                         url =~ /http(:\/\/.*svn\.sourceforge\.net.*)/ ? "https#{$1}" : url
27                 end
28
29                 def validate_server_connection
30                         return unless valid?
31                         begin
32                                 if head_token.nil?
33                                         @errors << [:failed, "The server did not respond to a 'svn info' command. Is the URL correct?"]
34                                 elsif self.url[0..root.length] != root
35                                         @errors << [:failed, "The URL did not match the Subversion root #{root}. Is the URL correct?"]
36                                 elsif ls.nil?
37                                         @errors << [:failed, "The server did not respond to a 'svn ls' command. Is the URL correct?"]
38                                 end
39                         rescue
40                                 logger.error { $!.inspect }
41                                 @errors << [:failed, "An error occured connecting to the server. Check the URL, username, and password."]
42                         end
43                 end
44
45                 # From the given URL, determine which part of it is the root and which part of it is the branch_name.
46                 # The current branch_name is overwritten.
47                 def recalc_branch_name
48                         @branch_name = @url ? @url[root.length..-1] : @branch_name
49                         @branch_name = @branch_name[0..-2] if @branch_name[-1..-1] == '/'
50                 end
51
52                 def guess_forge
53                         u = @url =~ /:\/\/(.*\.?svn\.)?([^\/^:]+)(:\d+)?\// ? $2 : nil
54                         case u
55                         when /(googlecode\.com$)/, /(tigris\.org$)/, /(sunsource\.net$)/, /(java\.net$)/,
56                                 /(openoffice\.org$)/, /(netbeans\.org$)/, /(dev2dev\.bea\.com$)/
57                                 $1
58                         else
59                                 u
60                         end
61                 end
62         end
63 end