Class: Repository

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

Overview

– copyright ChiliProject is a project management system.

Copyright (C) 2010-2013 the ChiliProject Team

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

See doc/COPYRIGHT.rdoc for more details. ++

Direct Known Subclasses

Bazaar, Cvs, Darcs, Filesystem, Git, Mercurial, Subversion

Defined Under Namespace

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Redmine::Ciphering

cipher_key, decrypt_text, encrypt_text, included

Class Method Details

.available_scmObject


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

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

.factory(klass_name, *args) ⇒ Object


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

def self.factory(klass_name, *args)
  klass = "Repository::#{klass_name}".constantize
  klass.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”


214
215
216
217
218
219
220
221
222
223
224
# File 'app/models/repository.rb', line 214

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

.scan_changesets_for_issue_idsObject

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


227
228
229
# File 'app/models/repository.rb', line 227

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

.scm_adapter_classObject


246
247
248
# File 'app/models/repository.rb', line 246

def self.scm_adapter_class
  nil
end

.scm_availableObject


270
271
272
273
274
275
276
277
278
# File 'app/models/repository.rb', line 270

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

.scm_commandObject


250
251
252
253
254
255
256
257
258
# File 'app/models/repository.rb', line 250

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

.scm_nameObject


231
232
233
# File 'app/models/repository.rb', line 231

def self.scm_name
  'Abstract'
end

.scm_version_stringObject


260
261
262
263
264
265
266
267
268
# File 'app/models/repository.rb', line 260

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

Instance Method Details

#branchesObject


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

def branches
  scm.branches
end

#cat(path, identifier = nil) ⇒ Object


103
104
105
# File 'app/models/repository.rb', line 103

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

#committer_ids=(h) ⇒ Object

Maps committers username to a user ids


158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'app/models/repository.rb', line 158

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.update_all("user_id = #{ new_user_id.nil? ? 'NULL' : new_user_id }", ["repository_id = ? AND committer = ?", id, committer])
      end
    end
    @committers = nil
    @found_committer_users = nil
    true
  else
    false
  end
end

#committersObject

Returns an array of committers usernames and associated user_id


153
154
155
# File 'app/models/repository.rb', line 153

def committers
  @committers ||= Changeset.connection.select_rows("SELECT DISTINCT committer, user_id FROM #{Changeset.table_name} WHERE repository_id = #{id}")
end

#default_branchObject


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

def default_branch
  scm.default_branch
end

#diff(path, rev, rev_to) ⇒ Object


107
108
109
# File 'app/models/repository.rb', line 107

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

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


111
112
113
114
115
116
# File 'app/models/repository.rb', line 111

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


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

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

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


79
80
81
# File 'app/models/repository.rb', line 79

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

#find_changeset_by_name(name) ⇒ Object

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


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

def find_changeset_by_name(name)
  return nil if name.blank?
  changesets.find(:first, :conditions => (name.match(/^\d*$/) ? ["revision = ?", name.to_s] : ["revision LIKE ?", name + '%']))
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


178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'app/models/repository.rb', line 178

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.find(:first, :conditions => {:committer => committer}, :include => :user)
    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

#latest_changesetObject


129
130
131
# File 'app/models/repository.rb', line 129

def latest_changeset
  @latest_changeset ||= changesets.find(:first)
end

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

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


135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/models/repository.rb', line 135

def latest_changesets(path, rev, limit=10)
  if path.blank?
    changesets.find(:all, :include => :user,
                          :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC",
                          :limit => limit)
  else
    changes.find(:all, :include => {:changeset => :user},
                       :conditions => ["path = ?", path.with_leading_slash],
                       :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC",
                       :limit => limit).collect(&:changeset)
  end
end

#log_encodingObject

Provide a log encoding even if the column was not created yet It's used by 20100714111653_build_initial_journals_for_acts_as_journalized for initial journal creaetion of the changesets while the colum is only created by 20110228000000_add_repositories_log_encoding


207
208
209
# File 'app/models/repository.rb', line 207

def log_encoding
  read_attribute(:log_encoding)
end

#passwordObject


40
41
42
# File 'app/models/repository.rb', line 40

def password
  read_ciphered_attribute(:password)
end

#password=(arg) ⇒ Object


44
45
46
# File 'app/models/repository.rb', line 44

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

#properties(path, identifier = nil) ⇒ Object


99
100
101
# File 'app/models/repository.rb', line 99

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

#relative_path(path) ⇒ Object

Returns a path relative to the url of the repository


119
120
121
# File 'app/models/repository.rb', line 119

def relative_path(path)
  path
end

#repo_log_encodingObject


198
199
200
201
# File 'app/models/repository.rb', line 198

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

#root_url=(arg) ⇒ Object

Removes leading and trailing whitespace


36
37
38
# File 'app/models/repository.rb', line 36

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

#scan_changesets_for_issue_idsObject


148
149
150
# File 'app/models/repository.rb', line 148

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

#scmObject


52
53
54
55
56
57
# File 'app/models/repository.rb', line 52

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

#scm_adapterObject


48
49
50
# File 'app/models/repository.rb', line 48

def scm_adapter
  self.class.scm_adapter_class
end

#scm_nameObject


59
60
61
# File 'app/models/repository.rb', line 59

def scm_name
  self.class.scm_name
end

#supports_all_revisions?Boolean

Returns:

  • (Boolean)

71
72
73
# File 'app/models/repository.rb', line 71

def supports_all_revisions?
  true
end

#supports_annotate?Boolean

Returns:

  • (Boolean)

67
68
69
# File 'app/models/repository.rb', line 67

def supports_annotate?
  scm.supports_annotate?
end

#supports_cat?Boolean

Returns:

  • (Boolean)

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

def supports_cat?
  scm.supports_cat?
end

#supports_directory_revisions?Boolean

Returns:

  • (Boolean)

75
76
77
# File 'app/models/repository.rb', line 75

def supports_directory_revisions?
  false
end

#tagsObject


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

def tags
  scm.tags
end

#url=(arg) ⇒ Object

Removes leading and trailing whitespace


31
32
33
# File 'app/models/repository.rb', line 31

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