To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / models / repository.rb @ 1298:4f746d8966dd

History | View | Annotate | Download (11.5 KB)

1 441:cbce1fd3b1b7 Chris
# Redmine - project management software
2 1295:622f24f53b42 Chris
# Copyright (C) 2006-2013  Jean-Philippe Lang
3 0:513646585e45 Chris
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
8 441:cbce1fd3b1b7 Chris
#
9 0:513646585e45 Chris
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13 441:cbce1fd3b1b7 Chris
#
14 0:513646585e45 Chris
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17
18 909:cbb26bc654de Chris
class ScmFetchError < Exception; end
19
20 0:513646585e45 Chris
class Repository < ActiveRecord::Base
21 245:051f544170fe Chris
  include Redmine::Ciphering
22 1115:433d4f72a19b Chris
  include Redmine::SafeAttributes
23
24
  # Maximum length for repository identifiers
25
  IDENTIFIER_MAX_LENGTH = 255
26 441:cbce1fd3b1b7 Chris
27 0:513646585e45 Chris
  belongs_to :project
28
  has_many :changesets, :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC"
29 1115:433d4f72a19b Chris
  has_many :filechanges, :class_name => 'Change', :through => :changesets
30 441:cbce1fd3b1b7 Chris
31
  serialize :extra_info
32
33 1115:433d4f72a19b Chris
  before_save :check_default
34
35 0:513646585e45 Chris
  # Raw SQL to delete changesets and changes in the database
36
  # has_many :changesets, :dependent => :destroy is too slow for big repositories
37
  before_destroy :clear_changesets
38 441:cbce1fd3b1b7 Chris
39 245:051f544170fe Chris
  validates_length_of :password, :maximum => 255, :allow_nil => true
40 1115:433d4f72a19b Chris
  validates_length_of :identifier, :maximum => IDENTIFIER_MAX_LENGTH, :allow_blank => true
41
  validates_presence_of :identifier, :unless => Proc.new { |r| r.is_default? || r.set_as_default? }
42
  validates_uniqueness_of :identifier, :scope => :project_id, :allow_blank => true
43
  validates_exclusion_of :identifier, :in => %w(show entry raw changes annotate diff show stats graph)
44
  # donwcase letters, digits, dashes, underscores but not digits only
45 1295:622f24f53b42 Chris
  validates_format_of :identifier, :with => /\A(?!\d+$)[a-z0-9\-_]*\z/, :allow_blank => true
46 0:513646585e45 Chris
  # Checks if the SCM is enabled when creating a repository
47 909:cbb26bc654de Chris
  validate :repo_create_validation, :on => :create
48
49 1115:433d4f72a19b Chris
  safe_attributes 'identifier',
50
    'login',
51
    'password',
52
    'path_encoding',
53
    'log_encoding',
54
    'is_default'
55
56
  safe_attributes 'url',
57
    :if => lambda {|repository, user| repository.new_record?}
58
59 909:cbb26bc654de Chris
  def repo_create_validation
60
    unless Setting.enabled_scm.include?(self.class.name.demodulize)
61
      errors.add(:type, :invalid)
62
    end
63
  end
64 245:051f544170fe Chris
65 1115:433d4f72a19b Chris
  def self.human_attribute_name(attribute_key_name, *args)
66
    attr_name = attribute_key_name.to_s
67 441:cbce1fd3b1b7 Chris
    if attr_name == "log_encoding"
68
      attr_name = "commit_logs_encoding"
69
    end
70 1115:433d4f72a19b Chris
    super(attr_name, *args)
71 441:cbce1fd3b1b7 Chris
  end
72
73 0:513646585e45 Chris
  # Removes leading and trailing whitespace
74
  def url=(arg)
75
    write_attribute(:url, arg ? arg.to_s.strip : nil)
76
  end
77 245:051f544170fe Chris
78 0:513646585e45 Chris
  # Removes leading and trailing whitespace
79
  def root_url=(arg)
80
    write_attribute(:root_url, arg ? arg.to_s.strip : nil)
81
  end
82 441:cbce1fd3b1b7 Chris
83 245:051f544170fe Chris
  def password
84
    read_ciphered_attribute(:password)
85
  end
86 441:cbce1fd3b1b7 Chris
87 245:051f544170fe Chris
  def password=(arg)
88
    write_ciphered_attribute(:password, arg)
89
  end
90
91
  def scm_adapter
92
    self.class.scm_adapter_class
93
  end
94 0:513646585e45 Chris
95
  def scm
