Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve handling of massive diffs #499

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
30 changes: 9 additions & 21 deletions lib/git_diff_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ def self.get_tagged_commit_diffs(repo_name, commit, options = {})
if GitHelper.blob_binary?(diff.a_blob) || GitHelper.blob_binary?(diff.b_blob)
data.binary = true
data.special_case = "This is a binary file."
elsif data.submodule?
elsif !(a_path =~ /\.(gen|properties|relations|functions)\.cs/).nil? || !(a_path =~ /PI_/).nil? || !(a_path =~ /DBSchema.xml$/).nil?
data.special_case = "Generated File - not showing diff."
elsif data.submodule?
data.special_case = data.submodule_special_case_message
elsif diff.diff.nil? && (data.file_mode_before != data.file_mode_after)
data.special_case = "File mode changed: #{data.file_mode_before} → #{data.file_mode_after}"
Expand All @@ -57,7 +59,10 @@ def self.get_tagged_commit_diffs(repo_name, commit, options = {})
# Diffs can be missing a_blob or b_blob if the change is an added or removed file.
before, after = [diff.a_blob, diff.b_blob].map { |blob| blob ? blob.data : "" }
end
raw_diff = GitDiffUtils.diff(diff.a_blob, diff.b_blob)

raw_diff = diff.diff
raw_diff.gsub! "\r", ""
raw_diff.sub %r{[^@]*}, ""

unless options[:warm_the_cache]
new_data = GitDiffUtils.tag_file(before, after, diff, raw_diff)
Expand Down Expand Up @@ -87,27 +92,10 @@ def self.tag_file(file_before, file_after, diff, raw_diff)
chunks = tag_diff(diff, raw_diff, before_lines, after_lines)

chunks.each_with_index do |chunk, i|
if chunk.original_line_start && chunk.original_line_start > orig_line
tagged_lines += before_lines[orig_line...chunk.original_line_start].map do |data|
diff_line += 1
orig_line += 1
LineDiff.new(:same, before_lines[orig_line - 1], orig_line, diff_line)
end
chunk_breaks << tagged_lines.size
end
tagged_lines += chunk.tagged_lines
orig_line += chunk.original_lines_changed
diff_line += chunk.new_lines_changed
end

