annotate app/helpers/.svn/text-base/repositories_helper.rb.svn-base @ 245:051f544170fe

* Update to SVN trunk revision 4993
author Chris Cannam
date Thu, 03 Mar 2011 11:42:28 +0000
parents 8661b858af72
children eeebe205a056 cbce1fd3b1b7
rev   line source
Chris@0 1 # redMine - project management software
Chris@0 2 # Copyright (C) 2006 Jean-Philippe Lang
Chris@0 3 #
Chris@0 4 # This program is free software; you can redistribute it and/or
Chris@0 5 # modify it under the terms of the GNU General Public License
Chris@0 6 # as published by the Free Software Foundation; either version 2
Chris@0 7 # of the License, or (at your option) any later version.
Chris@0 8 #
Chris@0 9 # This program is distributed in the hope that it will be useful,
Chris@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 12 # GNU General Public License for more details.
Chris@0 13 #
Chris@0 14 # You should have received a copy of the GNU General Public License
Chris@0 15 # along with this program; if not, write to the Free Software
Chris@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 17
Chris@0 18 require 'iconv'
Chris@0 19
Chris@0 20 module RepositoriesHelper
Chris@119 21 def format_revision(revision)
Chris@119 22 if revision.respond_to? :format_identifier
Chris@119 23 revision.format_identifier
Chris@119 24 else
Chris@119 25 revision.to_s
Chris@119 26 end
Chris@0 27 end
Chris@245 28
Chris@0 29 def truncate_at_line_break(text, length = 255)
Chris@0 30 if text
Chris@0 31 text.gsub(%r{^(.{#{length}}[^\n]*)\n.+$}m, '\\1...')
Chris@0 32 end
Chris@0 33 end
Chris@245 34
Chris@0 35 def render_properties(properties)
Chris@0 36 unless properties.nil? || properties.empty?
Chris@0 37 content = ''
Chris@0 38 properties.keys.sort.each do |property|
Chris@0 39 content << content_tag('li', "<b>#{h property}</b>: <span>#{h properties[property]}</span>")
Chris@0 40 end
Chris@0 41 content_tag('ul', content, :class => 'properties')
Chris@0 42 end
Chris@0 43 end
Chris@245 44
Chris@0 45 def render_changeset_changes
Chris@0 46 changes = @changeset.changes.find(:all, :limit => 1000, :order => 'path').collect do |change|
Chris@0 47 case change.action
Chris@0 48 when 'A'
Chris@0 49 # Detects moved/copied files
Chris@0 50 if !change.from_path.blank?
Chris@0 51 change.action = @changeset.changes.detect {|c| c.action == 'D' && c.path == change.from_path} ? 'R' : 'C'
Chris@0 52 end
Chris@0 53 change
Chris@0 54 when 'D'
Chris@0 55 @changeset.changes.detect {|c| c.from_path == change.path} ? nil : change
Chris@0 56 else
Chris@0 57 change
Chris@0 58 end
Chris@0 59 end.compact
Chris@0 60
Chris@0 61 tree = { }
Chris@0 62 changes.each do |change|
Chris@0 63 p = tree
Chris@0 64 dirs = change.path.to_s.split('/').select {|d| !d.blank?}
Chris@0 65 path = ''
Chris@0 66 dirs.each do |dir|
Chris@0 67 path += '/' + dir
Chris@0 68 p[:s] ||= {}
Chris@0 69 p = p[:s]
Chris@0 70 p[path] ||= {}
Chris@0 71 p = p[path]
Chris@0 72 end
Chris@0 73 p[:c] = change
Chris@0 74 end
Chris@0 75
Chris@0 76 render_changes_tree(tree[:s])
Chris@0 77 end
Chris@245 78
Chris@0 79 def render_changes_tree(tree)
Chris@0 80 return '' if tree.nil?
Chris@245 81
Chris@0 82 output = ''
Chris@0 83 output << '<ul>'
Chris@0 84 tree.keys.sort.each do |file|
Chris@0 85 style = 'change'
Chris@0 86 text = File.basename(h(file))
Chris@0 87 if s = tree[file][:s]
Chris@0 88 style << ' folder'
Chris@0 89 path_param = to_path_param(@repository.relative_path(file))
Chris@0 90 text = link_to(text, :controller => 'repositories',
Chris@0 91 :action => 'show',
Chris@0 92 :id => @project,
Chris@0 93 :path => path_param,
Chris@119 94 :rev => @changeset.identifier)
Chris@0 95 output << "<li class='#{style}'>#{text}</li>"
Chris@0 96 output << render_changes_tree(s)
Chris@0 97 elsif c = tree[file][:c]
Chris@0 98 style << " change-#{c.action}"
Chris@0 99 path_param = to_path_param(@repository.relative_path(c.path))
Chris@0 100 text = link_to(text, :controller => 'repositories',
Chris@0 101 :action => 'entry',
Chris@0 102 :id => @project,
Chris@0 103 :path => path_param,
Chris@119 104 :rev => @changeset.identifier) unless c.action == 'D'
Chris@0 105 text << " - #{c.revision}" unless c.revision.blank?
Chris@0 106 text << ' (' + link_to('diff', :controller => 'repositories',
Chris@0 107 :action => 'diff',
Chris@0 108 :id => @project,
Chris@0 109 :path => path_param,
Chris@119 110 :rev => @changeset.identifier) + ') ' if c.action == 'M'
Chris@0 111 text << ' ' + content_tag('span', c.from_path, :class => 'copied-from') unless c.from_path.blank?
Chris@0 112 output << "<li class='#{style}'>#{text}</li>"
Chris@0 113 end
Chris@0 114 end
Chris@0 115 output << '</ul>'
Chris@0 116 output
Chris@0 117 end
Chris@245 118
Chris@0 119 def to_utf8(str)
Chris@245 120 return str if str.blank?
Chris@119 121 if str.respond_to?(:force_encoding)
Chris@119 122 str.force_encoding('UTF-8')
Chris@119 123 else
Chris@245 124 # TODO:
Chris@245 125 # Japanese Shift_JIS(CP932) is not compatible with ASCII.
Chris@245 126 # UTF-7 and Japanese ISO-2022-JP are 7bits clean.
Chris@119 127 return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
Chris@119 128 end
Chris@245 129
Chris@0 130 @encodings ||= Setting.repositories_encodings.split(',').collect(&:strip)
Chris@0 131 @encodings.each do |encoding|
Chris@0 132 begin
Chris@0 133 return Iconv.conv('UTF-8', encoding, str)
Chris@0 134 rescue Iconv::Failure
Chris@0 135 # do nothing here and try the next encoding
Chris@0 136 end
Chris@0 137 end
Chris@245 138 str = replace_invalid_utf8(str)
Chris@245 139 end
Chris@245 140
Chris@245 141 def replace_invalid_utf8(str)
Chris@245 142 if str.respond_to?(:force_encoding)
Chris@245 143 str.force_encoding('UTF-8')
Chris@245 144 if ! str.valid_encoding?
Chris@245 145 str = str.encode("US-ASCII", :invalid => :replace,
Chris@245 146 :undef => :replace, :replace => '?').encode("UTF-8")
Chris@245 147 end
Chris@245 148 end
Chris@0 149 str
Chris@0 150 end
Chris@245 151
Chris@245 152 def repository_field_tags(form, repository)
Chris@0 153 method = repository.class.name.demodulize.underscore + "_field_tags"
Chris@245 154 if repository.is_a?(Repository) &&
Chris@245 155 respond_to?(method) && method != 'repository_field_tags'
Chris@245 156 send(method, form, repository)
Chris@245 157 end
Chris@0 158 end
Chris@245 159
Chris@0 160 def scm_select_tag(repository)
Chris@0 161 scm_options = [["--- #{l(:actionview_instancetag_blank_option)} ---", '']]
Chris@0 162 Redmine::Scm::Base.all.each do |scm|
Chris@245 163 if Setting.enabled_scm.include?(scm) ||
Chris@245 164 (repository && repository.class.name.demodulize == scm)
Chris@245 165 scm_options << ["Repository::#{scm}".constantize.scm_name, scm]
Chris@245 166 end
Chris@0 167 end
Chris@0 168 select_tag('repository_scm',
Chris@0 169 options_for_select(scm_options, repository.class.name.demodulize),
Chris@0 170 :disabled => (repository && !repository.new_record?),
Chris@245 171 :onchange => remote_function(
Chris@245 172 :url => {
Chris@245 173 :controller => 'repositories',
Chris@245 174 :action => 'edit',
Chris@245 175 :id => @project
Chris@245 176 },
Chris@245 177 :method => :get,
Chris@245 178 :with => "Form.serialize(this.form)")
Chris@0 179 )
Chris@0 180 end
Chris@245 181
Chris@0 182 def with_leading_slash(path)
Chris@0 183 path.to_s.starts_with?('/') ? path : "/#{path}"
Chris@0 184 end
Chris@245 185
Chris@0 186 def without_leading_slash(path)
Chris@0 187 path.gsub(%r{^/+}, '')
Chris@0 188 end
Chris@0 189
Chris@0 190 def subversion_field_tags(form, repository)
Chris@245 191 content_tag('p', form.text_field(:url, :size => 60, :required => true,
Chris@245 192 :disabled => (repository && !repository.root_url.blank?)) +
Chris@0 193 '<br />(file:///, http://, https://, svn://, svn+[tunnelscheme]://)') +
Chris@0 194 content_tag('p', form.text_field(:login, :size => 30)) +
Chris@245 195 content_tag('p', form.password_field(
Chris@245 196 :password, :size => 30, :name => 'ignore',
Chris@245 197 :value => ((repository.new_record? || repository.password.blank?) ? '' : ('x'*15)),
Chris@245 198 :onfocus => "this.value=''; this.name='repository[password]';",
Chris@245 199 :onchange => "this.name='repository[password]';"))
Chris@0 200 end
Chris@0 201
Chris@0 202 def darcs_field_tags(form, repository)
Chris@245 203 content_tag('p', form.text_field(:url, :label => 'Root directory',
Chris@245 204 :size => 60, :required => true,
Chris@245 205 :disabled => (repository && !repository.new_record?))) +
Chris@245 206 content_tag('p', form.select(:log_encoding, [nil] + Setting::ENCODINGS,
Chris@245 207 :label => 'Commit messages encoding', :required => true))
Chris@0 208 end
Chris@245 209
Chris@0 210 def mercurial_field_tags(form, repository)
Chris@245 211 content_tag('p', form.text_field(:url, :label => 'Root directory',
Chris@245 212 :size => 60, :required => true,
Chris@245 213 :disabled => (repository && !repository.root_url.blank?)) +
Chris@245 214 '<br />local repository (e.g. /hgrepo, c:\hgrepo)' )
Chris@0 215 end
Chris@0 216
Chris@0 217 def git_field_tags(form, repository)
Chris@245 218 content_tag('p', form.text_field(:url, :label => 'Path to repository',
Chris@245 219 :size => 60, :required => true,
Chris@245 220 :disabled => (repository && !repository.root_url.blank?)) +
Chris@245 221 '<br />a bare and local repository (e.g. /gitrepo, c:\gitrepo)')
Chris@0 222 end
Chris@0 223
Chris@0 224 def cvs_field_tags(form, repository)
Chris@245 225 content_tag('p', form.text_field(:root_url,
Chris@245 226 :label => 'CVSROOT', :size => 60, :required => true,
Chris@245 227 :disabled => !repository.new_record?)) +
Chris@245 228 content_tag('p', form.text_field(:url, :label => 'Module',
Chris@245 229 :size => 30, :required => true,
Chris@245 230 :disabled => !repository.new_record?)) +
Chris@245 231 content_tag('p', form.select(:log_encoding, [nil] + Setting::ENCODINGS,
Chris@245 232 :label => 'Commit messages encoding', :required => true))
Chris@0 233 end
Chris@0 234
Chris@0 235 def bazaar_field_tags(form, repository)
Chris@245 236 content_tag('p', form.text_field(:url, :label => 'Root directory',
Chris@245 237 :size => 60, :required => true,
Chris@245 238 :disabled => (repository && !repository.new_record?))) +
Chris@245 239 content_tag('p', form.select(:log_encoding, [nil] + Setting::ENCODINGS,
Chris@245 240 :label => 'Commit messages encoding', :required => true))
Chris@0 241 end
Chris@245 242
Chris@0 243 def filesystem_field_tags(form, repository)
Chris@245 244 content_tag('p', form.text_field(:url, :label => 'Root directory',
Chris@245 245 :size => 60, :required => true,
Chris@245 246 :disabled => (repository && !repository.root_url.blank?))) +
Chris@245 247 content_tag('p', form.select(
Chris@245 248 :path_encoding, [nil] + Setting::ENCODINGS,
Chris@245 249 :label => 'Path encoding') +
Chris@245 250 '<br />Default: UTF-8')
Chris@0 251 end
Chris@0 252 end