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 Changeset < ActiveRecord::Base Chris@1494: belongs_to :repository Chris@1494: belongs_to :user Chris@1494: has_many :filechanges, :class_name => 'Change', :dependent => :delete_all Chris@1494: has_and_belongs_to_many :issues Chris@1494: has_and_belongs_to_many :parents, Chris@1494: :class_name => "Changeset", Chris@1494: :join_table => "#{table_name_prefix}changeset_parents#{table_name_suffix}", Chris@1494: :association_foreign_key => 'parent_id', :foreign_key => 'changeset_id' Chris@1494: has_and_belongs_to_many :children, Chris@1494: :class_name => "Changeset", Chris@1494: :join_table => "#{table_name_prefix}changeset_parents#{table_name_suffix}", Chris@1494: :association_foreign_key => 'changeset_id', :foreign_key => 'parent_id' Chris@1494: Chris@1494: acts_as_event :title => Proc.new {|o| o.title}, Chris@1494: :description => :long_comments, Chris@1494: :datetime => :committed_on, Chris@1494: :url => Proc.new {|o| {:controller => 'repositories', :action => 'revision', :id => o.repository.project, :repository_id => o.repository.identifier_param, :rev => o.identifier}} Chris@1494: Chris@1494: acts_as_searchable :columns => 'comments', Chris@1494: :include => {:repository => :project}, Chris@1494: :project_key => "#{Repository.table_name}.project_id", Chris@1494: :date_column => 'committed_on' Chris@1494: Chris@1494: acts_as_activity_provider :timestamp => "#{table_name}.committed_on", Chris@1494: :author_key => :user_id, Chris@1494: :find_options => {:include => [:user, {:repository => :project}]} Chris@1494: Chris@1494: validates_presence_of :repository_id, :revision, :committed_on, :commit_date Chris@1494: validates_uniqueness_of :revision, :scope => :repository_id Chris@1494: validates_uniqueness_of :scmid, :scope => :repository_id, :allow_nil => true Chris@1494: Chris@1494: scope :visible, lambda {|*args| Chris@1494: includes(:repository => :project).where(Project.allowed_to_condition(args.shift || User.current, :view_changesets, *args)) Chris@1494: } Chris@1494: Chris@1494: after_create :scan_for_issues Chris@1494: before_create :before_create_cs Chris@1494: Chris@1494: def revision=(r) Chris@1494: write_attribute :revision, (r.nil? ? nil : r.to_s) Chris@1494: end Chris@1494: Chris@1494: # Returns the identifier of this changeset; depending on repository backends Chris@1494: def identifier Chris@1494: if repository.class.respond_to? :changeset_identifier Chris@1494: repository.class.changeset_identifier self Chris@1494: else Chris@1494: revision.to_s Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: def committed_on=(date) Chris@1494: self.commit_date = date Chris@1494: super Chris@1494: end Chris@1494: Chris@1494: # Returns the readable identifier Chris@1494: def format_identifier Chris@1494: if repository.class.respond_to? :format_changeset_identifier Chris@1494: repository.class.format_changeset_identifier self Chris@1494: else Chris@1494: identifier Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: def project Chris@1494: repository.project Chris@1494: end Chris@1494: Chris@1494: def author Chris@1494: user || committer.to_s.split('<').first Chris@1494: end Chris@1494: Chris@1494: def before_create_cs Chris@1494: self.committer = self.class.to_utf8(self.committer, repository.repo_log_encoding) Chris@1494: self.comments = self.class.normalize_comments( Chris@1494: self.comments, repository.repo_log_encoding) Chris@1494: self.user = repository.find_committer_user(self.committer) Chris@1494: end Chris@1494: Chris@1494: def scan_for_issues Chris@1494: scan_comment_for_issue_ids Chris@1494: end Chris@1494: Chris@1494: TIMELOG_RE = / Chris@1494: ( Chris@1494: ((\d+)(h|hours?))((\d+)(m|min)?)? Chris@1494: | Chris@1494: ((\d+)(h|hours?|m|min)) Chris@1494: | Chris@1494: (\d+):(\d+) Chris@1494: | Chris@1494: (\d+([\.,]\d+)?)h? Chris@1494: ) Chris@1494: /x Chris@1494: Chris@1494: def scan_comment_for_issue_ids Chris@1494: return if comments.blank? Chris@1494: # keywords used to reference issues Chris@1494: ref_keywords = Setting.commit_ref_keywords.downcase.split(",").collect(&:strip) Chris@1494: ref_keywords_any = ref_keywords.delete('*') Chris@1494: # keywords used to fix issues Chris@1494: fix_keywords = Setting.commit_update_keywords_array.map {|r| r['keywords']}.flatten.compact Chris@1494: Chris@1494: kw_regexp = (ref_keywords + fix_keywords).collect{|kw| Regexp.escape(kw)}.join("|") Chris@1494: Chris@1494: referenced_issues = [] Chris@1494: Chris@1494: comments.scan(/([\s\(\[,-]|^)((#{kw_regexp})[\s:]+)?(#\d+(\s+@#{TIMELOG_RE})?([\s,;&]+#\d+(\s+@#{TIMELOG_RE})?)*)(?=[[:punct:]]|\s|<|$)/i) do |match| Chris@1494: action, refs = match[2].to_s.downcase, match[3] Chris@1494: next unless action.present? || ref_keywords_any Chris@1494: Chris@1494: refs.scan(/#(\d+)(\s+@#{TIMELOG_RE})?/).each do |m| Chris@1494: issue, hours = find_referenced_issue_by_id(m[0].to_i), m[2] Chris@1494: if issue Chris@1494: referenced_issues << issue Chris@1494: # Don't update issues or log time when importing old commits Chris@1494: unless repository.created_on && committed_on && committed_on < repository.created_on Chris@1494: fix_issue(issue, action) if fix_keywords.include?(action) Chris@1494: log_time(issue, hours) if hours && Setting.commit_logtime_enabled? Chris@1494: end Chris@1494: end Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: referenced_issues.uniq! Chris@1494: self.issues = referenced_issues unless referenced_issues.empty? Chris@1494: end Chris@1494: Chris@1494: def short_comments Chris@1494: @short_comments || split_comments.first Chris@1494: end Chris@1494: Chris@1494: def long_comments Chris@1494: @long_comments || split_comments.last Chris@1494: end Chris@1494: Chris@1494: def text_tag(ref_project=nil) Chris@1494: repo = "" Chris@1494: if repository && repository.identifier.present? Chris@1494: repo = "#{repository.identifier}|" Chris@1494: end Chris@1494: tag = if scmid? Chris@1494: "commit:#{repo}#{scmid}" Chris@1494: else Chris@1494: "#{repo}r#{revision}" Chris@1494: end Chris@1494: if ref_project && project && ref_project != project Chris@1494: tag = "#{project.identifier}:#{tag}" Chris@1494: end Chris@1494: tag Chris@1494: end Chris@1494: Chris@1494: # Returns the title used for the changeset in the activity/search results Chris@1494: def title Chris@1494: repo = (repository && repository.identifier.present?) ? " (#{repository.identifier})" : '' Chris@1494: comm = short_comments.blank? ? '' : (': ' + short_comments) Chris@1494: "#{l(:label_revision)} #{format_identifier}#{repo}#{comm}" Chris@1494: end Chris@1494: Chris@1494: # Returns the previous changeset Chris@1494: def previous Chris@1494: @previous ||= Changeset.where(["id < ? AND repository_id = ?", id, repository_id]).order('id DESC').first Chris@1494: end Chris@1494: Chris@1494: # Returns the next changeset Chris@1494: def next Chris@1494: @next ||= Changeset.where(["id > ? AND repository_id = ?", id, repository_id]).order('id ASC').first Chris@1494: end Chris@1494: Chris@1494: # Creates a new Change from it's common parameters Chris@1494: def create_change(change) Chris@1494: Change.create(:changeset => self, Chris@1494: :action => change[:action], Chris@1494: :path => change[:path], Chris@1494: :from_path => change[:from_path], Chris@1494: :from_revision => change[:from_revision]) Chris@1494: end Chris@1494: Chris@1494: # Finds an issue that can be referenced by the commit message Chris@1494: def find_referenced_issue_by_id(id) Chris@1494: return nil if id.blank? Chris@1494: issue = Issue.find_by_id(id.to_i, :include => :project) Chris@1494: if Setting.commit_cross_project_ref? Chris@1494: # all issues can be referenced/fixed Chris@1494: elsif issue Chris@1494: # issue that belong to the repository project, a subproject or a parent project only Chris@1494: unless issue.project && Chris@1494: (project == issue.project || project.is_ancestor_of?(issue.project) || Chris@1494: project.is_descendant_of?(issue.project)) Chris@1494: issue = nil Chris@1494: end Chris@1494: end Chris@1494: issue Chris@1494: end Chris@1494: Chris@1494: private Chris@1494: Chris@1494: # Updates the +issue+ according to +action+ Chris@1494: def fix_issue(issue, action) Chris@1494: # the issue may have been updated by the closure of another one (eg. duplicate) Chris@1494: issue.reload Chris@1494: # don't change the status is the issue is closed Chris@1494: return if issue.status && issue.status.is_closed? Chris@1494: Chris@1494: journal = issue.init_journal(user || User.anonymous, Chris@1494: ll(Setting.default_language, Chris@1494: :text_status_changed_by_changeset, Chris@1494: text_tag(issue.project))) Chris@1494: rule = Setting.commit_update_keywords_array.detect do |rule| Chris@1494: rule['keywords'].include?(action) && Chris@1494: (rule['if_tracker_id'].blank? || rule['if_tracker_id'] == issue.tracker_id.to_s) Chris@1494: end Chris@1494: if rule Chris@1494: issue.assign_attributes rule.slice(*Issue.attribute_names) Chris@1494: end Chris@1494: Redmine::Hook.call_hook(:model_changeset_scan_commit_for_issue_ids_pre_issue_update, Chris@1494: { :changeset => self, :issue => issue, :action => action }) Chris@1494: unless issue.save Chris@1494: logger.warn("Issue ##{issue.id} could not be saved by changeset #{id}: #{issue.errors.full_messages}") if logger Chris@1494: end Chris@1494: issue Chris@1494: end Chris@1494: Chris@1494: def log_time(issue, hours) Chris@1494: time_entry = TimeEntry.new( Chris@1494: :user => user, Chris@1494: :hours => hours, Chris@1494: :issue => issue, Chris@1494: :spent_on => commit_date, Chris@1494: :comments => l(:text_time_logged_by_changeset, :value => text_tag(issue.project), Chris@1494: :locale => Setting.default_language) Chris@1494: ) Chris@1494: time_entry.activity = log_time_activity unless log_time_activity.nil? Chris@1494: Chris@1494: unless time_entry.save Chris@1494: logger.warn("TimeEntry could not be created by changeset #{id}: #{time_entry.errors.full_messages}") if logger Chris@1494: end Chris@1494: time_entry Chris@1494: end Chris@1494: Chris@1494: def log_time_activity Chris@1494: if Setting.commit_logtime_activity_id.to_i > 0 Chris@1494: TimeEntryActivity.find_by_id(Setting.commit_logtime_activity_id.to_i) Chris@1494: end Chris@1494: end Chris@1494: Chris@1494: def split_comments Chris@1494: comments =~ /\A(.+?)\r?\n(.*)$/m Chris@1494: @short_comments = $1 || comments Chris@1494: @long_comments = $2.to_s.strip Chris@1494: return @short_comments, @long_comments Chris@1494: end Chris@1494: Chris@1494: public Chris@1494: Chris@1494: # Strips and reencodes a commit log before insertion into the database Chris@1494: def self.normalize_comments(str, encoding) Chris@1494: Changeset.to_utf8(str.to_s.strip, encoding) Chris@1494: end Chris@1494: Chris@1494: def self.to_utf8(str, encoding) Chris@1494: Redmine::CodesetUtil.to_utf8(str, encoding) Chris@1494: end Chris@1494: end