This commit is contained in:
Alexander Machehin 2015-04-07 20:53:29 +05:00
parent d062e9a330
commit 2ec7a8bb91
6 changed files with 66 additions and 49 deletions

View File

@ -2,20 +2,25 @@ module CommitHelper
MAX_FILES_WITHOUT_COLLAPSE = 25 MAX_FILES_WITHOUT_COLLAPSE = 25
def render_commit_stats(options = {}) def render_commit_stats(options = {})
stats = options[:stats] stats = options[:stats]
diff = options[:diff] diff = options[:diff]
repo = options[:repo] repo = options[:repo]
commit = options[:commit] commit = options[:commit]
parent_commit = commit.parents.try(:first)
res = ["<ul class='list-group boffset0'>"] res = ["<ul class='list-group boffset0'>"]
ind=0 ind=0
stats.files.each do |filename, adds, deletes, total| stats.files.each do |filename, adds, deletes, total|
file_name = get_filename_in_diff(diff[ind], filename) file_name = get_filename_in_diff(diff[ind], filename)
file_status = t "layout.projects.diff.#{get_file_status_in_diff(diff[ind])}"
res << "<li class='list-group-item'>" res << "<li class='list-group-item'>"
res << "<div class='row'>" res << "<div class='row'>"
res << "<div class='col-sm-8'><a href='#diff-#{ind}'>#{diff_file_icon(diff[ind])} #{h(file_name)}</a></div>" res << "<div class='col-sm-8'>"
res << render_file_changes(diff: diff[ind], adds: adds, deletes: deletes, total: total, repo: repo, commit: commit) 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)
res << "</div" res << "</div"
res << "</li>" res << "</li>"
ind +=1 ind +=1
@ -87,8 +92,8 @@ module CommitHelper
diff.renamed_file ? (tree / diff.b_path) : (tree / (diff.a_path.presence || diff.b_path)) diff.renamed_file ? (tree / diff.b_path) : (tree / (diff.a_path.presence || diff.b_path))
end end
def get_commit_id_for_file(diff, commit) def get_commit_id_for_file(diff, commit, parent_commit)
diff.deleted_file ? commit.parents.try(:first).try(:id) : commit.id diff.deleted_file ? parent_commit.id : commit.id
end end
def get_file_status_in_diff(diff) def get_file_status_in_diff(diff)
@ -118,15 +123,15 @@ module CommitHelper
end end
def render_file_changes(options = {}) def render_file_changes(options = {})
diff = options[:diff] diff = options[:diff]
adds = options[:adds] adds = options[:adds]
deletes = options[:deletes] deletes = options[:deletes]
total = options[:total] total = options[:total]
repo = options[:repo] repo = options[:repo]
commit_id = get_commit_id_for_file(diff, options[:commit]) file_status = options[:file_status]
blob = file_blob_in_diff(repo, commit_id, diff) commit_id = get_commit_id_for_file(diff, options[:commit], options[:parent_commit])
blob = file_blob_in_diff(repo, commit_id, diff)
file_status = t "layout.projects.diff.#{get_file_status_in_diff(diff)}"
res = '' res = ''
res << "<div class='col-sm-3'>" res << "<div class='col-sm-3'>"
res << "<div class='pull-right'>" res << "<div class='pull-right'>"

View File

@ -1,26 +1,41 @@
module DiffHelper module DiffHelper
MAX_FILES_WITHOUT_COLLAPSE = 25 include CommitHelper
MAX_LINES_WITHOUT_COLLAPSE = 50 MAX_LINES_WITHOUT_COLLAPSE = 50
def render_diff_stats(stats, diff) def render_diff_stats(options = {})
res = ["<table class='table table-responsive boffset0'>"] stats = options[:stats]
diff = options[:diff]
repo = options[:repo]
commit = options[:commit]
parent_commit = options[:common_ancestor]
res = ["<ul class='list-group boffset0'>"]
stats.each_with_index do |stat, ind| stats.each_with_index do |stat, ind|
res << "<tr>" adds = stat.additions
res << "<td>#{link_to stat.filename.rtruncate(120), "#diff-#{ind}"}</td>" deletes = stat.deletions
res << "<td class='diffstat'>" total = adds + deletes
res << I18n.t("layout.projects.inline_changes_count", count: stat.additions + stat.deletions).strip + file_name = get_filename_in_diff(diff[ind], stat.filename)
" (" + file_status = t "layout.projects.diff.#{get_file_status_in_diff(diff[ind])}"
I18n.t("layout.projects.inline_additions_count", count: stat.additions).strip +
", " + res << "<li class='list-group-item'>"
I18n.t("layout.projects.inline_deletions_count", count: stat.deletions).strip + res << "<div class='row'>"
")" res << "<div class='col-sm-8'>"
res << "</td>" 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)
res << "</div"
res << "</li>"
ind +=1
end end
res << '</table>' res << "</ul>"
wrap_header_list(stats, res)
wrap_diff_header_list(stats, res)
end end
def wrap_header_list(stats, list) def wrap_diff_header_list(stats, list)
is_stats_open = stats.count <= MAX_FILES_WITHOUT_COLLAPSE ? 'in' : '' is_stats_open = stats.count <= MAX_FILES_WITHOUT_COLLAPSE ? 'in' : ''
res = ["<div class='panel-group' id='diff_header' role='tablist' aria-multiselectable='false'>"] res = ["<div class='panel-group' id='diff_header' role='tablist' aria-multiselectable='false'>"]
res << "<div class='panel panel-default'>" res << "<div class='panel panel-default'>"

