annotate app/models/changeset.rb @ 904:0a8317a50fa0 redmine-1.1

Close obsolete branch redmine-1.1
author Chris Cannam
date Fri, 14 Jan 2011 12:53:21 +0000
parents af80e5618e9b
children b859cc0c4fa1
rev   line source
Chris@0 1 # Redmine - project management software
Chris@0 2 # Copyright (C) 2006-2010 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@0 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@0 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@0 18 require 'iconv'
Chris@0 19
Chris@0 20 class Changeset < ActiveRecord::Base
Chris@0 21 belongs_to :repository
Chris@0 22 belongs_to :user
Chris@0 23 has_many :changes, :dependent => :delete_all
Chris@0 24 has_and_belongs_to_many :issues
Chris@0 25
Chris@117 26 acts_as_event :title => Proc.new {|o| "#{l(:label_revision)} #{o.format_identifier}" + (o.short_comments.blank? ? '' : (': ' + o.short_comments))},
Chris@0 27 :description => :long_comments,
Chris@0 28 :datetime => :committed_on,
Chris@117 29 :url => Proc.new {|o| {:controller => 'repositories', :action => 'revision', :id => o.repository.project, :rev => o.identifier}}
Chris@0 30
Chris@0 31 acts_as_searchable :columns => 'comments',
Chris@0 32 :include => {:repository => :project},
Chris@0 33 :project_key => "#{Repository.table_name}.project_id",
Chris@0 34 :date_column => 'committed_on'
Chris@0 35
Chris@0 36 acts_as_activity_provider :timestamp => "#{table_name}.committed_on",
Chris@0 37 :author_key => :user_id,
Chris@0 38 :find_options => {:include => [:user, {:repository => :project}]}
Chris@0 39
Chris@0 40 validates_presence_of :repository_id, :revision, :committed_on, :commit_date
Chris@0 41 validates_uniqueness_of :revision, :scope => :repository_id
Chris@0 42 validates_uniqueness_of :scmid, :scope => :repository_id, :allow_nil => true
Chris@0 43
Chris@0 44 named_scope :visible, lambda {|*args| { :include => {:repository => :project},
Chris@0 45 :conditions => Project.allowed_to_condition(args.first || User.current, :view_changesets) } }
Chris@0 46
Chris@0 47 def revision=(r)
Chris@0 48 write_attribute :revision, (r.nil? ? nil : r.to_s)
Chris@0 49 end
Chris@117 50
Chris@117 51 # Returns the identifier of this changeset; depending on repository backends
Chris@117 52 def identifier
Chris@117 53 if repository.class.respond_to? :changeset_identifier
Chris@117 54 repository.class.changeset_identifier self
Chris@117 55 else
Chris@117 56 revision.to_s
Chris@117 57 end
Chris@117 58 end
Chris@0 59
Chris@0 60 def comments=(comment)
Chris@0 61 write_attribute(:comments, Changeset.normalize_comments(comment))
Chris@0 62 end
Chris@0 63
Chris@0 64 def committed_on=(date)
Chris@0 65 self.commit_date = date
Chris@0 66 super
Chris@0 67 end
Chris@117 68
Chris@117 69 # Returns the readable identifier
Chris@117 70 def format_identifier
Chris@117 71 if repository.class.respond_to? :format_changeset_identifier
Chris@117 72 repository.class.format_changeset_identifier self
Chris@117 73 else
Chris@117 74 identifier
Chris@117 75 end
Chris@117 76 end
Chris@0 77
Chris@0 78 def committer=(arg)
Chris@0 79 write_attribute(:committer, self.class.to_utf8(arg.to_s))
Chris@0 80 end
Chris@0 81
Chris@0 82 def project
Chris@0 83 repository.project
Chris@0 84 end
Chris@0 85
Chris@0 86 def author
Chris@0 87 user || committer.to_s.split('<').first
Chris@0 88 end
Chris@0 89
Chris@0 90 def before_create
Chris@0 91 self.user = repository.find_committer_user(committer)
Chris@0 92 end
Chris@0 93
Chris@0 94 def after_create
Chris@0 95 scan_comment_for_issue_ids
Chris@0 96 end
Chris@0 97
Chris@117 98 TIMELOG_RE = /
Chris@117 99 (
Chris@117 100 (\d+([.,]\d+)?)h?
Chris@117 101 |
Chris@117 102 (\d+):(\d+)
Chris@117 103 |
Chris@117 104 ((\d+)(h|hours?))?((\d+)(m|min)?)?
Chris@117 105 )
Chris@117 106 /x
Chris@117 107
Chris@0 108 def scan_comment_for_issue_ids
Chris@0 109 return if comments.blank?
Chris@0 110 # keywords used to reference issues
Chris@0 111 ref_keywords = Setting.commit_ref_keywords.downcase.split(",").collect(&:strip)
Chris@117 112 ref_keywords_any = ref_keywords.delete('*')
Chris@0 113 # keywords used to fix issues
Chris@0 114 fix_keywords = Setting.commit_fix_keywords.downcase.split(",").collect(&:strip)
Chris@0 115
Chris@0 116 kw_regexp = (ref_keywords + fix_keywords).collect{|kw| Regexp.escape(kw)}.join("|")
Chris@0 117
Chris@0 118 referenced_issues = []
Chris@0 119
Chris@117 120 comments.scan(/([\s\(\[,-]|^)((#{kw_regexp})[\s:]+)?(#\d+(\s+@#{TIMELOG_RE})?([\s,;&]+#\d+(\s+@#{TIMELOG_RE})?)*)(?=[[:punct:]]|\s|<|$)/i) do |match|
Chris@117 121 action, refs = match[2], match[3]
Chris@117 122 next unless action.present? || ref_keywords_any
Chris@117 123
Chris@117 124 refs.scan(/#(\d+)(\s+@#{TIMELOG_RE})?/).each do |m|
Chris@117 125 issue, hours = find_referenced_issue_by_id(m[0].to_i), m[2]
Chris@117 126 if issue
Chris@117 127 referenced_issues << issue
Chris@117 128 fix_issue(issue) if fix_keywords.include?(action.to_s.downcase)
Chris@117 129 log_time(issue, hours) if hours && Setting.commit_logtime_enabled?
Chris@0 130 end
Chris@0 131 end
Chris@0 132 end
Chris@0 133
Chris@0 134 referenced_issues.uniq!
Chris@0 135 self.issues = referenced_issues unless referenced_issues.empty?
Chris@0 136 end
Chris@0 137
Chris@0 138 def short_comments
Chris@0 139 @short_comments || split_comments.first
Chris@0 140 end
Chris@0 141
Chris@0 142 def long_comments
Chris@0 143 @long_comments || split_comments.last
Chris@0 144 end
Chris@117 145
Chris@117 146 def text_tag
Chris@117 147 if scmid?
Chris@117 148 "commit:#{scmid}"
Chris@117 149 else
Chris@117 150 "r#{revision}"
Chris@117 151 end
Chris@117 152 end
Chris@0 153
Chris@0 154 # Returns the previous changeset
Chris@0 155 def previous
Chris@0 156 @previous ||= Changeset.find(:first, :conditions => ['id < ? AND repository_id = ?', self.id, self.repository_id], :order => 'id DESC')
Chris@0 157 end
Chris@0 158
Chris@0 159 # Returns the next changeset
Chris@0 160 def next
Chris@0 161 @next ||= Changeset.find(:first, :conditions => ['id > ? AND repository_id = ?', self.id, self.repository_id], :order => 'id ASC')
Chris@0 162 end
Chris@0 163
Chris@0 164 # Strips and reencodes a commit log before insertion into the database
Chris@0 165 def self.normalize_comments(str)
Chris@0 166 to_utf8(str.to_s.strip)
Chris@0 167 end
Chris@0 168
Chris@0 169 # Creates a new Change from it's common parameters
Chris@0 170 def create_change(change)
Chris@0 171 Change.create(:changeset => self,
Chris@0 172 :action => change[:action],
Chris@0 173 :path => change[:path],
Chris@0 174 :from_path => change[:from_path],
Chris@0 175 :from_revision => change[:from_revision])
Chris@0 176 end
Chris@0 177
Chris@0 178 private
Chris@0 179
Chris@117 180 # Finds an issue that can be referenced by the commit message
Chris@117 181 # i.e. an issue that belong to the repository project, a subproject or a parent project
Chris@117 182 def find_referenced_issue_by_id(id)
Chris@117 183 return nil if id.blank?
Chris@117 184 issue = Issue.find_by_id(id.to_i, :include => :project)
Chris@117 185 if issue
Chris@117 186 unless project == issue.project || project.is_ancestor_of?(issue.project) || project.is_descendant_of?(issue.project)
Chris@117 187 issue = nil
Chris@117 188 end
Chris@117 189 end
Chris@117 190 issue
Chris@117 191 end
Chris@117 192
Chris@117 193 def fix_issue(issue)
Chris@117 194 status = IssueStatus.find_by_id(Setting.commit_fix_status_id.to_i)
Chris@117 195 if status.nil?
Chris@117 196 logger.warn("No status macthes commit_fix_status_id setting (#{Setting.commit_fix_status_id})") if logger
Chris@117 197 return issue
Chris@117 198 end
Chris@117 199
Chris@117 200 # the issue may have been updated by the closure of another one (eg. duplicate)
Chris@117 201 issue.reload
Chris@117 202 # don't change the status is the issue is closed
Chris@117 203 return if issue.status && issue.status.is_closed?
Chris@117 204
Chris@117 205 journal = issue.init_journal(user || User.anonymous, ll(Setting.default_language, :text_status_changed_by_changeset, text_tag))
Chris@117 206 issue.status = status
Chris@117 207 unless Setting.commit_fix_done_ratio.blank?
Chris@117 208 issue.done_ratio = Setting.commit_fix_done_ratio.to_i
Chris@117 209 end
Chris@117 210 Redmine::Hook.call_hook(:model_changeset_scan_commit_for_issue_ids_pre_issue_update,
Chris@117 211 { :changeset => self, :issue => issue })
Chris@117 212 unless issue.save
Chris@117 213 logger.warn("Issue ##{issue.id} could not be saved by changeset #{id}: #{issue.errors.full_messages}") if logger
Chris@117 214 end
Chris@117 215 issue
Chris@117 216 end
Chris@117 217
Chris@117 218 def log_time(issue, hours)
Chris@117 219 time_entry = TimeEntry.new(
Chris@117 220 :user => user,
Chris@117 221 :hours => hours,
Chris@117 222 :issue => issue,
Chris@117 223 :spent_on => commit_date,
Chris@117 224 :comments => l(:text_time_logged_by_changeset, :value => text_tag, :locale => Setting.default_language)
Chris@117 225 )
Chris@117 226 time_entry.activity = log_time_activity unless log_time_activity.nil?
Chris@117 227
Chris@117 228 unless time_entry.save
Chris@117 229 logger.warn("TimeEntry could not be created by changeset #{id}: #{time_entry.errors.full_messages}") if logger
Chris@117 230 end
Chris@117 231 time_entry
Chris@117 232 end
Chris@117 233
Chris@117 234 def log_time_activity
Chris@117 235 if Setting.commit_logtime_activity_id.to_i > 0
Chris@117 236 TimeEntryActivity.find_by_id(Setting.commit_logtime_activity_id.to_i)
Chris@117 237 end
Chris@0 238 end
Chris@0 239
Chris@0 240 def split_comments
Chris@0 241 comments =~ /\A(.+?)\r?\n(.*)$/m
Chris@0 242 @short_comments = $1 || comments
Chris@0 243 @long_comments = $2.to_s.strip
Chris@0 244 return @short_comments, @long_comments
Chris@0 245 end
Chris@0 246
Chris@0 247 def self.to_utf8(str)
Chris@0 248 return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
Chris@0 249 encoding = Setting.commit_logs_encoding.to_s.strip
Chris@0 250 unless encoding.blank? || encoding == 'UTF-8'
Chris@0 251 begin
Chris@0 252 str = Iconv.conv('UTF-8', encoding, str)
Chris@0 253 rescue Iconv::Failure
Chris@0 254 # do nothing here
Chris@0 255 end
Chris@0 256 end
Chris@0 257 # removes invalid UTF8 sequences
Chris@0 258 begin
Chris@0 259 Iconv.conv('UTF-8//IGNORE', 'UTF-8', str + ' ')[0..-3]
Chris@0 260 rescue Iconv::InvalidEncoding
Chris@0 261 # "UTF-8//IGNORE" is not supported on some OS
Chris@0 262 str
Chris@0 263 end
Chris@0 264 end
Chris@0 265 end