2006-12-24 16:38:45 +03:00
|
|
|
# redMine - project management software
|
|
|
|
# Copyright (C) 2006 Jean-Philippe Lang
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2007-10-07 19:21:40 +04:00
|
|
|
require 'iconv'
|
2007-08-16 00:20:18 +04:00
|
|
|
|
2006-12-24 16:38:45 +03:00
|
|
|
module RepositoriesHelper
|
2011-01-02 12:45:05 +03:00
|
|
|
def format_revision(revision)
|
|
|
|
if revision.respond_to? :format_identifier
|
|
|
|
revision.format_identifier
|
|
|
|
else
|
|
|
|
revision.to_s
|
|
|
|
end
|
2008-03-12 23:28:49 +03:00
|
|
|
end
|
2011-03-03 06:30:10 +03:00
|
|
|
|
2008-09-22 23:50:10 +04:00
|
|
|
def truncate_at_line_break(text, length = 255)
|
|
|
|
if text
|
|
|
|
text.gsub(%r{^(.{#{length}}[^\n]*)\n.+$}m, '\\1...')
|
|
|
|
end
|
|
|
|
end
|
2011-03-03 06:30:10 +03:00
|
|
|
|
2008-07-05 12:59:04 +04:00
|
|
|
def render_properties(properties)
|
|
|
|
unless properties.nil? || properties.empty?
|
|
|
|
content = ''
|
|
|
|
properties.keys.sort.each do |property|
|
|
|
|
content << content_tag('li', "<b>#{h property}</b>: <span>#{h properties[property]}</span>")
|
|
|
|
end
|
|
|
|
content_tag('ul', content, :class => 'properties')
|
|
|
|
end
|
|
|
|
end
|
2011-03-03 06:30:10 +03:00
|
|
|
|
2008-09-17 20:39:23 +04:00
|
|
|
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?
|
|
|
|
change.action = @changeset.changes.detect {|c| c.action == 'D' && c.path == change.from_path} ? 'R' : 'C'
|
|
|
|
end
|
|
|
|
change
|
|
|
|
when 'D'
|
|
|
|
@changeset.changes.detect {|c| c.from_path == change.path} ? nil : change
|
|
|
|
else
|
|
|
|
change
|
|
|
|
end
|
2010-04-11 19:18:49 +04:00
|
|
|
end.compact
|
2008-09-17 20:39:23 +04:00
|
|
|
|
|
|
|
tree = { }
|
|
|
|
changes.each do |change|
|
|
|
|
p = tree
|
|
|
|
dirs = change.path.to_s.split('/').select {|d| !d.blank?}
|
2010-04-11 19:18:49 +04:00
|
|
|
path = ''
|
2008-09-17 20:39:23 +04:00
|
|
|
dirs.each do |dir|
|
2010-04-11 19:18:49 +04:00
|
|
|
path += '/' + dir
|
2008-09-17 20:39:23 +04:00
|
|
|
p[:s] ||= {}
|
|
|
|
p = p[:s]
|
2010-04-11 19:18:49 +04:00
|
|
|
p[path] ||= {}
|
|
|
|
p = p[path]
|
2008-09-17 20:39:23 +04:00
|
|
|
end
|
|
|
|
p[:c] = change
|
|
|
|
end
|
|
|
|
|
|
|
|
render_changes_tree(tree[:s])
|
|
|
|
end
|
2011-03-03 06:30:10 +03:00
|
|
|
|
2008-09-17 20:39:23 +04:00
|
|
|
def render_changes_tree(tree)
|
|
|
|
return '' if tree.nil?
|
2011-03-03 06:30:10 +03:00
|
|
|
|
2008-09-17 20:39:23 +04:00
|
|
|
output = ''
|
|
|
|
output << '<ul>'
|
|
|
|
tree.keys.sort.each do |file|
|
|
|
|
style = 'change'
|
2010-04-11 19:18:49 +04:00
|
|
|
text = File.basename(h(file))
|
|
|
|
if s = tree[file][:s]
|
|
|
|
style << ' folder'
|
|
|
|
path_param = to_path_param(@repository.relative_path(file))
|
|
|
|
text = link_to(text, :controller => 'repositories',
|
|
|
|
:action => 'show',
|
|
|
|
:id => @project,
|
|
|
|
:path => path_param,
|
2011-01-02 12:45:05 +03:00
|
|
|
:rev => @changeset.identifier)
|
2010-04-11 19:18:49 +04:00
|
|
|
output << "<li class='#{style}'>#{text}</li>"
|
|
|
|
output << render_changes_tree(s)
|
|
|
|
elsif c = tree[file][:c]
|
|
|
|
style << " change-#{c.action}"
|
2008-09-17 20:39:23 +04:00
|
|
|
path_param = to_path_param(@repository.relative_path(c.path))
|
|
|
|
text = link_to(text, :controller => 'repositories',
|
|
|
|
:action => 'entry',
|
|
|
|
:id => @project,
|
|
|
|
:path => path_param,
|
2011-01-02 12:45:05 +03:00
|
|
|
:rev => @changeset.identifier) unless c.action == 'D'
|
2008-09-17 20:39:23 +04:00
|
|
|
text << " - #{c.revision}" unless c.revision.blank?
|
|
|
|
text << ' (' + link_to('diff', :controller => 'repositories',
|
|
|
|
:action => 'diff',
|
|
|
|
:id => @project,
|
|
|
|
:path => path_param,
|
2011-01-02 12:45:05 +03:00
|
|
|
:rev => @changeset.identifier) + ') ' if c.action == 'M'
|
2008-09-17 20:39:23 +04:00
|
|
|
text << ' ' + content_tag('span', c.from_path, :class => 'copied-from') unless c.from_path.blank?
|
2010-04-11 19:18:49 +04:00
|
|
|
output << "<li class='#{style}'>#{text}</li>"
|
2008-09-17 20:39:23 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
output << '</ul>'
|
|
|
|
output
|
|
|
|
end
|
2011-03-03 06:30:10 +03:00
|
|
|
|
2007-10-07 19:21:40 +04:00
|
|
|
def to_utf8(str)
|
2011-02-28 11:03:43 +03:00
|
|
|
return str if str.blank?
|
2011-01-01 22:12:35 +03:00
|
|
|
if str.respond_to?(:force_encoding)
|
|
|
|
str.force_encoding('UTF-8')
|
|
|
|
else
|
2011-02-22 11:29:35 +03:00
|
|
|
# TODO:
|
2011-03-03 06:30:10 +03:00
|
|
|
# Japanese Shift_JIS(CP932) is not compatible with ASCII.
|
2011-02-22 11:29:35 +03:00
|
|
|
# UTF-7 and Japanese ISO-2022-JP are 7bits clean.
|
2011-01-01 22:12:35 +03:00
|
|
|
return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
|
|
|
|
end
|
2011-02-22 11:29:35 +03:00
|
|
|
|
2007-10-07 19:21:40 +04:00
|
|
|
@encodings ||= Setting.repositories_encodings.split(',').collect(&:strip)
|
|
|
|
@encodings.each do |encoding|
|
|
|
|
begin
|
|
|
|
return Iconv.conv('UTF-8', encoding, str)
|
|
|
|
rescue Iconv::Failure
|
|
|
|
# do nothing here and try the next encoding
|
|
|
|
end
|
|
|
|
end
|
2011-02-22 11:16:05 +03:00
|
|
|
str = replace_invalid_utf8(str)
|
|
|
|
end
|
|
|
|
|
|
|
|
def replace_invalid_utf8(str)
|
2011-02-21 17:28:41 +03:00
|
|
|
if str.respond_to?(:force_encoding)
|
2011-02-22 11:16:05 +03:00
|
|
|
str.force_encoding('UTF-8')
|
|
|
|
if ! str.valid_encoding?
|
|
|
|
str = str.encode("US-ASCII", :invalid => :replace,
|
2011-02-21 17:28:41 +03:00
|
|
|
:undef => :replace, :replace => '?').encode("UTF-8")
|
2011-02-22 11:16:05 +03:00
|
|
|
end
|
2011-02-21 17:28:41 +03:00
|
|
|
end
|
2007-10-07 19:21:40 +04:00
|
|
|
str
|
|
|
|
end
|
2011-02-22 11:16:05 +03:00
|
|
|
|
|
|
|
def repository_field_tags(form, repository)
|
2007-06-13 00:12:05 +04:00
|
|
|
method = repository.class.name.demodulize.underscore + "_field_tags"
|
2011-03-02 08:12:15 +03:00
|
|
|
if repository.is_a?(Repository) &&
|
|
|
|
respond_to?(method) && method != 'repository_field_tags'
|
|
|
|
send(method, form, repository)
|
|
|
|
end
|
2007-06-13 00:12:05 +04:00
|
|
|
end
|
2011-03-02 08:12:15 +03:00
|
|
|
|
2007-09-14 15:34:08 +04:00
|
|
|
def scm_select_tag(repository)
|
2008-06-08 18:59:26 +04:00
|
|
|
scm_options = [["--- #{l(:actionview_instancetag_blank_option)} ---", '']]
|
2010-02-17 01:41:59 +03:00
|
|
|
Redmine::Scm::Base.all.each do |scm|
|
2011-03-02 08:12:15 +03:00
|
|
|
if Setting.enabled_scm.include?(scm) ||
|
|
|
|
(repository && repository.class.name.demodulize == scm)
|
|
|
|
scm_options << ["Repository::#{scm}".constantize.scm_name, scm]
|
|
|
|
end
|
2008-06-08 18:59:26 +04:00
|
|
|
end
|
2007-06-13 00:12:05 +04:00
|
|
|
select_tag('repository_scm',
|
2008-06-08 18:59:26 +04:00
|
|
|
options_for_select(scm_options, repository.class.name.demodulize),
|
2007-09-14 15:34:08 +04:00
|
|
|
:disabled => (repository && !repository.new_record?),
|
2011-03-02 08:12:15 +03:00
|
|
|
:onchange => remote_function(
|
|
|
|
:url => {
|
|
|
|
:controller => 'repositories',
|
|
|
|
:action => 'edit',
|
|
|
|
:id => @project
|
|
|
|
},
|
|
|
|
:method => :get,
|
|
|
|
:with => "Form.serialize(this.form)")
|
2007-06-13 00:12:05 +04:00
|
|
|
)
|
|
|
|
end
|
2011-02-24 08:59:21 +03:00
|
|
|
|
2007-06-13 00:12:05 +04:00
|
|
|
def with_leading_slash(path)
|
2008-04-04 02:29:58 +04:00
|
|
|
path.to_s.starts_with?('/') ? path : "/#{path}"
|
|
|
|
end
|
2011-02-24 08:59:21 +03:00
|
|
|
|
2008-04-04 02:29:58 +04:00
|
|
|
def without_leading_slash(path)
|
|
|
|
path.gsub(%r{^/+}, '')
|
2007-06-13 00:12:05 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def subversion_field_tags(form, repository)
|
2011-02-24 08:59:21 +03:00
|
|
|
content_tag('p', form.text_field(:url, :size => 60, :required => true,
|
|
|
|
:disabled => (repository && !repository.root_url.blank?)) +
|
2009-05-10 14:08:02 +04:00
|
|
|
'<br />(file:///, http://, https://, svn://, svn+[tunnelscheme]://)') +
|
2007-06-13 00:12:05 +04:00
|
|
|
content_tag('p', form.text_field(:login, :size => 30)) +
|
2011-03-03 06:30:10 +03:00
|
|
|
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]';"))
|
2007-06-13 00:12:05 +04:00
|
|
|
end
|
|
|
|
|
2007-06-24 23:30:38 +04:00
|
|
|
def darcs_field_tags(form, repository)
|
2011-02-24 08:59:21 +03:00
|
|
|
content_tag('p', form.text_field(:url, :label => 'Root directory',
|
|
|
|
:size => 60, :required => true,
|
2011-03-01 13:27:30 +03:00
|
|
|
:disabled => (repository && !repository.new_record?))) +
|
|
|
|
content_tag('p', form.select(:log_encoding, [nil] + Setting::ENCODINGS,
|
|
|
|
:label => 'Commit messages encoding', :required => true))
|
2007-06-24 23:30:38 +04:00
|
|
|
end
|
2011-02-24 08:59:21 +03:00
|
|
|
|
2007-06-13 00:12:05 +04:00
|
|
|
def mercurial_field_tags(form, repository)
|
2011-03-04 16:39:00 +03:00
|
|
|
content_tag('p', form.text_field(:url, :label => 'Root directory',
|
2011-02-24 08:59:21 +03:00
|
|
|
:size => 60, :required => true,
|
2011-03-01 07:43:30 +03:00
|
|
|
:disabled => (repository && !repository.root_url.blank?)) +
|
2011-03-04 16:39:00 +03:00
|
|
|
'<br />local repository (e.g. /hgrepo, c:\hgrepo)' ) +
|
|
|
|
content_tag('p', form.select(
|
|
|
|
:path_encoding, [nil] + Setting::ENCODINGS,
|
|
|
|
:label => 'Path encoding') +
|
|
|
|
'<br />Default: UTF-8')
|
2007-06-13 00:12:05 +04:00
|
|
|
end
|
|
|
|
|
2008-03-12 23:28:49 +03:00
|
|
|
def git_field_tags(form, repository)
|
2011-02-19 09:42:50 +03:00
|
|
|
content_tag('p', form.text_field(:url, :label => 'Path to repository',
|
|
|
|
:size => 60, :required => true,
|
2011-03-01 07:43:30 +03:00
|
|
|
:disabled => (repository && !repository.root_url.blank?)) +
|
|
|
|
'<br />a bare and local repository (e.g. /gitrepo, c:\gitrepo)')
|
2008-03-12 23:28:49 +03:00
|
|
|
end
|
|
|
|
|
2007-06-13 00:12:05 +04:00
|
|
|
def cvs_field_tags(form, repository)
|
2011-02-24 08:59:21 +03:00
|
|
|
content_tag('p', form.text_field(:root_url,
|
|
|
|
:label => 'CVSROOT', :size => 60, :required => true,
|
|
|
|
:disabled => !repository.new_record?)) +
|
|
|
|
content_tag('p', form.text_field(:url, :label => 'Module',
|
|
|
|
:size => 30, :required => true,
|
2011-03-01 13:27:30 +03:00
|
|
|
:disabled => !repository.new_record?)) +
|
|
|
|
content_tag('p', form.select(:log_encoding, [nil] + Setting::ENCODINGS,
|
|
|
|
:label => 'Commit messages encoding', :required => true))
|
2007-06-13 00:12:05 +04:00
|
|
|
end
|
2007-12-03 20:40:43 +03:00
|
|
|
|
|
|
|
def bazaar_field_tags(form, repository)
|
2011-02-24 08:59:21 +03:00
|
|
|
content_tag('p', form.text_field(:url, :label => 'Root directory',
|
|
|
|
:size => 60, :required => true,
|
2011-03-01 13:27:30 +03:00
|
|
|
:disabled => (repository && !repository.new_record?))) +
|
|
|
|
content_tag('p', form.select(:log_encoding, [nil] + Setting::ENCODINGS,
|
|
|
|
:label => 'Commit messages encoding', :required => true))
|
2007-12-03 20:40:43 +03:00
|
|
|
end
|
2011-02-24 08:59:21 +03:00
|
|
|
|
2008-06-08 19:40:24 +04:00
|
|
|
def filesystem_field_tags(form, repository)
|
2011-02-24 08:59:21 +03:00
|
|
|
content_tag('p', form.text_field(:url, :label => 'Root directory',
|
|
|
|
:size => 60, :required => true,
|
2011-02-24 10:00:41 +03:00
|
|
|
:disabled => (repository && !repository.root_url.blank?))) +
|
2011-03-02 10:11:00 +03:00
|
|
|
content_tag('p', form.select(
|
|
|
|
:path_encoding, [nil] + Setting::ENCODINGS,
|
|
|
|
:label => 'Path encoding') +
|
|
|
|
'<br />Default: UTF-8')
|
2008-06-08 19:40:24 +04:00
|
|
|
end
|
2006-12-24 16:38:45 +03:00
|
|
|
end
|