2012-01-30 20:39:34 +00:00
|
|
|
# -*- encoding : utf-8 -*-
|
2011-04-01 01:36:34 +01:00
|
|
|
module GitHelper
|
|
|
|
|
|
|
|
def render_path
|
|
|
|
# TODO: Looks ugly, rewrite with clear mind.
|
|
|
|
if @path.present?
|
|
|
|
if @treeish == "master"
|
2011-10-24 11:19:04 +01:00
|
|
|
res = "#{link_to @project.name, tree_path(@project)} / "
|
2011-04-01 01:36:34 +01:00
|
|
|
else
|
2011-10-24 11:19:04 +01:00
|
|
|
res = "#{link_to @project.name, tree_path(@project, @treeish)} / "
|
2011-04-01 01:36:34 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
parts = @path.split("/")
|
|
|
|
|
|
|
|
current_path = parts.first
|
2011-10-24 11:19:04 +01:00
|
|
|
res += parts.length == 1 ? parts.first : link_to(parts.first, tree_path(@project, @treeish, current_path)) + " / "
|
2011-04-01 01:36:34 +01:00
|
|
|
|
|
|
|
parts[1..-2].each do |part|
|
|
|
|
current_path = File.join([current_path, part].compact)
|
2011-10-24 11:19:04 +01:00
|
|
|
res += link_to(part, tree_path(@project, @treeish, current_path))
|
2011-04-01 01:36:34 +01:00
|
|
|
res += " / "
|
|
|
|
end
|
|
|
|
|
|
|
|
res += parts.last if parts.length > 1
|
|
|
|
else
|
2011-10-24 11:19:04 +01:00
|
|
|
res = "#{link_to @project.name, tree_path(@project)} /"
|
2011-04-01 01:36:34 +01:00
|
|
|
end
|
|
|
|
|
2012-03-21 19:55:14 +00:00
|
|
|
res.html_safe
|
2012-02-08 17:51:30 +00:00
|
|
|
end
|
|
|
|
|
2011-04-01 01:36:34 +01:00
|
|
|
def render_line_numbers(n)
|
|
|
|
res = ""
|
2012-03-01 17:37:24 +00:00
|
|
|
1.upto(n) {|i| res += "<span>#{i}</span><br/>" }
|
2011-04-01 01:36:34 +01:00
|
|
|
|
2012-03-01 17:37:24 +00:00
|
|
|
res.html_safe
|
2011-04-01 01:36:34 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def render_blob(blob)
|
2012-03-21 19:55:14 +00:00
|
|
|
blob.data.split("\n").collect do |line|
|
|
|
|
content_tag :div, line.present? ? h(line) : tag(:br)
|
|
|
|
end.join.html_safe
|
2011-04-01 01:36:34 +01:00
|
|
|
end
|
|
|
|
|
2012-01-20 18:31:01 +00:00
|
|
|
def choose_render_way(blob)
|
2012-03-21 19:55:14 +00:00
|
|
|
case
|
|
|
|
when blob.mime_type.match(/image/); :image
|
|
|
|
when blob.binary?; :binary
|
|
|
|
else
|
|
|
|
@text = @blob.data.split("\n")
|
|
|
|
:text
|
|
|
|
end
|
2012-02-04 20:12:37 +00:00
|
|
|
end
|
|
|
|
|
2012-02-21 15:41:16 +00:00
|
|
|
def iterate_path(path, &block)
|
|
|
|
path.split(File::SEPARATOR).inject('') do |a, e|
|
|
|
|
if e != '.' and e != '..'
|
|
|
|
a = File.join(a, e)
|
|
|
|
a = a[1..-1] if a[0] == File::SEPARATOR
|
|
|
|
block.call(a, e) if a.length > 1
|
|
|
|
end
|
|
|
|
a
|
|
|
|
end
|
|
|
|
end
|
2012-02-29 01:30:22 +00:00
|
|
|
|
|
|
|
# TODO This is very dirty hack. Maybe need to be changed.
|
|
|
|
def branch_selector_options(project)
|
2012-03-21 19:55:14 +00:00
|
|
|
p = params.dup
|
|
|
|
p.delete(:path) if p[:path].present? # to root path
|
|
|
|
p.merge!(:project_id => project.id, :treeish => project.default_branch).delete(:id) unless p[:treeish].present?
|
|
|
|
current = url_for(p).split('?', 2).first
|
2012-02-29 01:30:22 +00:00
|
|
|
|
2012-03-21 19:55:14 +00:00
|
|
|
res = []
|
|
|
|
res << [I18n.t('layout.git.repositories.commits'), [truncate(params[:treeish], :length => 20)]] unless (project.branches + project.tags).map(&:name).include?(params[:treeish] || project.default_branch)
|
|
|
|
res << [I18n.t('layout.git.repositories.branches'), project.branches.map{|b| [truncate(b.name, :length => 20), url_for(p.merge :treeish => b.name).split('?', 2).first]}]
|
|
|
|
res << [I18n.t('layout.git.repositories.tags'), project.tags.map{|t| [truncate(t.name, :length => 20), url_for(p.merge :treeish => t.name).split('?', 2).first]}]
|
2012-02-29 01:30:22 +00:00
|
|
|
|
2012-03-21 19:55:14 +00:00
|
|
|
grouped_options_for_select(res, current)
|
2012-02-29 01:30:22 +00:00
|
|
|
end
|
2012-01-20 18:31:01 +00:00
|
|
|
end
|