Further repo browser/Dolt integration work
[gitorious:mainline.git] / lib / gitorious.rb
1 # encoding: utf-8
2 #--
3 #   Copyright (C) 2011-2012 Gitorious AS
4 #
5 #   This program is free software: you can redistribute it and/or modify
6 #   it under the terms of the GNU Affero General Public License as published by
7 #   the Free Software Foundation, either version 3 of the License, or
8 #   (at your option) any later version.
9 #
10 #   This program is distributed in the hope that it will be useful,
11 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #   GNU Affero General Public License for more details.
14 #
15 #   You should have received a copy of the GNU Affero General Public License
16 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #++
18 require "gitorious/configurable"
19 require "gitorious/mount_point"
20
21 module Gitorious
22   VERSION = "3.0.0"
23
24   # Application-wide configuration settings.
25   Configuration = Configurable.new("GITORIOUS")
26   Configuration.rename("gitorious_host", "host")
27   Configuration.rename("gitorious_client_host", "client_host")
28   Configuration.rename("gitorious_client_port", "client_port")
29   Configuration.rename("sender_email_address", "email_sender")
30   Configuration.rename("gitorious_support_email", "support_email")
31   Configuration.rename("gitorious_clone_host", "git_daemon_host")
32   Configuration.rename("use_ssl", "scheme", <<-EOF) { |use_ssl| use_ssl ? "https" : "http" }
33 The scheme setting should be set to "http"
34 or "https".
35   EOF
36   Configuration.rename("gitorious_user", "user")
37   Configuration.rename("repos_and_projects_private_by_default", "projects_default_private", <<-EOF)
38 Please note that this setting has been
39 split into two settings:
40 projects_default_private and repositories_default_private.
41   EOF
42   Configuration.rename("disable_record_throttling", "enable_record_throttling", <<-EOF) { |d| !d }
43 You should invert this value.
44   EOF
45   Configuration.rename("exception_notification_emails", "exception_recipients")
46   Configuration.rename("only_site_admins_can_create_projects", "enable_project_approvals")
47   Configuration.rename("hide_http_clone_urls", "enable_git_http", <<-MSG) { |d| !d }
48 You should invert this value.
49   MSG
50   Configuration.rename("hide_git_clone_urls", "enable_git_daemon", <<-MSG) { |d| !d }
51 You should invert this value.
52   MSG
53   Configuration.rename("custom_username_label", "username_label")
54   Configuration.rename("use_ldap_authorization", "enable_ldap_authorization")
55   Configuration.rename("show_license_descriptions_in_sidebar", "enable_sidebar_license_descriptions")
56
57   Configuration.on_deprecation do |old, new, comment|
58     $stderr.puts(<<-EOF)
59 WARNING! Setting '#{old}' in config/gitorious.yml is deprecated.
60 Use '#{new}' instead. Many configuration settings have changed
61 in Gitorious 3, please refer to config/gitorious.sample.yml for full documentation.
62 #{comment}
63     EOF
64   end
65
66   def self.site
67     return @site if @site && cache?
68     host = Gitorious::Configuration.get("host", "gitorious.local")
69     port = Gitorious::Configuration.get("port")
70     scheme = Gitorious::Configuration.get("scheme")
71     @site = Gitorious::HttpMountPoint.new(host, port, scheme)
72   end
73
74   def self.scheme; site.scheme; end
75   def self.host; site.host; end
76   def self.port; site.port; end
77   def self.ssl?; site.ssl?; end
78   def self.url(path); site.url(path); end
79
80   def self.client
81     return @client if @client && cache?
82     host = Gitorious::Configuration.get("client_host", "localhost")
83     port = Gitorious::Configuration.get("client_port")
84     @client = Gitorious::HttpMountPoint.new(host, port)
85   end
86
87   def self.git_daemon
88     return @git_daemon if @git_daemon && cache?
89     return nil if !Gitorious::Configuration.get("enable_git_daemon", true)
90     host = Gitorious::Configuration.get("git_daemon_host") { Gitorious.host }
91     port = Gitorious::Configuration.get("git_daemon_port")
92     @git_daemon = Gitorious::GitMountPoint.new(host, port)
93   end
94
95   def self.ssh_daemon
96     return @ssh_daemon if @ssh_daemon && cache?
97     return nil if !Gitorious::Configuration.get("enable_ssh_daemon", true)
98     host = Gitorious::Configuration.get("ssh_daemon_host") { Gitorious.host }
99     @ssh_daemon = Gitorious::GitSshMountPoint.new(Gitorious.user, host)
100   end
101
102   def self.git_http
103     return @git_http if @git_http && cache?
104     return nil if !Gitorious::Configuration.get("enable_git_http", true)
105     host = Gitorious::Configuration.get("git_http_host") { Gitorious.host }
106     port = Gitorious::Configuration.get("git_http_port") { Gitorious.port }
107     scheme = Gitorious::Configuration.get("git_http_scheme") { Gitorious.scheme }
108     @git_http = Gitorious::HttpMountPoint.new(host, port, scheme)
109   end
110
111   def self.default_remote_url(repository)
112     (ssh_daemon || git_daemon || git_http).url(repository.gitdir)
113   end
114
115   def self.email_sender
116     return @email_sender if @email_sender && cache?
117     default = "Gitorious <no-reply@#{host}>"
118     @email_sender = Gitorious::Configuration.get("email_sender", default)
119   end
120
121   def self.user
122     return @user if @user && cache?
123     @user = Gitorious::Configuration.get("user", "git")
124   end
125
126   def self.public?
127     return @public if !@public.nil? && cache?
128     @public = Gitorious::Configuration.get("public_mode", true)
129   end
130
131   def self.private_repositories?
132     return @private_repos if !@private_repos.nil? && cache?
133     @private_repos = Gitorious::Configuration.get("enable_private_repositories", false)
134   end
135
136   def self.projects_default_private?
137     return @projdefpriv if !@projdefpriv.nil? && cache?
138     @projdefpriv = private_repositories? && Gitorious::Configuration.get("projects_default_private", false)
139   end
140
141   def self.repositories_default_private?
142     return @repodefpriv if !@repodefpriv.nil? && cache?
143     @repodefpriv = private_repositories? && Gitorious::Configuration.get("repositories_default_private", false)
144   end
145
146   def self.support_email
147     return @support_email if @support_email && cache?
148     @support_email = Gitorious::Configuration.get("support_email") do
149       "gitorious-support@#{host}"
150     end
151   end
152
153   def self.remote_ops_ips
154     return @remote_ops_ips if @remote_ops_ips && cache?
155     ips = Gitorious::Configuration.get("remote_ops_ips", ["127.0.0.1"])
156     @remote_ops_ips = Array(ips)
157   end
158
159   def self.ops?(remote_addr)
160     remote_ops_ips.include?(remote_addr)
161   end
162
163   def self.archive_cache_dir
164     return @archive_cache_dir if @archive_cache_dir && cache?
165     @archive_cache_dir = Gitorious::Configuration.get("archive_cache_dir")
166   end
167
168   def self.archive_work_dir
169     return @archive_work_dir if @archive_work_dir && cache?
170     @archive_work_dir = Gitorious::Configuration.get("archive_work_dir")
171   end
172
173   def self.diff_timeout
174     return @diff_timeout if @diff_timeout && cache?
175     @diff_timeout = Gitorious::Configuration.get("merge_request_diff_timeout", 10).to_i
176   end
177
178   def self.frontend_server
179     return @frontend_server if @frontend_server && cache?
180     @frontend_server = Gitorious::Configuration.get("frontend_server", "nginx").downcase
181   end
182
183   def self.dot_org?
184     @is_gitorious_org = Gitorious::Configuration.get("is_gitorious_dot_org", true)
185   end
186
187   def self.git_binary
188     return @git_binary if @git_binary && cache?
189     @git_binary = Gitorious::Configuration.get("git_binary", "/usr/bin/env git")
190   end
191
192   def self.git_version
193     return @git_version if @git_version && cache?
194     @git_version = Gitorious::Configuration.get("git_version")
195   end
196
197   def self.site_name
198     return @site_name if @site_name && cache?
199     @site_name = Gitorious::Configuration.get("site_name", "Gitorious")
200   end
201
202   def self.configured?
203     @configured
204   end
205
206   def self.configured!
207     @configured = true
208   end
209
210   private
211   def self.cache?
212     return Rails.env.production? if defined?(Rails)
213     false
214   end
215 end