Chris@1494: # Redmine - project management software Chris@1494: # Copyright (C) 2006-2014 Jean-Philippe Lang Chris@1494: # Chris@1494: # This program is free software; you can redistribute it and/or Chris@1494: # modify it under the terms of the GNU General Public License Chris@1494: # as published by the Free Software Foundation; either version 2 Chris@1494: # of the License, or (at your option) any later version. Chris@1494: # Chris@1494: # This program is distributed in the hope that it will be useful, Chris@1494: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1494: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1494: # GNU General Public License for more details. Chris@1494: # Chris@1494: # You should have received a copy of the GNU General Public License Chris@1494: # along with this program; if not, write to the Free Software Chris@1494: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1494: Chris@1494: class ScmFetchError < Exception; end Chris@1494: Chris@1494: class Repository < ActiveRecord::Base Chris@1494: include Redmine::Ciphering Chris@1494: include Redmine::SafeAttributes Chris@1494: Chris@1494: # Maximum length for repository identifiers Chris@1494: IDENTIFIER_MAX_LENGTH = 255 Chris@1494: Chris@1494: belongs_to :project Chris@1494: has_many :changesets, :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC" Chris@1494: has_many :filechanges, :class_name => 'Change', :through => :changesets Chris@1494: Chris@1494: serialize :extra_info Chris@1494: Chris@1494: before_save :check_default Chris@1494: Chris@1494: # Raw SQL to delete changesets and changes in the database Chris@1494: # has_many :changesets, :dependent => :destroy is too slow for big repositories Chris@1494: before_destroy :clear_changesets Chris@1494: Chris@1494: validates_length_of :password, :maximum => 255, :allow_nil => true Chris@1494: validates_length_of :identifier, :maximum => IDENTIFIER_MAX_LENGTH, :allow_blank => true Chris@1494: validates_presence_of :identifier, :unless => Proc.new { |r| r.is_default? || r.set_as_default? } Chris@1494: validates_uniqueness_of :identifier, :scope => :project_id, :allow_blank => true Chris@1494: validates_exclusion_of :identifier, :in => %w(show entry raw changes annotate diff show stats graph) Chris@1494: # donwcase letters, digits, dashes, underscores but not digits only Chris@1494: validates_format_of :identifier, :with => /\A(?!\d+$)[a-z0-9\-_]*\z/, :allow_blank => true Chris@1494: # Checks if the SCM is enabled when creating a repository Chris@1494: validate :repo_create_validation, :on => :create Chris@1494: Chris@1494: safe_attributes 'identifier', Chris@1494: 'login', Chris@1494: 'password', Chris@1494: 'path_encoding', Chris@1494: 'log_encoding', Chris@1494: 'is_default' Chris@1494: Chris@1494: safe_attributes 'url', Chris@1494: :if => lambda {|repository, user| repository.new_record?} Chris@1494: Chris@1494: def repo_create_validation Chris@1494: unless Setting.enabled_scm.include?(self.class.name.demodulize) Chris@1494: errors.add(:type, :invalid) Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: def self.human_attribute_name(attribute_key_name, *args) Chris@1494: attr_name = attribute_key_name.to_s Chris@1494: if attr_name == "log_encoding" Chris@1494: attr_name = "commit_logs_encoding" Chris@1494: end Chris@1494: super(attr_name, *args) Chris@1494: end Chris@1494: Chris@1494: # Removes leading and trailing whitespace Chris@1494: def url=(arg) Chris@1494: write_attribute(:url, arg ? arg.to_s.strip : nil) Chris@1494: end Chris@1494: Chris@1494: # Removes leading and trailing whitespace Chris@1494: def root_url=(arg) Chris@1494: write_attribute(:root_url, arg ? arg.to_s.strip : nil) Chris@1494: end Chris@1494: Chris@1494: def password Chris@1494: read_ciphered_attribute(:password) Chris@1494: end Chris@1494: Chris@1494: def password=(arg) Chris@1494: write_ciphered_attribute(:password, arg) Chris@1494: end Chris@1494: Chris@1494: def scm_adapter Chris@1494: self.class.scm_adapter_class Chris@1494: end Chris@1494: Chris@1494: def scm Chris@1494: unless @scm Chris@1494: @scm = self.scm_adapter.new(url, root_url, Chris@1494: login, password, path_encoding) Chris@1494: if root_url.blank? && @scm.root_url.present? Chris@1494: update_attribute(:root_url, @scm.root_url) Chris@1494: end Chris@1494: end Chris@1494: @scm Chris@1494: end Chris@1494: Chris@1494: def scm_name Chris@1494: self.class.scm_name Chris@1494: end Chris@1494: Chris@1494: def name Chris@1494: if identifier.present? Chris@1494: identifier Chris@1494: elsif is_default? Chris@1494: l(:field_repository_is_default) Chris@1494: else Chris@1494: scm_name Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: def identifier=(identifier) Chris@1494: super unless identifier_frozen? Chris@1494: end Chris@1494: Chris@1494: def identifier_frozen? Chris@1494: errors[:identifier].blank? && !(new_record? || identifier.blank?) Chris@1494: end Chris@1494: Chris@1494: def identifier_param Chris@1494: if is_default? Chris@1494: nil Chris@1494: elsif identifier.present? Chris@1494: identifier Chris@1494: else Chris@1494: id.to_s Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: def <=>(repository) Chris@1494: if is_default? Chris@1494: -1 Chris@1494: elsif repository.is_default? Chris@1494: 1 Chris@1494: else Chris@1494: identifier.to_s <=> repository.identifier.to_s Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: def self.find_by_identifier_param(param) Chris@1494: if param.to_s =~ /^\d+$/ Chris@1494: find_by_id(param) Chris@1494: else Chris@1494: find_by_identifier(param) Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: # TODO: should return an empty hash instead of nil to avoid many ||{} Chris@1494: def extra_info Chris@1494: h = read_attribute(:extra_info) Chris@1494: h.is_a?(Hash) ? h : nil Chris@1494: end Chris@1494: Chris@1494: def merge_extra_info(arg) Chris@1494: h = extra_info || {} Chris@1494: return h if arg.nil? Chris@1494: h.merge!(arg) Chris@1494: write_attribute(:extra_info, h) Chris@1494: end Chris@1494: Chris@1494: def report_last_commit Chris@1494: true Chris@1494: end Chris@1494: Chris@1494: def supports_cat? Chris@1494: scm.supports_cat? Chris@1494: end Chris@1494: Chris@1494: def supports_annotate? Chris@1494: scm.supports_annotate? Chris@1494: end Chris@1494: Chris@1494: def supports_all_revisions? Chris@1494: true Chris@1494: end Chris@1494: Chris@1494: def supports_directory_revisions? Chris@1494: false Chris@1494: end Chris@1494: Chris@1494: def supports_revision_graph? Chris@1494: false Chris@1494: end Chris@1494: Chris@1494: def entry(path=nil, identifier=nil) Chris@1494: scm.entry(path, identifier) Chris@1494: end Chris@1494: Chris@1494: def entries(path=nil, identifier=nil) Chris@1494: entries = scm.entries(path, identifier) Chris@1494: load_entries_changesets(entries) Chris@1494: entries Chris@1494: end Chris@1494: Chris@1494: def branches Chris@1494: scm.branches Chris@1494: end Chris@1494: Chris@1494: def tags Chris@1494: scm.tags Chris@1494: end Chris@1494: Chris@1494: def default_branch Chris@1494: nil Chris@1494: end Chris@1494: Chris@1494: def properties(path, identifier=nil) Chris@1494: scm.properties(path, identifier) Chris@1494: end Chris@1494: Chris@1494: def cat(path, identifier=nil) Chris@1494: scm.cat(path, identifier) Chris@1494: end Chris@1494: Chris@1494: def diff(path, rev, rev_to) Chris@1494: scm.diff(path, rev, rev_to) Chris@1494: end Chris@1494: Chris@1494: def diff_format_revisions(cs, cs_to, sep=':') Chris@1494: text = "" Chris@1494: text << cs_to.format_identifier + sep if cs_to Chris@1494: text << cs.format_identifier if cs Chris@1494: text Chris@1494: end Chris@1494: Chris@1494: # Returns a path relative to the url of the repository Chris@1494: def relative_path(path) Chris@1494: path Chris@1494: end Chris@1494: Chris@1494: # Finds and returns a revision with a number or the beginning of a hash Chris@1494: def find_changeset_by_name(name) Chris@1494: return nil if name.blank? Chris@1494: s = name.to_s Chris@1494: if s.match(/^\d*$/) Chris@1494: changesets.where("revision = ?", s).first Chris@1494: else Chris@1494: changesets.where("revision LIKE ?", s + '%').first Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: def latest_changeset Chris@1494: @latest_changeset ||= changesets.first Chris@1494: end Chris@1494: Chris@1494: # Returns the latest changesets for +path+ Chris@1494: # Default behaviour is to search in cached changesets Chris@1494: def latest_changesets(path, rev, limit=10) Chris@1494: if path.blank? Chris@1494: changesets. Chris@1494: reorder("#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC"). Chris@1494: limit(limit). Chris@1494: preload(:user). Chris@1494: all Chris@1494: else Chris@1494: filechanges. Chris@1494: where("path = ?", path.with_leading_slash). Chris@1494: reorder("#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC"). Chris@1494: limit(limit). Chris@1494: preload(:changeset => :user). Chris@1494: collect(&:changeset) Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: def scan_changesets_for_issue_ids Chris@1494: self.changesets.each(&:scan_comment_for_issue_ids) Chris@1494: end Chris@1494: Chris@1494: # Returns an array of committers usernames and associated user_id Chris@1494: def committers Chris@1494: @committers ||= Changeset.connection.select_rows( Chris@1494: "SELECT DISTINCT committer, user_id FROM #{Changeset.table_name} WHERE repository_id = #{id}") Chris@1494: end Chris@1494: Chris@1494: # Maps committers username to a user ids Chris@1494: def committer_ids=(h) Chris@1494: if h.is_a?(Hash) Chris@1494: committers.each do |committer, user_id| Chris@1494: new_user_id = h[committer] Chris@1494: if new_user_id && (new_user_id.to_i != user_id.to_i) Chris@1494: new_user_id = (new_user_id.to_i > 0 ? new_user_id.to_i : nil) Chris@1494: Changeset.update_all( Chris@1494: "user_id = #{ new_user_id.nil? ? 'NULL' : new_user_id }", Chris@1494: ["repository_id = ? AND committer = ?", id, committer]) Chris@1494: end Chris@1494: end Chris@1494: @committers = nil Chris@1494: @found_committer_users = nil Chris@1494: true Chris@1494: else Chris@1494: false Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: # Returns the Redmine User corresponding to the given +committer+ Chris@1494: # It will return nil if the committer is not yet mapped and if no User Chris@1494: # with the same username or email was found Chris@1494: def find_committer_user(committer) Chris@1494: unless committer.blank? Chris@1494: @found_committer_users ||= {} Chris@1494: return @found_committer_users[committer] if @found_committer_users.has_key?(committer) Chris@1494: Chris@1494: user = nil Chris@1494: c = changesets.where(:committer => committer).includes(:user).first Chris@1494: if c && c.user Chris@1494: user = c.user Chris@1494: elsif committer.strip =~ /^([^<]+)(<(.*)>)?$/ Chris@1494: username, email = $1.strip, $3 Chris@1494: u = User.find_by_login(username) Chris@1494: u ||= User.find_by_mail(email) unless email.blank? Chris@1494: user = u Chris@1494: end Chris@1494: @found_committer_users[committer] = user Chris@1494: user Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: def repo_log_encoding Chris@1494: encoding = log_encoding.to_s.strip Chris@1494: encoding.blank? ? 'UTF-8' : encoding Chris@1494: end Chris@1494: Chris@1494: # Fetches new changesets for all repositories of active projects Chris@1494: # Can be called periodically by an external script Chris@1494: # eg. ruby script/runner "Repository.fetch_changesets" Chris@1494: def self.fetch_changesets Chris@1494: Project.active.has_module(:repository).all.each do |project| Chris@1494: project.repositories.each do |repository| Chris@1494: begin Chris@1494: repository.fetch_changesets Chris@1494: rescue Redmine::Scm::Adapters::CommandFailed => e Chris@1494: logger.error "scm: error during fetching changesets: #{e.message}" Chris@1494: end Chris@1494: end Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: # scan changeset comments to find related and fixed issues for all repositories Chris@1494: def self.scan_changesets_for_issue_ids Chris@1494: all.each(&:scan_changesets_for_issue_ids) Chris@1494: end Chris@1494: Chris@1494: def self.scm_name Chris@1494: 'Abstract' Chris@1494: end Chris@1494: Chris@1494: def self.available_scm Chris@1494: subclasses.collect {|klass| [klass.scm_name, klass.name]} Chris@1494: end Chris@1494: Chris@1494: def self.factory(klass_name, *args) Chris@1494: klass = "Repository::#{klass_name}".constantize Chris@1494: klass.new(*args) Chris@1494: rescue Chris@1494: nil Chris@1494: end Chris@1494: Chris@1494: def self.scm_adapter_class Chris@1494: nil Chris@1494: end Chris@1494: Chris@1494: def self.scm_command Chris@1494: ret = "" Chris@1494: begin Chris@1494: ret = self.scm_adapter_class.client_command if self.scm_adapter_class Chris@1494: rescue Exception => e Chris@1494: logger.error "scm: error during get command: #{e.message}" Chris@1494: end Chris@1494: ret Chris@1494: end Chris@1494: Chris@1494: def self.scm_version_string Chris@1494: ret = "" Chris@1494: begin Chris@1494: ret = self.scm_adapter_class.client_version_string if self.scm_adapter_class Chris@1494: rescue Exception => e Chris@1494: logger.error "scm: error during get version string: #{e.message}" Chris@1494: end Chris@1494: ret Chris@1494: end Chris@1494: Chris@1494: def self.scm_available Chris@1494: ret = false Chris@1494: begin Chris@1494: ret = self.scm_adapter_class.client_available if self.scm_adapter_class Chris@1494: rescue Exception => e Chris@1494: logger.error "scm: error during get scm available: #{e.message}" Chris@1494: end Chris@1494: ret Chris@1494: end Chris@1494: Chris@1494: def set_as_default? Chris@1494: new_record? && project && Repository.where(:project_id => project.id).empty? Chris@1494: end Chris@1494: Chris@1494: protected Chris@1494: Chris@1494: def check_default Chris@1494: if !is_default? && set_as_default? Chris@1494: self.is_default = true Chris@1494: end Chris@1494: if is_default? && is_default_changed? Chris@1494: Repository.update_all(["is_default = ?", false], ["project_id = ?", project_id]) Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: def load_entries_changesets(entries) Chris@1494: if entries Chris@1494: entries.each do |entry| Chris@1494: if entry.lastrev && entry.lastrev.identifier Chris@1494: entry.changeset = find_changeset_by_name(entry.lastrev.identifier) Chris@1494: end Chris@1494: end Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: private Chris@1494: Chris@1494: # Deletes repository data Chris@1494: def clear_changesets Chris@1494: cs = Changeset.table_name Chris@1494: ch = Change.table_name Chris@1494: ci = "#{table_name_prefix}changesets_issues#{table_name_suffix}" Chris@1494: cp = "#{table_name_prefix}changeset_parents#{table_name_suffix}" Chris@1494: Chris@1494: connection.delete("DELETE FROM #{ch} WHERE #{ch}.changeset_id IN (SELECT #{cs}.id FROM #{cs} WHERE #{cs}.repository_id = #{id})") Chris@1494: connection.delete("DELETE FROM #{ci} WHERE #{ci}.changeset_id IN (SELECT #{cs}.id FROM #{cs} WHERE #{cs}.repository_id = #{id})") Chris@1494: connection.delete("DELETE FROM #{cp} WHERE #{cp}.changeset_id IN (SELECT #{cs}.id FROM #{cs} WHERE #{cs}.repository_id = #{id})") Chris@1494: connection.delete("DELETE FROM #{cs} WHERE #{cs}.repository_id = #{id}") Chris@1494: clear_extra_info_of_changesets Chris@1494: end Chris@1494: Chris@1494: def clear_extra_info_of_changesets Chris@1494: end Chris@1494: end