Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
0ecf9ab8
Commit
0ecf9ab8
authored
Mar 18, 2022
by
Gary Holtz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adjusting some specs based on review
parent
d67b22ad
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
47 additions
and
17 deletions
+47
-17
config/feature_flags/development/convert_diff_to_utf8_with_replacement_symbol.yml
...elopment/convert_diff_to_utf8_with_replacement_symbol.yml
+2
-2
lib/gitlab/git/diff.rb
lib/gitlab/git/diff.rb
+8
-3
spec/lib/gitlab/git/diff_spec.rb
spec/lib/gitlab/git/diff_spec.rb
+37
-12
No files found.
config/feature_flags/development/convert_diff_to_utf8_with_replacement_symbol.yml
View file @
0ecf9ab8
---
name
:
convert_diff_to_utf8_with_replacement_symbol
introduced_by_url
:
rollout_issue_url
:
introduced_by_url
:
https://gitlab.com/gitlab-org/gitlab/-/merge_requests/79996
rollout_issue_url
:
https://gitlab.com/gitlab-org/gitlab/-/issues/354526
milestone
:
'
14.9'
type
:
development
group
:
group::code review
...
...
lib/gitlab/git/diff.rb
View file @
0ecf9ab8
...
...
@@ -158,9 +158,7 @@ module Gitlab
raise
"Invalid raw diff type:
#{
raw_diff
.
class
}
"
end
if
Feature
.
enabled?
(
:convert_diff_to_utf8_with_replacement_symbol
,
default_enabled: :yaml
)
@diff
=
Gitlab
::
EncodingHelper
.
encode_utf8_with_replacement_character
(
@diff
)
if
replace_invalid_utf8_chars
&&
!
detect_binary?
(
@diff
)
end
encode_diff_to_utf8
(
replace_invalid_utf8_chars
)
end
def
to_hash
...
...
@@ -231,6 +229,13 @@ module Gitlab
private
def
encode_diff_to_utf8
(
replace_invalid_utf8_chars
)
return
unless
Feature
.
enabled?
(
:convert_diff_to_utf8_with_replacement_symbol
,
default_enabled: :yaml
)
return
unless
replace_invalid_utf8_chars
&&
!
detect_binary?
(
@diff
)
@diff
=
Gitlab
::
EncodingHelper
.
encode_utf8_with_replacement_character
(
@diff
)
end
def
init_from_hash
(
hash
)
raw_diff
=
hash
.
symbolize_keys
...
...
spec/lib/gitlab/git/diff_spec.rb
View file @
0ecf9ab8
...
...
@@ -166,23 +166,44 @@ EOT
let
(
:bad_string
)
{
[
0xae
].
pack
(
"C*"
)
}
let
(
:bad_string_two
)
{
[
0x89
].
pack
(
"C*"
)
}
context
'when replace_invalid_utf8_chars is true'
do
let
(
:diff
)
{
described_class
.
new
(
@raw_diff_hash
.
merge
({
diff:
bad_string
}))
}
let
(
:diff_two
)
{
described_class
.
new
(
@raw_diff_hash
.
merge
({
diff:
bad_string_two
}))
}
let
(
:diff
)
{
described_class
.
new
(
@raw_diff_hash
.
merge
({
diff:
bad_string
}))
}
let
(
:diff_two
)
{
described_class
.
new
(
@raw_diff_hash
.
merge
({
diff:
bad_string_two
}))
}
context
'when replace_invalid_utf8_chars is true'
do
it
'will convert invalid characters and not cause an encoding error'
do
expect
(
diff
.
diff
).
to
include
(
Gitlab
::
EncodingHelper
::
UNICODE_REPLACEMENT_CHARACTER
)
expect
(
diff_two
.
diff
).
to
include
(
Gitlab
::
EncodingHelper
::
UNICODE_REPLACEMENT_CHARACTER
)
expect
{
Oj
.
dump
(
diff
)
}.
not_to
raise_error
(
EncodingError
)
expect
{
Oj
.
dump
(
diff_two
)
}.
not_to
raise_error
(
EncodingError
)
end
context
'when the diff is binary'
do
let
(
:project
)
{
create
(
:project
,
:repository
)
}
it
'will not try to replace characters'
do
expect
(
Gitlab
::
EncodingHelper
).
not_to
receive
(
:encode_utf8_with_replacement_character?
)
expect
(
binary_diff
(
project
).
diff
).
not_to
be_empty
end
end
context
'when convert_diff_to_utf8_with_replacement_symbol feature flag is disabled'
do
before
do
stub_feature_flags
(
convert_diff_to_utf8_with_replacement_symbol:
false
)
end
it
'will not try to convert invalid characters'
do
expect
(
Gitlab
::
EncodingHelper
).
not_to
receive
(
:encode_utf8_with_replacement_character?
)
end
end
end
context
'when replace_invalid_utf8_chars is false'
do
let
(
:
diff
)
{
described_class
.
new
(
@raw_diff_hash
.
merge
({
diff:
bad_string
}),
replace_invalid_utf8_chars:
false
)
}
let
(
:
diff_two
)
{
described_class
.
new
(
@raw_diff_hash
.
merge
({
diff:
bad_string_two
}),
replace_invalid_utf8_chars:
false
)
}
let
(
:
not_replaced_diff
)
{
described_class
.
new
(
@raw_diff_hash
.
merge
({
diff:
bad_string
,
replace_invalid_utf8_chars:
false
})
)
}
let
(
:
not_replaced_diff_two
)
{
described_class
.
new
(
@raw_diff_hash
.
merge
({
diff:
bad_string_two
,
replace_invalid_utf8_chars:
false
})
)
}
it
'will not convert any invalid characters'
do
expect
(
diff
).
not_to
include
(
Gitlab
::
EncodingHelper
::
UNICODE_REPLACEMENT_CHARACTER
)
expect
(
diff_two
).
not_to
include
(
Gitlab
::
EncodingHelper
::
UNICODE_REPLACEMENT_CHARACTER
)
it
'will not try to convert invalid characters'
do
expect
(
Gitlab
::
EncodingHelper
).
not_to
receive
(
:encode_utf8_with_replacement_character?
)
end
end
end
...
...
@@ -280,12 +301,11 @@ EOT
let
(
:project
)
{
create
(
:project
,
:repository
)
}
it
'fake binary message when it detects binary'
do
# Rugged will not detect this as binary, but we can fake it
diff_message
=
"Binary files files/images/icn-time-tracking.pdf and files/images/icn-time-tracking.pdf differ
\n
"
binary_diff
=
described_class
.
between
(
project
.
repository
,
'add-pdf-text-binary'
,
'add-pdf-text-binary^'
).
first
expect
(
binary_diff
.
diff
).
not_to
be_empty
expect
(
binary_diff
.
json_safe_diff
).
to
eq
(
diff_message
)
diff
=
binary_diff
(
project
)
expect
(
diff
.
diff
).
not_to
be_empty
expect
(
diff
.
json_safe_diff
).
to
eq
(
diff_message
)
end
it
'leave non-binary diffs as-is'
do
...
...
@@ -399,4 +419,9 @@ EOT
expect
(
diff
.
line_count
).
to
eq
(
0
)
end
end
def
binary_diff
(
project
)
# rugged will not detect this as binary, but we can fake it
described_class
.
between
(
project
.
repository
,
'add-pdf-text-binary'
,
'add-pdf-text-binary^'
).
first
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment