Class: Repository

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Redmine::Ciphering, Redmine::SafeAttributes
Defined in:
app/models/repository.rb

Overview

Since:

  • 0.4.0

Direct Known Subclasses

Bazaar, Cvs, Filesystem, Git, Mercurial, Subversion

Defined Under Namespace

Classes: Bazaar, Cvs, Filesystem, Git, Mercurial, Subversion

Constant Summary collapse

IDENTIFIER_MAX_LENGTH =

Maximum length for repository identifiers

255

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Redmine::SafeAttributes

#delete_unsafe_attributes, #safe_attribute?, #safe_attribute_names, #safe_attributes=

Methods included from Redmine::Ciphering

cipher_key, decrypt_text, encrypt_text, logger

Class Method Details

.available_scmObject

Since:

  • 0.5.1



360
361
362
# File 'app/models/repository.rb', line 360

def self.available_scm
  subclasses.collect {|klass| [klass.scm_name, klass.name]}
end

.factory(klass_name, *args) ⇒ Object

Since:

  • 0.5.1



364
365
366
# File 'app/models/repository.rb', line 364

def self.factory(klass_name, *args)
  repository_class(klass_name).new(*args) rescue nil
end

.fetch_changesetsObject

Fetches new changesets for all repositories of active projects Can be called periodically by an external script eg. ruby script/runner “Repository.fetch_changesets”

Since:

  • 0.5.0



339
340
341
342
343
344
345
346
347
348
349
# File 'app/models/repository.rb', line 339

def self.fetch_changesets
  Project.active.has_module(:repository).all.each do |project|
    project.repositories.each do |repository|
      begin
        repository.fetch_changesets
      rescue Redmine::Scm::Adapters::CommandFailed => e
        logger.error "scm: error during fetching changesets: #{e.message}"
      end
    end
  end
end

.find_by_identifier_param(param) ⇒ Object

Since:

  • 1.4.0



149
150
151
152
153
154
155
# File 'app/models/repository.rb', line 149

def self.find_by_identifier_param(param)
  if param.to_s =~ /^\d+$/
    find_by_id(param)
  else
    find_by_identifier(param)
  end
end

.human_attribute_name(attribute_key_name, *args) ⇒ Object

Since:

  • 1.2.0



68
69
70
71
72
73
74
# File 'app/models/repository.rb', line 68

def self.human_attribute_name(attribute_key_name, *args)
  attr_name = attribute_key_name.to_s
  if attr_name == "log_encoding"
    attr_name = "commit_logs_encoding"
  end
  super(attr_name, *args)
end

.repository_class(class_name) ⇒ Object

Since:

  • 3.1.7



368
369
370
371
372
373
# File 'app/models/repository.rb', line 368

def self.repository_class(class_name)
  class_name = class_name.to_s.camelize
  if Redmine::Scm::Base.all.include?(class_name)
    "Repository::#{class_name}".constantize
  end
end

.scan_changesets_for_issue_idsObject

scan changeset comments to find related and fixed issues for all repositories

Since:

  • 0.5.1



352
353
354
# File 'app/models/repository.rb', line 352

def self.scan_changesets_for_issue_ids
  all.each(&:scan_changesets_for_issue_ids)
end

.scm_adapter_classObject

Since:

  • 1.2.0



375
376
377
# File 'app/models/repository.rb', line 375

def self.scm_adapter_class
  nil
end

.scm_availableObject

Since:

  • 1.2.0



399
400
401
402
403
404
405
406
407
# File 'app/models/repository.rb', line 399

def self.scm_available
  ret = false
  begin
    ret = self.scm_adapter_class.client_available if self.scm_adapter_class
  rescue Exception => e
    logger.error "scm: error during get scm available: #{e.message}"
  end
  ret
end

.scm_commandObject

Since:

  • 1.2.0



379
380
381
382
383
384
385
386
387
# File 'app/models/repository.rb', line 379

def self.scm_command
  ret = ""
  begin
    ret = self.scm_adapter_class.client_command if self.scm_adapter_class
  rescue Exception => e
    logger.error "scm: error during get command: #{e.message}"
  end
  ret
end

.scm_nameObject

Since:

  • 0.5.1



356
357
358
# File 'app/models/repository.rb', line 356

def self.scm_name
  'Abstract'
end

.scm_version_stringObject

Since:

  • 1.2.0



389
390
391
392
393
394
395
396
397
# File 'app/models/repository.rb', line 389