96 1115:433d4f72a19b Chris
    unless @scm
97
      @scm = self.scm_adapter.new(url, root_url,
98 245:051f544170fe Chris
                                  login, password, path_encoding)
99 1115:433d4f72a19b Chris
      if root_url.blank? && @scm.root_url.present?
100
        update_attribute(:root_url, @scm.root_url)
101
      end
102
    end
103 0:513646585e45 Chris
    @scm
104
  end
105 245:051f544170fe Chris
106 0:513646585e45 Chris
  def scm_name
107
    self.class.scm_name
108
  end
109 245:051f544170fe Chris
110 1115:433d4f72a19b Chris
  def name
111
    if identifier.present?
112
      identifier
113
    elsif is_default?
114
      l(:field_repository_is_default)
115
    else
116
      scm_name
117
    end
118
  end
119
120
  def identifier=(identifier)
121
    super unless identifier_frozen?
122
  end
123
124
  def identifier_frozen?
125
    errors[:identifier].blank? && !(new_record? || identifier.blank?)
126
  end
127
128
  def identifier_param
129
    if is_default?
130
      nil
131
    elsif identifier.present?
132
      identifier
133
    else
134
      id.to_s
135
    end
136
  end
137
138
  def <=>(repository)
139
    if is_default?
140
      -1
141
    elsif repository.is_default?
142
      1
143
    else
144
      identifier.to_s <=> repository.identifier.to_s
145
    end
146
  end
147
148
  def self.find_by_identifier_param(param)
149
    if param.to_s =~ /^\d+$/
150
      find_by_id(param)
151
    else
152
      find_by_identifier(param)
153
    end
154
  end
155
156 441:cbce1fd3b1b7 Chris
  def merge_extra_info(arg)
157
    h = extra_info || {}
158
    return h if arg.nil?
159
    h.merge!(arg)
160
    write_attribute(:extra_info, h)
161
  end
162
163
  def report_last_commit
164
    true
165
  end
166
167 0:513646585e45 Chris
  def supports_cat?
168
    scm.supports_cat?
169
  end
170
171
  def supports_annotate?
172
    scm.supports_annotate?
173
  end
174 441:cbce1fd3b1b7 Chris
175
  def supports_all_revisions?
176
    true
177
  end
178
179
  def supports_directory_revisions?
180
    false
181
  end
182
183 909:cbb26bc654de Chris
  def supports_revision_graph?
184
    false
185
  end
186
187 0:513646585e45 Chris
  def entry(path=nil, identifier=nil)
188
    scm.entry(path, identifier)
189
  end
190 441:cbce1fd3b1b7 Chris
191 0:513646585e45 Chris
  def entries(path=nil, identifier=nil)
192 1115:433d4f72a19b Chris
    entries = scm.entries(path, identifier)
193
    load_entries_changesets(entries)
194
    entries
195 0:513646585e45 Chris
  end
196
197
  def branches
198
    scm.branches
199
  end
200
201
  def tags
202
    scm.tags
203
  end
204
205
  def default_branch
206 507:0c939c159af4 Chris
    nil
207 0:513646585e45 Chris
  end
208 441:cbce1fd3b1b7 Chris
209 0:513646585e45 Chris
  def properties(path, identifier=nil)
210
    scm.properties(path, identifier)
211
  end
212 441:cbce1fd3b1b7 Chris
213 0:513646585e45 Chris
  def cat(path, identifier=nil)
214
    scm.cat(path, identifier)
215
  end
216 441:cbce1fd3b1b7 Chris
217 0:513646585e45 Chris
  def diff(path, rev, rev_to)
218
    scm.diff(path, rev, rev_to)
219
  end
220 119:8661b858af72 Chris
221
  def diff_format_revisions(cs, cs_to, sep=':')
222
    text = ""
223
    text << cs_to.format_identifier + sep if cs_to
224
    text << cs.format_identifier if cs
225
    text
226
  end
227
228 0:513646585e45 Chris
  # Returns a path relative to the url of the repository
229
  def relative_path(path)
230
    path
231
  end
232 128:07fa8a8b56a8 Chris
233 0:513646585e45 Chris
  # Finds and returns a revision with a number or the beginning of a hash
234
  def find_changeset_by_name(name)
235 128:07fa8a8b56a8 Chris
    return nil if name.blank?
236 1115:433d4f72a19b Chris
    s = name.to_s
237 1295:622f24f53b42 Chris
    if s.match(/^\d*$/)
238
      changesets.where("revision = ?", s).first
239
    else
