wip: fake its a binary diff

This commit is contained in:
micael.bergeron 2017-09-03 07:45:44 -04:00
parent b97f9629ca
commit bca72f5906
4 changed files with 30 additions and 7 deletions

View file

@ -104,7 +104,7 @@ module API
not_found! 'Commit' unless commit not_found! 'Commit' unless commit
commit.raw_diffs.to_a present commit.raw_diffs.to_a, with: Entities::RepoDiff
end end
desc "Get a commit's comments" do desc "Get a commit's comments" do

View file

@ -291,10 +291,11 @@ module API
end end
class RepoDiff < Grape::Entity class RepoDiff < Grape::Entity
expose :old_path, :new_path, :a_mode, :b_mode, :diff expose :old_path, :new_path, :a_mode, :b_mode
expose :new_file?, as: :new_file expose :new_file?, as: :new_file
expose :renamed_file?, as: :renamed_file expose :renamed_file?, as: :renamed_file
expose :deleted_file?, as: :deleted_file expose :deleted_file?, as: :deleted_file
expose :diff
end end
class ProtectedRefAccess < Grape::Entity class ProtectedRefAccess < Grape::Entity

View file

@ -13,6 +13,8 @@ module Gitlab
# https://gitlab.com/gitlab-org/gitlab_git/merge_requests/77#note_4754193 # https://gitlab.com/gitlab-org/gitlab_git/merge_requests/77#note_4754193
ENCODING_CONFIDENCE_THRESHOLD = 50 ENCODING_CONFIDENCE_THRESHOLD = 50
#
#
def encode!(message) def encode!(message)
return nil unless message.respond_to? :force_encoding return nil unless message.respond_to? :force_encoding
@ -22,20 +24,26 @@ module Gitlab
# return message if message type is binary # return message if message type is binary
detect = CharlockHolmes::EncodingDetector.detect(message) detect = CharlockHolmes::EncodingDetector.detect(message)
return message.force_encoding("BINARY") if detect && detect[:type] == :binary return message.force_encoding("BINARY") if binary?(message, detect)
# force detected encoding if we have sufficient confidence.
if detect && detect[:encoding] && detect[:confidence] > ENCODING_CONFIDENCE_THRESHOLD if detect && detect[:encoding] && detect[:confidence] > ENCODING_CONFIDENCE_THRESHOLD
# force detected encoding if we have sufficient confidence.
message.force_encoding(detect[:encoding]) message.force_encoding(detect[:encoding])
end end
# encode and clean the bad chars # encode and clean the bad chars
message.replace clean(message) message.replace clean(message)
rescue rescue => e
byebug
encoding = detect ? detect[:encoding] : "unknown" encoding = detect ? detect[:encoding] : "unknown"
"--broken encoding: #{encoding}" "--broken encoding: #{encoding}"
end end
def binary?(message, detect=nil)
detect ||= CharlockHolmes::EncodingDetector.detect(message)
detect && detect[:type] == :binary && detect[:confidence] == 100
end
def encode_utf8(message) def encode_utf8(message)
detect = CharlockHolmes::EncodingDetector.detect(message) detect = CharlockHolmes::EncodingDetector.detect(message)
if detect && detect[:encoding] if detect && detect[:encoding]
@ -50,7 +58,7 @@ module Gitlab
clean(message) clean(message)
end end
end end
private private
def clean(message) def clean(message)

View file

@ -116,6 +116,13 @@ module Gitlab
filtered_opts filtered_opts
end end
# Return a binary diff message like:
#
# "Binary files a/file/path and b/file/path differ\n"
def binary_message(old_path, new_path)
"Binary files #{old_path} and #{new_path} differ\n"
end
end end
def initialize(raw_diff, expanded: true) def initialize(raw_diff, expanded: true)
@ -214,7 +221,14 @@ module Gitlab
# binary we're not going to display anything so we skip the size check. # binary we're not going to display anything so we skip the size check.
return if !patch.delta.binary? && prune_large_patch(patch) return if !patch.delta.binary? && prune_large_patch(patch)
@diff = encode!(strip_diff_headers(patch.to_s)) diff = strip_diff_headers(patch.to_s)
@diff = if binary?(diff)
# the diff is binary, let's make a message for it
Diff::binary_message(patch.delta.old_file[:path],
patch.delta.new_file[:path])
else
encode!(diff)
end
end end
def init_from_hash(hash) def init_from_hash(hash)