##// END OF EJS Templates
scm: return if str.blank? in to_utf8(str)....
Toshi MARUYAMA -
r4840:f03e338880e9
parent child
Show More
@@ -1,228 +1,229
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006 Jean-Philippe Lang
2 # Copyright (C) 2006 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 module RepositoriesHelper
20 module RepositoriesHelper
21 def format_revision(revision)
21 def format_revision(revision)
22 if revision.respond_to? :format_identifier
22 if revision.respond_to? :format_identifier
23 revision.format_identifier
23 revision.format_identifier
24 else
24 else
25 revision.to_s
25 revision.to_s
26 end
26 end
27 end
27 end
28
28
29 def truncate_at_line_break(text, length = 255)
29 def truncate_at_line_break(text, length = 255)
30 if text
30 if text
31 text.gsub(%r{^(.{#{length}}[^\n]*)\n.+$}m, '\\1...')
31 text.gsub(%r{^(.{#{length}}[^\n]*)\n.+$}m, '\\1...')
32 end
32 end
33 end
33 end
34
34
35 def render_properties(properties)
35 def render_properties(properties)
36 unless properties.nil? || properties.empty?
36 unless properties.nil? || properties.empty?
37 content = ''
37 content = ''
38 properties.keys.sort.each do |property|
38 properties.keys.sort.each do |property|
39 content << content_tag('li', "<b>#{h property}</b>: <span>#{h properties[property]}</span>")
39 content << content_tag('li', "<b>#{h property}</b>: <span>#{h properties[property]}</span>")
40 end
40 end
41 content_tag('ul', content, :class => 'properties')
41 content_tag('ul', content, :class => 'properties')
42 end
42 end
43 end
43 end
44
44
45 def render_changeset_changes
45 def render_changeset_changes
46 changes = @changeset.changes.find(:all, :limit => 1000, :order => 'path').collect do |change|
46 changes = @changeset.changes.find(:all, :limit => 1000, :order => 'path').collect do |change|
47 case change.action
47 case change.action
48 when 'A'
48 when 'A'
49 # Detects moved/copied files
49 # Detects moved/copied files
50 if !change.from_path.blank?
50 if !change.from_path.blank?
51 change.action = @changeset.changes.detect {|c| c.action == 'D' && c.path == change.from_path} ? 'R' : 'C'
51 change.action = @changeset.changes.detect {|c| c.action == 'D' && c.path == change.from_path} ? 'R' : 'C'
52 end
52 end
53 change
53 change
54 when 'D'
54 when 'D'
55 @changeset.changes.detect {|c| c.from_path == change.path} ? nil : change
55 @changeset.changes.detect {|c| c.from_path == change.path} ? nil : change
56 else
56 else
57 change
57 change
58 end
58 end
59 end.compact
59 end.compact
60
60
61 tree = { }
61 tree = { }
62 changes.each do |change|
62 changes.each do |change|
63 p = tree
63 p = tree
64 dirs = change.path.to_s.split('/').select {|d| !d.blank?}
64 dirs = change.path.to_s.split('/').select {|d| !d.blank?}
65 path = ''
65 path = ''
66 dirs.each do |dir|
66 dirs.each do |dir|
67 path += '/' + dir
67 path += '/' + dir
68 p[:s] ||= {}
68 p[:s] ||= {}
69 p = p[:s]
69 p = p[:s]
70 p[path] ||= {}
70 p[path] ||= {}
71 p = p[path]
71 p = p[path]
72 end
72 end
73 p[:c] = change
73 p[:c] = change
74 end
74 end
75
75
76 render_changes_tree(tree[:s])
76 render_changes_tree(tree[:s])
77 end
77 end
78
78
79 def render_changes_tree(tree)
79 def render_changes_tree(tree)
80 return '' if tree.nil?
80 return '' if tree.nil?
81
81
82 output = ''
82 output = ''
83 output << '<ul>'
83 output << '<ul>'
84 tree.keys.sort.each do |file|
84 tree.keys.sort.each do |file|
85 style = 'change'
85 style = 'change'
86 text = File.basename(h(file))
86 text = File.basename(h(file))
87 if s = tree[file][:s]
87 if s = tree[file][:s]
88 style << ' folder'
88 style << ' folder'
89 path_param = to_path_param(@repository.relative_path(file))
89 path_param = to_path_param(@repository.relative_path(file))
90 text = link_to(text, :controller => 'repositories',
90 text = link_to(text, :controller => 'repositories',
91 :action => 'show',
91 :action => 'show',
92 :id => @project,
92 :id => @project,
93 :path => path_param,
93 :path => path_param,
94 :rev => @changeset.identifier)
94 :rev => @changeset.identifier)
95 output << "<li class='#{style}'>#{text}</li>"
95 output << "<li class='#{style}'>#{text}</li>"
96 output << render_changes_tree(s)
96 output << render_changes_tree(s)
97 elsif c = tree[file][:c]
97 elsif c = tree[file][:c]
98 style << " change-#{c.action}"
98 style << " change-#{c.action}"
99 path_param = to_path_param(@repository.relative_path(c.path))
99 path_param = to_path_param(@repository.relative_path(c.path))
100 text = link_to(text, :controller => 'repositories',
100 text = link_to(text, :controller => 'repositories',
101 :action => 'entry',
101 :action => 'entry',
102 :id => @project,
102 :id => @project,
103 :path => path_param,
103 :path => path_param,
104 :rev => @changeset.identifier) unless c.action == 'D'
104 :rev => @changeset.identifier) unless c.action == 'D'
105 text << " - #{c.revision}" unless c.revision.blank?
105 text << " - #{c.revision}" unless c.revision.blank?
106 text << ' (' + link_to('diff', :controller => 'repositories',
106 text << ' (' + link_to('diff', :controller => 'repositories',
107 :action => 'diff',
107 :action => 'diff',
108 :id => @project,
108 :id => @project,
109 :path => path_param,
109 :path => path_param,
110 :rev => @changeset.identifier) + ') ' if c.action == 'M'
110 :rev => @changeset.identifier) + ') ' if c.action == 'M'
111 text << ' ' + content_tag('span', c.from_path, :class => 'copied-from') unless c.from_path.blank?
111 text << ' ' + content_tag('span', c.from_path, :class => 'copied-from') unless c.from_path.blank?
112 output << "<li class='#{style}'>#{text}</li>"
112 output << "<li class='#{style}'>#{text}</li>"
113 end
113 end
114 end
114 end
115 output << '</ul>'
115 output << '</ul>'
116 output
116 output
117 end
117 end
118
118
119 def to_utf8(str)
119 def to_utf8(str)
120 return str if str.blank?
120 if str.respond_to?(:force_encoding)
121 if str.respond_to?(:force_encoding)
121 str.force_encoding('UTF-8')
122 str.force_encoding('UTF-8')
122 else
123 else
123 # TODO:
124 # TODO:
124 # Japanese Shift_JIS(CP932) is not compatible with ASCII.
125 # Japanese Shift_JIS(CP932) is not compatible with ASCII.
125 # UTF-7 and Japanese ISO-2022-JP are 7bits clean.
126 # UTF-7 and Japanese ISO-2022-JP are 7bits clean.
126 return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
127 return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
127 end
128 end
128
129
129 @encodings ||= Setting.repositories_encodings.split(',').collect(&:strip)
130 @encodings ||= Setting.repositories_encodings.split(',').collect(&:strip)
130 @encodings.each do |encoding|
131 @encodings.each do |encoding|
131 begin
132 begin
132 return Iconv.conv('UTF-8', encoding, str)
133 return Iconv.conv('UTF-8', encoding, str)
133 rescue Iconv::Failure
134 rescue Iconv::Failure
134 # do nothing here and try the next encoding
135 # do nothing here and try the next encoding
135 end
136 end
136 end
137 end
137 str = replace_invalid_utf8(str)
138 str = replace_invalid_utf8(str)
138 end
139 end
139
140
140 def replace_invalid_utf8(str)
141 def replace_invalid_utf8(str)
141 if str.respond_to?(:force_encoding)
142 if str.respond_to?(:force_encoding)
142 str.force_encoding('UTF-8')
143 str.force_encoding('UTF-8')
143 if ! str.valid_encoding?
144 if ! str.valid_encoding?
144 str = str.encode("US-ASCII", :invalid => :replace,
145 str = str.encode("US-ASCII", :invalid => :replace,
145 :undef => :replace, :replace => '?').encode("UTF-8")
146 :undef => :replace, :replace => '?').encode("UTF-8")
146 end
147 end
147 end
148 end
148 str
149 str
149 end
150 end
150
151
151 def repository_field_tags(form, repository)
152 def repository_field_tags(form, repository)
152 method = repository.class.name.demodulize.underscore + "_field_tags"
153 method = repository.class.name.demodulize.underscore + "_field_tags"
153 send(method, form, repository) if repository.is_a?(Repository) && respond_to?(method) && method != 'repository_field_tags'
154 send(method, form, repository) if repository.is_a?(Repository) && respond_to?(method) && method != 'repository_field_tags'
154 end
155 end
155
156
156 def scm_select_tag(repository)
157 def scm_select_tag(repository)
157 scm_options = [["--- #{l(:actionview_instancetag_blank_option)} ---", '']]
158 scm_options = [["--- #{l(:actionview_instancetag_blank_option)} ---", '']]
158 Redmine::Scm::Base.all.each do |scm|
159 Redmine::Scm::Base.all.each do |scm|
159 scm_options << ["Repository::#{scm}".constantize.scm_name, scm] if Setting.enabled_scm.include?(scm) || (repository && repository.class.name.demodulize == scm)
160 scm_options << ["Repository::#{scm}".constantize.scm_name, scm] if Setting.enabled_scm.include?(scm) || (repository && repository.class.name.demodulize == scm)
160 end
161 end
161
162
162 select_tag('repository_scm',
163 select_tag('repository_scm',
163 options_for_select(scm_options, repository.class.name.demodulize),
164 options_for_select(scm_options, repository.class.name.demodulize),
164 :disabled => (repository && !repository.new_record?),
165 :disabled => (repository && !repository.new_record?),
165 :onchange => remote_function(:url => { :controller => 'repositories', :action => 'edit', :id => @project }, :method => :get, :with => "Form.serialize(this.form)")
166 :onchange => remote_function(:url => { :controller => 'repositories', :action => 'edit', :id => @project }, :method => :get, :with => "Form.serialize(this.form)")
166 )
167 )
167 end
168 end
168
169
169 def with_leading_slash(path)
170 def with_leading_slash(path)
170 path.to_s.starts_with?('/') ? path : "/#{path}"
171 path.to_s.starts_with?('/') ? path : "/#{path}"
171 end
172 end
172
173
173 def without_leading_slash(path)
174 def without_leading_slash(path)
174 path.gsub(%r{^/+}, '')
175 path.gsub(%r{^/+}, '')
175 end
176 end
176
177
177 def subversion_field_tags(form, repository)
178 def subversion_field_tags(form, repository)
178 content_tag('p', form.text_field(:url, :size => 60, :required => true,
179 content_tag('p', form.text_field(:url, :size => 60, :required => true,
179 :disabled => (repository && !repository.root_url.blank?)) +
180 :disabled => (repository && !repository.root_url.blank?)) +
180 '<br />(file:///, http://, https://, svn://, svn+[tunnelscheme]://)') +
181 '<br />(file:///, http://, https://, svn://, svn+[tunnelscheme]://)') +
181 content_tag('p', form.text_field(:login, :size => 30)) +
182 content_tag('p', form.text_field(:login, :size => 30)) +
182 content_tag('p', form.password_field(:password, :size => 30, :name => 'ignore',
183 content_tag('p', form.password_field(:password, :size => 30, :name => 'ignore',
183 :value => ((repository.new_record? || repository.password.blank?) ? '' : ('x'*15)),
184 :value => ((repository.new_record? || repository.password.blank?) ? '' : ('x'*15)),
184 :onfocus => "this.value=''; this.name='repository[password]';",
185 :onfocus => "this.value=''; this.name='repository[password]';",
185 :onchange => "this.name='repository[password]';"))
186 :onchange => "this.name='repository[password]';"))
186 end
187 end
187
188
188 def darcs_field_tags(form, repository)
189 def darcs_field_tags(form, repository)
189 content_tag('p', form.text_field(:url, :label => 'Root directory',
190 content_tag('p', form.text_field(:url, :label => 'Root directory',
190 :size => 60, :required => true,
191 :size => 60, :required => true,
191 :disabled => (repository && !repository.new_record?)))
192 :disabled => (repository && !repository.new_record?)))
192 end
193 end
193
194
194 def mercurial_field_tags(form, repository)
195 def mercurial_field_tags(form, repository)
195 content_tag('p', form.text_field(:url, :label => 'Root directory',
196 content_tag('p', form.text_field(:url, :label => 'Root directory',
196 :size => 60, :required => true,
197 :size => 60, :required => true,
197 :disabled => (repository && !repository.root_url.blank?)))
198 :disabled => (repository && !repository.root_url.blank?)))
198 end
199 end
199
200
200 def git_field_tags(form, repository)
201 def git_field_tags(form, repository)
201 content_tag('p', form.text_field(:url, :label => 'Path to repository',
202 content_tag('p', form.text_field(:url, :label => 'Path to repository',
202 :size => 60, :required => true,
203 :size => 60, :required => true,
203 :disabled => (repository && !repository.root_url.blank?)))
204 :disabled => (repository && !repository.root_url.blank?)))
204 end
205 end
205
206
206 def cvs_field_tags(form, repository)
207 def cvs_field_tags(form, repository)
207 content_tag('p', form.text_field(:root_url,
208 content_tag('p', form.text_field(:root_url,
208 :label => 'CVSROOT', :size => 60, :required => true,
209 :label => 'CVSROOT', :size => 60, :required => true,
209 :disabled => !repository.new_record?)) +
210 :disabled => !repository.new_record?)) +
210 content_tag('p', form.text_field(:url, :label => 'Module',
211 content_tag('p', form.text_field(:url, :label => 'Module',
211 :size => 30, :required => true,
212 :size => 30, :required => true,
212 :disabled => !repository.new_record?))
213 :disabled => !repository.new_record?))
213 end
214 end
214
215
215 def bazaar_field_tags(form, repository)
216 def bazaar_field_tags(form, repository)
216 content_tag('p', form.text_field(:url, :label => 'Root directory',
217 content_tag('p', form.text_field(:url, :label => 'Root directory',
217 :size => 60, :required => true,
218 :size => 60, :required => true,
218 :disabled => (repository && !repository.new_record?)))
219 :disabled => (repository && !repository.new_record?)))
219 end
220 end
220
221
221 def filesystem_field_tags(form, repository)
222 def filesystem_field_tags(form, repository)
222 content_tag('p', form.text_field(:url, :label => 'Root directory',
223 content_tag('p', form.text_field(:url, :label => 'Root directory',
223 :size => 60, :required => true,
224 :size => 60, :required => true,
224 :disabled => (repository && !repository.root_url.blank?))) +
225 :disabled => (repository && !repository.root_url.blank?))) +
225 content_tag('p', form.select(:path_encoding, [nil] + Setting::ENCODINGS,
226 content_tag('p', form.select(:path_encoding, [nil] + Setting::ENCODINGS,
226 :label => 'Path encoding'))
227 :label => 'Path encoding'))
227 end
228 end
228 end
229 end
@@ -1,274 +1,275
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.format_identifier}" + (o.short_comments.blank? ? '' : (': ' + o.short_comments))},
26 acts_as_event :title => Proc.new {|o| "#{l(:label_revision)} #{o.format_identifier}" + (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.identifier}}
29 :url => Proc.new {|o| {:controller => 'repositories', :action => 'revision', :id => o.repository.project, :rev => o.identifier}}
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 # Returns the identifier of this changeset; depending on repository backends
51 # Returns the identifier of this changeset; depending on repository backends
52 def identifier
52 def identifier
53 if repository.class.respond_to? :changeset_identifier
53 if repository.class.respond_to? :changeset_identifier
54 repository.class.changeset_identifier self
54 repository.class.changeset_identifier self
55 else
55 else
56 revision.to_s
56 revision.to_s
57 end
57 end
58 end
58 end
59
59
60 def comments=(comment)
60 def comments=(comment)
61 write_attribute(:comments, Changeset.normalize_comments(comment))
61 write_attribute(:comments, Changeset.normalize_comments(comment))
62 end
62 end
63
63
64 def committed_on=(date)
64 def committed_on=(date)
65 self.commit_date = date
65 self.commit_date = date
66 super
66 super
67 end
67 end
68
68
69 # Returns the readable identifier
69 # Returns the readable identifier
70 def format_identifier
70 def format_identifier
71 if repository.class.respond_to? :format_changeset_identifier
71 if repository.class.respond_to? :format_changeset_identifier
72 repository.class.format_changeset_identifier self
72 repository.class.format_changeset_identifier self
73 else
73 else
74 identifier
74 identifier
75 end
75 end
76 end
76 end
77
77
78 def committer=(arg)
78 def committer=(arg)
79 write_attribute(:committer, self.class.to_utf8(arg.to_s))
79 write_attribute(:committer, self.class.to_utf8(arg.to_s))
80 end
80 end
81
81
82 def project
82 def project
83 repository.project
83 repository.project
84 end
84 end
85
85
86 def author
86 def author
87 user || committer.to_s.split('<').first
87 user || committer.to_s.split('<').first
88 end
88 end
89
89
90 def before_create
90 def before_create
91 self.user = repository.find_committer_user(committer)
91 self.user = repository.find_committer_user(committer)
92 end
92 end
93
93
94 def after_create
94 def after_create
95 scan_comment_for_issue_ids
95 scan_comment_for_issue_ids
96 end
96 end
97
97
98 TIMELOG_RE = /
98 TIMELOG_RE = /
99 (
99 (
100 ((\d+)(h|hours?))((\d+)(m|min)?)?
100 ((\d+)(h|hours?))((\d+)(m|min)?)?
101 |
101 |
102 ((\d+)(h|hours?|m|min))
102 ((\d+)(h|hours?|m|min))
103 |
103 |
104 (\d+):(\d+)
104 (\d+):(\d+)
105 |
105 |
106 (\d+([\.,]\d+)?)h?
106 (\d+([\.,]\d+)?)h?
107 )
107 )
108 /x
108 /x
109
109
110 def scan_comment_for_issue_ids
110 def scan_comment_for_issue_ids
111 return if comments.blank?
111 return if comments.blank?
112 # keywords used to reference issues
112 # keywords used to reference issues
113 ref_keywords = Setting.commit_ref_keywords.downcase.split(",").collect(&:strip)
113 ref_keywords = Setting.commit_ref_keywords.downcase.split(",").collect(&:strip)
114 ref_keywords_any = ref_keywords.delete('*')
114 ref_keywords_any = ref_keywords.delete('*')
115 # keywords used to fix issues
115 # keywords used to fix issues
116 fix_keywords = Setting.commit_fix_keywords.downcase.split(",").collect(&:strip)
116 fix_keywords = Setting.commit_fix_keywords.downcase.split(",").collect(&:strip)
117
117
118 kw_regexp = (ref_keywords + fix_keywords).collect{|kw| Regexp.escape(kw)}.join("|")
118 kw_regexp = (ref_keywords + fix_keywords).collect{|kw| Regexp.escape(kw)}.join("|")
119
119
120 referenced_issues = []
120 referenced_issues = []
121
121
122 comments.scan(/([\s\(\[,-]|^)((#{kw_regexp})[\s:]+)?(#\d+(\s+@#{TIMELOG_RE})?([\s,;&]+#\d+(\s+@#{TIMELOG_RE})?)*)(?=[[:punct:]]|\s|<|$)/i) do |match|
122 comments.scan(/([\s\(\[,-]|^)((#{kw_regexp})[\s:]+)?(#\d+(\s+@#{TIMELOG_RE})?([\s,;&]+#\d+(\s+@#{TIMELOG_RE})?)*)(?=[[:punct:]]|\s|<|$)/i) do |match|
123 action, refs = match[2], match[3]
123 action, refs = match[2], match[3]
124 next unless action.present? || ref_keywords_any
124 next unless action.present? || ref_keywords_any
125
125
126 refs.scan(/#(\d+)(\s+@#{TIMELOG_RE})?/).each do |m|
126 refs.scan(/#(\d+)(\s+@#{TIMELOG_RE})?/).each do |m|
127 issue, hours = find_referenced_issue_by_id(m[0].to_i), m[2]
127 issue, hours = find_referenced_issue_by_id(m[0].to_i), m[2]
128 if issue
128 if issue
129 referenced_issues << issue
129 referenced_issues << issue
130 fix_issue(issue) if fix_keywords.include?(action.to_s.downcase)
130 fix_issue(issue) if fix_keywords.include?(action.to_s.downcase)
131 log_time(issue, hours) if hours && Setting.commit_logtime_enabled?
131 log_time(issue, hours) if hours && Setting.commit_logtime_enabled?
132 end
132 end
133 end
133 end
134 end
134 end
135
135
136 referenced_issues.uniq!
136 referenced_issues.uniq!
137 self.issues = referenced_issues unless referenced_issues.empty?
137 self.issues = referenced_issues unless referenced_issues.empty?
138 end
138 end
139
139
140 def short_comments
140 def short_comments
141 @short_comments || split_comments.first
141 @short_comments || split_comments.first
142 end
142 end
143
143
144 def long_comments
144 def long_comments
145 @long_comments || split_comments.last
145 @long_comments || split_comments.last
146 end
146 end
147
147
148 def text_tag
148 def text_tag
149 if scmid?
149 if scmid?
150 "commit:#{scmid}"
150 "commit:#{scmid}"
151 else
151 else
152 "r#{revision}"
152 "r#{revision}"
153 end
153 end
154 end
154 end
155
155
156 # Returns the previous changeset
156 # Returns the previous changeset
157 def previous
157 def previous
158 @previous ||= Changeset.find(:first, :conditions => ['id < ? AND repository_id = ?', self.id, self.repository_id], :order => 'id DESC')
158 @previous ||= Changeset.find(:first, :conditions => ['id < ? AND repository_id = ?', self.id, self.repository_id], :order => 'id DESC')
159 end
159 end
160
160
161 # Returns the next changeset
161 # Returns the next changeset
162 def next
162 def next
163 @next ||= Changeset.find(:first, :conditions => ['id > ? AND repository_id = ?', self.id, self.repository_id], :order => 'id ASC')
163 @next ||= Changeset.find(:first, :conditions => ['id > ? AND repository_id = ?', self.id, self.repository_id], :order => 'id ASC')
164 end
164 end
165
165
166 # Strips and reencodes a commit log before insertion into the database
166 # Strips and reencodes a commit log before insertion into the database
167 def self.normalize_comments(str)
167 def self.normalize_comments(str)
168 to_utf8(str.to_s.strip)
168 to_utf8(str.to_s.strip)
169 end
169 end
170
170
171 # Creates a new Change from it's common parameters
171 # Creates a new Change from it's common parameters
172 def create_change(change)
172 def create_change(change)
173 Change.create(:changeset => self,
173 Change.create(:changeset => self,
174 :action => change[:action],
174 :action => change[:action],
175 :path => change[:path],
175 :path => change[:path],
176 :from_path => change[:from_path],
176 :from_path => change[:from_path],
177 :from_revision => change[:from_revision])
177 :from_revision => change[:from_revision])
178 end
178 end
179
179
180 private
180 private
181
181
182 # Finds an issue that can be referenced by the commit message
182 # Finds an issue that can be referenced by the commit message
183 # i.e. an issue that belong to the repository project, a subproject or a parent project
183 # i.e. an issue that belong to the repository project, a subproject or a parent project
184 def find_referenced_issue_by_id(id)
184 def find_referenced_issue_by_id(id)
185 return nil if id.blank?
185 return nil if id.blank?
186 issue = Issue.find_by_id(id.to_i, :include => :project)
186 issue = Issue.find_by_id(id.to_i, :include => :project)
187 if issue
187 if issue
188 unless project == issue.project || project.is_ancestor_of?(issue.project) || project.is_descendant_of?(issue.project)
188 unless project == issue.project || project.is_ancestor_of?(issue.project) || project.is_descendant_of?(issue.project)
189 issue = nil
189 issue = nil
190 end
190 end
191 end
191 end
192 issue
192 issue
193 end
193 end
194
194
195 def fix_issue(issue)
195 def fix_issue(issue)
196 status = IssueStatus.find_by_id(Setting.commit_fix_status_id.to_i)
196 status = IssueStatus.find_by_id(Setting.commit_fix_status_id.to_i)
197 if status.nil?
197 if status.nil?
198 logger.warn("No status macthes commit_fix_status_id setting (#{Setting.commit_fix_status_id})") if logger
198 logger.warn("No status macthes commit_fix_status_id setting (#{Setting.commit_fix_status_id})") if logger
199 return issue
199 return issue
200 end
200 end
201
201
202 # the issue may have been updated by the closure of another one (eg. duplicate)
202 # the issue may have been updated by the closure of another one (eg. duplicate)
203 issue.reload
203 issue.reload
204 # don't change the status is the issue is closed
204 # don't change the status is the issue is closed
205 return if issue.status && issue.status.is_closed?
205 return if issue.status && issue.status.is_closed?
206
206
207 journal = issue.init_journal(user || User.anonymous, ll(Setting.default_language, :text_status_changed_by_changeset, text_tag))
207 journal = issue.init_journal(user || User.anonymous, ll(Setting.default_language, :text_status_changed_by_changeset, text_tag))
208 issue.status = status
208 issue.status = status
209 unless Setting.commit_fix_done_ratio.blank?
209 unless Setting.commit_fix_done_ratio.blank?
210 issue.done_ratio = Setting.commit_fix_done_ratio.to_i
210 issue.done_ratio = Setting.commit_fix_done_ratio.to_i
211 end
211 end
212 Redmine::Hook.call_hook(:model_changeset_scan_commit_for_issue_ids_pre_issue_update,
212 Redmine::Hook.call_hook(:model_changeset_scan_commit_for_issue_ids_pre_issue_update,
213 { :changeset => self, :issue => issue })
213 { :changeset => self, :issue => issue })
214 unless issue.save
214 unless issue.save
215 logger.warn("Issue ##{issue.id} could not be saved by changeset #{id}: #{issue.errors.full_messages}") if logger
215 logger.warn("Issue ##{issue.id} could not be saved by changeset #{id}: #{issue.errors.full_messages}") if logger
216 end
216 end
217 issue
217 issue
218 end
218 end
219
219
220 def log_time(issue, hours)
220 def log_time(issue, hours)
221 time_entry = TimeEntry.new(
221 time_entry = TimeEntry.new(
222 :user => user,
222 :user => user,
223 :hours => hours,
223 :hours => hours,
224 :issue => issue,
224 :issue => issue,
225 :spent_on => commit_date,
225 :spent_on => commit_date,
226 :comments => l(:text_time_logged_by_changeset, :value => text_tag, :locale => Setting.default_language)
226 :comments => l(:text_time_logged_by_changeset, :value => text_tag, :locale => Setting.default_language)
227 )
227 )
228 time_entry.activity = log_time_activity unless log_time_activity.nil?
228 time_entry.activity = log_time_activity unless log_time_activity.nil?
229
229
230 unless time_entry.save
230 unless time_entry.save
231 logger.warn("TimeEntry could not be created by changeset #{id}: #{time_entry.errors.full_messages}") if logger
231 logger.warn("TimeEntry could not be created by changeset #{id}: #{time_entry.errors.full_messages}") if logger
232 end
232 end
233 time_entry
233 time_entry
234 end
234 end
235
235
236 def log_time_activity
236 def log_time_activity
237 if Setting.commit_logtime_activity_id.to_i > 0
237 if Setting.commit_logtime_activity_id.to_i > 0
238 TimeEntryActivity.find_by_id(Setting.commit_logtime_activity_id.to_i)
238 TimeEntryActivity.find_by_id(Setting.commit_logtime_activity_id.to_i)
239 end
239 end
240 end
240 end
241
241
242 def split_comments
242 def split_comments
243 comments =~ /\A(.+?)\r?\n(.*)$/m
243 comments =~ /\A(.+?)\r?\n(.*)$/m
244 @short_comments = $1 || comments
244 @short_comments = $1 || comments
245 @long_comments = $2.to_s.strip
245 @long_comments = $2.to_s.strip
246 return @short_comments, @long_comments
246 return @short_comments, @long_comments
247 end
247 end
248
248
249 def self.to_utf8(str)
249 def self.to_utf8(str)
250 return str if str.blank?
250 encoding = Setting.commit_logs_encoding.to_s.strip
251 encoding = Setting.commit_logs_encoding.to_s.strip
251 unless encoding.blank? || encoding == 'UTF-8'
252 unless encoding.blank? || encoding == 'UTF-8'
252 begin
253 begin
253 str = Iconv.conv('UTF-8', encoding, str)
254 str = Iconv.conv('UTF-8', encoding, str)
254 rescue Iconv::Failure
255 rescue Iconv::Failure
255 # do nothing here
256 # do nothing here
256 end
257 end
257 end
258 end
258 if str.respond_to?(:force_encoding)
259 if str.respond_to?(:force_encoding)
259 str.force_encoding('UTF-8')
260 str.force_encoding('UTF-8')
260 if ! str.valid_encoding?
261 if ! str.valid_encoding?
261 str = str.encode("US-ASCII", :invalid => :replace,
262 str = str.encode("US-ASCII", :invalid => :replace,
262 :undef => :replace, :replace => '?').encode("UTF-8")
263 :undef => :replace, :replace => '?').encode("UTF-8")
263 end
264 end
264 else
265 else
265 # removes invalid UTF8 sequences
266 # removes invalid UTF8 sequences
266 begin
267 begin
267 str = Iconv.conv('UTF-8//IGNORE', 'UTF-8', str + ' ')[0..-3]
268 str = Iconv.conv('UTF-8//IGNORE', 'UTF-8', str + ' ')[0..-3]
268 rescue Iconv::InvalidEncoding
269 rescue Iconv::InvalidEncoding
269 # "UTF-8//IGNORE" is not supported on some OS
270 # "UTF-8//IGNORE" is not supported on some OS
270 end
271 end
271 end
272 end
272 str
273 str
273 end
274 end
274 end
275 end
General Comments 0
You need to be logged in to leave comments. Login now