240
      changesets.where("revision LIKE ?", s + '%').first
241
    end
242 0:513646585e45 Chris
  end
243 128:07fa8a8b56a8 Chris
244 0:513646585e45 Chris
  def latest_changeset
245 1295:622f24f53b42 Chris
    @latest_changeset ||= changesets.first
246 0:513646585e45 Chris
  end
247
248
  # Returns the latest changesets for +path+
249
  # Default behaviour is to search in cached changesets
250
  def latest_changesets(path, rev, limit=10)
251
    if path.blank?
252 441:cbce1fd3b1b7 Chris
      changesets.find(
253
         :all,
254
         :include => :user,
255
         :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC",
256
         :limit => limit)
257 0:513646585e45 Chris
    else
258 1115:433d4f72a19b Chris
      filechanges.find(
259 441:cbce1fd3b1b7 Chris
         :all,
260
         :include => {:changeset => :user},
261
         :conditions => ["path = ?", path.with_leading_slash],
262
         :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC",
263
         :limit => limit
264
       ).collect(&:changeset)
265 0:513646585e45 Chris
    end
266
  end
267 441:cbce1fd3b1b7 Chris
268 0:513646585e45 Chris
  def scan_changesets_for_issue_ids
269
    self.changesets.each(&:scan_comment_for_issue_ids)
270
  end
271
272
  # Returns an array of committers usernames and associated user_id
273
  def committers
274 441:cbce1fd3b1b7 Chris
    @committers ||= Changeset.connection.select_rows(
275
         "SELECT DISTINCT committer, user_id FROM #{Changeset.table_name} WHERE repository_id = #{id}")
276 0:513646585e45 Chris
  end
277 441:cbce1fd3b1b7 Chris
278 0:513646585e45 Chris
  # Maps committers username to a user ids
279
  def committer_ids=(h)
280
    if h.is_a?(Hash)
281
      committers.each do |committer, user_id|
282
        new_user_id = h[committer]
283
        if new_user_id && (new_user_id.to_i != user_id.to_i)
284
          new_user_id = (new_user_id.to_i > 0 ? new_user_id.to_i : nil)
285 441:cbce1fd3b1b7 Chris
          Changeset.update_all(
286
               "user_id = #{ new_user_id.nil? ? 'NULL' : new_user_id }",
287
               ["repository_id = ? AND committer = ?", id, committer])
288 0:513646585e45 Chris
        end
289
      end
290 441:cbce1fd3b1b7 Chris
      @committers            = nil
291 0:513646585e45 Chris
      @found_committer_users = nil
292
      true
293
    else
294
      false
295
    end
296
  end
297 441:cbce1fd3b1b7 Chris
298 0:513646585e45 Chris
  # Returns the Redmine User corresponding to the given +committer+
299
  # It will return nil if the committer is not yet mapped and if no User
300
  # with the same username or email was found
301
  def find_committer_user(committer)
302
    unless committer.blank?
303
      @found_committer_users ||= {}
304
      return @found_committer_users[committer] if @found_committer_users.has_key?(committer)
305 441:cbce1fd3b1b7 Chris
306 0:513646585e45 Chris
      user = nil
307 1295:622f24f53b42 Chris
      c = changesets.where(:committer => committer).includes(:user).first
308 0:513646585e45 Chris
      if c && c.user
309
        user = c.user
310
      elsif committer.strip =~ /^([^<]+)(<(.*)>)?$/
311
        username, email = $1.strip, $3
312
        u = User.find_by_login(username)
313
        u ||= User.find_by_mail(email) unless email.blank?
314
        user = u
315
      end
316
      @found_committer_users[committer] = user
317
      user
318
    end
319
  end
320 245:051f544170fe Chris
321
  def repo_log_encoding
322
    encoding = log_encoding.to_s.strip
323
    encoding.blank? ? 'UTF-8' : encoding
324
  end
325
326 0:513646585e45 Chris
  # Fetches new changesets for all repositories of active projects
327
  # Can be called periodically by an external script
328
  # eg. ruby script/runner "Repository.fetch_changesets"
329
  def self.fetch_changesets
330 1115:433d4f72a19b Chris
    Project.active.has_module(:repository).all.each do |project|
331
      project.repositories.each do |repository|
332 245:051f544170fe Chris
        begin
333 1115:433d4f72a19b Chris
          repository.fetch_changesets
334 245:051f544170fe Chris
        rescue Redmine::Scm::Adapters::CommandFailed => e
335
          logger.error "scm: error during fetching changesets: #{e.message}"
