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