2011-04-01 01:36:34 +01:00
|
|
|
module GitHelper
|
|
|
|
|
2013-04-11 12:19:24 +01:00
|
|
|
def submodule_url(node, treeish)
|
|
|
|
# node.url(treeish) looks like:
|
|
|
|
# - http://0.0.0.0:3000/abf/git@abf.rosalinux.ru:abf/rhel-scripts.git
|
|
|
|
# - git://github.com/avokhmin/mdv-scripts.git
|
2013-06-14 11:17:30 +01:00
|
|
|
# - empty string if ".gitmodules" does not exist
|
|
|
|
url = node.url(treeish)
|
|
|
|
return nil if url.blank?
|
2013-06-14 10:54:08 +01:00
|
|
|
url.gsub!(/.git$/, '')
|
2013-04-11 12:19:24 +01:00
|
|
|
if url =~ /^git:/
|
|
|
|
url.gsub!(/^git/, 'http')
|
2013-04-11 13:07:29 +01:00
|
|
|
elsif str = /git@.*:.*/.match(url)
|
|
|
|
str = str[0].gsub(/^git@/, '')
|
2013-04-11 12:19:24 +01:00
|
|
|
domen = str.gsub(/:.*/, '')
|
|
|
|
owner = str.gsub(/^#{domen}:/, '').gsub(/\/.*/, '')
|
|
|
|
project = str.gsub(/.*\//, '')
|
|
|
|
url = "http://#{domen}/#{owner}/#{project}"
|
|
|
|
end
|
|
|
|
url
|
|
|
|
end
|
|
|
|
|
2011-04-01 01:36:34 +01:00
|
|
|
def render_path
|
|
|
|
# TODO: Looks ugly, rewrite with clear mind.
|
|
|
|
if @path.present?
|
2012-07-17 09:02:56 +01:00
|
|
|
if @treeish == @project.default_branch
|
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
|
2013-12-30 16:08:42 +00: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)
|
2013-12-30 16:08:42 +00:00
|
|
|
res << link_to(part, tree_path(@project, @treeish, current_path))
|
|
|
|
res << " / "
|
2011-04-01 01:36:34 +01:00
|
|
|
end
|
|
|
|
|
2013-12-30 16:08:42 +00:00
|
|
|
res << parts.last if parts.length > 1
|
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)} /"
|
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 = ""
|
2013-12-30 16:08:42 +00:00
|
|
|
1.upto(n){ |i| res << "<span id='L#{i}'><a href='#L#{i}'>#{i}</a></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
|
|
|
|
|
2014-07-09 10:38:31 +01:00
|
|
|
def iterate_path(path)
|
|
|
|
tree = []
|
|
|
|
path.split("\/").each do |name|
|
|
|
|
if tree.last
|
|
|
|
tree << [File.join(tree.try(:last).try(:first), name), name]
|
|
|
|
else
|
|
|
|
tree << [name, name]
|
2012-02-21 15:41:16 +00:00
|
|
|
end
|
|
|
|
end
|
2014-07-09 10:38:31 +01:00
|
|
|
tree
|
2012-02-21 15:41:16 +00:00
|
|
|
end
|
2012-02-29 01:30:22 +00:00
|
|
|
|
|
|
|
def branch_selector_options(project)
|
2013-02-22 10:26:06 +00:00
|
|
|
p, tag_enabled = params.dup, !(controller_name == 'trees' && action_name == 'branches')
|
2012-03-21 19:55:14 +00:00
|
|
|
p.delete(:path) if p[:path].present? # to root path
|
2014-01-21 04:51:49 +00:00
|
|
|
p.merge!(project_id: project.id, treeish: project.default_branch).delete(:id) unless p[:treeish].present?
|
2012-03-21 19:55:14 +00:00
|
|
|
current = url_for(p).split('?', 2).first
|
2012-02-29 01:30:22 +00:00
|
|
|
|
2012-03-21 19:55:14 +00:00
|
|
|
res = []
|
2012-07-31 15:35:18 +01:00
|
|
|
if params[:treeish].present? && !project.repo.branches_and_tags.map(&:name).include?(params[:treeish])
|
|
|
|
res << [I18n.t('layout.git.repositories.commits'), [params[:treeish].truncate(20)]]
|
|
|
|
end
|
2014-01-21 04:51:49 +00:00
|
|
|
linking = Proc.new {|name| [name.truncate(20), url_for(p.merge treeish: name).split('?', 2).first]}
|
2013-04-22 18:29:02 +01:00
|
|
|
res << [I18n.t('layout.git.repositories.branches'), project.repo.branches.map(&:name).sort.map(&linking)]
|
2013-02-22 10:26:06 +00:00
|
|
|
if tag_enabled
|
2013-04-22 18:29:02 +01:00
|
|
|
res << [I18n.t('layout.git.repositories.tags'), project.repo.tags.map(&:name).sort.map(&linking)]
|
2013-02-22 09:34:12 +00:00
|
|
|
else
|
2014-01-21 04:51:49 +00:00
|
|
|
res << [I18n.t('layout.git.repositories.tags'), project.repo.tags.map(&:name).sort.map {|name| [name.truncate(20), {disabled: true}]}]
|
2013-02-22 09:34:12 +00:00
|
|
|
end
|
2012-03-21 19:55:14 +00:00
|
|
|
grouped_options_for_select(res, current)
|
2012-02-29 01:30:22 +00:00
|
|
|
end
|
2012-05-25 11:48:44 +01:00
|
|
|
|
2012-07-17 09:02:56 +01:00
|
|
|
def versions_for_group_select(project)
|
2013-03-27 12:18:24 +00:00
|
|
|
return [] unless project
|
2014-11-11 19:43:01 +00:00
|
|
|
[
|
|
|
|
[I18n.t('layout.git.repositories.branches'), project.repo.branches.map(&:name).sort],
|
|
|
|
[I18n.t('layout.git.repositories.tags'), project.repo.tags.map(&:name).sort]
|
|
|
|
]
|
2012-07-17 09:02:56 +01:00
|
|
|
end
|
2012-07-27 13:24:35 +01:00
|
|
|
|
2012-05-25 11:48:44 +01:00
|
|
|
def split_commits_by_date(commits)
|
2014-06-30 21:21:35 +01:00
|
|
|
# See: https://github.com/gitlabhq/gitlabhq/blob/master/app/views/projects/commits/_commits.html.haml#L1
|
|
|
|
commits.sort{|x, y| y.committed_date <=> x.committed_date}.inject({}) do |h, commit|
|
|
|
|
dt = commit.committed_date
|
2012-05-25 11:48:44 +01:00
|
|
|
h[dt.year] ||= {}
|
|
|
|
h[dt.year][dt.month] ||= {}
|
|
|
|
h[dt.year][dt.month][dt.day] ||= []
|
|
|
|
h[dt.year][dt.month][dt.day] << commit
|
|
|
|
h
|
|
|
|
end
|
|
|
|
end
|
2014-04-15 10:11:14 +01:00
|
|
|
|
|
|
|
def blob_highlight(blob)
|
2014-10-09 08:22:37 +01:00
|
|
|
return if blob.nil? || blob.data.blank?
|
2014-10-09 09:24:43 +01:00
|
|
|
result = if blob.mime_type == 'text/rpm-spec'
|
2014-11-10 18:46:44 +00:00
|
|
|
Pygments.highlight blob.data, lexer: 'spec', options: {linenos: true}
|
2014-10-09 09:24:43 +01:00
|
|
|
else
|
2014-11-10 18:46:44 +00:00
|
|
|
blob.colorize(options: {linenos: true, lineanchors: 'lc', linespans: 'ln', anchorlinenos: true})
|
2014-10-09 09:24:43 +01:00
|
|
|
end
|
|
|
|
result.present? ? result.html_safe : blob.data
|
2014-10-09 08:26:54 +01:00
|
|
|
rescue MentosError, Yajl::ParseError => e
|
2014-10-07 17:38:52 +01:00
|
|
|
blob.data.html_safe
|
2014-04-15 10:11:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def blame_highlight(blob, text)
|
2014-10-09 08:22:37 +01:00
|
|
|
return if blob.nil? || text.blank?
|
2014-10-09 09:24:43 +01:00
|
|
|
result = if blob.mime_type == 'text/rpm-spec'
|
|
|
|
Pygments.highlight(text, lexer: 'spec')
|
|
|
|
else
|
|
|
|
blob.lexer.highlight text
|
|
|
|
end
|
|
|
|
result.present? ? result.html_safe : text
|
2014-10-09 08:26:54 +01:00
|
|
|
rescue MentosError, Yajl::ParseError => e
|
2014-10-07 17:38:52 +01:00
|
|
|
text.html_safe
|
2014-04-15 10:11:14 +01:00
|
|
|
end
|
2012-01-20 18:31:01 +00:00
|
|
|
end
|