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