Don't show timeout message for empty diffs (ie. blank file added)
[gitorious.git] / lib / gitorious.rb
blobc8a60bf1c8aae7fe84d933b8da08d493a12decb3
1 # encoding: utf-8
2 #--
3 #   Copyright (C) 2011-2014 Gitorious AS
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.
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.
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 require "gitorious/openid"
21 require "gitorious/kerberos"
23 module Gitorious
24   VERSION = "3.2.0"
26   # Application-wide configuration settings.
27   Configuration = Configurable.new("GITORIOUS")
28   Configuration.rename("gitorious_host", "host")
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")
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
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
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.default_port?; site.default_port?; end
79   def self.url(path); site.url(path); end
81   def self.git_daemon
82     return @git_daemon if @git_daemon && cache?
83     return nil if !Gitorious::Configuration.get("enable_git_daemon", false)
84     host = Gitorious::Configuration.get("git_daemon_host") { Gitorious.host }
85     port = Gitorious::Configuration.get("git_daemon_port")
86     @git_daemon = Gitorious::GitMountPoint.new(host, port)
87   end
89   def self.ssh_daemon
90     return @ssh_daemon if @ssh_daemon && cache?
91     return nil if !Gitorious::Configuration.get("enable_ssh_daemon", true)
92     host = Gitorious::Configuration.get("ssh_daemon_host") { Gitorious.host }
93     port = Gitorious::Configuration.get("ssh_daemon_port") { 22 }
94     @ssh_daemon = Gitorious::GitSshMountPoint.new(Gitorious.user, host, port)
95   end
97   def self.git_http
98     return @git_http if @git_http && cache?
99     return nil if !Gitorious::Configuration.get("enable_git_http", true)
100     host = Gitorious::Configuration.get("git_http_host") { Gitorious.host }
101     port = Gitorious::Configuration.get("git_http_port") { Gitorious.port }
102     scheme = Gitorious::Configuration.get("git_http_scheme") { Gitorious.scheme }
103     @git_http = Gitorious::HttpMountPoint.new(host, port, scheme)
104   end
106   def self.default_remote_url(repository)
107     (ssh_daemon || git_daemon || git_http).url(repository.gitdir)
108   end
110   def self.email_sender
111     return @email_sender if @email_sender && cache?
112     default = "Gitorious <no-reply@#{host}>"
113     @email_sender = Gitorious::Configuration.get("email_sender", default)
114   end
116   def self.user
117     return @user if @user && cache?
118     @user = Gitorious::Configuration.get("user", "git")
119   end
121   def self.public?
122     return @public if !@public.nil? && cache?
123     @public = Gitorious::Configuration.get("public_mode", true)
124   end
126   def self.private_repositories?
127     return @private_repos if !@private_repos.nil? && cache?
128     @private_repos = Gitorious::Configuration.get("enable_private_repositories", false)
129   end
131   def self.projects_default_private?
132     return @projdefpriv if !@projdefpriv.nil? && cache?
133     @projdefpriv = private_repositories? && Gitorious::Configuration.get("projects_default_private", false)
134   end
136   def self.repositories_default_private?
137     return @repodefpriv if !@repodefpriv.nil? && cache?
138     @repodefpriv = private_repositories? && Gitorious::Configuration.get("repositories_default_private", false)
139   end
141   def self.support_email
142     return @support_email if @support_email && cache?
143     @support_email = Gitorious::Configuration.get("support_email") do
144       "gitorious-support@#{host}"
145     end
146   end
148   def self.archive_cache_dir
149     return @archive_cache_dir if @archive_cache_dir && cache?
150     @archive_cache_dir = Gitorious::Configuration.get("archive_cache_dir")
151   end
153   def self.archive_work_dir
154     return @archive_work_dir if @archive_work_dir && cache?
155     @archive_work_dir = Gitorious::Configuration.get("archive_work_dir")
156   end
158   def self.diff_timeout
159     return @diff_timeout if @diff_timeout && cache?
160     @diff_timeout = Gitorious::Configuration.get("merge_request_diff_timeout", 10).to_i
161   end
163   def self.dot_org?
164     @is_gitorious_org = Gitorious::Configuration.get("is_gitorious_dot_org", false)
165   end
167   def self.git_binary
168     return @git_binary if @git_binary && cache?
169     @git_binary = Gitorious::Configuration.get("git_binary", "/usr/bin/env git")
170   end
172   def self.git_version
173     return @git_version if @git_version && cache?
174     @git_version = Gitorious::Configuration.get("git_version")
175   end
177   def self.site_name
178     return @site_name if @site_name && cache?
179     @site_name = Gitorious::Configuration.get("site_name", "Gitorious")
180   end
182   def self.registrations_enabled?
183     Gitorious::Configuration.get("enable_registrations", Gitorious.public?)
184   end
186   def self.max_tarball_size
187     return @max_tarball_size if @max_tarball_size && cache?
188     mts = Gitorious::Configuration.get("max_tarball_size", 0)
189     return mts.to_i * 1024 if mts =~ /k$/i
190     return mts.to_i * 1024 * 1024 if mts =~ /m$/i
191     return mts.to_i * 1024 * 1024 * 1024 if mts =~ /g$/i
192     @max_tarball_size = mts.to_i
193   end
195   def self.tarballable?(repository)
196     return true if max_tarball_size == 0 || repository.disk_usage.nil?
197     return repository.disk_usage <= max_tarball_size
198   end
200   def self.configured?
201     @configured
202   end
204   def self.configured!
205     @configured = true
206   end
208   def self.restrict_team_creation_to_site_admins?
209     Gitorious::Configuration.get("only_site_admins_can_create_teams")
210   end
212   def self.executor
213     return @executor if @executor
215     if Rails.env.test?
216       require 'gitorious/test_executor'
217       @executor = TestExecutor.new
218     else
219       require 'gitorious/command_executor'
220       @executor = CommandExecutor.new
221     end
222   end
224   def self.mirrors
225     return @mirrors if @mirrors
226     @mirrors = Gitorious::MirrorManager.new(Gitorious::Configuration.get('mirrors', []))
227   end
229   private
230   def self.cache?
231     return Rails.env.production? if defined?(Rails)
232     false
233   end