336
        end
337 0:513646585e45 Chris
      end
338
    end
339
  end
340 245:051f544170fe Chris
341 0:513646585e45 Chris
  # scan changeset comments to find related and fixed issues for all repositories
342
  def self.scan_changesets_for_issue_ids
343 1295:622f24f53b42 Chris
    all.each(&:scan_changesets_for_issue_ids)
344 0:513646585e45 Chris
  end
345
346
  def self.scm_name
347
    'Abstract'
348
  end
349 441:cbce1fd3b1b7 Chris
350 0:513646585e45 Chris
  def self.available_scm
351
    subclasses.collect {|klass| [klass.scm_name, klass.name]}
352
  end
353 245:051f544170fe Chris
354 0:513646585e45 Chris
  def self.factory(klass_name, *args)
355
    klass = "Repository::#{klass_name}".constantize
356
    klass.new(*args)
357
  rescue
358
    nil
359
  end
360 245:051f544170fe Chris
361 437:102056ec2de9 chris
  def clear_cache
362
    clear_changesets
363
  end
364 443:350acce374a2 Chris
365 245:051f544170fe Chris
  def self.scm_adapter_class
366
    nil
367
  end
368
369
  def self.scm_command
370
    ret = ""
371
    begin
372
      ret = self.scm_adapter_class.client_command if self.scm_adapter_class
373 441:cbce1fd3b1b7 Chris
    rescue Exception => e
374 245:051f544170fe Chris
      logger.error "scm: error during get command: #{e.message}"
375
    end
376
    ret
377
  end
378
379
  def self.scm_version_string
380
    ret = ""
381
    begin
382
      ret = self.scm_adapter_class.client_version_string if self.scm_adapter_class
383 441:cbce1fd3b1b7 Chris
    rescue Exception => e
384 245:051f544170fe Chris
      logger.error "scm: error during get version string: #{e.message}"
385
    end
386
    ret
387
  end
388
389
  def self.scm_available
390
    ret = false
391
    begin
392 441:cbce1fd3b1b7 Chris
      ret = self.scm_adapter_class.client_available if self.scm_adapter_class
393
    rescue Exception => e
394 245:051f544170fe Chris
      logger.error "scm: error during get scm available: #{e.message}"
395
    end
396
    ret
397
  end
398
399 1115:433d4f72a19b Chris
  def set_as_default?
400
    new_record? && project && !Repository.first(:conditions => {:project_id => project.id})
401
  end
402
403
  protected
404
405
  def check_default
406
    if !is_default? && set_as_default?
407
      self.is_default = true
408
    end
409
    if is_default? && is_default_changed?
410
      Repository.update_all(["is_default = ?", false], ["project_id = ?", project_id])
411
    end
412
  end
413
414
  def load_entries_changesets(entries)
415
    if entries
416
      entries.each do |entry|
417
        if entry.lastrev && entry.lastrev.identifier
418
          entry.changeset = find_changeset_by_name(entry.lastrev.identifier)
419
        end
420
      end
421
    end
422
  end
423
424 0:513646585e45 Chris
  private
425 245:051f544170fe Chris
426 1115:433d4f72a19b Chris
  # Deletes repository data
427 0:513646585e45 Chris
  def clear_changesets
428 1115:433d4f72a19b Chris
    cs = Changeset.table_name
429
    ch = Change.table_name
430
    ci = "#{table_name_prefix}changesets_issues#{table_name_suffix}"
431
    cp = "#{table_name_prefix}changeset_parents#{table_name_suffix}"
432
433 0:513646585e45 Chris
    connection.delete("DELETE FROM #{ch} WHERE #{ch}.changeset_id IN (SELECT #{cs}.id FROM #{cs} WHERE #{cs}.repository_id = #{id})")
434
    connection.delete("DELETE FROM #{ci} WHERE #{ci}.changeset_id IN (SELECT #{cs}.id FROM #{cs} WHERE #{cs}.repository_id = #{id})")
435 1115:433d4f72a19b Chris
    connection.delete("DELETE FROM #{cp} WHERE #{cp}.changeset_id IN (SELECT #{cs}.id FROM #{cs} WHERE #{cs}.repository_id = #{id})")
436 0:513646585e45 Chris
    connection.delete("DELETE FROM #{cs} WHERE #{cs}.repository_id = #{id}")
437 1115:433d4f72a19b Chris
    clear_extra_info_of_changesets
438
  end
439
440
  def clear_extra_info_of_changesets
441 0:513646585e45 Chris
  end
442
end