repositories_helper.rb
286 lines
| 10.9 KiB
| text/x-ruby
|
RubyLexer
|
r5710 | # Redmine - project management software | ||
# Copyright (C) 2006-2011 Jean-Philippe Lang | ||||
|
r103 | # | ||
# This program is free software; you can redistribute it and/or | ||||
# modify it under the terms of the GNU General Public License | ||||
# as published by the Free Software Foundation; either version 2 | ||||
# of the License, or (at your option) any later version. | ||||
|
r5710 | # | ||
|
r103 | # This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU General Public License for more details. | ||||
|
r5710 | # | ||
|
r103 | # You should have received a copy of the GNU General Public License | ||
# along with this program; if not, write to the Free Software | ||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
|
r803 | require 'iconv' | ||
|
r5354 | require 'redmine/codeset_util' | ||
|
r638 | |||
|
r103 | module RepositoriesHelper | ||
|
r4493 | def format_revision(revision) | ||
if revision.respond_to? :format_identifier | ||||
revision.format_identifier | ||||
else | ||||
revision.to_s | ||||
end | ||||
|
r1222 | end | ||
|
r4870 | |||
|
r1898 | def truncate_at_line_break(text, length = 255) | ||
if text | ||||
text.gsub(%r{^(.{#{length}}[^\n]*)\n.+$}m, '\\1...') | ||||
end | ||||
end | ||||
|
r4870 | |||
|
r1613 | def render_properties(properties) | ||
unless properties.nil? || properties.empty? | ||||
content = '' | ||||
properties.keys.sort.each do |property| | ||||
|
r7427 | content << content_tag('li', "<b>#{h property}</b>: <span>#{h properties[property]}</span>".html_safe) | ||
|
r1613 | end | ||
|
r7427 | content_tag('ul', content.html_safe, :class => 'properties') | ||
|
r1613 | end | ||
end | ||||
|
r4870 | |||
|
r1868 | def render_changeset_changes | ||
changes = @changeset.changes.find(:all, :limit => 1000, :order => 'path').collect do |change| | ||||
case change.action | ||||
when 'A' | ||||
# Detects moved/copied files | ||||
if !change.from_path.blank? | ||||
|
r5386 | change.action = | ||
@changeset.changes.detect {|c| c.action == 'D' && c.path == change.from_path} ? 'R' : 'C' | ||||
|
r1868 | end | ||
change | ||||
when 'D' | ||||
@changeset.changes.detect {|c| c.from_path == change.path} ? nil : change | ||||
else | ||||
change | ||||
end | ||||
|
r5386 | end.compact | ||
|
r5710 | |||
|
r1868 | tree = { } | ||
changes.each do |change| | ||||
p = tree | ||||
dirs = change.path.to_s.split('/').select {|d| !d.blank?} | ||||
|
r3545 | path = '' | ||
|
r1868 | dirs.each do |dir| | ||
|
r3545 | path += '/' + dir | ||
|
r1868 | p[:s] ||= {} | ||
p = p[:s] | ||||
|
r3545 | p[path] ||= {} | ||
p = p[path] | ||||
|
r1868 | end | ||
p[:c] = change | ||||
end | ||||
render_changes_tree(tree[:s]) | ||||
end | ||||
|
r4870 | |||
|
r1868 | def render_changes_tree(tree) | ||
return '' if tree.nil? | ||||
output = '' | ||||
output << '<ul>' | ||||
tree.keys.sort.each do |file| | ||||
style = 'change' | ||||
|
r3545 | text = File.basename(h(file)) | ||
if s = tree[file][:s] | ||||
style << ' folder' | ||||
path_param = to_path_param(@repository.relative_path(file)) | ||||
|
r6235 | text = link_to(h(text), :controller => 'repositories', | ||
|
r3545 | :action => 'show', | ||
:id => @project, | ||||
:path => path_param, | ||||
|
r4493 | :rev => @changeset.identifier) | ||
|
r3545 | output << "<li class='#{style}'>#{text}</li>" | ||
output << render_changes_tree(s) | ||||
elsif c = tree[file][:c] | ||||
style << " change-#{c.action}" | ||||
|
r1868 | path_param = to_path_param(@repository.relative_path(c.path)) | ||
|
r6235 | text = link_to(h(text), :controller => 'repositories', | ||
|
r1868 | :action => 'entry', | ||
:id => @project, | ||||
:path => path_param, | ||||
|
r4493 | :rev => @changeset.identifier) unless c.action == 'D' | ||
|
r6235 | text << " - #{h(c.revision)}" unless c.revision.blank? | ||
|
r7425 | text << ' ('.html_safe + link_to(l(:label_diff), :controller => 'repositories', | ||
|
r1868 | :action => 'diff', | ||
:id => @project, | ||||
:path => path_param, | ||||
|
r7425 | :rev => @changeset.identifier) + ') '.html_safe if c.action == 'M' | ||
text << ' '.html_safe + content_tag('span', h(c.from_path), :class => 'copied-from') unless c.from_path.blank? | ||||
|
r3545 | output << "<li class='#{style}'>#{text}</li>" | ||
|
r1868 | end | ||
end | ||||
output << '</ul>' | ||||
|
r7425 | output.html_safe | ||
|
r1868 | end | ||
|
r4870 | |||
|
r803 | def to_utf8(str) | ||
|
r5045 | return str if str.nil? | ||
str = to_utf8_internal(str) | ||||
if str.respond_to?(:force_encoding) | ||||
str.force_encoding('UTF-8') | ||||
end | ||||
str | ||||
end | ||||
def to_utf8_internal(str) | ||||
return str if str.nil? | ||||
|
r5038 | if str.respond_to?(:force_encoding) | ||
str.force_encoding('ASCII-8BIT') | ||||
end | ||||
return str if str.empty? | ||||
return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii | ||||
|
r4485 | if str.respond_to?(:force_encoding) | ||
str.force_encoding('UTF-8') | ||||
end | ||||
|
r803 | @encodings ||= Setting.repositories_encodings.split(',').collect(&:strip) | ||
@encodings.each do |encoding| | ||||
begin | ||||
return Iconv.conv('UTF-8', encoding, str) | ||||
rescue Iconv::Failure | ||||
# do nothing here and try the next encoding | ||||
end | ||||
end | ||||
|
r5354 | str = Redmine::CodesetUtil.replace_invalid_utf8(str) | ||
|
r4801 | end | ||
|
r5045 | private :to_utf8_internal | ||
|
r4801 | |||
def repository_field_tags(form, repository) | ||||
|
r556 | method = repository.class.name.demodulize.underscore + "_field_tags" | ||
|
r4865 | if repository.is_a?(Repository) && | ||
respond_to?(method) && method != 'repository_field_tags' | ||||
send(method, form, repository) | ||||
end | ||||
|
r556 | end | ||
|
r4865 | |||
|
r714 | def scm_select_tag(repository) | ||
|
r1493 | scm_options = [["--- #{l(:actionview_instancetag_blank_option)} ---", '']] | ||
|
r3326 | Redmine::Scm::Base.all.each do |scm| | ||
|
r4865 | if Setting.enabled_scm.include?(scm) || | ||
(repository && repository.class.name.demodulize == scm) | ||||
scm_options << ["Repository::#{scm}".constantize.scm_name, scm] | ||||
end | ||||
|
r1493 | end | ||
|
r5498 | select_tag('repository_scm', | ||
|
r1493 | options_for_select(scm_options, repository.class.name.demodulize), | ||
|
r714 | :disabled => (repository && !repository.new_record?), | ||
|
r4865 | :onchange => remote_function( | ||
:url => { | ||||
:controller => 'repositories', | ||||
|
r5498 | :action => 'edit', | ||
:id => @project | ||||
}, | ||||
|
r4865 | :method => :get, | ||
|
r5498 | :with => "Form.serialize(this.form)") | ||
) | ||||
|
r556 | end | ||
|
r4822 | |||
|
r556 | def with_leading_slash(path) | ||
|
r1310 | path.to_s.starts_with?('/') ? path : "/#{path}" | ||
end | ||||
|
r4822 | |||
|
r1310 | def without_leading_slash(path) | ||
path.gsub(%r{^/+}, '') | ||||
|
r556 | end | ||
def subversion_field_tags(form, repository) | ||||
|
r4822 | content_tag('p', form.text_field(:url, :size => 60, :required => true, | ||
:disabled => (repository && !repository.root_url.blank?)) + | ||||
|
r7432 | '<br />'.html_safe + | ||
'(file:///, http://, https://, svn://, svn+[tunnelscheme]://)') + | ||||
|
r556 | content_tag('p', form.text_field(:login, :size => 30)) + | ||
|
r4870 | content_tag('p', form.password_field( | ||
:password, :size => 30, :name => 'ignore', | ||||
:value => ((repository.new_record? || repository.password.blank?) ? '' : ('x'*15)), | ||||
:onfocus => "this.value=''; this.name='repository[password]';", | ||||
:onchange => "this.name='repository[password]';")) | ||||
|
r556 | end | ||
|
r570 | def darcs_field_tags(form, repository) | ||
|
r5409 | content_tag('p', form.text_field( | ||
|
r5555 | :url, :label => l(:field_path_to_repository), | ||
|
r5395 | :size => 60, :required => true, | ||
:disabled => (repository && !repository.new_record?))) + | ||||
|
r5399 | content_tag('p', form.select( | ||
:log_encoding, [nil] + Setting::ENCODINGS, | ||||
|
r5555 | :label => l(:field_commit_logs_encoding), :required => true)) | ||
|
r570 | end | ||
|
r4822 | |||
|
r556 | def mercurial_field_tags(form, repository) | ||
|
r5408 | content_tag('p', form.text_field( | ||
|
r5556 | :url, :label => l(:field_path_to_repository), | ||
|
r4822 | :size => 60, :required => true, | ||
|
r5408 | :disabled => (repository && !repository.root_url.blank?) | ||
) + | ||||
|
r7428 | '<br />'.html_safe + l(:text_mercurial_repository_note)) + | ||
|
r4881 | content_tag('p', form.select( | ||
:path_encoding, [nil] + Setting::ENCODINGS, | ||||
|
r5556 | :label => l(:field_scm_path_encoding) | ||
|
r5403 | ) + | ||
|
r7428 | '<br />'.html_safe + l(:text_scm_path_encoding_note)) | ||
|
r556 | end | ||
|
r1222 | def git_field_tags(form, repository) | ||
|
r5409 | content_tag('p', form.text_field( | ||
|
r5557 | :url, :label => l(:field_path_to_repository), | ||
|
r4763 | :size => 60, :required => true, | ||
|
r5409 | :disabled => (repository && !repository.root_url.blank?) | ||
) + | ||||
|
r7429 | '<br />'.html_safe + | ||
|
r6518 | l(:text_git_repository_note)) + | ||
|
r4944 | content_tag('p', form.select( | ||
:path_encoding, [nil] + Setting::ENCODINGS, | ||||
|
r5557 | :label => l(:field_scm_path_encoding) | ||
|
r5403 | ) + | ||
|
r7429 | '<br />'.html_safe + l(:text_scm_path_encoding_note)) + | ||
|
r5651 | content_tag('p', form.check_box( | ||
:extra_report_last_commit, | ||||
|
r5656 | :label => l(:label_git_report_last_commit) | ||
|
r5651 | )) | ||
|
r1222 | end | ||
|
r556 | def cvs_field_tags(form, repository) | ||
|
r5415 | content_tag('p', form.text_field( | ||
:root_url, | ||||
|
r5558 | :label => l(:field_cvsroot), | ||
|
r5415 | :size => 60, :required => true, | ||
|
r5268 | :disabled => !repository.new_record?)) + | ||
|
r5415 | content_tag('p', form.text_field( | ||
:url, | ||||
|
r5558 | :label => l(:field_cvs_module), | ||
|
r5268 | :size => 30, :required => true, | ||
:disabled => !repository.new_record?)) + | ||||
|
r5399 | content_tag('p', form.select( | ||
:log_encoding, [nil] + Setting::ENCODINGS, | ||||
|
r5558 | :label => l(:field_commit_logs_encoding), :required => true)) + | ||
|
r5345 | content_tag('p', form.select( | ||
|
r5403 | :path_encoding, [nil] + Setting::ENCODINGS, | ||
|
r5558 | :label => l(:field_scm_path_encoding) | ||
|
r5403 | ) + | ||
|
r7430 | '<br />'.html_safe + l(:text_scm_path_encoding_note)) | ||
|
r556 | end | ||
|
r937 | |||
def bazaar_field_tags(form, repository) | ||||
|
r5409 | content_tag('p', form.text_field( | ||
|
r5559 | :url, :label => l(:field_path_to_repository), | ||
|
r5268 | :size => 60, :required => true, | ||
:disabled => (repository && !repository.new_record?))) + | ||||
|
r5399 | content_tag('p', form.select( | ||
:log_encoding, [nil] + Setting::ENCODINGS, | ||||
|
r5559 | :label => l(:field_commit_logs_encoding), :required => true)) | ||
|
r937 | end | ||
|
r4822 | |||
|
r1494 | def filesystem_field_tags(form, repository) | ||
|
r5412 | content_tag('p', form.text_field( | ||
|
r5560 | :url, :label => l(:field_root_directory), | ||
|
r4822 | :size => 60, :required => true, | ||
|
r4824 | :disabled => (repository && !repository.root_url.blank?))) + | ||
|
r4868 | content_tag('p', form.select( | ||
|
r5403 | :path_encoding, [nil] + Setting::ENCODINGS, | ||
|
r5560 | :label => l(:field_scm_path_encoding) | ||
|
r5403 | ) + | ||
|
r7431 | '<br />'.html_safe + l(:text_scm_path_encoding_note)) | ||
|
r1494 | end | ||
|
r103 | end | ||