2006-12-24 16:38:45 +03:00
|
|
|
# redMine - project management software
|
2007-03-25 16:12:15 +04:00
|
|
|
# Copyright (C) 2006-2007 Jean-Philippe Lang
|
2006-12-24 16:38:45 +03:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
class Repository < ActiveRecord::Base
|
|
|
|
belongs_to :project
|
2008-07-14 01:55:13 +04:00
|
|
|
has_many :changesets, :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC"
|
2007-03-25 21:11:46 +04:00
|
|
|
has_many :changes, :through => :changesets
|
2008-06-08 18:59:26 +04:00
|
|
|
|
2008-07-14 01:55:13 +04:00
|
|
|
# Raw SQL to delete changesets and changes in the database
|
|
|
|
# has_many :changesets, :dependent => :destroy is too slow for big repositories
|
|
|
|
before_destroy :clear_changesets
|
|
|
|
|
2008-06-08 18:59:26 +04:00
|
|
|
# Checks if the SCM is enabled when creating a repository
|
2009-02-21 14:04:50 +03:00
|
|
|
validate_on_create { |r| r.errors.add(:type, :invalid) unless Setting.enabled_scm.include?(r.class.name.demodulize) }
|
2008-06-08 18:59:26 +04:00
|
|
|
|
2008-03-14 21:59:10 +03:00
|
|
|
# Removes leading and trailing whitespace
|
|
|
|
def url=(arg)
|
|
|
|
write_attribute(:url, arg ? arg.to_s.strip : nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Removes leading and trailing whitespace
|
|
|
|
def root_url=(arg)
|
|
|
|
write_attribute(:root_url, arg ? arg.to_s.strip : nil)
|
|
|
|
end
|
|
|
|
|
2006-12-24 16:38:45 +03:00
|
|
|
def scm
|
2007-06-13 00:12:05 +04:00
|
|
|
@scm ||= self.scm_adapter.new url, root_url, login, password
|
2007-03-18 18:48:05 +03:00
|
|
|
update_attribute(:root_url, @scm.root_url) if root_url.blank?
|
|
|
|
@scm
|
|
|
|
end
|
|
|
|
|
2007-06-13 00:12:05 +04:00
|
|
|
def scm_name
|
|
|
|
self.class.scm_name
|
2007-03-25 16:12:15 +04:00
|
|
|
end
|
|
|
|
|
2007-06-24 23:30:38 +04:00
|
|
|
def supports_cat?
|
|
|
|
scm.supports_cat?
|
|
|
|
end
|
2007-12-02 23:58:02 +03:00
|
|
|
|
|
|
|
def supports_annotate?
|
|
|
|
scm.supports_annotate?
|
|
|
|
end
|
2007-06-24 23:30:38 +04:00
|
|
|
|
2008-06-15 19:47:28 +04:00
|
|
|
def entry(path=nil, identifier=nil)
|
|
|
|
scm.entry(path, identifier)
|
|
|
|
end
|
|
|
|
|
2007-06-13 00:12:05 +04:00
|
|
|
def entries(path=nil, identifier=nil)
|
|
|
|
scm.entries(path, identifier)
|
2007-03-25 22:18:29 +04:00
|
|
|
end
|
2009-08-16 02:41:40 +04:00
|
|
|
|
|
|
|
def branches
|
|
|
|
scm.branches
|
|
|
|
end
|
|
|
|
|
|
|
|
def tags
|
|
|
|
scm.tags
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_branch
|
|
|
|
scm.default_branch
|
|
|
|
end
|
2007-03-25 22:18:29 +04:00
|
|
|
|
2008-07-05 12:59:04 +04:00
|
|
|
def properties(path, identifier=nil)
|
|
|
|
scm.properties(path, identifier)
|
|
|
|
end
|
|
|
|
|
2008-06-15 19:47:28 +04:00
|
|
|
def cat(path, identifier=nil)
|
|
|
|
scm.cat(path, identifier)
|
|
|
|
end
|
|
|
|
|
2008-06-08 20:28:42 +04:00
|
|
|
def diff(path, rev, rev_to)
|
|
|
|
scm.diff(path, rev, rev_to)
|
2007-03-25 16:12:15 +04:00
|
|
|
end
|
|
|
|
|
2007-10-15 22:41:27 +04:00
|
|
|
# Default behaviour: we search in cached changesets
|
2009-02-01 22:54:50 +03:00
|
|
|
def changesets_for_path(path, options={})
|
2007-10-15 22:41:27 +04:00
|
|
|
path = "/#{path}" unless path.starts_with?('/')
|
2008-11-11 15:50:11 +03:00
|
|
|
Change.find(:all, :include => {:changeset => :user},
|
2009-02-01 22:54:50 +03:00
|
|
|
:conditions => ["repository_id = ? AND path = ?", id, path],
|
|
|
|
:order => "committed_on DESC, #{Changeset.table_name}.id DESC",
|
|
|
|
:limit => options[:limit]).collect(&:changeset)
|
2007-10-15 22:41:27 +04:00
|
|
|
end
|
|
|
|
|
2008-05-24 21:58:34 +04:00
|
|
|
# Returns a path relative to the url of the repository
|
|
|
|
def relative_path(path)
|
|
|
|
path
|
|
|
|
end
|
|
|
|
|
2007-06-13 00:12:05 +04:00
|
|
|
def latest_changeset
|
|
|
|
@latest_changeset ||= changesets.find(:first)
|
|
|
|
end
|
2009-08-16 02:41:40 +04:00
|
|
|
|
|
|
|
def latest_changesets(path,rev,limit=10)
|
2009-08-17 18:12:51 +04:00
|
|
|
@latest_changesets ||= changesets.find(:all, :limit => limit, :order => "committed_on DESC")
|
2009-08-16 02:41:40 +04:00
|
|
|
end
|
2007-06-13 00:12:05 +04:00
|
|
|
|
2007-04-24 17:57:27 +04:00
|
|
|
def scan_changesets_for_issue_ids
|
|
|
|
self.changesets.each(&:scan_comment_for_issue_ids)
|
|
|
|
end
|
2009-08-16 02:41:40 +04:00
|
|
|
|
2008-11-10 21:59:06 +03:00
|
|
|
# Returns an array of committers usernames and associated user_id
|
|
|
|
def committers
|
|
|
|
@committers ||= Changeset.connection.select_rows("SELECT DISTINCT committer, user_id FROM #{Changeset.table_name} WHERE repository_id = #{id}")
|
|
|
|
end
|
|
|
|
|
|
|
|
# Maps committers username to a user ids
|
|
|
|
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
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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
|
|
|
|
def find_committer_user(committer)
|
|
|
|
if committer
|
|
|
|
c = changesets.find(:first, :conditions => {:committer => committer}, :include => :user)
|
|
|
|
if c && c.user
|
|
|
|
c.user
|
|
|
|
elsif committer.strip =~ /^([^<]+)(<(.*)>)?$/
|
|
|
|
username, email = $1.strip, $3
|
|
|
|
u = User.find_by_login(username)
|
|
|
|
u ||= User.find_by_mail(email) unless email.blank?
|
|
|
|
u
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-03-25 16:12:15 +04:00
|
|
|
# fetch new changesets for all repositories
|
|
|
|
# can be called periodically by an external script
|
|
|
|
# eg. ruby script/runner "Repository.fetch_changesets"
|
|
|
|
def self.fetch_changesets
|
|
|
|
find(:all).each(&:fetch_changesets)
|
2006-12-24 16:38:45 +03:00
|
|
|
end
|
2007-04-24 17:57:27 +04:00
|
|
|
|
|
|
|
# scan changeset comments to find related and fixed issues for all repositories
|
|
|
|
def self.scan_changesets_for_issue_ids
|
|
|
|
find(:all).each(&:scan_changesets_for_issue_ids)
|
|
|
|
end
|
2007-06-13 00:12:05 +04:00
|
|
|
|
|
|
|
def self.scm_name
|
|
|
|
'Abstract'
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.available_scm
|
|
|
|
subclasses.collect {|klass| [klass.scm_name, klass.name]}
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.factory(klass_name, *args)
|
|
|
|
klass = "Repository::#{klass_name}".constantize
|
|
|
|
klass.new(*args)
|
|
|
|
rescue
|
|
|
|
nil
|
|
|
|
end
|
2008-03-14 21:59:10 +03:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def before_save
|
|
|
|
# Strips url and root_url
|
|
|
|
url.strip!
|
|
|
|
root_url.strip!
|
|
|
|
true
|
|
|
|
end
|
2008-07-14 01:55:13 +04:00
|
|
|
|
|
|
|
def clear_changesets
|
2009-05-08 12:05:45 +04:00
|
|
|
cs, ch, ci = Changeset.table_name, Change.table_name, "#{table_name_prefix}changesets_issues#{table_name_suffix}"
|
|
|
|
connection.delete("DELETE FROM #{ch} WHERE #{ch}.changeset_id IN (SELECT #{cs}.id FROM #{cs} WHERE #{cs}.repository_id = #{id})")
|
|
|
|
connection.delete("DELETE FROM #{ci} WHERE #{ci}.changeset_id IN (SELECT #{cs}.id FROM #{cs} WHERE #{cs}.repository_id = #{id})")
|
|
|
|
connection.delete("DELETE FROM #{cs} WHERE #{cs}.repository_id = #{id}")
|
2008-07-14 01:55:13 +04:00
|
|
|
end
|
2006-12-24 16:38:45 +03:00
|
|
|
end
|