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

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

View File

@ -1,26 +1,41 @@
module DiffHelper
MAX_FILES_WITHOUT_COLLAPSE = 25
include CommitHelper
MAX_LINES_WITHOUT_COLLAPSE = 50
def render_diff_stats(stats, diff)
res = ["<table class='table table-responsive boffset0'>"]
def render_diff_stats(options = {})
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|
res << "<tr>"
res << "<td>#{link_to stat.filename.rtruncate(120), "#diff-#{ind}"}</td>"
res << "<td class='diffstat'>"
res << I18n.t("layout.projects.inline_changes_count", count: stat.additions + stat.deletions).strip +
" (" +
I18n.t("layout.projects.inline_additions_count", count: stat.additions).strip +
", " +
I18n.t("layout.projects.inline_deletions_count", count: stat.deletions).strip +
")"
res << "</td>"
adds = stat.additions
deletes = stat.deletions
total = adds + deletes
file_name = get_filename_in_diff(diff[ind], stat.filename)
file_status = t "layout.projects.diff.#{get_file_status_in_diff(diff[ind])}"
res << "<li class='list-group-item'>"
res << "<div class='row'>"
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)
res << "</div"
res << "</li>"
ind +=1
end
res << '</table>'
wrap_header_list(stats, res)
res << "</ul>"
wrap_diff_header_list(stats, res)
end
def wrap_header_list(stats, list)
def wrap_diff_header_list(stats, list)
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 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"
- blob = file_blob_in_diff(@project.repo, commit_id, diff)
- is_file_open = 'in' if is_file_open_in_diff(blob, diff)
@ -20,7 +21,10 @@
.clearfix
.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?
== 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
- if get_file_status_in_diff(diff) == :changed_file
.col-md-6.col-xs-12
p.text-center.text-danger= t 'layout.projects.diff.deleted_file'
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;' ]
.col-md-6.col-xs-12

View File

@ -12,19 +12,13 @@
h3= title
#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
- diffs = @project.repo.diff @common_ancestor.id, @commit.id
== render_diff_stats @stats, diffs
-# diffs = Grit::Commit.diff(@project.repo, @common_ancestor.id, @commit.id)
== render partial: 'projects/git/commits/diff', collection: diffs, locals: { parent_commit: @common_ancestor }
== render_diff_stats(stats: @stats, diff: diffs, repo: @project.repo,
commit: @commit, common_ancestor: @common_ancestor)
== render partial: 'projects/git/commits/diff', collection: diffs,
locals: { commit: @commit, common_ancestor: @common_ancestor}
- rescue Grit::Git::GitTimeout
p= t 'layout.git.repositories.commit_diff_too_big'

View File

@ -82,7 +82,7 @@ module Grit
def diff_stats(a,b)
stats = []
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?
files = []
while lines.first =~ /^([-\d]+)\s+([-\d]+)\s+(.+)/