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