def self.scm_version_string
  ret = ""
  begin
    ret = self.scm_adapter_class.client_version_string if self.scm_adapter_class
  rescue Exception => e
    logger.error "scm: error during get version string: #{e.message}"
  end
  ret
end

Instance Method Details

#<=>(repository) ⇒ Object

Since:

  • 1.4.0



139
140
141
142
143
144
145
146
147
# File 'app/models/repository.rb', line 139

def <=>(repository)
  if is_default?
    -1
  elsif repository.is_default?
    1
  else
    identifier.to_s <=> repository.identifier.to_s
  end
end

#branchesObject

Since:

  • 0.9.0



209
210
211
# File 'app/models/repository.rb', line 209

def branches
  scm.branches
end

#cat(path, identifier = nil) ⇒ Object

Since:

  • 0.8.0



225
226
227
# File 'app/models/repository.rb', line 225

def cat(path, identifier=nil)
  scm.cat(path, identifier)
end

#committer_ids=(h) ⇒ Object

Maps committers username to a user ids

Since:

  • 0.8.0



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'app/models/repository.rb', line 289

def committer_ids=(h)
  if h.is_a?(Hash)
    committers.each do |committer, user_id|
      new_user_id = h[committer]
      if new_user_id && (new_user_id.to_i != user_id.to_i)
        new_user_id = (new_user_id.to_i > 0 ? new_user_id.to_i : nil)
        Changeset.where(["repository_id = ? AND committer = ?", id, committer]).
          update_all("user_id = #{new_user_id.nil? ? 'NULL' : new_user_id}")
      end
    end
    @committers            = nil
    @found_committer_users = nil
    true
  else
    false
  end
end

#committersObject

Returns an array of committers usernames and associated user_id

Since:

  • 0.8.0



284
285
286
# File 'app/models/repository.rb', line 284

def committers
  @committers ||= Changeset.where(:repository_id => id).distinct.pluck(:committer, :user_id)
end

#default_branchObject

Since:

  • 0.9.0



217
218
219
# File 'app/models/repository.rb', line 217

def default_branch
  nil
end

#diff(path, rev, rev_to) ⇒ Object

Since:

  • 0.5.1



229
230
231
# File 'app/models/repository.rb', line 229

def diff(path, rev, rev_to)
  scm.diff(path, rev, rev_to)
end

#diff_format_revisions(cs, cs_to, sep = ':') ⇒ Object

Since:

  • 1.2.0



233
234
235
236
237
238
# File 'app/models/repository.rb', line 233

def diff_format_revisions(cs, cs_to, sep=':')
  text = ""
  text << cs_to.format_identifier + sep if cs_to
  text << cs.format_identifier if cs
  text
end

#entries(path = nil, identifier = nil) ⇒ Object

Since:

  • 0.5.1



203
204
205
206
207
# File 'app/models/repository.rb', line 203

def entries(path=nil, identifier=nil)
  entries = scm_entries(path, identifier)
  load_entries_changesets(entries)
  entries
end

#entry(path = nil, identifier = nil) ⇒ Object

Since:

  • 0.8.0



194
195
196
# File 'app/models/repository.rb', line 194

def entry(path=nil, identifier=nil)
  scm.entry(path, identifier)
end

#extra_infoObject

TODO: should return an empty hash instead of nil to avoid many ||{}

Since:

  • 2.4.3



158
159
160
161
# File 'app/models/repository.rb', line 158

def extra_info
  h = read_attribute(:extra_info)
  h.is_a?(Hash) ? h : nil
end

#find_changeset_by_name(name) ⇒ Object

Finds and returns a revision with a number or the beginning of a hash

Since:

  • 0.9.0



246
247
248
249
250
251
252
253
254
# File 'app/models/repository.rb', line 246

def find_changeset_by_name(name)
  return nil if name.blank?
  s = name.to_s
  if s.match(/^\d*$/)
    changesets.find_by(:revision => s)
  else
    changesets.where("revision LIKE ?", s + '%').first
  end
end

#find_committer_user(committer) ⇒ Object

Returns the Redmine User corresponding to the given committer It will return nil if the committer is not yet mapped and if no User with the same username or email was found

Since:

  • 0.8.0



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'app/models/repository.rb', line 310

