Chris@0: # redMine - project management software
Chris@0: # Copyright (C) 2006 Jean-Philippe Lang
Chris@0: #
Chris@0: # This program is free software; you can redistribute it and/or
Chris@0: # modify it under the terms of the GNU General Public License
Chris@0: # as published by the Free Software Foundation; either version 2
Chris@0: # of the License, or (at your option) any later version.
Chris@0: #
Chris@0: # This program is distributed in the hope that it will be useful,
Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0: # GNU General Public License for more details.
Chris@0: #
Chris@0: # You should have received a copy of the GNU General Public License
Chris@0: # along with this program; if not, write to the Free Software
Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0:
Chris@0: require 'iconv'
Chris@0:
Chris@0: module RepositoriesHelper
Chris@117: def format_revision(revision)
Chris@117: if revision.respond_to? :format_identifier
Chris@117: revision.format_identifier
Chris@117: else
Chris@117: revision.to_s
Chris@117: end
Chris@0: end
Chris@0:
Chris@0: def truncate_at_line_break(text, length = 255)
Chris@0: if text
Chris@0: text.gsub(%r{^(.{#{length}}[^\n]*)\n.+$}m, '\\1...')
Chris@0: end
Chris@0: end
Chris@0:
Chris@0: def render_properties(properties)
Chris@0: unless properties.nil? || properties.empty?
Chris@0: content = ''
Chris@0: properties.keys.sort.each do |property|
Chris@0: content << content_tag('li', "#{h property}: #{h properties[property]}")
Chris@0: end
Chris@0: content_tag('ul', content, :class => 'properties')
Chris@0: end
Chris@0: end
Chris@0:
Chris@0: def render_changeset_changes
Chris@0: changes = @changeset.changes.find(:all, :limit => 1000, :order => 'path').collect do |change|
Chris@0: case change.action
Chris@0: when 'A'
Chris@0: # Detects moved/copied files
Chris@0: if !change.from_path.blank?
Chris@0: change.action = @changeset.changes.detect {|c| c.action == 'D' && c.path == change.from_path} ? 'R' : 'C'
Chris@0: end
Chris@0: change
Chris@0: when 'D'
Chris@0: @changeset.changes.detect {|c| c.from_path == change.path} ? nil : change
Chris@0: else
Chris@0: change
Chris@0: end
Chris@0: end.compact
Chris@0:
Chris@0: tree = { }
Chris@0: changes.each do |change|
Chris@0: p = tree
Chris@0: dirs = change.path.to_s.split('/').select {|d| !d.blank?}
Chris@0: path = ''
Chris@0: dirs.each do |dir|
Chris@0: path += '/' + dir
Chris@0: p[:s] ||= {}
Chris@0: p = p[:s]
Chris@0: p[path] ||= {}
Chris@0: p = p[path]
Chris@0: end
Chris@0: p[:c] = change
Chris@0: end
Chris@0:
Chris@0: render_changes_tree(tree[:s])
Chris@0: end
Chris@0:
Chris@0: def render_changes_tree(tree)
Chris@0: return '' if tree.nil?
Chris@0:
Chris@0: output = ''
Chris@0: output << '
'
Chris@0: tree.keys.sort.each do |file|
Chris@0: style = 'change'
Chris@0: text = File.basename(h(file))
Chris@0: if s = tree[file][:s]
Chris@0: style << ' folder'
Chris@0: path_param = to_path_param(@repository.relative_path(file))
Chris@0: text = link_to(text, :controller => 'repositories',
Chris@0: :action => 'show',
Chris@0: :id => @project,
Chris@0: :path => path_param,
Chris@117: :rev => @changeset.identifier)
Chris@0: output << "- #{text}
"
Chris@0: output << render_changes_tree(s)
Chris@0: elsif c = tree[file][:c]
Chris@0: style << " change-#{c.action}"
Chris@0: path_param = to_path_param(@repository.relative_path(c.path))
Chris@0: text = link_to(text, :controller => 'repositories',
Chris@0: :action => 'entry',
Chris@0: :id => @project,
Chris@0: :path => path_param,
Chris@117: :rev => @changeset.identifier) unless c.action == 'D'
Chris@0: text << " - #{c.revision}" unless c.revision.blank?
Chris@0: text << ' (' + link_to('diff', :controller => 'repositories',
Chris@0: :action => 'diff',
Chris@0: :id => @project,
Chris@0: :path => path_param,
Chris@117: :rev => @changeset.identifier) + ') ' if c.action == 'M'
Chris@0: text << ' ' + content_tag('span', c.from_path, :class => 'copied-from') unless c.from_path.blank?
Chris@0: output << "- #{text}
"
Chris@0: end
Chris@0: end
Chris@0: output << '
'
Chris@0: output
Chris@0: end
Chris@0:
Chris@0: def to_utf8(str)
Chris@0: return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
Chris@0: @encodings ||= Setting.repositories_encodings.split(',').collect(&:strip)
Chris@0: @encodings.each do |encoding|
Chris@0: begin
Chris@0: return Iconv.conv('UTF-8', encoding, str)
Chris@0: rescue Iconv::Failure
Chris@0: # do nothing here and try the next encoding
Chris@0: end
Chris@0: end
Chris@0: str
Chris@0: end
Chris@0:
Chris@0: def repository_field_tags(form, repository)
Chris@0: method = repository.class.name.demodulize.underscore + "_field_tags"
Chris@0: send(method, form, repository) if repository.is_a?(Repository) && respond_to?(method) && method != 'repository_field_tags'
Chris@0: end
Chris@0:
Chris@0: def scm_select_tag(repository)
Chris@0: scm_options = [["--- #{l(:actionview_instancetag_blank_option)} ---", '']]
Chris@0: Redmine::Scm::Base.all.each do |scm|
Chris@0: scm_options << ["Repository::#{scm}".constantize.scm_name, scm] if Setting.enabled_scm.include?(scm) || (repository && repository.class.name.demodulize == scm)
Chris@0: end
Chris@0:
Chris@0: select_tag('repository_scm',
Chris@0: options_for_select(scm_options, repository.class.name.demodulize),
Chris@0: :disabled => (repository && !repository.new_record?),
Chris@0: :onchange => remote_function(:url => { :controller => 'repositories', :action => 'edit', :id => @project }, :method => :get, :with => "Form.serialize(this.form)")
Chris@0: )
Chris@0: end
Chris@0:
Chris@0: def with_leading_slash(path)
Chris@0: path.to_s.starts_with?('/') ? path : "/#{path}"
Chris@0: end
Chris@0:
Chris@0: def without_leading_slash(path)
Chris@0: path.gsub(%r{^/+}, '')
Chris@0: end
Chris@0:
Chris@0: def subversion_field_tags(form, repository)
Chris@0: content_tag('p', form.text_field(:url, :size => 60, :required => true, :disabled => (repository && !repository.root_url.blank?)) +
Chris@0: '
(file:///, http://, https://, svn://, svn+[tunnelscheme]://)') +
Chris@0: content_tag('p', form.text_field(:login, :size => 30)) +
Chris@0: content_tag('p', form.password_field(:password, :size => 30, :name => 'ignore',
Chris@0: :value => ((repository.new_record? || repository.password.blank?) ? '' : ('x'*15)),
Chris@0: :onfocus => "this.value=''; this.name='repository[password]';",
Chris@0: :onchange => "this.name='repository[password]';"))
Chris@0: end
Chris@0:
Chris@0: def darcs_field_tags(form, repository)
Chris@0: content_tag('p', form.text_field(:url, :label => 'Root directory', :size => 60, :required => true, :disabled => (repository && !repository.new_record?)))
Chris@0: end
Chris@0:
Chris@0: def mercurial_field_tags(form, repository)
Chris@0: content_tag('p', form.text_field(:url, :label => 'Root directory', :size => 60, :required => true, :disabled => (repository && !repository.root_url.blank?)))
Chris@0: end
Chris@0:
Chris@0: def git_field_tags(form, repository)
Chris@0: content_tag('p', form.text_field(:url, :label => 'Path to .git directory', :size => 60, :required => true, :disabled => (repository && !repository.root_url.blank?)))
Chris@0: end
Chris@0:
Chris@0: def cvs_field_tags(form, repository)
Chris@0: content_tag('p', form.text_field(:root_url, :label => 'CVSROOT', :size => 60, :required => true, :disabled => !repository.new_record?)) +
Chris@0: content_tag('p', form.text_field(:url, :label => 'Module', :size => 30, :required => true, :disabled => !repository.new_record?))
Chris@0: end
Chris@0:
Chris@0: def bazaar_field_tags(form, repository)
Chris@0: content_tag('p', form.text_field(:url, :label => 'Root directory', :size => 60, :required => true, :disabled => (repository && !repository.new_record?)))
Chris@0: end
Chris@0:
Chris@0: def filesystem_field_tags(form, repository)
Chris@0: content_tag('p', form.text_field(:url, :label => 'Root directory', :size => 60, :required => true, :disabled => (repository && !repository.root_url.blank?)))
Chris@0: end
Chris@0: end