new commit diff

This commit is contained in:
Alexander Machehin 2015-04-07 19:16:04 +05:00
parent f202d1d449
commit d062e9a330
11 changed files with 152 additions and 78 deletions

View File

@ -1,5 +1,5 @@
$(document).ready ->
$(document).on 'click', '#diff_header .panel-body table a', ->
$(document).on 'click', '#diff_header .panel-body li.list-group-item a', ->
href = $(this).attr('href')
$(".diff_data.collapse#"+href.slice(1)+"_content").collapse('show')

View File

@ -3,7 +3,7 @@
overflow-x: auto
table.table.diff.inline
//border: 1px solid #DDD
margin-bottom: 0
tr.changes
pre
@ -66,17 +66,6 @@ table.table.diff.inline
.line-comment, #new_inline_comment
max-width: 700px
td.diff-image
text-align: center
span.diff-image
text-align: center
margin: 0
padding: 0
img
margin-top: 5px
div.file div.top
min-height: 28px
background: #ededed

View File

@ -1,30 +1,21 @@
module CommitHelper
MAX_FILES_WITHOUT_COLLAPSE = 25
def render_commit_stats(stats, diff)
def render_commit_stats(options = {})
stats = options[:stats]
diff = options[:diff]
repo = options[:repo]
commit = options[:commit]
res = ["<ul class='list-group boffset0'>"]
ind=0
stats.files.each do |filename, adds, deletes, total|
file_name = if diff[ind].renamed_file
"#{diff[ind].a_path.rtruncate 60}=>#{diff[ind].b_path.rtruncate 60}"
else
filename.rtruncate(120)
end
file_name = get_filename_in_diff(diff[ind], filename)
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 << "<div class='col-sm-2'>"
res << "<div class='pull-right'>"
res << "<strong class='text-success'>+#{adds}</strong> <strong class='text-danger'>-#{deletes}</strong>"
res << "</div>"
res << "</div>"
res << "<div class='col-sm-2'>"
res << render_progress_bar(adds, deletes)
res << "</div>"
res << render_file_changes(diff: diff[ind], adds: adds, deletes: deletes, total: total, repo: repo, commit: commit)
res << "</div"
res << "</li>"
ind +=1
@ -91,19 +82,89 @@ module CommitHelper
diff.diff.present? && diff.diff.split("\n").count <= DiffHelper::MAX_LINES_WITHOUT_COLLAPSE
end
def file_blob_in_diff(repo, commit_id, diff)
tree = repo.tree(commit_id)
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
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
"#{diff.a_path.rtruncate 60} => #{diff.b_path.rtruncate 60}"
else
filename.rtruncate(120)
end
end
protected
def commits_pluralization_arr
pluralize ||= t('layout.commits.pluralize').map {|base, title| title.to_s}
end
def render_progress_bar(adds, deletes)
return if adds+deletes == 0
def render_file_changes(options = {})
diff = options[:diff]
adds = options[:adds]
deletes = options[:deletes]
total = options[:total]
repo = options[:repo]
commit_id = get_commit_id_for_file(diff, options[: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'>"
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
res << "<div class='progress' style='margin-bottom: 0'>"
title = if total >0
t 'layout.projects.inline_changes_count', count: total
elsif !blob.binary?
t 'layout.projects.diff.without_changes'
else
'BIN'
end
res << "<div class='progress' style='margin-bottom: 0' data-toggle='tooltip' data-placement='top' title='#{title}'>"
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>"
@ -111,14 +172,17 @@ module CommitHelper
end
def diff_file_icon(diff)
icon = if diff.renamed_file
icon = case get_file_status_in_diff(diff)
when :renamed_file
'fa-caret-square-o-right text-info'
elsif diff.new_file
when :new_file
'fa-plus-square text-success'
elsif diff.deleted_file
when :deleted_file
'fa-minus-square text-danger'
when :changed_file
'fa-pencil-square text-primary'
else
'fa-pencil-square text-warning'
'fa-exclamation-circle text-danger'
end
"<i class='fa #{icon}'></i>"
end

View File

@ -1,10 +1,6 @@
- commit_id = diff.deleted_file ? parent_commit.try(:id) : @commit.id
- commit_id = get_commit_id_for_file(diff, commit)
- diff_counter_content = "diff-#{diff_counter}_content"
- if diff.renamed_file
- blob = @project.repo.tree(commit_id) / diff.b_path
- else
- blob = @project.repo.tree(commit_id) / diff.a_path
- blob = file_blob_in_diff(@project.repo, commit_id, diff)
- is_file_open = 'in' if is_file_open_in_diff(blob, diff)
.file.offset10
@ -16,25 +12,19 @@
aria-expanded = 'true'
aria-controls = diff_counter_content ]
span.fa class= (is_file_open ? 'fa-chevron-down' : 'fa-chevron-up')
=< h(diff.a_path.rtruncate 120)
=< diff_file_icon(diff).html_safe
=< get_filename_in_diff(diff, diff.a_path)
- if diff.b_path.present?
button.btn.btn-link.pull-right
= link_to "view file @ #{shortest_hash_id(commit_id)}", blob_path(@project, commit_id, diff.b_path)
.clearfix
- if diff.diff.present?
.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 diff.a_mode != diff.b_mode && diff.diff.blank?
== render 'file_change_mode', blob: blob, diff: diff
- if blob.render_as == :image
table.table.diff.inline.table-bordered[ cellspacing = 0 cellpadding = 0 ]
tr
td.diff-image
span.diff-image
img[ src = "data:#{blob.mime_type};base64,#{Base64.encode64(blob.data)}"
style = 'max-width: 600px;' ]
- elsif !blob.binary?
- if (@project.repo.tree(commit_id) / diff.b_path).nil?
= "a_path=#{diff.a_path}; b_path=#{diff.b_path}"
== render_diff(diff, diff_counter: diff_counter, comments: @comments)
- if diff.a_mode != diff.b_mode && diff.diff.blank?
== render 'file_change_mode', blob: blob, diff: diff, diff_counter_content: diff_counter_content

View File

@ -0,0 +1,10 @@
.table-responsive.overflow-auto
table.table.diff.inline.table-borderless cellspacing= 0 cellpadding= 0
tr.header
td.line_numbers[] ...
td.line_numbers[] ...
td.header.text-danger= "oldmode #{diff.a_mode}"
tr.header
td.line_numbers[] ...
td.line_numbers[] ...
td.header.text-success= "newmode #{diff.b_mode}"

View File

@ -1,7 +1,7 @@
- begin
- diffs = @commit.show
- stats = Grit::CommitStats.find_all(@project.repo, @commit.sha, max_count: 1, skip: 0, M: true)[0][-1]
= render_commit_stats(stats, diffs)
= render_commit_stats(stats: stats, diff: @commit.show, repo: @project.repo, commit: @commit)
.pull-right
=> link_to 'raw diff', commit_path(@project, @commit.id, :diff)
@ -9,6 +9,6 @@
=< link_to 'patch', commit_path(@project, @commit.id, :patch)
.clearfix
== render partial: 'diff', collection: diffs, locals: { parent_commit: @commit.parents.try(:first) }
== render partial: 'diff', collection: diffs, locals: { commit: @commit }
- rescue Grit::Git::GitTimeout
h3.text-danger= t('layout.git.repositories.commit_diff_too_big')

View File

@ -0,0 +1,19 @@
- #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)}"
style= 'border: 1px solid red;' ]
.col-md-6.col-xs-12
p.text-center.text-success= t 'layout.projects.diff.new_file'
img[ class= 'img-responsive center-block'
src= "data:#{blob.mime_type};base64,#{Base64.encode64(blob.data)}"
style= 'border: 1px solid green;' ]
- else
.col-xs-12
img[ class= 'img-responsive center-block offset10'
src= "data:#{blob.mime_type};base64,#{Base64.encode64(blob.data)}"
style= 'border: 1px solid #87CEFA;' ]

