Chris@1295: # encoding: utf-8 Chris@1295: # Chris@1295: # Redmine - project management software Chris@1295: # Copyright (C) 2006-2012 Jean-Philippe Lang Chris@1295: # Chris@1295: # This program is free software; you can redistribute it and/or Chris@1295: # modify it under the terms of the GNU General Public License Chris@1295: # as published by the Free Software Foundation; either version 2 Chris@1295: # of the License, or (at your option) any later version. Chris@1295: # Chris@1295: # This program is distributed in the hope that it will be useful, Chris@1295: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1295: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1295: # GNU General Public License for more details. Chris@1295: # Chris@1295: # You should have received a copy of the GNU General Public License Chris@1295: # along with this program; if not, write to the Free Software Chris@1295: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1295: Chris@1295: module RepositoriesHelper Chris@1295: def format_revision(revision) Chris@1295: if revision.respond_to? :format_identifier Chris@1295: revision.format_identifier Chris@1295: else Chris@1295: revision.to_s Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def truncate_at_line_break(text, length = 255) Chris@1295: if text Chris@1295: text.gsub(%r{^(.{#{length}}[^\n]*)\n.+$}m, '\\1...') Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def render_properties(properties) Chris@1295: unless properties.nil? || properties.empty? Chris@1295: content = '' Chris@1295: properties.keys.sort.each do |property| Chris@1295: content << content_tag('li', "#{h property}: #{h properties[property]}".html_safe) Chris@1295: end Chris@1295: content_tag('ul', content.html_safe, :class => 'properties') Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def render_changeset_changes Chris@1295: changes = @changeset.filechanges.find(:all, :limit => 1000, :order => 'path').collect do |change| Chris@1295: case change.action Chris@1295: when 'A' Chris@1295: # Detects moved/copied files Chris@1295: if !change.from_path.blank? Chris@1295: change.action = Chris@1295: @changeset.filechanges.detect {|c| c.action == 'D' && c.path == change.from_path} ? 'R' : 'C' Chris@1295: end Chris@1295: change Chris@1295: when 'D' Chris@1295: @changeset.filechanges.detect {|c| c.from_path == change.path} ? nil : change Chris@1295: else Chris@1295: change Chris@1295: end Chris@1295: end.compact Chris@1295: Chris@1295: tree = { } Chris@1295: changes.each do |change| Chris@1295: p = tree Chris@1295: dirs = change.path.to_s.split('/').select {|d| !d.blank?} Chris@1295: path = '' Chris@1295: dirs.each do |dir| Chris@1295: path += '/' + dir Chris@1295: p[:s] ||= {} Chris@1295: p = p[:s] Chris@1295: p[path] ||= {} Chris@1295: p = p[path] Chris@1295: end Chris@1295: p[:c] = change Chris@1295: end Chris@1295: render_changes_tree(tree[:s]) Chris@1295: end Chris@1295: Chris@1295: def render_changes_tree(tree) Chris@1295: return '' if tree.nil? Chris@1295: output = '' Chris@1295: output << '' Chris@1295: output.html_safe Chris@1295: end Chris@1295: Chris@1295: def repository_field_tags(form, repository) Chris@1295: method = repository.class.name.demodulize.underscore + "_field_tags" Chris@1295: if repository.is_a?(Repository) && Chris@1295: respond_to?(method) && method != 'repository_field_tags' Chris@1295: send(method, form, repository) Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def scm_select_tag(repository) Chris@1295: scm_options = [["--- #{l(:actionview_instancetag_blank_option)} ---", '']] Chris@1295: Redmine::Scm::Base.all.each do |scm| Chris@1295: if Setting.enabled_scm.include?(scm) || Chris@1295: (repository && repository.class.name.demodulize == scm) Chris@1295: scm_options << ["Repository::#{scm}".constantize.scm_name, scm] Chris@1295: end Chris@1295: end Chris@1295: select_tag('repository_scm', Chris@1295: options_for_select(scm_options, repository.class.name.demodulize), Chris@1295: :disabled => (repository && !repository.new_record?), Chris@1295: :data => {:remote => true, :method => 'get'}) Chris@1295: end Chris@1295: Chris@1295: def with_leading_slash(path) Chris@1295: path.to_s.starts_with?('/') ? path : "/#{path}" Chris@1295: end Chris@1295: Chris@1295: def without_leading_slash(path) Chris@1295: path.gsub(%r{^/+}, '') Chris@1295: end Chris@1295: Chris@1295: def subversion_field_tags(form, repository) Chris@1295: content_tag('p', form.text_field(:url, :size => 60, :required => true, Chris@1295: :disabled => !repository.safe_attribute?('url')) + Chris@1295: '
'.html_safe + Chris@1295: '(file:///, http://, https://, svn://, svn+[tunnelscheme]://)') + Chris@1295: content_tag('p', form.text_field(:login, :size => 30)) + Chris@1295: content_tag('p', form.password_field( Chris@1295: :password, :size => 30, :name => 'ignore', Chris@1295: :value => ((repository.new_record? || repository.password.blank?) ? '' : ('x'*15)), Chris@1295: :onfocus => "this.value=''; this.name='repository[password]';", Chris@1295: :onchange => "this.name='repository[password]';")) Chris@1295: end Chris@1295: Chris@1295: def darcs_field_tags(form, repository) Chris@1295: content_tag('p', form.text_field( Chris@1295: :url, :label => l(:field_path_to_repository), Chris@1295: :size => 60, :required => true, Chris@1295: :disabled => !repository.safe_attribute?('url'))) + Chris@1295: content_tag('p', form.select( Chris@1295: :log_encoding, [nil] + Setting::ENCODINGS, Chris@1295: :label => l(:field_commit_logs_encoding), :required => true)) Chris@1295: end Chris@1295: Chris@1295: def mercurial_field_tags(form, repository) Chris@1295: content_tag('p', form.text_field( Chris@1295: :url, :label => l(:field_path_to_repository), Chris@1295: :size => 60, :required => true, Chris@1295: :disabled => !repository.safe_attribute?('url') Chris@1295: ) + Chris@1295: '
'.html_safe + l(:text_mercurial_repository_note)) + Chris@1295: content_tag('p', form.select( Chris@1295: :path_encoding, [nil] + Setting::ENCODINGS, Chris@1295: :label => l(:field_scm_path_encoding) Chris@1295: ) + Chris@1295: '
'.html_safe + l(:text_scm_path_encoding_note)) Chris@1295: end Chris@1295: Chris@1295: def git_field_tags(form, repository) Chris@1295: content_tag('p', form.text_field( Chris@1295: :url, :label => l(:field_path_to_repository), Chris@1295: :size => 60, :required => true, Chris@1295: :disabled => !repository.safe_attribute?('url') Chris@1295: ) + Chris@1295: '
'.html_safe + Chris@1295: l(:text_git_repository_note)) + Chris@1295: content_tag('p', form.select( Chris@1295: :path_encoding, [nil] + Setting::ENCODINGS, Chris@1295: :label => l(:field_scm_path_encoding) Chris@1295: ) + Chris@1295: '
'.html_safe + l(:text_scm_path_encoding_note)) + Chris@1295: content_tag('p', form.check_box( Chris@1295: :extra_report_last_commit, Chris@1295: :label => l(:label_git_report_last_commit) Chris@1295: )) Chris@1295: end Chris@1295: Chris@1295: def cvs_field_tags(form, repository) Chris@1295: content_tag('p', form.text_field( Chris@1295: :root_url, Chris@1295: :label => l(:field_cvsroot), Chris@1295: :size => 60, :required => true, Chris@1295: :disabled => !repository.safe_attribute?('root_url'))) + Chris@1295: content_tag('p', form.text_field( Chris@1295: :url, Chris@1295: :label => l(:field_cvs_module), Chris@1295: :size => 30, :required => true, Chris@1295: :disabled => !repository.safe_attribute?('url'))) + Chris@1295: content_tag('p', form.select( Chris@1295: :log_encoding, [nil] + Setting::ENCODINGS, Chris@1295: :label => l(:field_commit_logs_encoding), :required => true)) + Chris@1295: content_tag('p', form.select( Chris@1295: :path_encoding, [nil] + Setting::ENCODINGS, Chris@1295: :label => l(:field_scm_path_encoding) Chris@1295: ) + Chris@1295: '
'.html_safe + l(:text_scm_path_encoding_note)) Chris@1295: end Chris@1295: Chris@1295: def bazaar_field_tags(form, repository) Chris@1295: content_tag('p', form.text_field( Chris@1295: :url, :label => l(:field_path_to_repository), Chris@1295: :size => 60, :required => true, Chris@1295: :disabled => !repository.safe_attribute?('url'))) + Chris@1295: content_tag('p', form.select( Chris@1295: :log_encoding, [nil] + Setting::ENCODINGS, Chris@1295: :label => l(:field_commit_logs_encoding), :required => true)) Chris@1295: end Chris@1295: Chris@1295: def filesystem_field_tags(form, repository) Chris@1295: content_tag('p', form.text_field( Chris@1295: :url, :label => l(:field_root_directory), Chris@1295: :size => 60, :required => true, Chris@1295: :disabled => !repository.safe_attribute?('url'))) + Chris@1295: content_tag('p', form.select( Chris@1295: :path_encoding, [nil] + Setting::ENCODINGS, Chris@1295: :label => l(:field_scm_path_encoding) Chris@1295: ) + Chris@1295: '
'.html_safe + l(:text_scm_path_encoding_note)) Chris@1295: end Chris@1295: Chris@1295: def index_commits(commits, heads) Chris@1295: return nil if commits.nil? or commits.first.parents.nil? Chris@1295: refs_map = {} Chris@1295: heads.each do |head| Chris@1295: refs_map[head.scmid] ||= [] Chris@1295: refs_map[head.scmid] << head Chris@1295: end Chris@1295: commits_by_scmid = {} Chris@1295: commits.reverse.each_with_index do |commit, commit_index| Chris@1295: commits_by_scmid[commit.scmid] = { Chris@1295: :parent_scmids => commit.parents.collect { |parent| parent.scmid }, Chris@1295: :rdmid => commit_index, Chris@1295: :refs => refs_map.include?(commit.scmid) ? refs_map[commit.scmid].join(" ") : nil, Chris@1295: :scmid => commit.scmid, Chris@1295: :href => block_given? ? yield(commit.scmid) : commit.scmid Chris@1295: } Chris@1295: end Chris@1295: heads.sort! { |head1, head2| head1.to_s <=> head2.to_s } Chris@1295: space = nil Chris@1295: heads.each do |head| Chris@1295: if commits_by_scmid.include? head.scmid Chris@1295: space = index_head((space || -1) + 1, head, commits_by_scmid) Chris@1295: end Chris@1295: end Chris@1295: # when no head matched anything use first commit Chris@1295: space ||= index_head(0, commits.first, commits_by_scmid) Chris@1295: return commits_by_scmid, space Chris@1295: end Chris@1295: Chris@1295: def index_head(space, commit, commits_by_scmid) Chris@1295: stack = [[space, commits_by_scmid[commit.scmid]]] Chris@1295: max_space = space Chris@1295: until stack.empty? Chris@1295: space, commit = stack.pop Chris@1295: commit[:space] = space if commit[:space].nil? Chris@1295: space -= 1 Chris@1295: commit[:parent_scmids].each_with_index do |parent_scmid, parent_index| Chris@1295: parent_commit = commits_by_scmid[parent_scmid] Chris@1295: if parent_commit and parent_commit[:space].nil? Chris@1295: stack.unshift [space += 1, parent_commit] Chris@1295: end Chris@1295: end Chris@1295: max_space = space if max_space < space Chris@1295: end Chris@1295: max_space Chris@1295: end Chris@1295: end