annotate app/models/changeset.rb @ 711:60773bbfe050 feature_36_testing

This approach lead to a dead end.
author Luis Figueira <luis.figueira@eecs.qmul.ac.uk>
date Wed, 21 Sep 2011 15:52:05 +0100
parents 753f1380d6bc
children 5e80956cc792
rev   line source
Chris@0 1 # Redmine - project management software
Chris@441 2 # Copyright (C) 2006-2011 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@441 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@441 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@119 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@119 29 :url => Proc.new {|o| {:controller => 'repositories', :action => 'revision', :id => o.repository.project, :rev => o.identifier}}
Chris@441 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@441 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@441 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@441 43
Chris@0 44 named_scope :visible, lambda {|*args| { :include => {:repository => :project},
Chris@441 45 :conditions => Project.allowed_to_condition(args.shift || User.current, :view_changesets, *args) } }
Chris@441 46
Chris@0 47 def revision=(r)
Chris@0 48 write_attribute :revision, (r.nil? ? nil : r.to_s)
Chris@0 49 end
Chris@119 50
Chris@119 51 # Returns the identifier of this changeset; depending on repository backends
Chris@119 52 def identifier
Chris@119 53 if repository.class.respond_to? :changeset_identifier
Chris@119 54 repository.class.changeset_identifier self
Chris@119 55 else
Chris@119 56 revision.to_s
Chris@119 57 end
Chris@119 58 end
Chris@0 59
Chris@0 60 def committed_on=(date)
Chris@0 61 self.commit_date = date
Chris@0 62 super
Chris@0 63 end
Chris@119 64
Chris@119 65 # Returns the readable identifier
Chris@119 66 def format_identifier
Chris@119 67 if repository.class.respond_to? :format_changeset_identifier
Chris@119 68 repository.class.format_changeset_identifier self
Chris@119 69 else
Chris@119 70 identifier
Chris@119 71 end
Chris@119 72 end
Chris@441 73
Chris@0 74 def project
Chris@0 75 repository.project
Chris@0 76 end
Chris@441 77
Chris@0 78 def author
Chris@0 79 user || committer.to_s.split('<').first
Chris@0 80 end
Chris@441 81
Chris@0 82 def before_create
Chris@245 83 self.committer = self.class.to_utf8(self.committer, repository.repo_log_encoding)
Chris@441 84 self.comments = self.class.normalize_comments(
Chris@441 85 self.comments, repository.repo_log_encoding)
Chris@245 86 self.user = repository.find_committer_user(self.committer)
Chris@0 87 end
Chris@245 88
Chris@0 89 def after_create
Chris@0 90 scan_comment_for_issue_ids
Chris@0 91 end
Chris@441 92
Chris@119 93 TIMELOG_RE = /
Chris@119 94 (
Chris@245 95 ((\d+)(h|hours?))((\d+)(m|min)?)?
Chris@245 96 |
Chris@245 97 ((\d+)(h|hours?|m|min))
Chris@119 98 |
Chris@119 99 (\d+):(\d+)
Chris@119 100 |
Chris@245 101 (\d+([\.,]\d+)?)h?
Chris@119 102 )
Chris@119 103 /x
Chris@441 104
Chris@0 105 def scan_comment_for_issue_ids
Chris@0 106 return if comments.blank?
Chris@0 107 # keywords used to reference issues
Chris@0 108 ref_keywords = Setting.commit_ref_keywords.downcase.split(",").collect(&:strip)
Chris@119 109 ref_keywords_any = ref_keywords.delete('*')
Chris@0 110 # keywords used to fix issues
Chris@0 111 fix_keywords = Setting.commit_fix_keywords.downcase.split(",").collect(&:strip)
Chris@441 112
Chris@0 113 kw_regexp = (ref_keywords + fix_keywords).collect{|kw| Regexp.escape(kw)}.join("|")
Chris@441 114
Chris@0 115 referenced_issues = []
Chris@441 116
Chris@119 117 comments.scan(/([\s\(\[,-]|^)((#{kw_regexp})[\s:]+)?(#\d+(\s+@#{TIMELOG_RE})?([\s,;&]+#\d+(\s+@#{TIMELOG_RE})?)*)(?=[[:punct:]]|\s|<|$)/i) do |match|
Chris@119 118 action, refs = match[2], match[3]
Chris@119 119 next unless action.present? || ref_keywords_any
Chris@441 120
Chris@119 121 refs.scan(/#(\d+)(\s+@#{TIMELOG_RE})?/).each do |m|
Chris@119 122 issue, hours = find_referenced_issue_by_id(m[0].to_i), m[2]
Chris@119 123 if issue
Chris@119 124 referenced_issues << issue
Chris@119 125 fix_issue(issue) if fix_keywords.include?(action.to_s.downcase)
Chris@119 126 log_time(issue, hours) if hours && Setting.commit_logtime_enabled?
Chris@0 127 end
Chris@0 128 end
Chris@0 129 end
Chris@441 130
Chris@0 131 referenced_issues.uniq!
Chris@0 132 self.issues = referenced_issues unless referenced_issues.empty?
Chris@0 133 end
Chris@441 134
Chris@0 135 def short_comments
Chris@0 136 @short_comments || split_comments.first
Chris@0 137 end
Chris@441 138
Chris@0 139 def long_comments
Chris@0 140 @long_comments || split_comments.last
Chris@0 141 end
Chris@119 142
Chris@119 143 def text_tag
Chris@119 144 if scmid?
Chris@119 145 "commit:#{scmid}"
Chris@119 146 else
Chris@119 147 "r#{revision}"
Chris@119 148 end
Chris@119 149 end
Chris@441 150
Chris@0 151 # Returns the previous changeset
Chris@0 152 def previous
Chris@441 153 @previous ||= Changeset.find(:first,
Chris@441 154 :conditions => ['id < ? AND repository_id = ?',
Chris@441 155 self.id, self.repository_id],
Chris@441 156 :order => 'id DESC')
Chris@0 157 end
Chris@0 158
Chris@0 159 # Returns the next changeset
Chris@0 160 def next
Chris@441 161 @next ||= Changeset.find(:first,
Chris@441 162 :conditions => ['id > ? AND repository_id = ?',
Chris@441 163 self.id, self.repository_id],
Chris@441 164 :order => 'id ASC')
Chris@0 165 end
Chris@441 166
Chris@0 167 # Creates a new Change from it's common parameters
Chris@0 168 def create_change(change)
Chris@441 169 Change.create(:changeset => self,
Chris@441 170 :action => change[:action],
Chris@441 171 :path => change[:path],
Chris@441 172 :from_path => change[:from_path],
Chris@0 173 :from_revision => change[:from_revision])
Chris@0 174 end
Chris@245 175
Chris@0 176 private
Chris@0 177
Chris@119 178 # Finds an issue that can be referenced by the commit message
Chris@119 179 # i.e. an issue that belong to the repository project, a subproject or a parent project
Chris@119 180 def find_referenced_issue_by_id(id)
Chris@119 181 return nil if id.blank?
Chris@119 182 issue = Issue.find_by_id(id.to_i, :include => :project)
Chris@119 183 if issue
Chris@441 184 unless issue.project &&
Chris@441 185 (project == issue.project || project.is_ancestor_of?(issue.project) ||
Chris@441 186 project.is_descendant_of?(issue.project))
Chris@119 187 issue = nil
Chris@119 188 end
Chris@119 189 end
Chris@119 190 issue
Chris@119 191 end
Chris@441 192
Chris@119 193 def fix_issue(issue)
Chris@119 194 status = IssueStatus.find_by_id(Setting.commit_fix_status_id.to_i)
Chris@119 195 if status.nil?
Chris@119 196 logger.warn("No status macthes commit_fix_status_id setting (#{Setting.commit_fix_status_id})") if logger
Chris@119 197 return issue
Chris@119 198 end
Chris@441 199
Chris@119 200 # the issue may have been updated by the closure of another one (eg. duplicate)
Chris@119 201 issue.reload
Chris@119 202 # don't change the status is the issue is closed
Chris@119 203 return if issue.status && issue.status.is_closed?
Chris@441 204
Chris@119 205 journal = issue.init_journal(user || User.anonymous, ll(Setting.default_language, :text_status_changed_by_changeset, text_tag))
Chris@119 206 issue.status = status
Chris@119 207 unless Setting.commit_fix_done_ratio.blank?
Chris@119 208 issue.done_ratio = Setting.commit_fix_done_ratio.to_i
Chris@119 209 end
Chris@119 210 Redmine::Hook.call_hook(:model_changeset_scan_commit_for_issue_ids_pre_issue_update,
Chris@119 211 { :changeset => self, :issue => issue })
Chris@119 212 unless issue.save
Chris@119 213 logger.warn("Issue ##{issue.id} could not be saved by changeset #{id}: #{issue.errors.full_messages}") if logger
Chris@119 214 end
Chris@119 215 issue
Chris@119 216 end
Chris@441 217
Chris@119 218 def log_time(issue, hours)
Chris@119 219 time_entry = TimeEntry.new(
Chris@119 220 :user => user,
Chris@119 221 :hours => hours,
Chris@119 222 :issue => issue,
Chris@119 223 :spent_on => commit_date,
Chris@441 224 :comments => l(:text_time_logged_by_changeset, :value => text_tag,
Chris@441 225 :locale => Setting.default_language)
Chris@119 226 )
Chris@119 227 time_entry.activity = log_time_activity unless log_time_activity.nil?
Chris@441 228
Chris@119 229 unless time_entry.save
Chris@119 230 logger.warn("TimeEntry could not be created by changeset #{id}: #{time_entry.errors.full_messages}") if logger
Chris@119 231 end
Chris@119 232 time_entry
Chris@119 233 end
Chris@441 234
Chris@119 235 def log_time_activity
Chris@119 236 if Setting.commit_logtime_activity_id.to_i > 0
Chris@119 237 TimeEntryActivity.find_by_id(Setting.commit_logtime_activity_id.to_i)
Chris@119 238 end
Chris@0 239 end
Chris@441 240
Chris@0 241 def split_comments
Chris@0 242 comments =~ /\A(.+?)\r?\n(.*)$/m
Chris@0 243 @short_comments = $1 || comments
Chris@0 244 @long_comments = $2.to_s.strip
Chris@0 245 return @short_comments, @long_comments
Chris@0 246 end
Chris@0 247
Chris@245 248 public
Chris@119 249
Chris@245 250 # Strips and reencodes a commit log before insertion into the database
Chris@245 251 def self.normalize_comments(str, encoding)
Chris@245 252 Changeset.to_utf8(str.to_s.strip, encoding)
Chris@245 253 end
Chris@245 254
Chris@245 255 def self.to_utf8(str, encoding)
Chris@441 256 return str if str.nil?
Chris@441 257 str.force_encoding("ASCII-8BIT") if str.respond_to?(:force_encoding)
Chris@441 258 if str.empty?
Chris@441 259 str.force_encoding("UTF-8") if str.respond_to?(:force_encoding)
Chris@441 260 return str
Chris@0 261 end
Chris@441 262 enc = encoding.blank? ? "UTF-8" : encoding
Chris@245 263 if str.respond_to?(:force_encoding)
Chris@441 264 if enc.upcase != "UTF-8"
Chris@441 265 str.force_encoding(enc)
Chris@441 266 str = str.encode("UTF-8", :invalid => :replace,
Chris@441 267 :undef => :replace, :replace => '?')
Chris@441 268 else
Chris@441 269 str.force_encoding("UTF-8")
Chris@441 270 if ! str.valid_encoding?
Chris@441 271 str = str.encode("US-ASCII", :invalid => :replace,
Chris@441 272 :undef => :replace, :replace => '?').encode("UTF-8")
Chris@441 273 end
Chris@245 274 end
Chris@245 275 else
Chris@441 276 ic = Iconv.new('UTF-8', enc)
Chris@441 277 txtar = ""
Chris@245 278 begin
Chris@441 279 txtar += ic.iconv(str)
Chris@441 280 rescue Iconv::IllegalSequence
Chris@441 281 txtar += $!.success
Chris@441 282 str = '?' + $!.failed[1,$!.failed.length]
Chris@441 283 retry
Chris@441 284 rescue
Chris@441 285 txtar += $!.success
Chris@245 286 end
Chris@441 287 str = txtar
Chris@0 288 end
Chris@245 289 str
Chris@0 290 end
Chris@0 291 end