repositories_helper.rb
319 lines
| 12.0 KiB
| text/x-ruby
|
RubyLexer
|
r8090 | # encoding: utf-8 | ||
# | ||||
|
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, | ||||
|
r8530 | :repository_id => @repository.identifier_param, | ||
|
r3545 | :path => path_param, | ||
|
r4493 | :rev => @changeset.identifier) | ||
|
r8386 | output << "<li class='#{style}'>#{text}" | ||
|
r3545 | output << render_changes_tree(s) | ||
|
r8386 | output << "</li>" | ||
|
r3545 | 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, | ||||
|
r8530 | :repository_id => @repository.identifier_param, | ||
|
r1868 | :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, | ||||
|
r8530 | :repository_id => @repository.identifier_param, | ||
|
r1868 | :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 | |||
|
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( | ||
|
r8528 | :url => new_project_repository_path(@project), | ||
:method => :get, | ||||
:update => 'content', | ||||
:with => "Form.serialize(this.form)") | ||||
|
r5498 | ) | ||
|
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 | ||
|
r7605 | |||
|
r8653 | def index_commits(commits, heads) | ||
|
r7605 | return nil if commits.nil? or commits.first.parents.nil? | ||
|
r8653 | |||
|
r7605 | refs_map = {} | ||
|
r8653 | heads.each do |head| | ||
refs_map[head.scmid] ||= [] | ||||
refs_map[head.scmid] << head | ||||
|
r7605 | end | ||
|
r8653 | |||
commits_by_scmid = {} | ||||
commits.reverse.each_with_index do |commit, commit_index| | ||||
commits_by_scmid[commit.scmid] = { | ||||
:parent_scmids => commit.parents.collect { |parent| parent.scmid }, | ||||
:rdmid => commit_index, | ||||
:space => 0, | ||||
:refs => refs_map.include?(commit.scmid) ? refs_map[commit.scmid].join(" ") : nil, | ||||
:scmid => commit.scmid, | ||||
:href => block_given? ? yield(commit.scmid) : commit.scmid | ||||
} | ||||
|
r7605 | end | ||
|
r8653 | |||
heads.sort! { |head1, head2| head1.to_s <=> head2.to_s } | ||||
mark_index = 0 | ||||
heads.each do |head| | ||||
if commits_by_scmid.include? head.scmid | ||||
mark_index = mark_chain(mark_index += 1, commits_by_scmid[head.scmid], commits_by_scmid) | ||||
|
r7605 | end | ||
end | ||||
# when no head matched anything use first commit | ||||
|
r8653 | if mark_index == 0 | ||
mark_chain(mark_index += 1, commits_by_scmid.values.first, commits_by_scmid) | ||||
|
r7605 | end | ||
|
r8653 | commits_by_scmid | ||
|
r7605 | end | ||
|
r8653 | def mark_chain(mark_index, commit, commits_by_scmid) | ||
stack = [[mark_index, commit]] | ||||
mark_max_index = mark_index | ||||
|
r7605 | until stack.empty? | ||
|
r8653 | mark_index, commit = stack.pop | ||
commit[:space] = mark_index if commit[:space] == 0 | ||||
mark_index -=1 | ||||
commit[:parent_scmids].each_with_index do |parent_scmid, parent_index| | ||||
parent_commit = commits_by_scmid[parent_scmid] | ||||
if parent_commit and parent_commit[:space] == 0 | ||||
stack.unshift [mark_index += 1, parent_commit] | ||||
|
r7605 | end | ||
end | ||||
|
r8653 | mark_max_index = mark_index if mark_max_index < mark_index | ||
|
r7605 | end | ||
|
r8653 | mark_max_index | ||
|
r7605 | end | ||
|
r103 | end | ||