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