def find_committer_user(committer)
  unless committer.blank?
    @found_committer_users ||= {}
    return @found_committer_users[committer] if @found_committer_users.has_key?(committer)

    user = nil
    c = changesets.where(:committer => committer).
          includes(:user).references(:user).first
    if c && c.user
      user = c.user
    elsif committer.strip =~ /^([^<]+)(<(.*)>)?$/
      username, email = $1.strip, $3
      u = User.(username)
      u ||= User.find_by_mail(email) unless email.blank?
      user = u
    end
    @found_committer_users[committer] = user
    user
  end
end

#identifier=(identifier) ⇒ Object

Since:

  • 2.1.0



123
124
125
# File 'app/models/repository.rb', line 123

def identifier=(identifier)
  super unless identifier_frozen?
end

#identifier_frozen?Boolean

Returns:

  • (Boolean)

Since:

  • 2.1.0



127
128
129
# File 'app/models/repository.rb', line 127

def identifier_frozen?
  errors[:identifier].blank? && !(new_record? || identifier.blank?)
end

#identifier_paramObject

Since:

  • 1.4.0



131
132
133
134
135
136
137
# File 'app/models/repository.rb', line 131

def identifier_param
  if identifier.present?
    identifier
  else
    id.to_s
  end
end

#latest_changesetObject

Since:

  • 0.5.1



256
257
258
# File 'app/models/repository.rb', line 256

def latest_changeset
  @latest_changeset ||= changesets.first
end

#latest_changesets(path, rev, limit = 10) ⇒ Object

Returns the latest changesets for path Default behaviour is to search in cached changesets

Since:

  • 0.9.0



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'app/models/repository.rb', line 262

def latest_changesets(path, rev, limit=10)
  if path.blank?
    changesets.
      reorder("#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC").
      limit(limit).
      preload(:user).
      to_a
  else
    filechanges.
      where("path = ?", path.with_leading_slash).
      reorder("#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC").
      limit(limit).
      preload(:changeset => :user).
      collect(&:changeset)
  end
end

#merge_extra_info(arg) ⇒ Object

Since:

  • 1.2.0



163
164
165
166
167
168
# File 'app/models/repository.rb', line 163

def merge_extra_info(arg)
  h = extra_info || {}
  return h if arg.nil?
  h.merge!(arg)
  write_attribute(:extra_info, h)
end

#nameObject

Since:

  • 1.4.0



113
114
115
116
117
118
119
120
121
# File 'app/models/repository.rb', line 113

def name
  if identifier.present?
    identifier
  elsif is_default?
    l(:field_repository_is_default)
  else
    scm_name
  end
end

#passwordObject

Since:

  • 1.2.0



86
87
88
# File 'app/models/repository.rb', line 86

def password
  read_ciphered_attribute(:password)
end

#password=(arg) ⇒ Object

Since:

  • 1.2.0



90
91
92
# File 'app/models/repository.rb', line 90

def password=(arg)
  write_ciphered_attribute(:password, arg)
end

#properties(path, identifier = nil) ⇒ Object

Since:

  • 0.8.0



221
222
223
# File 'app/models/repository.rb', line 221

def properties(path, identifier=nil)
  scm.properties(path, identifier)
end

#relative_path(path) ⇒ Object

Returns a path relative to the url of the repository

Since:

  • 0.7.2



241
242
243
# File 'app/models/repository.rb', line 241

def relative_path(path)
  path
end

#repo_create_validationObject

Since:

  • 1.3.0



62
63
64
65
66
# File 'app/models/repository.rb', line 62

def repo_create_validation
  unless Setting.enabled_scm.include?(self.class.name.demodulize)
    errors.add(:type, :invalid)
  end
end

#repo_log_encodingObject

Since:

  • 1.2.0



331
332
333
334
# File 'app/models/repository.rb', line 331

def repo_log_encoding
  encoding = log_encoding.to_s.strip
  encoding.blank? ? 'UTF-8' : encoding
end

#report_last_commitObject

Since:

  • 1.2.0



170
171
172
# File 'app/models/repository.rb', line 170

def report_last_commit
  true
end

#root_url=(arg) ⇒ Object

Removes leading and trailing whitespace

Since:

  • 0.7.0



82
83
84
# File 'app/models/repository.rb', line 82

def root_url=(arg)
  write_attribute(:root_url, arg ? arg.to_s.strip : nil)
end

#same_commits_in_scope(scope, changeset) ⇒ Object

Returns a scope of changesets that come from the same commit as the given changeset in different repositories that point to the same backend

Since:

  • 2.6.0