if !before_lines.empty? && orig_line <= before_lines.count
chunk_breaks << tagged_lines.size
tagged_lines += before_lines[orig_line..before_lines.count].map do |data|
diff_line += 1
orig_line += 1
LineDiff.new(:same, before_lines[orig_line - 1], orig_line, diff_line )
end
end
lines_added = tagged_lines.select { |line| line.tag == :added }.count
lines_removed = tagged_lines.select { |line| line.tag == :removed }.count
{ :lines => tagged_lines, :breaks => chunk_breaks,
Expand Down Expand Up @@ -189,10 +177,10 @@ def self.tag_diff(diff, raw_diff, before_highlighted, after_highlighted)

def self.show(repo, commit)
if commit.parents.size > 0
diff = repo.git.native(:diff, { :full_index => true, :find_renames => true }, commit.parents[0].id,
diff = repo.git.native(:diff, { :inter_hunk_context => 33, :unified => 9, :ignore_all_space => true, :full_index => true, :find_renames => true }, commit.parents[0].id,
commit.id)
else
raw_diff = repo.git.native(:show, { :full_index => true, :pretty => "raw" }, commit.id)
raw_diff = repo.git.native(:show, { :inter_hunk_context => 33, :unified => 9, :ignore_all_space => true, :full_index => true, :pretty => "raw" }, commit.id)
# git show has a lot of headers in the diff, we try to strip it out here
cutpoint = raw_diff.index("diff --git a")
diff = cutpoint ? raw_diff[cutpoint, raw_diff.length] : ""
Expand Down
2 changes: 1 addition & 1 deletion lib/git_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ def self.rev_list(repo, command_options, mode = :commits)
def self.blob_binary?(blob)
blob && !blob.data.empty? && blob.data.index("\0")
end
end
end
49 changes: 25 additions & 24 deletions public/coffee/commit.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,29 @@ window.Commit =
SIDE_BY_SIDE_CODE_WIDTH: 830

init: ->
$(".addCommentButton").click (e) => @onAddCommentMouseAction e
$(document).on("click", ".addCommentButton", (e) => @onAddCommentMouseAction e)
$("a.tipsyCommentCount").tipsy(gravity: "w")
$(".diffLine").dblclick (e) => @onAddCommentMouseAction e
$(".reply").live "click", (e) => @onAddCommentMouseAction e
$(".diffLine").hover(((e) => @selectLine(e)), ((e) => @clearSelectedLine()))
$(".commentForm").live "submit", (e) => @onCommentSubmit e
$(".commentPreview").click (e) => @onCommentPreview e
$(".commentEditForm").live "submit", (e) => @onCommentEditSubmit e
$("#approveButton").live "click", (e) => @onApproveClicked e
$("#disapproveButton").live "click", (e) => @onDisapproveClicked e
$(".delete").live "click", (e) => @onCommentDelete e
$(".edit").live "click", (e) => @onCommentEdit e
$("#sideBySideButton").live "click", => @toggleSideBySide true
$("#requestReviewButton").click (e) => @toggleReviewRequest()
$("#hideCommentButton").live "click", (e) => @toggleComments()
$(".diffCommentCount > a").live "click", (e) => @toggleSingleComment(e)
$("#requestInput button").click (e) => @submitReviewRequest()
$(".expandLink.all").click (e) => @expandContextAll(e)
$(".expandLink.below").click (e) => @expandContext(e, 10, "below")
$(".expandLink.above").click (e) => @expandContext(e, 10, "above")
$("#commit .file").on("mouseenter", ".contextExpander", @expandContextHoverIn)
$("#commit .file").on("mouseleave", ".contextExpander", @expandContextHoverOut)
$(document).on("dblclick", ".diffLine", (e) => @onAddCommentMouseAction e)
$(document).on("click", ".reply", (e) => @onAddCommentMouseAction e)
$(document).on("mouseenter", ".diffLine", (e) => @selectLine(e))
$(document).on("mouseleave", ".diffLine", (e) => @clearSelectedLine())
$(document).on("submit", ".commentForm", (e) => @onCommentSubmit e)
$(document).on("click", ".commentPreview", (e) => @onCommentPreview e)
$(document).on("submit", ".commentEditForm", (e) => @onCommentEditSubmit e)
$(document).on("click", "#approveButton", (e) => @onApproveClicked e)
$(document).on("click", "#disapproveButton", (e) => @onDisapproveClicked e)
$(document).on("click", ".delete", (e) => @onCommentDelete e)
$(document).on("click", ".edit", (e) => @onCommentEdit e)
$(document).on("click", "#sideBySideButton", => @toggleSideBySide true)
$(document).on("click", "#requestReviewButton", (e) => @toggleReviewRequest())
$(document).on("click", "#hideCommentButton", (e) => @toggleComments())
$(document).on("click", ".diffCommentCount > a", (e) => @toggleSingleComment(e))
$(document).on("click", "#requestInput button", (e) => @submitReviewRequest())
$(document).on("click", ".expandLink.all", (e) => @expandContextAll(e))
$(document).on("click", ".expandLink.below", (e) => @expandContext(e, 10, "below"))
$(document).on("click", ".expandLink.above", (e) => @expandContext(e, 10, "above"))
$(document).on("mouseenter", "#commit .file .contextExpander", @expandContextHoverIn)
$(document).on("mouseleave", "#commit .file .contextExpander", @expandContextHoverOut)

@currentlyScrollingTimer = null

Expand Down Expand Up @@ -315,9 +316,9 @@ window.Commit =
createContextExpander: (codeLines, attachDirection, top, bottom, incremental, refreshLine) ->
renderedExpander = Snippets.contextExpander(top, bottom, codeLines.attr("diff-line-number"), incremental)
contextExpander = $(renderedExpander)
contextExpander.find(".expandLink.all").click (e) => @expandContextAll(e)
contextExpander.find(".expandLink.above").click (e) => @expandContext(e, 10, "above")
contextExpander.find(".expandLink.below").click (e) => @expandContext(e, 10, "below")
contextExpander.on("click", ".expandLink.all", (e) => @expandContextAll(e))
contextExpander.on("click", ".expandLink.above", (e) => @expandContext(e, 10, "above"))
contextExpander.on("click", ".expandLink.below", (e) => @expandContext(e, 10, "below"))
codeLines.before(contextExpander) if attachDirection == "above"
codeLines.after(contextExpander) if attachDirection == "below"
expander = if attachDirection == "above" then codeLines.prev() else codeLines.next()
Expand Down