repository.rb
431 lines
| 11.4 KiB
| text/x-ruby
|
RubyLexer
|
r5533 | # Redmine - project management software | ||
|
r9453 | # Copyright (C) 2006-2012 Jean-Philippe Lang | ||
|
r103 | # | ||
# 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. | ||||
|
r5533 | # | ||
|
r103 | # 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. | ||||
|
r5533 | # | ||
|
r103 | # 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. | ||||
|
r5779 | class ScmFetchError < Exception; end | ||
|
r103 | class Repository < ActiveRecord::Base | ||
|
r4830 | include Redmine::Ciphering | ||
|
r9693 | include Redmine::SafeAttributes | ||
|
r9898 | |||
# Maximum length for repository identifiers | ||||
IDENTIFIER_MAX_LENGTH = 255 | ||||
|
r5533 | |||
|
r103 | belongs_to :project | ||
|
r1651 | has_many :changesets, :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC" | ||
|
r9576 | has_many :filechanges, :class_name => 'Change', :through => :changesets | ||
|
r5533 | |||
|
r5642 | serialize :extra_info | ||
|
r8530 | before_save :check_default | ||
|
r1651 | # 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 | ||||
|
r5533 | |||
|
r4830 | validates_length_of :password, :maximum => 255, :allow_nil => true | ||
|
r9898 | validates_length_of :identifier, :maximum => IDENTIFIER_MAX_LENGTH, :allow_blank => true | ||
|
r8530 | validates_presence_of :identifier, :unless => Proc.new { |r| r.is_default? || r.set_as_default? } | ||
validates_uniqueness_of :identifier, :scope => :project_id, :allow_blank => true | ||||
validates_exclusion_of :identifier, :in => %w(show entry raw changes annotate diff show stats graph) | ||||
|
r9692 | # donwcase letters, digits, dashes, underscores but not digits only | ||
validates_format_of :identifier, :with => /^(?!\d+$)[a-z0-9\-_]*$/, :allow_blank => true | ||||
|
r1493 | # Checks if the SCM is enabled when creating a repository | ||
|
r6597 | validate :repo_create_validation, :on => :create | ||
|
r9693 | safe_attributes 'identifier', | ||
'login', | ||||
'password', | ||||
'path_encoding', | ||||
'log_encoding', | ||||
'is_default' | ||||
|
r9695 | safe_attributes 'url', | ||
:if => lambda {|repository, user| repository.new_record?} | ||||
|
r6597 | def repo_create_validation | ||
unless Setting.enabled_scm.include?(self.class.name.demodulize) | ||||
errors.add(:type, :invalid) | ||||
end | ||||
end | ||||
|
r4821 | |||
|
r8166 | def self.human_attribute_name(attribute_key_name, *args) | ||
|
r8850 | attr_name = attribute_key_name.to_s | ||
|
r5399 | if attr_name == "log_encoding" | ||
attr_name = "commit_logs_encoding" | ||||
end | ||||
|
r8166 | super(attr_name, *args) | ||
|
r5399 | end | ||
|
r1234 | # Removes leading and trailing whitespace | ||
def url=(arg) | ||||
write_attribute(:url, arg ? arg.to_s.strip : nil) | ||||
end | ||||
|
r4821 | |||
|
r1234 | # Removes leading and trailing whitespace | ||
def root_url=(arg) | ||||
write_attribute(:root_url, arg ? arg.to_s.strip : nil) | ||||
end | ||||
|
r5533 | |||
|
r4830 | def password | ||
read_ciphered_attribute(:password) | ||||
end | ||||
|
r5533 | |||
|
r4830 | def password=(arg) | ||
write_ciphered_attribute(:password, arg) | ||||
end | ||||
|
r1234 | |||
|
r4702 | def scm_adapter | ||
self.class.scm_adapter_class | ||||
end | ||||
|
r103 | def scm | ||
|
r8538 | unless @scm | ||
@scm = self.scm_adapter.new(url, root_url, | ||||
|
r4821 | login, password, path_encoding) | ||
|
r8538 | if root_url.blank? && @scm.root_url.present? | ||
update_attribute(:root_url, @scm.root_url) | ||||
end | ||||
end | ||||
|
r341 | @scm | ||
end | ||||
|
r4821 | |||
|
r556 | def scm_name | ||
self.class.scm_name | ||||
|
r374 | end | ||
|
r4702 | |||
|
r8530 | def name | ||
|
r8534 | if identifier.present? | ||
|
r8530 | identifier | ||
|
r8534 | elsif is_default? | ||
l(:field_repository_is_default) | ||||
|
r8530 | else | ||
scm_name | ||||
end | ||||
end | ||||
|
r9898 | def identifier=(identifier) | ||
super unless identifier_frozen? | ||||
end | ||||
def identifier_frozen? | ||||
errors[:identifier].blank? && !(new_record? || identifier.blank?) | ||||
end | ||||
|
r8530 | def identifier_param | ||
if is_default? | ||||
nil | ||||
elsif identifier.present? | ||||
identifier | ||||
else | ||||
id.to_s | ||||
end | ||||
end | ||||
def <=>(repository) | ||||
if is_default? | ||||
-1 | ||||
elsif repository.is_default? | ||||
1 | ||||
else | ||||
|
r9436 | identifier.to_s <=> repository.identifier.to_s | ||
|
r8530 | end | ||
end | ||||
def self.find_by_identifier_param(param) | ||||
if param.to_s =~ /^\d+$/ | ||||
find_by_id(param) | ||||
else | ||||
find_by_identifier(param) | ||||
end | ||||
end | ||||
|
r5646 | def merge_extra_info(arg) | ||
h = extra_info || {} | ||||
return h if arg.nil? | ||||
h.merge!(arg) | ||||
write_attribute(:extra_info, h) | ||||
end | ||||
|
r5655 | def report_last_commit | ||
true | ||||
end | ||||
|
r570 | def supports_cat? | ||
scm.supports_cat? | ||||
end | ||||
|
r934 | |||
def supports_annotate? | ||||
scm.supports_annotate? | ||||
end | ||||
|
r5023 | |||
def supports_all_revisions? | ||||
true | ||||
end | ||||
|
r5533 | |||
|
r5024 | def supports_directory_revisions? | ||
false | ||||
end | ||||
|
r5533 | |||
|
r7596 | def supports_revision_graph? | ||
false | ||||
end | ||||
|
r1539 | def entry(path=nil, identifier=nil) | ||
scm.entry(path, identifier) | ||||
end | ||||
|
r5533 | |||
|
r556 | def entries(path=nil, identifier=nil) | ||
|
r9622 | entries = scm.entries(path, identifier) | ||
load_entries_changesets(entries) | ||||
entries | ||||
|
r378 | end | ||
|
r2735 | |||
def branches | ||||
scm.branches | ||||
end | ||||
def tags | ||||
scm.tags | ||||
end | ||||
def default_branch | ||||
|
r6010 | nil | ||
|
r2735 | end | ||
|
r5023 | |||
|
r1613 | def properties(path, identifier=nil) | ||
scm.properties(path, identifier) | ||||
end | ||||
|
r5023 | |||
|
r1539 | def cat(path, identifier=nil) | ||
scm.cat(path, identifier) | ||||
end | ||||
|
r5023 | |||
|
r1499 | def diff(path, rev, rev_to) | ||
scm.diff(path, rev, rev_to) | ||||
|
r374 | end | ||
|
r4578 | |||
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 | ||||
|
r1432 | # Returns a path relative to the url of the repository | ||
def relative_path(path) | ||||
path | ||||
end | ||||
|
r4592 | |||
|
r2784 | # Finds and returns a revision with a number or the beginning of a hash | ||
def find_changeset_by_name(name) | ||||
|
r4592 | return nil if name.blank? | ||
|
r8811 | s = name.to_s | ||
changesets.find(:first, :conditions => (s.match(/^\d*$/) ? | ||||
["revision = ?", s] : ["revision LIKE ?", s + '%'])) | ||||
|
r2784 | end | ||
|
r4592 | |||
|
r556 | def latest_changeset | ||
@latest_changeset ||= changesets.find(:first) | ||||
end | ||||
|
r2735 | |||
|
r2739 | # Returns the latest changesets for +path+ | ||
# Default behaviour is to search in cached changesets | ||||
def latest_changesets(path, rev, limit=10) | ||||
if path.blank? | ||||
|
r5533 | changesets.find( | ||
:all, | ||||
:include => :user, | ||||
:order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC", | ||||
:limit => limit) | ||||
|
r2739 | else | ||
|
r9576 | filechanges.find( | ||
|
r5533 | :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) | ||||
|
r2739 | end | ||
|
r2735 | end | ||
|
r5527 | |||
|
r470 | def scan_changesets_for_issue_ids | ||
self.changesets.each(&:scan_comment_for_issue_ids) | ||||
end | ||||
|
r2735 | |||
|
r2004 | # Returns an array of committers usernames and associated user_id | ||
def committers | ||||
|
r5527 | @committers ||= Changeset.connection.select_rows( | ||
"SELECT DISTINCT committer, user_id FROM #{Changeset.table_name} WHERE repository_id = #{id}") | ||||
|
r2004 | end | ||
|
r5527 | |||
|
r2004 | # 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) | ||||
|
r5527 | Changeset.update_all( | ||
"user_id = #{ new_user_id.nil? ? 'NULL' : new_user_id }", | ||||
["repository_id = ? AND committer = ?", id, committer]) | ||||
|
r2004 | end | ||
end | ||||
|
r5527 | @committers = nil | ||
|
r3358 | @found_committer_users = nil | ||
|
r2004 | true | ||
else | ||||
false | ||||
end | ||||
end | ||||
|
r5527 | |||
|
r2004 | # 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) | ||||
|
r3358 | unless committer.blank? | ||
@found_committer_users ||= {} | ||||
return @found_committer_users[committer] if @found_committer_users.has_key?(committer) | ||||
|
r5527 | |||
|
r3358 | user = nil | ||
|
r2004 | c = changesets.find(:first, :conditions => {:committer => committer}, :include => :user) | ||
if c && c.user | ||||
|
r3358 | user = c.user | ||
|
r2004 | elsif committer.strip =~ /^([^<]+)(<(.*)>)?$/ | ||
username, email = $1.strip, $3 | ||||
u = User.find_by_login(username) | ||||
u ||= User.find_by_mail(email) unless email.blank? | ||||
|
r3358 | user = u | ||
|
r2004 | end | ||
|
r3358 | @found_committer_users[committer] = user | ||
user | ||||
|
r2004 | end | ||
end | ||||
|
r4762 | |||
|
r4842 | def repo_log_encoding | ||
|
r4862 | encoding = log_encoding.to_s.strip | ||
|
r4842 | encoding.blank? ? 'UTF-8' : encoding | ||
end | ||||
|
r3288 | # Fetches new changesets for all repositories of active projects | ||
# Can be called periodically by an external script | ||||
|
r374 | # eg. ruby script/runner "Repository.fetch_changesets" | ||
def self.fetch_changesets | ||||
|
r8530 | Project.active.has_module(:repository).all.each do |project| | ||
project.repositories.each do |repository| | ||||
|
r4704 | begin | ||
|
r8530 | repository.fetch_changesets | ||
|
r4704 | rescue Redmine::Scm::Adapters::CommandFailed => e | ||
|
r4762 | logger.error "scm: error during fetching changesets: #{e.message}" | ||
|
r4704 | end | ||
|
r3288 | end | ||
end | ||||
|
r103 | end | ||
|
r4762 | |||
|
r470 | # 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 | ||||
|
r556 | |||
def self.scm_name | ||||
'Abstract' | ||||
end | ||||
|
r5533 | |||
|
r556 | def self.available_scm | ||
subclasses.collect {|klass| [klass.scm_name, klass.name]} | ||||
end | ||||
|
r4842 | |||
|
r556 | def self.factory(klass_name, *args) | ||
klass = "Repository::#{klass_name}".constantize | ||||
klass.new(*args) | ||||
rescue | ||||
nil | ||||
end | ||||
|
r4702 | |||
def self.scm_adapter_class | ||||
nil | ||||
end | ||||
def self.scm_command | ||||
|
r4762 | ret = "" | ||
begin | ||||
ret = self.scm_adapter_class.client_command if self.scm_adapter_class | ||||
|
r5879 | rescue Exception => e | ||
|
r4762 | logger.error "scm: error during get command: #{e.message}" | ||
end | ||||
ret | ||||
|
r4702 | end | ||
def self.scm_version_string | ||||
|
r4762 | ret = "" | ||
begin | ||||
ret = self.scm_adapter_class.client_version_string if self.scm_adapter_class | ||||
|
r5879 | rescue Exception => e | ||
|
r4762 | logger.error "scm: error during get version string: #{e.message}" | ||
end | ||||
ret | ||||
|
r4702 | end | ||
def self.scm_available | ||||
|
r4762 | ret = false | ||
begin | ||||
|
r5533 | ret = self.scm_adapter_class.client_available if self.scm_adapter_class | ||
|
r5879 | rescue Exception => e | ||
|
r4762 | logger.error "scm: error during get scm available: #{e.message}" | ||
end | ||||
ret | ||||
|
r4702 | end | ||
|
r8530 | def set_as_default? | ||
new_record? && project && !Repository.first(:conditions => {:project_id => project.id}) | ||||
end | ||||
protected | ||||
def check_default | ||||
if !is_default? && set_as_default? | ||||
self.is_default = true | ||||
end | ||||
if is_default? && is_default_changed? | ||||
Repository.update_all(["is_default = ?", false], ["project_id = ?", project_id]) | ||||
end | ||||
end | ||||
|
r9622 | def load_entries_changesets(entries) | ||
if entries | ||||
entries.each do |entry| | ||||
if entry.lastrev && entry.lastrev.identifier | ||||
entry.changeset = find_changeset_by_name(entry.lastrev.identifier) | ||||
end | ||||
end | ||||
end | ||||
end | ||||
|
r1234 | private | ||
|
r4821 | |||
|
r8727 | # Deletes repository data | ||
|
r1651 | def clear_changesets | ||
|
r8727 | cs = Changeset.table_name | ||
ch = Change.table_name | ||||
ci = "#{table_name_prefix}changesets_issues#{table_name_suffix}" | ||||
cp = "#{table_name_prefix}changeset_parents#{table_name_suffix}" | ||||
|
r2619 | 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})") | ||||
|
r8727 | connection.delete("DELETE FROM #{cp} WHERE #{cp}.changeset_id IN (SELECT #{cs}.id FROM #{cs} WHERE #{cs}.repository_id = #{id})") | ||
|
r2619 | connection.delete("DELETE FROM #{cs} WHERE #{cs}.repository_id = #{id}") | ||
|
r1651 | end | ||
|
r103 | end | ||