Chris@909: # encoding: utf-8
Chris@909: #
Chris@441: # Redmine - project management software
Chris@1115: # Copyright (C) 2006-2012 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@441: #
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@441: #
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: module RepositoriesHelper
Chris@119: def format_revision(revision)
Chris@119: if revision.respond_to? :format_identifier
Chris@119: revision.format_identifier
Chris@119: else
Chris@119: revision.to_s
Chris@119: end
Chris@0: end
Chris@245:
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@245:
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@909: content << content_tag('li', "#{h property}: #{h properties[property]}".html_safe)
Chris@0: end
Chris@909: content_tag('ul', content.html_safe, :class => 'properties')
Chris@0: end
Chris@0: end
Chris@245:
Chris@0: def render_changeset_changes
Chris@1115: changes = @changeset.filechanges.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@441: change.action =
Chris@1115: @changeset.filechanges.detect {|c| c.action == 'D' && c.path == change.from_path} ? 'R' : 'C'
Chris@0: end
Chris@0: change
Chris@0: when 'D'
Chris@1115: @changeset.filechanges.detect {|c| c.from_path == change.path} ? nil : change
Chris@0: else
Chris@0: change
Chris@0: end
Chris@441: end.compact
Chris@441:
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: render_changes_tree(tree[:s])
Chris@0: end
Chris@245:
Chris@0: def render_changes_tree(tree)
Chris@0: return '' if tree.nil?
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@909: text = link_to(h(text), :controller => 'repositories',
Chris@0: :action => 'show',
Chris@0: :id => @project,
Chris@1115: :repository_id => @repository.identifier_param,
Chris@0: :path => path_param,
Chris@119: :rev => @changeset.identifier)
Chris@1115: output << "- #{text}"
Chris@0: output << render_changes_tree(s)
Chris@1115: output << "
"
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@909: text = link_to(h(text), :controller => 'repositories',
Chris@0: :action => 'entry',
Chris@0: :id => @project,
Chris@1115: :repository_id => @repository.identifier_param,
Chris@0: :path => path_param,
Chris@119: :rev => @changeset.identifier) unless c.action == 'D'
Chris@909: text << " - #{h(c.revision)}" unless c.revision.blank?
Chris@909: text << ' ('.html_safe + link_to(l(:label_diff), :controller => 'repositories',
Chris@0: :action => 'diff',
Chris@0: :id => @project,
Chris@1115: :repository_id => @repository.identifier_param,
Chris@0: :path => path_param,
Chris@909: :rev => @changeset.identifier) + ') '.html_safe if c.action == 'M'
Chris@909: text << ' '.html_safe + content_tag('span', h(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@909: output.html_safe
Chris@0: end
Chris@245:
Chris@245: def repository_field_tags(form, repository)
Chris@0: method = repository.class.name.demodulize.underscore + "_field_tags"
Chris@245: if repository.is_a?(Repository) &&
Chris@245: respond_to?(method) && method != 'repository_field_tags'
Chris@245: send(method, form, repository)
Chris@245: end
Chris@0: end
Chris@245:
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@245: if Setting.enabled_scm.include?(scm) ||
Chris@245: (repository && repository.class.name.demodulize == scm)
Chris@245: scm_options << ["Repository::#{scm}".constantize.scm_name, scm]
Chris@245: end
Chris@0: end
Chris@441: select_tag('repository_scm',
Chris@0: options_for_select(scm_options, repository.class.name.demodulize),
Chris@0: :disabled => (repository && !repository.new_record?),
Chris@1115: :data => {:remote => true, :method => 'get'})
Chris@0: end
Chris@245:
Chris@0: def with_leading_slash(path)
Chris@0: path.to_s.starts_with?('/') ? path : "/#{path}"
Chris@0: end
Chris@245:
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@245: content_tag('p', form.text_field(:url, :size => 60, :required => true,
Chris@1115: :disabled => !repository.safe_attribute?('url')) +
Chris@909: '
'.html_safe +
Chris@909: '(file:///, http://, https://, svn://, svn+[tunnelscheme]://)') +
Chris@0: content_tag('p', form.text_field(:login, :size => 30)) +
Chris@245: content_tag('p', form.password_field(
Chris@245: :password, :size => 30, :name => 'ignore',
Chris@245: :value => ((repository.new_record? || repository.password.blank?) ? '' : ('x'*15)),
Chris@245: :onfocus => "this.value=''; this.name='repository[password]';",
Chris@245: :onchange => "this.name='repository[password]';"))
Chris@0: end
Chris@0:
Chris@0: def darcs_field_tags(form, repository)
Chris@441: content_tag('p', form.text_field(
Chris@441: :url, :label => l(:field_path_to_repository),
Chris@441: :size => 60, :required => true,
Chris@1115: :disabled => !repository.safe_attribute?('url'))) +
Chris@441: content_tag('p', form.select(
Chris@441: :log_encoding, [nil] + Setting::ENCODINGS,
Chris@441: :label => l(:field_commit_logs_encoding), :required => true))
Chris@0: end
Chris@245:
Chris@0: def mercurial_field_tags(form, repository)
Chris@441: content_tag('p', form.text_field(
Chris@441: :url, :label => l(:field_path_to_repository),
Chris@245: :size => 60, :required => true,
Chris@1115: :disabled => !repository.safe_attribute?('url')
Chris@441: ) +
Chris@909: '
'.html_safe + l(:text_mercurial_repository_note)) +
Chris@441: content_tag('p', form.select(
Chris@441: :path_encoding, [nil] + Setting::ENCODINGS,
Chris@441: :label => l(:field_scm_path_encoding)
Chris@441: ) +
Chris@909: '
'.html_safe + l(:text_scm_path_encoding_note))
Chris@0: end
Chris@0:
Chris@0: def git_field_tags(form, repository)
Chris@441: content_tag('p', form.text_field(
Chris@441: :url, :label => l(:field_path_to_repository),
Chris@245: :size => 60, :required => true,
Chris@1115: :disabled => !repository.safe_attribute?('url')
Chris@441: ) +
Chris@909: '
'.html_safe +
Chris@909: l(:text_git_repository_note)) +
Chris@441: content_tag('p', form.select(
Chris@441: :path_encoding, [nil] + Setting::ENCODINGS,
Chris@441: :label => l(:field_scm_path_encoding)
Chris@441: ) +
Chris@909: '
'.html_safe + l(:text_scm_path_encoding_note)) +
Chris@441: content_tag('p', form.check_box(
Chris@441: :extra_report_last_commit,
Chris@441: :label => l(:label_git_report_last_commit)
Chris@441: ))
Chris@0: end
Chris@0:
Chris@0: def cvs_field_tags(form, repository)
Chris@441: content_tag('p', form.text_field(
Chris@441: :root_url,
Chris@441: :label => l(:field_cvsroot),
Chris@441: :size => 60, :required => true,
Chris@1115: :disabled => !repository.safe_attribute?('root_url'))) +
Chris@441: content_tag('p', form.text_field(
Chris@441: :url,
Chris@441: :label => l(:field_cvs_module),
Chris@441: :size => 30, :required => true,
Chris@1115: :disabled => !repository.safe_attribute?('url'))) +
Chris@441: content_tag('p', form.select(
Chris@441: :log_encoding, [nil] + Setting::ENCODINGS,
Chris@441: :label => l(:field_commit_logs_encoding), :required => true)) +
Chris@441: content_tag('p', form.select(
Chris@441: :path_encoding, [nil] + Setting::ENCODINGS,
Chris@441: :label => l(:field_scm_path_encoding)
Chris@441: ) +
Chris@909: '
'.html_safe + l(:text_scm_path_encoding_note))
Chris@0: end
Chris@0:
Chris@0: def bazaar_field_tags(form, repository)
Chris@441: content_tag('p', form.text_field(
Chris@441: :url, :label => l(:field_path_to_repository),
Chris@441: :size => 60, :required => true,
Chris@1115: :disabled => !repository.safe_attribute?('url'))) +
Chris@441: content_tag('p', form.select(
Chris@441: :log_encoding, [nil] + Setting::ENCODINGS,
Chris@441: :label => l(:field_commit_logs_encoding), :required => true))
Chris@0: end
Chris@245:
Chris@0: def filesystem_field_tags(form, repository)
Chris@441: content_tag('p', form.text_field(
Chris@441: :url, :label => l(:field_root_directory),
Chris@245: :size => 60, :required => true,
Chris@1115: :disabled => !repository.safe_attribute?('url'))) +
Chris@245: content_tag('p', form.select(
Chris@245: :path_encoding, [nil] + Setting::ENCODINGS,
Chris@441: :label => l(:field_scm_path_encoding)
Chris@441: ) +
Chris@909: '
'.html_safe + l(:text_scm_path_encoding_note))
Chris@909: end
Chris@909:
Chris@1115: def index_commits(commits, heads)
Chris@909: return nil if commits.nil? or commits.first.parents.nil?
Chris@909: refs_map = {}
Chris@1115: heads.each do |head|
Chris@1115: refs_map[head.scmid] ||= []
Chris@1115: refs_map[head.scmid] << head
Chris@909: end
Chris@1115: commits_by_scmid = {}
Chris@1115: commits.reverse.each_with_index do |commit, commit_index|
Chris@1115: commits_by_scmid[commit.scmid] = {
Chris@1115: :parent_scmids => commit.parents.collect { |parent| parent.scmid },
Chris@1115: :rdmid => commit_index,
Chris@1115: :refs => refs_map.include?(commit.scmid) ? refs_map[commit.scmid].join(" ") : nil,
Chris@1115: :scmid => commit.scmid,
Chris@1115: :href => block_given? ? yield(commit.scmid) : commit.scmid
Chris@1115: }
Chris@909: end
Chris@1115: heads.sort! { |head1, head2| head1.to_s <=> head2.to_s }
Chris@1115: space = nil
Chris@1115: heads.each do |head|
Chris@1115: if commits_by_scmid.include? head.scmid
Chris@1115: space = index_head((space || -1) + 1, head, commits_by_scmid)
Chris@909: end
Chris@909: end
Chris@909: # when no head matched anything use first commit
Chris@1115: space ||= index_head(0, commits.first, commits_by_scmid)
Chris@1115: return commits_by_scmid, space
Chris@909: end
Chris@909:
Chris@1115: def index_head(space, commit, commits_by_scmid)
Chris@1115: stack = [[space, commits_by_scmid[commit.scmid]]]
Chris@1115: max_space = space
Chris@909: until stack.empty?
Chris@1115: space, commit = stack.pop
Chris@1115: commit[:space] = space if commit[:space].nil?
Chris@1115: space -= 1
Chris@1115: commit[:parent_scmids].each_with_index do |parent_scmid, parent_index|
Chris@1115: parent_commit = commits_by_scmid[parent_scmid]
Chris@1115: if parent_commit and parent_commit[:space].nil?
Chris@1115: stack.unshift [space += 1, parent_commit]
Chris@909: end
Chris@909: end
Chris@1115: max_space = space if max_space < space
Chris@909: end
Chris@1115: max_space
Chris@0: end
chris@741:
chris@741: # Generates a link to a downloadable archive for a revision
chris@741: # Options:
chris@741: # * :text - Link text (default to the formatted revision)
chris@741: def link_to_revision_archive(repository, revision, project, options={})
chris@741: method = repository.class.name.demodulize.underscore + "_link_to_revision_archive"
chris@741: if repository.is_a?(Repository) &&
chris@741: respond_to?(method) && method != 'link_to_revision_archive'
chris@741: send(method, repository, revision, project, options)
chris@741: end
chris@741: end
chris@741:
chris@741: def mercurial_link_to_revision_archive(repository, revision, project, options={})
chris@741: text = options.delete(:text) || format_revision(revision)
chris@741: rev = revision.respond_to?(:identifier) ? revision.identifier : revision
chris@741: if rev.blank? then rev = 'tip' end
chris@741: content_tag('a', h(text),
chris@741: { :href => "/hg/#{project.identifier}/archive/#{rev}.zip" }.merge(options));
chris@741: end
chris@741:
Chris@0: end