Class: Repository

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

Overview

redMine - project management software Copyright (C) 2006-2007 Jean-Philippe Lang

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.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

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



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

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

.factory(klass_name, *args) ⇒ Object



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

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”



209
210
211
212
213
214
215
216
217
218
219
# File 'app/models/repository.rb', line 209

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



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

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

.scm_adapter_classObject



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

def self.scm_adapter_class
  nil
end

.scm_availableObject



265
266
267
268
269
270
271
272
273
# File 'app/models/repository.rb', line 265

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



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

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



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

def self.scm_name
  'Abstract'
end

.scm_version_stringObject



255
256
257
258
259
260
261
262
263
# File 'app/models/repository.rb', line 255

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



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

def branches
  scm.branches
end

#cat(path, identifier = nil) ⇒ Object



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

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

#committer_ids=(h) ⇒ Object

Maps committers username to a user ids



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

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



156
157
158
# File 'app/models/repository.rb', line 156

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

#default_branchObject



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

def default_branch
  scm.default_branch
end

#diff(path, rev, rev_to) ⇒ Object



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

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

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



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

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



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

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

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



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

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



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

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



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

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



132
133
134
# File 'app/models/repository.rb', line 132

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



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

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

#passwordObject



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

def password
  read_ciphered_attribute(:password)
end

#password=(arg) ⇒ Object



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

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

#properties(path, identifier = nil) ⇒ Object



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

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

#relative_path(path) ⇒ Object

Returns a path relative to the url of the repository



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

def relative_path(path)
  path
end

#repo_log_encodingObject



201
202
203
204
# File 'app/models/repository.rb', line 201

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



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

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

#scan_changesets_for_issue_idsObject



151
152
153
# File 'app/models/repository.rb', line 151

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

#scmObject



55
56
57
58
59
60
# File 'app/models/repository.rb', line 55

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



51
52
53
# File 'app/models/repository.rb', line 51

def scm_adapter
  self.class.scm_adapter_class
end

#scm_nameObject



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

def scm_name
  self.class.scm_name
end

#supports_all_revisions?Boolean

Returns:

  • (Boolean)


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

def supports_all_revisions?
  true
end

#supports_annotate?Boolean

Returns:

  • (Boolean)


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

def supports_annotate?
  scm.supports_annotate?
end

#supports_cat?Boolean

Returns:

  • (Boolean)


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

def supports_cat?
  scm.supports_cat?
end

#supports_directory_revisions?Boolean

Returns:

  • (Boolean)


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

def supports_directory_revisions?
  false
end

#tagsObject



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

def tags
  scm.tags
end

#url=(arg) ⇒ Object

Removes leading and trailing whitespace



34
35
36
# File 'app/models/repository.rb', line 34

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