annotate app/helpers/.svn/text-base/repositories_helper.rb.svn-base @ 8:0c83d98252d9 yuya

* Add custom repo prefix and proper auth realm, remove auth cache (seems like an unwise feature), pass DB handle around, various other bits of tidying
author Chris Cannam
date Thu, 12 Aug 2010 15:31:37 +0100
parents 513646585e45
children af80e5618e9b 8661b858af72
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@0 21 def format_revision(txt)
Chris@0 22 txt.to_s[0,8]
Chris@0 23 end
Chris@0 24
Chris@0 25 def truncate_at_line_break(text, length = 255)
Chris@0 26 if text
Chris@0 27 text.gsub(%r{^(.{#{length}}[^\n]*)\n.+$}m, '\\1...')
Chris@0 28 end
Chris@0 29 end
Chris@0 30
Chris@0 31 def render_properties(properties)
Chris@0 32 unless properties.nil? || properties.empty?
Chris@0 33 content = ''
Chris@0 34 properties.keys.sort.each do |property|
Chris@0 35 content << content_tag('li', "<b>#{h property}</b>: <span>#{h properties[property]}</span>")
Chris@0 36 end
Chris@0 37 content_tag('ul', content, :class => 'properties')
Chris@0 38 end
Chris@0 39 end
Chris@0 40
Chris@0 41 def render_changeset_changes
Chris@0 42 changes = @changeset.changes.find(:all, :limit => 1000, :order => 'path').collect do |change|
Chris@0 43 case change.action
Chris@0 44 when 'A'
Chris@0 45 # Detects moved/copied files
Chris@0 46 if !change.from_path.blank?
Chris@0 47 change.action = @changeset.changes.detect {|c| c.action == 'D' && c.path == change.from_path} ? 'R' : 'C'
Chris@0 48 end
Chris@0 49 change
Chris@0 50 when 'D'
Chris@0 51 @changeset.changes.detect {|c| c.from_path == change.path} ? nil : change
Chris@0 52 else
Chris@0 53 change
Chris@0 54 end
Chris@0 55 end.compact
Chris@0 56
Chris@0 57 tree = { }
Chris@0 58 changes.each do |change|
Chris@0 59 p = tree
Chris@0 60 dirs = change.path.to_s.split('/').select {|d| !d.blank?}
Chris@0 61 path = ''
Chris@0 62 dirs.each do |dir|
Chris@0 63 path += '/' + dir
Chris@0 64 p[:s] ||= {}
Chris@0 65 p = p[:s]
Chris@0 66 p[path] ||= {}
Chris@0 67 p = p[path]
Chris@0 68 end
Chris@0 69 p[:c] = change
Chris@0 70 end
Chris@0 71
Chris@0 72 render_changes_tree(tree[:s])
Chris@0 73 end
Chris@0 74
Chris@0 75 def render_changes_tree(tree)
Chris@0 76 return '' if tree.nil?
Chris@0 77
Chris@0 78 output = ''
Chris@0 79 output << '<ul>'
Chris@0 80 tree.keys.sort.each do |file|
Chris@0 81 style = 'change'
Chris@0 82 text = File.basename(h(file))
Chris@0 83 if s = tree[file][:s]
Chris@0 84 style << ' folder'
Chris@0 85 path_param = to_path_param(@repository.relative_path(file))
Chris@0 86 text = link_to(text, :controller => 'repositories',
Chris@0 87 :action => 'show',
Chris@0 88 :id => @project,
Chris@0 89 :path => path_param,
Chris@0 90 :rev => @changeset.revision)
Chris@0 91 output << "<li class='#{style}'>#{text}</li>"
Chris@0 92 output << render_changes_tree(s)
Chris@0 93 elsif c = tree[file][:c]
Chris@0 94 style << " change-#{c.action}"
Chris@0 95 path_param = to_path_param(@repository.relative_path(c.path))
Chris@0 96 text = link_to(text, :controller => 'repositories',
Chris@0 97 :action => 'entry',
Chris@0 98 :id => @project,
Chris@0 99 :path => path_param,
Chris@0 100 :rev => @changeset.revision) unless c.action == 'D'
Chris@0 101 text << " - #{c.revision}" unless c.revision.blank?
Chris@0 102 text << ' (' + link_to('diff', :controller => 'repositories',
Chris@0 103 :action => 'diff',
Chris@0 104 :id => @project,
Chris@0 105 :path => path_param,
Chris@0 106 :rev => @changeset.revision) + ') ' if c.action == 'M'
Chris@0 107 text << ' ' + content_tag('span', c.from_path, :class => 'copied-from') unless c.from_path.blank?
Chris@0 108 output << "<li class='#{style}'>#{text}</li>"
Chris@0 109 end
Chris@0 110 end
Chris@0 111 output << '</ul>'
Chris@0 112 output
Chris@0 113 end
Chris@0 114
Chris@0 115 def to_utf8(str)
Chris@0 116 return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
Chris@0 117 @encodings ||= Setting.repositories_encodings.split(',').collect(&:strip)
Chris@0 118 @encodings.each do |encoding|
Chris@0 119 begin
Chris@0 120 return Iconv.conv('UTF-8', encoding, str)
Chris@0 121 rescue Iconv::Failure
Chris@0 122 # do nothing here and try the next encoding
Chris@0 123 end
Chris@0 124 end
Chris@0 125 str
Chris@0 126 end
Chris@0 127
Chris@0 128 def repository_field_tags(form, repository)
Chris@0 129 method = repository.class.name.demodulize.underscore + "_field_tags"
Chris@0 130 send(method, form, repository) if repository.is_a?(Repository) && respond_to?(method) && method != 'repository_field_tags'
Chris@0 131 end
Chris@0 132
Chris@0 133 def scm_select_tag(repository)
Chris@0 134 scm_options = [["--- #{l(:actionview_instancetag_blank_option)} ---", '']]
Chris@0 135 Redmine::Scm::Base.all.each do |scm|
Chris@0 136 scm_options << ["Repository::#{scm}".constantize.scm_name, scm] if Setting.enabled_scm.include?(scm) || (repository && repository.class.name.demodulize == scm)
Chris@0 137 end
Chris@0 138
Chris@0 139 select_tag('repository_scm',
Chris@0 140 options_for_select(scm_options, repository.class.name.demodulize),
Chris@0 141 :disabled => (repository && !repository.new_record?),
Chris@0 142 :onchange => remote_function(:url => { :controller => 'repositories', :action => 'edit', :id => @project }, :method => :get, :with => "Form.serialize(this.form)")
Chris@0 143 )
Chris@0 144 end
Chris@0 145
Chris@0 146 def with_leading_slash(path)
Chris@0 147 path.to_s.starts_with?('/') ? path : "/#{path}"
Chris@0 148 end
Chris@0 149
Chris@0 150 def without_leading_slash(path)
Chris@0 151 path.gsub(%r{^/+}, '')
Chris@0 152 end
Chris@0 153
Chris@0 154 def subversion_field_tags(form, repository)
Chris@0 155 content_tag('p', form.text_field(:url, :size => 60, :required => true, :disabled => (repository && !repository.root_url.blank?)) +
Chris@0 156 '<br />(file:///, http://, https://, svn://, svn+[tunnelscheme]://)') +
Chris@0 157 content_tag('p', form.text_field(:login, :size => 30)) +
Chris@0 158 content_tag('p', form.password_field(:password, :size => 30, :name => 'ignore',
Chris@0 159 :value => ((repository.new_record? || repository.password.blank?) ? '' : ('x'*15)),
Chris@0 160 :onfocus => "this.value=''; this.name='repository[password]';",
Chris@0 161 :onchange => "this.name='repository[password]';"))
Chris@0 162 end
Chris@0 163
Chris@0 164 def darcs_field_tags(form, repository)
Chris@0 165 content_tag('p', form.text_field(:url, :label => 'Root directory', :size => 60, :required => true, :disabled => (repository && !repository.new_record?)))
Chris@0 166 end
Chris@0 167
Chris@0 168 def mercurial_field_tags(form, repository)
Chris@0 169 content_tag('p', form.text_field(:url, :label => 'Root directory', :size => 60, :required => true, :disabled => (repository && !repository.root_url.blank?)))
Chris@0 170 end
Chris@0 171
Chris@0 172 def git_field_tags(form, repository)
Chris@0 173 content_tag('p', form.text_field(:url, :label => 'Path to .git directory', :size => 60, :required => true, :disabled => (repository && !repository.root_url.blank?)))
Chris@0 174 end
Chris@0 175
Chris@0 176 def cvs_field_tags(form, repository)
Chris@0 177 content_tag('p', form.text_field(:root_url, :label => 'CVSROOT', :size => 60, :required => true, :disabled => !repository.new_record?)) +
Chris@0 178 content_tag('p', form.text_field(:url, :label => 'Module', :size => 30, :required => true, :disabled => !repository.new_record?))
Chris@0 179 end
Chris@0 180
Chris@0 181 def bazaar_field_tags(form, repository)
Chris@0 182 content_tag('p', form.text_field(:url, :label => 'Root directory', :size => 60, :required => true, :disabled => (repository && !repository.new_record?)))
Chris@0 183 end
Chris@0 184
Chris@0 185 def filesystem_field_tags(form, repository)
Chris@0 186 content_tag('p', form.text_field(:url, :label => 'Root directory', :size => 60, :required => true, :disabled => (repository && !repository.root_url.blank?)))
Chris@0 187 end
Chris@0 188 end