BigW Consortium Gitlab
Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gitlab-ce
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Forest Godfrey
gitlab-ce
Commits
6faa01e0
Commit
6faa01e0
authored
Jun 01, 2017
by
Robert Speicher
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'bvl-move-gitlab-git-encodinghelper' into 'master'
Rename `Gitlab::Git::EncodingHelper` to `Gitlab::EncodingHelper` See merge request !11772
parents
8d131eb8
e564fe97
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
62 additions
and
64 deletions
+62
-64
merge_request_diff.rb
app/models/merge_request_diff.rb
+1
-1
dynamic_path_validator.rb
app/validators/dynamic_path_validator.rb
+1
-1
encoding_helper.rb
lib/gitlab/encoding_helper.rb
+51
-53
blame.rb
lib/gitlab/git/blame.rb
+1
-1
blob.rb
lib/gitlab/git/blob.rb
+1
-1
commit.rb
lib/gitlab/git/commit.rb
+1
-1
diff.rb
lib/gitlab/git/diff.rb
+1
-1
ref.rb
lib/gitlab/git/ref.rb
+1
-1
tree.rb
lib/gitlab/git/tree.rb
+1
-1
encoding_helper_spec.rb
spec/lib/gitlab/encoding_helper_spec.rb
+2
-2
repository_spec.rb
spec/lib/gitlab/git/repository_spec.rb
+1
-1
No files found.
app/models/merge_request_diff.rb
View file @
6faa01e0
class
MergeRequestDiff
<
ActiveRecord
::
Base
class
MergeRequestDiff
<
ActiveRecord
::
Base
include
Sortable
include
Sortable
include
Importable
include
Importable
include
Gitlab
::
Git
::
EncodingHelper
include
Gitlab
::
EncodingHelper
# Prevent store of diff if commits amount more then 500
# Prevent store of diff if commits amount more then 500
COMMITS_SAFE_SIZE
=
100
COMMITS_SAFE_SIZE
=
100
...
...
app/validators/dynamic_path_validator.rb
View file @
6faa01e0
...
@@ -6,7 +6,7 @@
...
@@ -6,7 +6,7 @@
# Values are checked for formatting and exclusion from a list of illegal path
# Values are checked for formatting and exclusion from a list of illegal path
# names.
# names.
class
DynamicPathValidator
<
ActiveModel
::
EachValidator
class
DynamicPathValidator
<
ActiveModel
::
EachValidator
extend
Gitlab
::
Git
::
EncodingHelper
extend
Gitlab
::
EncodingHelper
class
<<
self
class
<<
self
def
valid_user_path?
(
path
)
def
valid_user_path?
(
path
)
...
...
lib/gitlab/
git/
encoding_helper.rb
→
lib/gitlab/encoding_helper.rb
View file @
6faa01e0
module
Gitlab
module
Gitlab
module
Git
module
EncodingHelper
module
EncodingHelper
extend
self
extend
self
# This threshold is carefully tweaked to prevent usage of encodings detected
# This threshold is carefully tweaked to prevent usage of encodings detected
# by CharlockHolmes with low confidence. If CharlockHolmes confidence is low,
# by CharlockHolmes with low confidence. If CharlockHolmes confidence is low,
# we're better off sticking with utf8 encoding.
# we're better off sticking with utf8 encoding.
# Reason: git diff can return strings with invalid utf8 byte sequences if it
# Reason: git diff can return strings with invalid utf8 byte sequences if it
# truncates a diff in the middle of a multibyte character. In this case
# truncates a diff in the middle of a multibyte character. In this case
# CharlockHolmes will try to guess the encoding and will likely suggest an
# CharlockHolmes will try to guess the encoding and will likely suggest an
# obscure encoding with low confidence.
# obscure encoding with low confidence.
# There is a lot more info with this merge request:
# There is a lot more info with this merge request:
# 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
=
40
ENCODING_CONFIDENCE_THRESHOLD
=
40
def
encode!
(
message
)
def
encode!
(
message
)
return
nil
unless
message
.
respond_to?
:force_encoding
return
nil
unless
message
.
respond_to?
:force_encoding
# if message is utf-8 encoding, just return it
# if message is utf-8 encoding, just return it
message
.
force_encoding
(
"UTF-8"
)
message
.
force_encoding
(
"UTF-8"
)
return
message
if
message
.
valid_encoding?
return
message
if
message
.
valid_encoding?
# 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
detect
&&
detect
[
:type
]
==
:binary
# force detected encoding if we have sufficient confidence.
# 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
message
.
force_encoding
(
detect
[
:encoding
])
message
.
force_encoding
(
detect
[
:encoding
])
end
# encode and clean the bad chars
message
.
replace
clean
(
message
)
rescue
encoding
=
detect
?
detect
[:
encoding
]
:
"unknown"
"--broken encoding:
#{
encoding
}
"
end
end
def
encode_utf8
(
message
)
# encode and clean the bad chars
detect
=
CharlockHolmes
::
EncodingDetector
.
detect
(
message
)
message
.
replace
clean
(
message
)
if
detect
rescue
begin
encoding
=
detect
?
detect
[:
encoding
]
:
"unknown"
CharlockHolmes
::
Converter
.
convert
(
message
,
detect
[
:encoding
],
'UTF-8'
)
"--broken encoding:
#{
encoding
}
"
rescue
ArgumentError
=>
e
end
Rails
.
logger
.
warn
(
"Ignoring error converting
#{
detect
[
:encoding
]
}
into UTF8:
#{
e
.
message
}
"
)
def
encode_utf8
(
message
)
detect
=
CharlockHolmes
::
EncodingDetector
.
detect
(
message
)
if
detect
begin
CharlockHolmes
::
Converter
.
convert
(
message
,
detect
[
:encoding
],
'UTF-8'
)
rescue
ArgumentError
=>
e
Rails
.
logger
.
warn
(
"Ignoring error converting
#{
detect
[
:encoding
]
}
into UTF8:
#{
e
.
message
}
"
)
''
''
end
else
clean
(
message
)
end
end
else
clean
(
message
)
end
end
end
private
private
def
clean
(
message
)
def
clean
(
message
)
message
.
encode
(
"UTF-16BE"
,
undef: :replace
,
invalid: :replace
,
replace:
""
)
message
.
encode
(
"UTF-16BE"
,
undef: :replace
,
invalid: :replace
,
replace:
""
)
.
encode
(
"UTF-8"
)
.
encode
(
"UTF-8"
)
.
gsub
(
"
\0
"
.
encode
(
"UTF-8"
),
""
)
.
gsub
(
"
\0
"
.
encode
(
"UTF-8"
),
""
)
end
end
end
end
end
end
end
lib/gitlab/git/blame.rb
View file @
6faa01e0
module
Gitlab
module
Gitlab
module
Git
module
Git
class
Blame
class
Blame
include
Gitlab
::
Git
::
EncodingHelper
include
Gitlab
::
EncodingHelper
attr_reader
:lines
,
:blames
attr_reader
:lines
,
:blames
...
...
lib/gitlab/git/blob.rb
View file @
6faa01e0
...
@@ -2,7 +2,7 @@ module Gitlab
...
@@ -2,7 +2,7 @@ module Gitlab
module
Git
module
Git
class
Blob
class
Blob
include
Linguist
::
BlobHelper
include
Linguist
::
BlobHelper
include
Gitlab
::
Git
::
EncodingHelper
include
Gitlab
::
EncodingHelper
# This number is the maximum amount of data that we want to display to
# This number is the maximum amount of data that we want to display to
# the user. We load as much as we can for encoding detection
# the user. We load as much as we can for encoding detection
...
...
lib/gitlab/git/commit.rb
View file @
6faa01e0
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
module
Gitlab
module
Gitlab
module
Git
module
Git
class
Commit
class
Commit
include
Gitlab
::
Git
::
EncodingHelper
include
Gitlab
::
EncodingHelper
attr_accessor
:raw_commit
,
:head
,
:refs
attr_accessor
:raw_commit
,
:head
,
:refs
...
...
lib/gitlab/git/diff.rb
View file @
6faa01e0
...
@@ -3,7 +3,7 @@ module Gitlab
...
@@ -3,7 +3,7 @@ module Gitlab
module
Git
module
Git
class
Diff
class
Diff
TimeoutError
=
Class
.
new
(
StandardError
)
TimeoutError
=
Class
.
new
(
StandardError
)
include
Gitlab
::
Git
::
EncodingHelper
include
Gitlab
::
EncodingHelper
# Diff properties
# Diff properties
attr_accessor
:old_path
,
:new_path
,
:a_mode
,
:b_mode
,
:diff
attr_accessor
:old_path
,
:new_path
,
:a_mode
,
:b_mode
,
:diff
...
...
lib/gitlab/git/ref.rb
View file @
6faa01e0
module
Gitlab
module
Gitlab
module
Git
module
Git
class
Ref
class
Ref
include
Gitlab
::
Git
::
EncodingHelper
include
Gitlab
::
EncodingHelper
# Branch or tag name
# Branch or tag name
# without "refs/tags|heads" prefix
# without "refs/tags|heads" prefix
...
...
lib/gitlab/git/tree.rb
View file @
6faa01e0
module
Gitlab
module
Gitlab
module
Git
module
Git
class
Tree
class
Tree
include
Gitlab
::
Git
::
EncodingHelper
include
Gitlab
::
EncodingHelper
attr_accessor
:id
,
:root_id
,
:name
,
:path
,
:type
,
attr_accessor
:id
,
:root_id
,
:name
,
:path
,
:type
,
:mode
,
:commit_id
,
:submodule_url
:mode
,
:commit_id
,
:submodule_url
...
...
spec/lib/gitlab/
git/
encoding_helper_spec.rb
→
spec/lib/gitlab/encoding_helper_spec.rb
View file @
6faa01e0
require
"spec_helper"
require
"spec_helper"
describe
Gitlab
::
Git
::
EncodingHelper
do
describe
Gitlab
::
EncodingHelper
do
let
(
:ext_class
)
{
Class
.
new
{
extend
Gitlab
::
Git
::
EncodingHelper
}
}
let
(
:ext_class
)
{
Class
.
new
{
extend
Gitlab
::
EncodingHelper
}
}
let
(
:binary_string
)
{
File
.
read
(
Rails
.
root
+
"spec/fixtures/dk.png"
)
}
let
(
:binary_string
)
{
File
.
read
(
Rails
.
root
+
"spec/fixtures/dk.png"
)
}
describe
'#encode!'
do
describe
'#encode!'
do
...
...
spec/lib/gitlab/git/repository_spec.rb
View file @
6faa01e0
require
"spec_helper"
require
"spec_helper"
describe
Gitlab
::
Git
::
Repository
,
seed_helper:
true
do
describe
Gitlab
::
Git
::
Repository
,
seed_helper:
true
do
include
Gitlab
::
Git
::
EncodingHelper
include
Gitlab
::
EncodingHelper
let
(
:repository
)
{
Gitlab
::
Git
::
Repository
.
new
(
'default'
,
TEST_REPO_PATH
)
}
let
(
:repository
)
{
Gitlab
::
Git
::
Repository
.
new
(
'default'
,
TEST_REPO_PATH
)
}
...
...
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