##// END OF EJS Templates
scm: Ruby 1.9 compatibility for log....
Toshi MARUYAMA -
r4805:9f50e6358371
parent child
Show More
@@ -1,271 +1,264
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+([.,]\d+)?)h?
101 101 |
102 102 (\d+):(\d+)
103 103 |
104 104 ((\d+)(h|hours?))?((\d+)(m|min)?)?
105 105 )
106 106 /x
107 107
108 108 def scan_comment_for_issue_ids
109 109 return if comments.blank?
110 110 # keywords used to reference issues
111 111 ref_keywords = Setting.commit_ref_keywords.downcase.split(",").collect(&:strip)
112 112 ref_keywords_any = ref_keywords.delete('*')
113 113 # keywords used to fix issues
114 114 fix_keywords = Setting.commit_fix_keywords.downcase.split(",").collect(&:strip)
115 115
116 116 kw_regexp = (ref_keywords + fix_keywords).collect{|kw| Regexp.escape(kw)}.join("|")
117 117
118 118 referenced_issues = []
119 119
120 120 comments.scan(/([\s\(\[,-]|^)((#{kw_regexp})[\s:]+)?(#\d+(\s+@#{TIMELOG_RE})?([\s,;&]+#\d+(\s+@#{TIMELOG_RE})?)*)(?=[[:punct:]]|\s|<|$)/i) do |match|
121 121 action, refs = match[2], match[3]
122 122 next unless action.present? || ref_keywords_any
123 123
124 124 refs.scan(/#(\d+)(\s+@#{TIMELOG_RE})?/).each do |m|
125 125 issue, hours = find_referenced_issue_by_id(m[0].to_i), m[2]
126 126 if issue
127 127 referenced_issues << issue
128 128 fix_issue(issue) if fix_keywords.include?(action.to_s.downcase)
129 129 log_time(issue, hours) if hours && Setting.commit_logtime_enabled?
130 130 end
131 131 end
132 132 end
133 133
134 134 referenced_issues.uniq!
135 135 self.issues = referenced_issues unless referenced_issues.empty?
136 136 end
137 137
138 138 def short_comments
139 139 @short_comments || split_comments.first
140 140 end
141 141
142 142 def long_comments
143 143 @long_comments || split_comments.last
144 144 end
145 145
146 146 def text_tag
147 147 if scmid?
148 148 "commit:#{scmid}"
149 149 else
150 150 "r#{revision}"
151 151 end
152 152 end
153 153
154 154 # Returns the previous changeset
155 155 def previous
156 156 @previous ||= Changeset.find(:first, :conditions => ['id < ? AND repository_id = ?', self.id, self.repository_id], :order => 'id DESC')
157 157 end
158 158
159 159 # Returns the next changeset
160 160 def next
161 161 @next ||= Changeset.find(:first, :conditions => ['id > ? AND repository_id = ?', self.id, self.repository_id], :order => 'id ASC')
162 162 end
163 163
164 164 # Strips and reencodes a commit log before insertion into the database
165 165 def self.normalize_comments(str)
166 166 to_utf8(str.to_s.strip)
167 167 end
168 168
169 169 # Creates a new Change from it's common parameters
170 170 def create_change(change)
171 171 Change.create(:changeset => self,
172 172 :action => change[:action],
173 173 :path => change[:path],
174 174 :from_path => change[:from_path],
175 175 :from_revision => change[:from_revision])
176 176 end
177 177
178 178 private
179 179
180 180 # Finds an issue that can be referenced by the commit message
181 181 # i.e. an issue that belong to the repository project, a subproject or a parent project
182 182 def find_referenced_issue_by_id(id)
183 183 return nil if id.blank?
184 184 issue = Issue.find_by_id(id.to_i, :include => :project)
185 185 if issue
186 186 unless project == issue.project || project.is_ancestor_of?(issue.project) || project.is_descendant_of?(issue.project)
187 187 issue = nil
188 188 end
189 189 end
190 190 issue
191 191 end
192 192
193 193 def fix_issue(issue)
194 194 status = IssueStatus.find_by_id(Setting.commit_fix_status_id.to_i)
195 195 if status.nil?
196 196 logger.warn("No status macthes commit_fix_status_id setting (#{Setting.commit_fix_status_id})") if logger
197 197 return issue
198 198 end
199 199
200 200 # the issue may have been updated by the closure of another one (eg. duplicate)
201 201 issue.reload
202 202 # don't change the status is the issue is closed
203 203 return if issue.status && issue.status.is_closed?
204 204
205 205 journal = issue.init_journal(user || User.anonymous, ll(Setting.default_language, :text_status_changed_by_changeset, text_tag))
206 206 issue.status = status
207 207 unless Setting.commit_fix_done_ratio.blank?
208 208 issue.done_ratio = Setting.commit_fix_done_ratio.to_i
209 209 end
210 210 Redmine::Hook.call_hook(:model_changeset_scan_commit_for_issue_ids_pre_issue_update,
211 211 { :changeset => self, :issue => issue })
212 212 unless issue.save
213 213 logger.warn("Issue ##{issue.id} could not be saved by changeset #{id}: #{issue.errors.full_messages}") if logger
214 214 end
215 215 issue
216 216 end
217 217
218 218 def log_time(issue, hours)
219 219 time_entry = TimeEntry.new(
220 220 :user => user,
221 221 :hours => hours,
222 222 :issue => issue,
223 223 :spent_on => commit_date,
224 224 :comments => l(:text_time_logged_by_changeset, :value => text_tag, :locale => Setting.default_language)
225 225 )
226 226 time_entry.activity = log_time_activity unless log_time_activity.nil?
227 227
228 228 unless time_entry.save
229 229 logger.warn("TimeEntry could not be created by changeset #{id}: #{time_entry.errors.full_messages}") if logger
230 230 end
231 231 time_entry
232 232 end
233 233
234 234 def log_time_activity
235 235 if Setting.commit_logtime_activity_id.to_i > 0
236 236 TimeEntryActivity.find_by_id(Setting.commit_logtime_activity_id.to_i)
237 237 end
238 238 end
239 239
240 240 def split_comments
241 241 comments =~ /\A(.+?)\r?\n(.*)$/m
242 242 @short_comments = $1 || comments
243 243 @long_comments = $2.to_s.strip
244 244 return @short_comments, @long_comments
245 245 end
246 246
247 247 def self.to_utf8(str)
248 if str.respond_to?(:force_encoding)
249 str.force_encoding('UTF-8')
250 return str if str.valid_encoding?
251 else
252 return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
253 end
254
255 248 encoding = Setting.commit_logs_encoding.to_s.strip
256 249 unless encoding.blank? || encoding == 'UTF-8'
257 250 begin
258 251 str = Iconv.conv('UTF-8', encoding, str)
259 252 rescue Iconv::Failure
260 253 # do nothing here
261 254 end
262 255 end
263 256 # removes invalid UTF8 sequences
264 257 begin
265 258 Iconv.conv('UTF-8//IGNORE', 'UTF-8', str + ' ')[0..-3]
266 259 rescue Iconv::InvalidEncoding
267 260 # "UTF-8//IGNORE" is not supported on some OS
268 261 str
269 262 end
270 263 end
271 264 end
@@ -1,228 +1,247
1 1 # encoding: utf-8
2 2 #
3 3 # Redmine - project management software
4 4 # Copyright (C) 2006-2010 Jean-Philippe Lang
5 5 #
6 6 # This program is free software; you can redistribute it and/or
7 7 # modify it under the terms of the GNU General Public License
8 8 # as published by the Free Software Foundation; either version 2
9 9 # of the License, or (at your option) any later version.
10 10 #
11 11 # This program is distributed in the hope that it will be useful,
12 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 14 # GNU General Public License for more details.
15 15 #
16 16 # You should have received a copy of the GNU General Public License
17 17 # along with this program; if not, write to the Free Software
18 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 19
20 20 require File.expand_path('../../test_helper', __FILE__)
21 21
22 22 class ChangesetTest < ActiveSupport::TestCase
23 23 fixtures :projects, :repositories, :issues, :issue_statuses, :changesets, :changes, :issue_categories, :enumerations, :custom_fields, :custom_values, :users, :members, :member_roles, :trackers
24 24
25 25 def setup
26 26 end
27 27
28 28 def test_ref_keywords_any
29 29 ActionMailer::Base.deliveries.clear
30 30 Setting.commit_fix_status_id = IssueStatus.find(:first, :conditions => ["is_closed = ?", true]).id
31 31 Setting.commit_fix_done_ratio = '90'
32 32 Setting.commit_ref_keywords = '*'
33 33 Setting.commit_fix_keywords = 'fixes , closes'
34 34
35 35 c = Changeset.new(:repository => Project.find(1).repository,
36 36 :committed_on => Time.now,
37 37 :comments => 'New commit (#2). Fixes #1')
38 38 c.scan_comment_for_issue_ids
39 39
40 40 assert_equal [1, 2], c.issue_ids.sort
41 41 fixed = Issue.find(1)
42 42 assert fixed.closed?
43 43 assert_equal 90, fixed.done_ratio
44 44 assert_equal 1, ActionMailer::Base.deliveries.size
45 45 end
46 46
47 47 def test_ref_keywords
48 48 Setting.commit_ref_keywords = 'refs'
49 49 Setting.commit_fix_keywords = ''
50 50
51 51 c = Changeset.new(:repository => Project.find(1).repository,
52 52 :committed_on => Time.now,
53 53 :comments => 'Ignores #2. Refs #1')
54 54 c.scan_comment_for_issue_ids
55 55
56 56 assert_equal [1], c.issue_ids.sort
57 57 end
58 58
59 59 def test_ref_keywords_any_only
60 60 Setting.commit_ref_keywords = '*'
61 61 Setting.commit_fix_keywords = ''
62 62
63 63 c = Changeset.new(:repository => Project.find(1).repository,
64 64 :committed_on => Time.now,
65 65 :comments => 'Ignores #2. Refs #1')
66 66 c.scan_comment_for_issue_ids
67 67
68 68 assert_equal [1, 2], c.issue_ids.sort
69 69 end
70 70
71 71 def test_ref_keywords_any_with_timelog
72 72 Setting.commit_ref_keywords = '*'
73 73 Setting.commit_logtime_enabled = '1'
74 74
75 75 c = Changeset.new(:repository => Project.find(1).repository,
76 76 :committed_on => 24.hours.ago,
77 77 :comments => 'Worked on this issue #1 @2h',
78 78 :revision => '520',
79 79 :user => User.find(2))
80 80 assert_difference 'TimeEntry.count' do
81 81 c.scan_comment_for_issue_ids
82 82 end
83 83 assert_equal [1], c.issue_ids.sort
84 84
85 85 time = TimeEntry.first(:order => 'id desc')
86 86 assert_equal 1, time.issue_id
87 87 assert_equal 1, time.project_id
88 88 assert_equal 2, time.user_id
89 89 assert_equal 2.0, time.hours
90 90 assert_equal Date.yesterday, time.spent_on
91 91 assert time.activity.is_default?
92 92 assert time.comments.include?('r520'), "r520 was expected in time_entry comments: #{time.comments}"
93 93 end
94 94
95 95 def test_ref_keywords_closing_with_timelog
96 96 Setting.commit_fix_status_id = IssueStatus.find(:first, :conditions => ["is_closed = ?", true]).id
97 97 Setting.commit_ref_keywords = '*'
98 98 Setting.commit_fix_keywords = 'fixes , closes'
99 99 Setting.commit_logtime_enabled = '1'
100 100
101 101 c = Changeset.new(:repository => Project.find(1).repository,
102 102 :committed_on => Time.now,
103 103 :comments => 'This is a comment. Fixes #1 @2.5, #2 @1',
104 104 :user => User.find(2))
105 105 assert_difference 'TimeEntry.count', 2 do
106 106 c.scan_comment_for_issue_ids
107 107 end
108 108
109 109 assert_equal [1, 2], c.issue_ids.sort
110 110 assert Issue.find(1).closed?
111 111 assert Issue.find(2).closed?
112 112
113 113 times = TimeEntry.all(:order => 'id desc', :limit => 2)
114 114 assert_equal [1, 2], times.collect(&:issue_id).sort
115 115 end
116 116
117 117 def test_ref_keywords_any_line_start
118 118 Setting.commit_ref_keywords = '*'
119 119
120 120 c = Changeset.new(:repository => Project.find(1).repository,
121 121 :committed_on => Time.now,
122 122 :comments => '#1 is the reason of this commit')
123 123 c.scan_comment_for_issue_ids
124 124
125 125 assert_equal [1], c.issue_ids.sort
126 126 end
127 127
128 128 def test_ref_keywords_allow_brackets_around_a_issue_number
129 129 Setting.commit_ref_keywords = '*'
130 130
131 131 c = Changeset.new(:repository => Project.find(1).repository,
132 132 :committed_on => Time.now,
133 133 :comments => '[#1] Worked on this issue')
134 134 c.scan_comment_for_issue_ids
135 135
136 136 assert_equal [1], c.issue_ids.sort
137 137 end
138 138
139 139 def test_ref_keywords_allow_brackets_around_multiple_issue_numbers
140 140 Setting.commit_ref_keywords = '*'
141 141
142 142 c = Changeset.new(:repository => Project.find(1).repository,
143 143 :committed_on => Time.now,
144 144 :comments => '[#1 #2, #3] Worked on these')
145 145 c.scan_comment_for_issue_ids
146 146
147 147 assert_equal [1,2,3], c.issue_ids.sort
148 148 end
149 149
150 150 def test_commit_referencing_a_subproject_issue
151 151 c = Changeset.new(:repository => Project.find(1).repository,
152 152 :committed_on => Time.now,
153 153 :comments => 'refs #5, a subproject issue')
154 154 c.scan_comment_for_issue_ids
155 155
156 156 assert_equal [5], c.issue_ids.sort
157 157 assert c.issues.first.project != c.project
158 158 end
159 159
160 160 def test_commit_referencing_a_parent_project_issue
161 161 # repository of child project
162 162 r = Repository::Subversion.create!(:project => Project.find(3), :url => 'svn://localhost/test')
163 163
164 164 c = Changeset.new(:repository => r,
165 165 :committed_on => Time.now,
166 166 :comments => 'refs #2, an issue of a parent project')
167 167 c.scan_comment_for_issue_ids
168 168
169 169 assert_equal [2], c.issue_ids.sort
170 170 assert c.issues.first.project != c.project
171 171 end
172 172
173 173 def test_text_tag_revision
174 174 c = Changeset.new(:revision => '520')
175 175 assert_equal 'r520', c.text_tag
176 176 end
177 177
178 178 def test_text_tag_hash
179 179 c = Changeset.new(:scmid => '7234cb2750b63f47bff735edc50a1c0a433c2518', :revision => '7234cb2750b63f47bff735edc50a1c0a433c2518')
180 180 assert_equal 'commit:7234cb2750b63f47bff735edc50a1c0a433c2518', c.text_tag
181 181 end
182 182
183 183 def test_text_tag_hash_all_number
184 184 c = Changeset.new(:scmid => '0123456789', :revision => '0123456789')
185 185 assert_equal 'commit:0123456789', c.text_tag
186 186 end
187 187
188 188 def test_previous
189 189 changeset = Changeset.find_by_revision('3')
190 190 assert_equal Changeset.find_by_revision('2'), changeset.previous
191 191 end
192 192
193 193 def test_previous_nil
194 194 changeset = Changeset.find_by_revision('1')
195 195 assert_nil changeset.previous
196 196 end
197 197
198 198 def test_next
199 199 changeset = Changeset.find_by_revision('2')
200 200 assert_equal Changeset.find_by_revision('3'), changeset.next
201 201 end
202 202
203 203 def test_next_nil
204 204 changeset = Changeset.find_by_revision('10')
205 205 assert_nil changeset.next
206 206 end
207 207
208 208 def test_comments_should_be_converted_to_utf8
209 209 with_settings :commit_logs_encoding => 'ISO-8859-1' do
210 210 c = Changeset.new
211 211 c.comments = File.read("#{RAILS_ROOT}/test/fixtures/encoding/iso-8859-1.txt")
212 212 assert_equal "Texte encodé en ISO-8859-1.", c.comments
213 213 end
214 214 end
215 215
216 216 def test_invalid_utf8_sequences_in_comments_should_be_stripped
217 217 with_settings :commit_logs_encoding => 'UTF-8' do
218 218 c = Changeset.new
219 219 c.comments = File.read("#{RAILS_ROOT}/test/fixtures/encoding/iso-8859-1.txt")
220 220 assert_equal "Texte encod en ISO-8859-1.", c.comments
221 221 end
222 222 end
223 223
224 def test_comments_should_be_converted_all_latin1_to_utf8
225 with_settings :commit_logs_encoding => 'ISO-8859-1' do
226 c = Changeset.new
227 s1 = "\xC2\x80"
228 s2 = "\xc3\x82\xc2\x80"
229 if s1.respond_to?(:force_encoding)
230 s3 = s1
231 s4 = s2
232 s1.force_encoding('ASCII-8BIT')
233 s2.force_encoding('ASCII-8BIT')
234 s3.force_encoding('ISO-8859-1')
235 s4.force_encoding('UTF-8')
236 assert_equal s3.encode('UTF-8'), s4
237 end
238 c.comments = s1
239 assert_equal s2, c.comments
240 end
241 end
242
224 243 def test_identifier
225 244 c = Changeset.find_by_revision('1')
226 245 assert_equal c.revision, c.identifier
227 246 end
228 247 end
General Comments 0
You need to be logged in to leave comments. Login now