Commit bca72f59 authored by micael.bergeron's avatar micael.bergeron

wip: fake its a binary diff

parent b97f9629
......@@ -104,7 +104,7 @@ module API
not_found! 'Commit' unless commit
commit.raw_diffs.to_a
present commit.raw_diffs.to_a, with: Entities::RepoDiff
end
desc "Get a commit's comments" do
......
......@@ -291,10 +291,11 @@ module API
end
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 :renamed_file?, as: :renamed_file
expose :deleted_file?, as: :deleted_file
expose :diff
end
class ProtectedRefAccess < Grape::Entity
......
......@@ -13,6 +13,8 @@ module Gitlab
# https://gitlab.com/gitlab-org/gitlab_git/merge_requests/77#note_4754193
ENCODING_CONFIDENCE_THRESHOLD = 50
#
#
def encode!(message)
return nil unless message.respond_to? :force_encoding
......@@ -22,20 +24,26 @@ module Gitlab
# return message if message type is binary
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
# force detected encoding if we have sufficient confidence.
message.force_encoding(detect[:encoding])
end
# encode and clean the bad chars
message.replace clean(message)
rescue
rescue => e
byebug
encoding = detect ? detect[:encoding] : "unknown"
"--broken encoding: #{encoding}"
end
def binary?(message, detect=nil)
detect ||= CharlockHolmes::EncodingDetector.detect(message)
detect && detect[:type] == :binary && detect[:confidence] == 100
end
def encode_utf8(message)
detect = CharlockHolmes::EncodingDetector.detect(message)
if detect && detect[:encoding]
......
......@@ -116,6 +116,13 @@ module Gitlab
filtered_opts
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
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.
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
def init_from_hash(hash)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment