rosa-build/app/helpers/git_helper.rb

97 lines
2.6 KiB
Ruby
Raw Normal View History

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"
res = "#{link_to @project.name, tree_path(@project)} / "
2011-04-01 01:36:34 +01:00
else
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
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)
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
res = "#{link_to @project.name, tree_path(@project)} /"
2011-04-01 01:36:34 +01:00
end
2012-02-04 20:12:37 +00:00
res.encode_to_default.html_safe
2011-04-01 01:36:34 +01:00
end
def blob_file_path
if @commit_hash.present?
blob_commit_path(@project, @commit_hash, @path)
else
blob_path(@project, @treeish, @path)
end
end
2011-04-01 01:36:34 +01:00
def render_line_numbers(n)
res = ""
1.upto(n) {|i| res += "<span>#{i}</span><br/>" }
2011-04-01 01:36:34 +01:00
res.html_safe
2011-04-01 01:36:34 +01:00
end
def render_blob(blob)
res = ""
2012-02-06 13:58:27 +00:00
blob.data.encode_to_default.split("\n").collect do |line|
2012-02-04 20:12:37 +00:00
"<div>#{line.present? ? h(line) : "<br>"}</div>"
end.join
2011-04-01 01:36:34 +01:00
end
def choose_render_way(blob)
return :image if blob.mime_type.match(/image/)
return :binary if blob.binary?
:text
# return :text if blob.mime_type.match(/text|xml|json/)
# :binary
end
2012-02-04 20:12:37 +00:00
def force_encoding_to_site(string)
2012-02-06 13:58:27 +00:00
string.dup.encode_to_default
2012-02-04 20:12:37 +00:00
end
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
# TODO This is very dirty hack. Maybe need to be changed.
def branch_selector_options(project)
2012-03-02 16:48:19 +00:00
tmp = params.dup
unless tmp['treeish'].present?
tmp.merge!('project_id' => project.id, 'treeish' => project.default_branch).delete('id')
end
2012-03-02 16:48:19 +00:00
tmp.delete('treeish') if tmp['commit_hash'].present?
res = {}
current = url_for(tmp).split('?', 2).first
2012-03-02 16:48:19 +00:00
tmp['commit_hash'] = truncate(tmp['commit_hash'], :length => 20) if tmp['commit_hash']
res = project.branches.inject(res) do |h, branch|
2012-03-02 16:48:19 +00:00
h[truncate(branch.name, :length => 20)] = url_for(tmp.merge('treeish' => branch.name)).split('?', 2).first
h
end
2012-03-02 16:48:19 +00:00
res.merge!(tmp['commit_hash'] || tmp['treeish'] => current)
options_for_select(res.sort, current).html_safe
end
end