Chris@909: # Redmine - project management software Chris@909: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@909: # Chris@909: # This program is free software; you can redistribute it and/or Chris@909: # modify it under the terms of the GNU General Public License Chris@909: # as published by the Free Software Foundation; either version 2 Chris@909: # of the License, or (at your option) any later version. Chris@909: # Chris@909: # This program is distributed in the hope that it will be useful, Chris@909: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@909: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@909: # GNU General Public License for more details. Chris@909: # Chris@909: # You should have received a copy of the GNU General Public License Chris@909: # along with this program; if not, write to the Free Software Chris@909: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@909: Chris@909: require 'iconv' Chris@909: Chris@909: class Changeset < ActiveRecord::Base Chris@909: belongs_to :repository Chris@909: belongs_to :user Chris@909: has_many :changes, :dependent => :delete_all Chris@909: has_and_belongs_to_many :issues Chris@909: has_and_belongs_to_many :parents, Chris@909: :class_name => "Changeset", Chris@909: :join_table => "#{table_name_prefix}changeset_parents#{table_name_suffix}", Chris@909: :association_foreign_key => 'parent_id', :foreign_key => 'changeset_id' Chris@909: has_and_belongs_to_many :children, Chris@909: :class_name => "Changeset", Chris@909: :join_table => "#{table_name_prefix}changeset_parents#{table_name_suffix}", Chris@909: :association_foreign_key => 'changeset_id', :foreign_key => 'parent_id' Chris@909: Chris@909: acts_as_event :title => Proc.new {|o| "#{l(:label_revision)} #{o.format_identifier}" + (o.short_comments.blank? ? '' : (': ' + o.short_comments))}, Chris@909: :description => :long_comments, Chris@909: :datetime => :committed_on, Chris@909: :url => Proc.new {|o| {:controller => 'repositories', :action => 'revision', :id => o.repository.project, :rev => o.identifier}} Chris@909: Chris@909: acts_as_searchable :columns => 'comments', Chris@909: :include => {:repository => :project}, Chris@909: :project_key => "#{Repository.table_name}.project_id", Chris@909: :date_column => 'committed_on' Chris@909: Chris@909: acts_as_activity_provider :timestamp => "#{table_name}.committed_on", Chris@909: :author_key => :user_id, Chris@909: :find_options => {:include => [:user, {:repository => :project}]} Chris@909: Chris@909: validates_presence_of :repository_id, :revision, :committed_on, :commit_date Chris@909: validates_uniqueness_of :revision, :scope => :repository_id Chris@909: validates_uniqueness_of :scmid, :scope => :repository_id, :allow_nil => true Chris@909: Chris@909: named_scope :visible, lambda {|*args| { :include => {:repository => :project}, Chris@909: :conditions => Project.allowed_to_condition(args.shift || User.current, :view_changesets, *args) } } Chris@909: Chris@909: after_create :scan_for_issues Chris@909: before_create :before_create_cs Chris@909: Chris@909: def revision=(r) Chris@909: write_attribute :revision, (r.nil? ? nil : r.to_s) Chris@909: end Chris@909: Chris@909: # Returns the identifier of this changeset; depending on repository backends Chris@909: def identifier Chris@909: if repository.class.respond_to? :changeset_identifier Chris@909: repository.class.changeset_identifier self Chris@909: else Chris@909: revision.to_s Chris@909: end Chris@909: end Chris@909: Chris@909: def committed_on=(date) Chris@909: self.commit_date = date Chris@909: super Chris@909: end Chris@909: Chris@909: # Returns the readable identifier Chris@909: def format_identifier Chris@909: if repository.class.respond_to? :format_changeset_identifier Chris@909: repository.class.format_changeset_identifier self Chris@909: else Chris@909: identifier Chris@909: end Chris@909: end Chris@909: Chris@909: def project Chris@909: repository.project Chris@909: end Chris@909: Chris@909: def author Chris@909: user || committer.to_s.split('<').first Chris@909: end Chris@909: Chris@909: def before_create_cs Chris@909: self.committer = self.class.to_utf8(self.committer, repository.repo_log_encoding) Chris@909: self.comments = self.class.normalize_comments( Chris@909: self.comments, repository.repo_log_encoding) Chris@909: self.user = repository.find_committer_user(self.committer) Chris@909: end Chris@909: Chris@909: def scan_for_issues Chris@909: scan_comment_for_issue_ids Chris@909: end Chris@909: Chris@909: TIMELOG_RE = / Chris@909: ( Chris@909: ((\d+)(h|hours?))((\d+)(m|min)?)? Chris@909: | Chris@909: ((\d+)(h|hours?|m|min)) Chris@909: | Chris@909: (\d+):(\d+) Chris@909: | Chris@909: (\d+([\.,]\d+)?)h? Chris@909: ) Chris@909: /x Chris@909: Chris@909: def scan_comment_for_issue_ids Chris@909: return if comments.blank? Chris@909: # keywords used to reference issues Chris@909: ref_keywords = Setting.commit_ref_keywords.downcase.split(",").collect(&:strip) Chris@909: ref_keywords_any = ref_keywords.delete('*') Chris@909: # keywords used to fix issues Chris@909: fix_keywords = Setting.commit_fix_keywords.downcase.split(",").collect(&:strip) Chris@909: Chris@909: kw_regexp = (ref_keywords + fix_keywords).collect{|kw| Regexp.escape(kw)}.join("|") Chris@909: Chris@909: referenced_issues = [] Chris@909: Chris@909: comments.scan(/([\s\(\[,-]|^)((#{kw_regexp})[\s:]+)?(#\d+(\s+@#{TIMELOG_RE})?([\s,;&]+#\d+(\s+@#{TIMELOG_RE})?)*)(?=[[:punct:]]|\s|<|$)/i) do |match| Chris@909: action, refs = match[2], match[3] Chris@909: next unless action.present? || ref_keywords_any Chris@909: Chris@909: refs.scan(/#(\d+)(\s+@#{TIMELOG_RE})?/).each do |m| Chris@909: issue, hours = find_referenced_issue_by_id(m[0].to_i), m[2] Chris@909: if issue Chris@909: referenced_issues << issue Chris@909: fix_issue(issue) if fix_keywords.include?(action.to_s.downcase) Chris@909: log_time(issue, hours) if hours && Setting.commit_logtime_enabled? Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: referenced_issues.uniq! Chris@909: self.issues = referenced_issues unless referenced_issues.empty? Chris@909: end Chris@909: Chris@909: def short_comments Chris@909: @short_comments || split_comments.first Chris@909: end Chris@909: Chris@909: def long_comments Chris@909: @long_comments || split_comments.last Chris@909: end Chris@909: Chris@909: def text_tag Chris@909: if scmid? Chris@909: "commit:#{scmid}" Chris@909: else Chris@909: "r#{revision}" Chris@909: end Chris@909: end Chris@909: Chris@909: # Returns the previous changeset Chris@909: def previous Chris@909: @previous ||= Changeset.find(:first, Chris@909: :conditions => ['id < ? AND repository_id = ?', Chris@909: self.id, self.repository_id], Chris@909: :order => 'id DESC') Chris@909: end Chris@909: Chris@909: # Returns the next changeset Chris@909: def next Chris@909: @next ||= Changeset.find(:first, Chris@909: :conditions => ['id > ? AND repository_id = ?', Chris@909: self.id, self.repository_id], Chris@909: :order => 'id ASC') Chris@909: end Chris@909: Chris@909: # Creates a new Change from it's common parameters Chris@909: def create_change(change) Chris@909: Change.create(:changeset => self, Chris@909: :action => change[:action], Chris@909: :path => change[:path], Chris@909: :from_path => change[:from_path], Chris@909: :from_revision => change[:from_revision]) Chris@909: end Chris@909: Chris@909: private Chris@909: Chris@909: # Finds an issue that can be referenced by the commit message Chris@909: # i.e. an issue that belong to the repository project, a subproject or a parent project Chris@909: def find_referenced_issue_by_id(id) Chris@909: return nil if id.blank? Chris@909: issue = Issue.find_by_id(id.to_i, :include => :project) Chris@909: if issue Chris@909: unless issue.project && Chris@909: (project == issue.project || project.is_ancestor_of?(issue.project) || Chris@909: project.is_descendant_of?(issue.project)) Chris@909: issue = nil Chris@909: end Chris@909: end Chris@909: issue Chris@909: end Chris@909: Chris@909: def fix_issue(issue) Chris@909: status = IssueStatus.find_by_id(Setting.commit_fix_status_id.to_i) Chris@909: if status.nil? Chris@909: logger.warn("No status matches commit_fix_status_id setting (#{Setting.commit_fix_status_id})") if logger Chris@909: return issue Chris@909: end Chris@909: Chris@909: # the issue may have been updated by the closure of another one (eg. duplicate) Chris@909: issue.reload Chris@909: # don't change the status is the issue is closed Chris@909: return if issue.status && issue.status.is_closed? Chris@909: Chris@909: journal = issue.init_journal(user || User.anonymous, ll(Setting.default_language, :text_status_changed_by_changeset, text_tag)) Chris@909: issue.status = status Chris@909: unless Setting.commit_fix_done_ratio.blank? Chris@909: issue.done_ratio = Setting.commit_fix_done_ratio.to_i Chris@909: end Chris@909: Redmine::Hook.call_hook(:model_changeset_scan_commit_for_issue_ids_pre_issue_update, Chris@909: { :changeset => self, :issue => issue }) Chris@909: unless issue.save Chris@909: logger.warn("Issue ##{issue.id} could not be saved by changeset #{id}: #{issue.errors.full_messages}") if logger Chris@909: end Chris@909: issue Chris@909: end Chris@909: Chris@909: def log_time(issue, hours) Chris@909: time_entry = TimeEntry.new( Chris@909: :user => user, Chris@909: :hours => hours, Chris@909: :issue => issue, Chris@909: :spent_on => commit_date, Chris@909: :comments => l(:text_time_logged_by_changeset, :value => text_tag, Chris@909: :locale => Setting.default_language) Chris@909: ) Chris@909: time_entry.activity = log_time_activity unless log_time_activity.nil? Chris@909: Chris@909: unless time_entry.save Chris@909: logger.warn("TimeEntry could not be created by changeset #{id}: #{time_entry.errors.full_messages}") if logger Chris@909: end Chris@909: time_entry Chris@909: end Chris@909: Chris@909: def log_time_activity Chris@909: if Setting.commit_logtime_activity_id.to_i > 0 Chris@909: TimeEntryActivity.find_by_id(Setting.commit_logtime_activity_id.to_i) Chris@909: end Chris@909: end Chris@909: Chris@909: def split_comments Chris@909: comments =~ /\A(.+?)\r?\n(.*)$/m Chris@909: @short_comments = $1 || comments Chris@909: @long_comments = $2.to_s.strip Chris@909: return @short_comments, @long_comments Chris@909: end Chris@909: Chris@909: public Chris@909: Chris@909: # Strips and reencodes a commit log before insertion into the database Chris@909: def self.normalize_comments(str, encoding) Chris@909: Changeset.to_utf8(str.to_s.strip, encoding) Chris@909: end Chris@909: Chris@909: def self.to_utf8(str, encoding) Chris@909: Redmine::CodesetUtil.to_utf8(str, encoding) Chris@909: end Chris@909: end