452
453
454
455
456
457
458
459
460
# File 'app/models/repository.rb', line 452

def same_commits_in_scope(scope, changeset)
  scope = scope.joins(:repository).where(:repositories => {:url => url, :root_url => root_url, :type => type})
  if changeset.scmid.present?
    scope = scope.where(:scmid => changeset.scmid)
  else
    scope = scope.where(:revision => changeset.revision)
  end
  scope
end

#scan_changesets_for_issue_idsObject

Since:

  • 0.5.1



279
280
281
# File 'app/models/repository.rb', line 279

def scan_changesets_for_issue_ids
  self.changesets.each(&:scan_comment_for_issue_ids)
end

#scmObject



98
99
100
101
102
103
104
105
106
107
# File 'app/models/repository.rb', line 98

def scm
  unless @scm
    @scm = self.scm_adapter.new(url, root_url,
                                , password, path_encoding)
    if root_url.blank? && @scm.root_url.present?
      update_attribute(:root_url, @scm.root_url)
    end
  end
  @scm
end

#scm_adapterObject

Since:

  • 1.2.0



94
95
96
# File 'app/models/repository.rb', line 94

def scm_adapter
  self.class.scm_adapter_class
end

#scm_nameObject

Since:

  • 0.5.1



109
110
111
# File 'app/models/repository.rb', line 109

def scm_name
  self.class.scm_name
end

#set_as_default?Boolean

Returns:

  • (Boolean)

Since:

  • 1.4.0



409
410
411
# File 'app/models/repository.rb', line 409

def set_as_default?
  new_record? && project && Repository.where(:project_id => project.id).empty?
end

#stats_by_authorObject

Returns a hash with statistics by author in the following form: {

"John Smith" => { :commits => 45, :changes => 324 },
"Bob" => { ... }

}

Notes:

  • this hash honnors the users mapping defined for the repository

Since:

  • 2.6.0



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
# File 'app/models/repository.rb', line 421

def stats_by_author
  commits = Changeset.where("repository_id = ?", id).select("committer, user_id, count(*) as count").group("committer, user_id")

  #TODO: restore ordering ; this line probably never worked
  #commits.to_a.sort! {|x, y| x.last <=> y.last}

  changes = Change.joins(:changeset).where("#{Changeset.table_name}.repository_id = ?", id).select("committer, user_id, count(*) as count").group("committer, user_id")

  user_ids = changesets.map(&:user_id).compact.uniq
  authors_names = User.where(:id => user_ids).inject({}) do |memo, user|
    memo[user.id] = user.to_s
    memo
  end

  (commits + changes).inject({}) do |hash, element|
    mapped_name = element.committer
    if username = authors_names[element.user_id.to_i]
      mapped_name = username
    end
    hash[mapped_name] ||= { :commits_count => 0, :changes_count => 0 }
    if element.is_a?(Changeset)
      hash[mapped_name][:commits_count] += element.count.to_i
    else
      hash[mapped_name][:changes_count] += element.count.to_i
    end
    hash
  end
end

#supports_all_revisions?Boolean

Returns:

  • (Boolean)

Since:

  • 1.2.0



182
183
184
# File 'app/models/repository.rb', line 182

def supports_all_revisions?
  true
end

#supports_annotate?Boolean

Returns:

  • (Boolean)

Since:

  • 0.6.1



178
179
180
# File 'app/models/repository.rb', line 178

def supports_annotate?
  scm.supports_annotate?
end

#supports_cat?Boolean

Returns:

  • (Boolean)

Since:

  • 0.5.1



174
175
176
# File 'app/models/repository.rb', line 174

def supports_cat?
  scm.supports_cat?
end

#supports_directory_revisions?Boolean

Returns:

  • (Boolean)

Since:

  • 1.2.0



186
187
188
# File 'app/models/repository.rb', line 186

def supports_directory_revisions?
  false
end

#supports_revision_graph?Boolean

Returns:

  • (Boolean)

Since:

  • 1.3.0



190
191
192
# File 'app/models/repository.rb', line 190

def supports_revision_graph?
  false
end

#tagsObject

Since:

  • 0.9.0



213
214
215
# File 'app/models/repository.rb', line 213

def tags
  scm.tags
end

#url=(arg) ⇒ Object

Removes leading and trailing whitespace

Since:

  • 0.5.0



77
78
79
# File 'app/models/repository.rb', line 77

def url=(arg)
  write_attribute(:url, arg ? arg.to_s.strip : nil)
end