View File

@ -1,4 +1,5 @@
- commit_id = get_commit_id_for_file(diff, commit) - parent_commit = defined?(common_ancestor) ? common_ancestor : commit.parents.try(:first)
- commit_id = get_commit_id_for_file diff, commit, parent_commit
- diff_counter_content = "diff-#{diff_counter}_content" - diff_counter_content = "diff-#{diff_counter}_content"
- blob = file_blob_in_diff(@project.repo, commit_id, diff) - blob = file_blob_in_diff(@project.repo, commit_id, diff)
- is_file_open = 'in' if is_file_open_in_diff(blob, diff) - is_file_open = 'in' if is_file_open_in_diff(blob, diff)
@ -20,7 +21,10 @@
.clearfix .clearfix
.diff_data.collapse id= diff_counter_content class= is_file_open .diff_data.collapse id= diff_counter_content class= is_file_open
== render('show_image', diff: diff, blob: blob) if blob.render_as == :image - if blob.render_as == :image
- old_blob = file_blob_in_diff(@project.repo, parent_commit.id, diff)
== render('show_image', diff: diff, blob: blob, old_blob: old_blob)
- if diff.a_mode != diff.b_mode && diff.diff.blank? - if diff.a_mode != diff.b_mode && diff.diff.blank?
== render 'file_change_mode', blob: blob, diff: diff == render 'file_change_mode', blob: blob, diff: diff

View File

@ -1,10 +1,9 @@
- #table.table.table-responsive.diff.inline.table-borderless cellspacing= 0 cellpadding= 0
.row .row
- if get_file_status_in_diff(diff) == :changed_file - if get_file_status_in_diff(diff) == :changed_file
.col-md-6.col-xs-12 .col-md-6.col-xs-12
p.text-center.text-danger= t 'layout.projects.diff.deleted_file' p.text-center.text-danger= t 'layout.projects.diff.deleted_file'
img[ class= 'img-responsive center-block' img[ class= 'img-responsive center-block'
src= "data:#{blob.mime_type};base64,#{Base64.encode64(diff.a_blob.data)}" src= "data:#{old_blob.mime_type};base64,#{Base64.encode64(old_blob.data)}"
style= 'border: 1px solid red;' ] style= 'border: 1px solid red;' ]
.col-md-6.col-xs-12 .col-md-6.col-xs-12

View File

@ -12,19 +12,13 @@
h3= title h3= title
#repo-wrapper #repo-wrapper
.leftside
-total_additions = @stats.inject(0) {|sum, n| sum + n.additions}
-total_deletions = @stats.inject(0) {|sum, n| sum + n.deletions}
h5= t("layout.projects.diff_show_header",
files: t("layout.projects.commit_files_count", count: @stats.count),
additions: t("layout.projects.commit_additions_count", count: total_additions),
deletions: t("layout.projects.commit_deletions_count", count: total_deletions))
-begin -begin
- diffs = @project.repo.diff @common_ancestor.id, @commit.id - diffs = @project.repo.diff @common_ancestor.id, @commit.id
== render_diff_stats @stats, diffs == render_diff_stats(stats: @stats, diff: diffs, repo: @project.repo,
-# diffs = Grit::Commit.diff(@project.repo, @common_ancestor.id, @commit.id) commit: @commit, common_ancestor: @common_ancestor)
== render partial: 'projects/git/commits/diff', collection: diffs, locals: { parent_commit: @common_ancestor }
== render partial: 'projects/git/commits/diff', collection: diffs,
locals: { commit: @commit, common_ancestor: @common_ancestor}
- rescue Grit::Git::GitTimeout - rescue Grit::Git::GitTimeout
p= t 'layout.git.repositories.commit_diff_too_big' p= t 'layout.git.repositories.commit_diff_too_big'

View File

@ -82,7 +82,7 @@ module Grit
def diff_stats(a,b) def diff_stats(a,b)
stats = [] stats = []
Dir.chdir(path) do Dir.chdir(path) do
lines = self.git.native(:diff, {numstat: true}, "#{a}...#{b}").split("\n") lines = self.git.native(:diff, {numstat: true, M: true}, "#{a}...#{b}").split("\n")
while !lines.empty? while !lines.empty?
files = [] files = []
while lines.first =~ /^([-\d]+)\s+([-\d]+)\s+(.+)/ while lines.first =~ /^([-\d]+)\s+([-\d]+)\s+(.+)/