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