rosa-build/app/helpers/commit_helper.rb

197 lines
6.5 KiB
Ruby
Raw Normal View History

2011-03-10 13:33:50 +00:00
module CommitHelper
2015-02-04 16:47:24 +00:00
MAX_FILES_WITHOUT_COLLAPSE = 25
2015-04-07 15:16:04 +01:00
def render_commit_stats(options = {})
2015-04-07 16:53:29 +01:00
stats = options[:stats]
diff = options[:diff]
repo = options[:repo]
commit = options[:commit]
parent_commit = commit.parents.try(:first)
2015-04-07 15:16:04 +01:00
2015-04-06 18:44:37 +01:00
res = ["<ul class='list-group boffset0'>"]
ind=0
2011-03-10 13:33:50 +00:00
stats.files.each do |filename, adds, deletes, total|
2015-04-07 15:16:04 +01:00
file_name = get_filename_in_diff(diff[ind], filename)
2015-04-07 16:53:29 +01:00
file_status = t "layout.projects.diff.#{get_file_status_in_diff(diff[ind])}"
2015-04-06 18:44:37 +01:00
res << "<li class='list-group-item'>"
res << "<div class='row'>"
2015-04-07 16:53:29 +01:00
res << "<div class='col-sm-8'>"
res << "<a href='#diff-#{ind}' data-toggle='tooltip' data-placement='top' title='#{file_status}'>"
res << "#{diff_file_icon(diff[ind])} #{h(file_name)}"
res << "</a></div>"
res << render_file_changes(diff: diff[ind], adds: adds, deletes: deletes, total: total,
repo: repo, commit: commit, parent_commit: parent_commit, file_status: file_status)
2015-04-06 18:44:37 +01:00
res << "</div"
res << "</li>"
ind +=1
2011-03-10 13:33:50 +00:00
end
2015-04-06 18:44:37 +01:00
res << "</ul>"
2011-03-10 13:33:50 +00:00
2015-02-04 16:47:24 +00:00
wrap_commit_header_list(stats, res)
end
def wrap_commit_header_list(stats, list)
is_stats_open = stats.files.count <= MAX_FILES_WITHOUT_COLLAPSE ? 'in' : ''
res = ["<div class='panel-group' id='diff_header' role='tablist' aria-multiselectable='false'>"]
res << "<div class='panel panel-default'>"
res << "<div class='panel-heading' role='tab' id='heading'>"
res << "<h4 class='panel-title'>"
res << "<a data-toggle='collapse' data-parent='#diff_header' href='#collapseList' aria-expanded='true' aria-controls='collapseList'>"
res << "<span class='fa fa-chevron-#{is_stats_open ? 'down' : 'up'}'></span>"
res << " #{diff_commit_header_message(stats)}</a>"
2015-02-04 16:47:24 +00:00
res << "</h4>"
res << "</div>"
res << "<div id='collapseList' class='panel-collapse collapse #{is_stats_open}' role='tabpanel' aria-labelledby='collapseList'>"
res << "<div class='panel-body'>"
res += list
res << "</div>"
res << "</div>"
res << "</div>"
res << "</div>"
2014-01-15 19:34:54 +00:00
res.join("\n").html_safe
2011-03-10 13:33:50 +00:00
end
2015-02-04 16:47:24 +00:00
def diff_commit_header_message(stats)
t("layout.projects.diff_show_header",
files: t("layout.projects.commit_files_count", count: stats.files.size),
additions: t("layout.projects.commit_additions_count", count: stats.additions),
deletions: t("layout.projects.commit_deletions_count", count: stats.deletions))
end
2011-03-10 13:33:50 +00:00
def commit_date(date)
2014-01-21 04:51:49 +00:00
I18n.localize(date, { format: "%d %B %Y" })
2011-03-10 13:33:50 +00:00
end
2011-04-01 01:36:34 +01:00
def short_hash_id(id)
id[0..19]
end
def shortest_hash_id(id, size=10)
id[0..size-1]
2011-04-04 12:28:33 +01:00
end
2012-03-05 15:29:05 +00:00
def commit_author_link(author)
name = author.name
2012-03-05 15:29:05 +00:00
email = author.email
2014-01-21 04:51:49 +00:00
u = User.where(email: email).first
2012-03-05 15:29:05 +00:00
u.present? ? link_to(name, user_path(u)) : mail_to(email, name)
end
2013-09-08 12:56:43 +01:00
def commits_pluralize(commits_count)
Russian.p(commits_count, *commits_pluralization_arr)
end
2015-04-06 18:44:37 +01:00
def is_file_open_in_diff(blob, diff)
return true if blob.binary? && blob.render_as == :image
return true if diff.diff.blank? && diff.a_mode != diff.b_mode
diff.diff.present? && diff.diff.split("\n").count <= DiffHelper::MAX_LINES_WITHOUT_COLLAPSE
end
2015-04-07 15:16:04 +01:00
def file_blob_in_diff(repo, commit_id, diff)
return if repo.nil? || commit_id.nil? || diff.nil?
2015-04-07 15:16:04 +01:00
tree = repo.tree(commit_id)
blob = diff.renamed_file ? (tree / diff.b_path) : (tree / (diff.a_path || diff.b_path))
2015-04-09 17:33:33 +01:00
blob || diff.a_blob || diff.b_blob
2015-04-07 15:16:04 +01:00
end
2015-04-07 16:53:29 +01:00
def get_commit_id_for_file(diff, commit, parent_commit)
diff.deleted_file ? parent_commit.id : commit.id
2015-04-07 15:16:04 +01:00
end
def get_file_status_in_diff(diff)
if diff.renamed_file
:renamed_file
elsif diff.new_file
:new_file
elsif diff.deleted_file
:deleted_file
else
:changed_file
end
end
def get_filename_in_diff(diff, filename)
if diff.renamed_file
2015-04-07 18:14:18 +01:00
"#{diff.a_path.rtruncate 50} => #{diff.b_path.rtruncate 50}"
2015-04-07 15:16:04 +01:00
else
2015-04-07 18:14:18 +01:00
filename.rtruncate(100)
2015-04-07 15:16:04 +01:00
end
end
2013-09-08 12:56:43 +01:00
protected
def commits_pluralization_arr
pluralize ||= t('layout.commits.pluralize').map {|base, title| title.to_s}
end
2015-04-06 18:44:37 +01:00
2015-04-07 15:16:04 +01:00
def render_file_changes(options = {})
2015-04-07 16:53:29 +01:00
diff = options[:diff]
adds = options[:adds]
deletes = options[:deletes]
total = options[:total]
repo = options[:repo]
file_status = options[:file_status]
commit_id = get_commit_id_for_file(diff, options[:commit], options[:parent_commit])
blob = file_blob_in_diff(repo, commit_id, diff)
2015-04-06 18:44:37 +01:00
res = ''
2015-04-07 15:16:04 +01:00
res << "<div class='col-sm-3'>"
res << "<div class='pull-right'>"
if blob.binary?
res << "<strong class='text-primary'>#{t 'layout.projects.diff.binary'} #{file_status}</strong>"
elsif total > 0
res << "<strong class='text-success'>+#{adds}</strong> <strong class='text-danger'>-#{deletes}</strong>"
else # total == 0
res << "<strong class='text-primary'>#{t 'layout.projects.diff.without_changes'}</strong>"
end
res << "</div>"
res << "</div>"
res << "<div class='col-sm-1'>"
res << render_progress_bar(adds, deletes, total, blob)
res << "</div>"
end
def render_progress_bar(adds, deletes, total, blob)
res = ''
pluses = 0
minuses = 0
if total > 0
pluses = ((adds/(adds+deletes).to_f)*100).round
minuses = 100 - pluses
end
title = if total >0
t 'layout.projects.inline_changes_count', count: total
elsif !blob.binary?
t 'layout.projects.diff.without_changes'
else
'BIN'
end
2015-04-06 18:44:37 +01:00
2015-04-07 15:16:04 +01:00
res << "<div class='progress' style='margin-bottom: 0' data-toggle='tooltip' data-placement='top' title='#{title}'>"
2015-04-06 18:44:37 +01:00
res << "<div class='progress-bar progress-bar-success' style='width: #{pluses}%'></div>"
res << "<div class='progress-bar progress-bar-danger' style='width: #{minuses}%'></div>"
res << "</div>"
res
end
def diff_file_icon(diff)
2015-04-07 15:16:04 +01:00
icon = case get_file_status_in_diff(diff)
when :renamed_file
2015-04-06 18:44:37 +01:00
'fa-caret-square-o-right text-info'
2015-04-07 15:16:04 +01:00
when :new_file
2015-04-06 18:44:37 +01:00
'fa-plus-square text-success'
2015-04-07 15:16:04 +01:00
when :deleted_file
2015-04-06 18:44:37 +01:00
'fa-minus-square text-danger'
2015-04-07 15:16:04 +01:00
when :changed_file
'fa-pencil-square text-primary'
2015-04-06 18:44:37 +01:00
else
2015-04-07 15:16:04 +01:00
'fa-exclamation-circle text-danger'
2015-04-06 18:44:37 +01:00
end
"<i class='fa #{icon}'></i>"
end
2012-01-30 20:39:34 +00:00
end