View File

@ -94,6 +94,14 @@ en:
public: Public
private: Private
diff:
binary: Binary
new_file: file added
deleted_file: file removed
renamed_file: file renamed
changed_file: file changed
without_changes: without changes
flash:
project:
mass_import_added_to_queue: Mass import added to queue

View File

@ -95,6 +95,14 @@ ru:
public: Публичные
private: Приватные
diff:
binary: Бинарный
new_file: файл добавлен
deleted_file: файл удален
renamed_file: файл переименован
changed_file: файл изменен
without_changes: без изменений
flash:
project:
mass_import_added_to_queue: Массовый импорт добавлен в очередь

View File

@ -11,12 +11,6 @@ en:
one: "%{count} deletion"
other: "%{count} deletions"
inline_additions_count:
one: "%{count} addition"
other: "%{count} additions"
inline_deletions_count:
one: "%{count} deletion"
other: "%{count} deletions"
inline_changes_count:
one: "%{count} change"
other: "%{count} changes"

View File

@ -14,14 +14,6 @@ ru:
few: "%{count} удалениями"
many: "%{count} удалениями"
inline_additions_count:
one: "%{count} добавление"
few: "%{count} добавления"
many: "%{count} добавлений"
inline_deletions_count:
one: "%{count} удаление"
few: "%{count} удаления"
many: "%{count} удалений"
inline_changes_count:
one: "%{count} изменение"
few: "%{count} изменения"