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

wip: fake its a binary diff

parent b97f9629
...@@ -104,7 +104,7 @@ module API ...@@ -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
......
...@@ -291,10 +291,11 @@ module API ...@@ -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
......
...@@ -13,6 +13,8 @@ module Gitlab ...@@ -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 ...@@ -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 ...@@ -50,7 +58,7 @@ module Gitlab
clean(message) clean(message)
end end
end end
private private
def clean(message) def clean(message)
......
...@@ -116,6 +116,13 @@ module Gitlab ...@@ -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 ...@@ -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)
......
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