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
fba059da
Commit
fba059da
authored
Dec 06, 2021
by
Eduardo Bonet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moving CustomDiff to /lib folder
parent
7bcf737a
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
123 additions
and
58 deletions
+123
-58
app/helpers/custom_diff_helper.rb
app/helpers/custom_diff_helper.rb
+0
-54
app/presenters/blob_presenter.rb
app/presenters/blob_presenter.rb
+2
-2
lib/gitlab/diff/custom_diff.rb
lib/gitlab/diff/custom_diff.rb
+57
-0
lib/gitlab/diff/file.rb
lib/gitlab/diff/file.rb
+1
-1
spec/lib/gitlab/diff/custom_diff_spec.rb
spec/lib/gitlab/diff/custom_diff_spec.rb
+62
-0
spec/presenters/blob_presenter_spec.rb
spec/presenters/blob_presenter_spec.rb
+1
-1
No files found.
app/helpers/custom_diff_helper.rb
deleted
100644 → 0
View file @
7bcf737a
# frozen_string_literal: true
module
CustomDiffHelper
class
<<
self
def
preprocess_before_diff
(
path
,
old_blob
,
new_blob
)
return
unless
path
.
ends_with?
'.ipynb'
transformed_diff
=
IpynbDiff
.
diff
(
old_blob
&
.
data
,
new_blob
&
.
data
,
diff_opts:
{
context:
5
,
include_diff_info:
true
},
transform_options:
{
cell_decorator: :percent
},
raise_if_invalid_notebook:
true
)
new_diff
=
strip_diff_frontmatter
(
transformed_diff
)
transformed_for_diff
(
new_blob
,
old_blob
)
if
new_diff
Gitlab
::
AppLogger
.
info
({
message:
new_diff
?
'IPYNB_DIFF_GENERATED'
:
'IPYNB_DIFF_NIL'
})
new_diff
rescue
IpynbDiff
::
InvalidNotebookError
=>
e
Gitlab
::
ErrorTracking
.
log_exception
(
e
)
nil
end
def
transformed_blob_language
(
blob
)
'md'
if
transformed_for_diff?
(
blob
)
end
def
transformed_blob_data
(
blob
)
if
transformed_for_diff?
(
blob
)
IpynbDiff
.
transform
(
blob
.
data
,
raise_errors:
true
,
options:
{
include_metadata:
false
,
cell_decorator: :percent
})
end
end
def
strip_diff_frontmatter
(
diff_content
)
diff_content
.
scan
(
/.*\n/
)[
2
..-
1
]
&
.
join
(
''
)
if
diff_content
.
present?
end
def
blobs_with_transformed_diffs
@blobs_with_transformed_diffs
||=
{}
end
def
transformed_for_diff?
(
blob
)
blobs_with_transformed_diffs
[
blob
]
end
def
transformed_for_diff
(
*
blobs
)
blobs
.
each
do
|
b
|
blobs_with_transformed_diffs
[
b
]
=
true
if
b
end
end
end
end
app/presenters/blob_presenter.rb
View file @
fba059da
...
@@ -15,8 +15,8 @@ class BlobPresenter < Gitlab::View::Presenter::Delegated
...
@@ -15,8 +15,8 @@ class BlobPresenter < Gitlab::View::Presenter::Delegated
Gitlab
::
Highlight
.
highlight
(
Gitlab
::
Highlight
.
highlight
(
blob
.
path
,
blob
.
path
,
CustomDiffHelper
.
transformed_blob_data
(
blob
)
||
limited_blob_data
(
to:
to
),
Gitlab
::
Diff
::
CustomDiff
.
transformed_blob_data
(
blob
)
||
limited_blob_data
(
to:
to
),
language:
CustomDiffHelper
.
transformed_blob_language
(
blob
)
||
language
,
language:
Gitlab
::
Diff
::
CustomDiff
.
transformed_blob_language
(
blob
)
||
language
,
plain:
plain
plain:
plain
)
)
end
end
...
...
lib/gitlab/diff/custom_diff.rb
0 → 100644
View file @
fba059da
# frozen_string_literal: true
module
Gitlab
module
Diff
module
CustomDiff
class
<<
self
def
preprocess_before_diff
(
path
,
old_blob
,
new_blob
)
return
unless
path
.
ends_with?
'.ipynb'
transformed_diff
=
IpynbDiff
.
diff
(
old_blob
&
.
data
,
new_blob
&
.
data
,
diff_opts:
{
context:
5
,
include_diff_info:
true
},
transform_options:
{
cell_decorator: :percent
},
raise_if_invalid_notebook:
true
)
new_diff
=
strip_diff_frontmatter
(
transformed_diff
)
transformed_for_diff
(
new_blob
,
old_blob
)
if
new_diff
Gitlab
::
AppLogger
.
info
({
message:
new_diff
?
'IPYNB_DIFF_GENERATED'
:
'IPYNB_DIFF_NIL'
})
new_diff
rescue
IpynbDiff
::
InvalidNotebookError
=>
e
Gitlab
::
ErrorTracking
.
log_exception
(
e
)
nil
end
def
transformed_blob_language
(
blob
)
'md'
if
transformed_for_diff?
(
blob
)
end
def
transformed_blob_data
(
blob
)
if
transformed_for_diff?
(
blob
)
IpynbDiff
.
transform
(
blob
.
data
,
raise_errors:
true
,
options:
{
include_metadata:
false
,
cell_decorator: :percent
})
end
end
def
strip_diff_frontmatter
(
diff_content
)
diff_content
.
scan
(
/.*\n/
)[
2
..-
1
]
&
.
join
(
''
)
if
diff_content
.
present?
end
def
blobs_with_transformed_diffs
@blobs_with_transformed_diffs
||=
{}
end
def
transformed_for_diff?
(
blob
)
blobs_with_transformed_diffs
[
blob
]
end
def
transformed_for_diff
(
*
blobs
)
blobs
.
each
do
|
b
|
blobs_with_transformed_diffs
[
b
]
=
true
if
b
end
end
end
end
end
end
lib/gitlab/diff/file.rb
View file @
fba059da
...
@@ -44,7 +44,7 @@ module Gitlab
...
@@ -44,7 +44,7 @@ module Gitlab
new_blob_lazy
new_blob_lazy
old_blob_lazy
old_blob_lazy
diff
.
diff
=
CustomDiffHelper
.
preprocess_before_diff
(
diff
.
new_path
,
old_blob_lazy
,
new_blob_lazy
)
||
diff
.
diff
if
use_custom_diff?
diff
.
diff
=
Gitlab
::
Diff
::
CustomDiff
.
preprocess_before_diff
(
diff
.
new_path
,
old_blob_lazy
,
new_blob_lazy
)
||
diff
.
diff
if
use_custom_diff?
end
end
def
use_custom_diff?
def
use_custom_diff?
...
...
spec/
helpers/custom_diff_helper
_spec.rb
→
spec/
lib/gitlab/diff/custom_diff
_spec.rb
View file @
fba059da
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
require
'spec_helper'
require
'spec_helper'
RSpec
.
describe
CustomDiffHelper
do
RSpec
.
describe
Gitlab
::
Diff
::
CustomDiff
do
include
RepoHelpers
include
RepoHelpers
let
(
:project
)
{
create
(
:project
,
:repository
)
}
let
(
:project
)
{
create
(
:project
,
:repository
)
}
...
@@ -10,53 +10,53 @@ RSpec.describe CustomDiffHelper do
...
@@ -10,53 +10,53 @@ RSpec.describe CustomDiffHelper do
let
(
:ipynb_blob
)
{
repository
.
blob_at
(
'f6b7a707'
,
'files/ipython/markdown-table.ipynb'
)
}
let
(
:ipynb_blob
)
{
repository
.
blob_at
(
'f6b7a707'
,
'files/ipython/markdown-table.ipynb'
)
}
let
(
:blob
)
{
repository
.
blob_at
(
'HEAD'
,
'files/ruby/regex.rb'
)
}
let
(
:blob
)
{
repository
.
blob_at
(
'HEAD'
,
'files/ruby/regex.rb'
)
}
describe
"#preprocess_before_diff"
do
describe
'#preprocess_before_diff'
do
context
'for ipynb files'
do
context
'for ipynb files'
do
it
'transforms the diff'
do
it
'transforms the diff'
do
expect
(
CustomDiffHelper
.
preprocess_before_diff
(
ipynb_blob
.
path
,
nil
,
ipynb_blob
)).
not_to
include
(
'cells'
)
expect
(
described_class
.
preprocess_before_diff
(
ipynb_blob
.
path
,
nil
,
ipynb_blob
)).
not_to
include
(
'cells'
)
end
end
it
'adds the blob to the list of transformed blobs'
do
it
'adds the blob to the list of transformed blobs'
do
CustomDiffHelper
.
preprocess_before_diff
(
ipynb_blob
.
path
,
nil
,
ipynb_blob
)
described_class
.
preprocess_before_diff
(
ipynb_blob
.
path
,
nil
,
ipynb_blob
)
expect
(
CustomDiffHelper
.
transformed_for_diff?
(
ipynb_blob
)).
to
be_truthy
expect
(
described_class
.
transformed_for_diff?
(
ipynb_blob
)).
to
be_truthy
end
end
end
end
context
'for other files'
do
context
'for other files'
do
it
'returns nil'
do
it
'returns nil'
do
expect
(
CustomDiffHelper
.
preprocess_before_diff
(
blob
.
path
,
nil
,
blob
)).
to
be_nil
expect
(
described_class
.
preprocess_before_diff
(
blob
.
path
,
nil
,
blob
)).
to
be_nil
end
end
it
'does not add the blob to the list of transformed blobs'
do
it
'does not add the blob to the list of transformed blobs'
do
CustomDiffHelper
.
preprocess_before_diff
(
blob
.
path
,
nil
,
blob
)
described_class
.
preprocess_before_diff
(
blob
.
path
,
nil
,
blob
)
expect
(
CustomDiffHelper
.
transformed_for_diff?
(
blob
)).
to
be_falsey
expect
(
described_class
.
transformed_for_diff?
(
blob
)).
to
be_falsey
end
end
end
end
end
end
describe
"#transformed_blob_data"
do
describe
'#transformed_blob_data'
do
it
'transforms blob data if file was processed'
do
it
'transforms blob data if file was processed'
do
CustomDiffHelper
.
preprocess_before_diff
(
ipynb_blob
.
path
,
nil
,
ipynb_blob
)
described_class
.
preprocess_before_diff
(
ipynb_blob
.
path
,
nil
,
ipynb_blob
)
expect
(
CustomDiffHelper
.
transformed_blob_data
(
ipynb_blob
)).
not_to
include
(
'cells'
)
expect
(
described_class
.
transformed_blob_data
(
ipynb_blob
)).
not_to
include
(
'cells'
)
end
end
it
'
transforms
blob data if file was not processed'
do
it
'
does not transform
blob data if file was not processed'
do
expect
(
CustomDiffHelper
.
transformed_blob_data
(
ipynb_blob
)).
to
be_nil
expect
(
described_class
.
transformed_blob_data
(
ipynb_blob
)).
to
be_nil
end
end
end
end
describe
"#transformed_blob_language"
do
describe
'#transformed_blob_language'
do
it
'is when file was preprocessed'
do
it
'is
md
when file was preprocessed'
do
CustomDiffHelper
.
preprocess_before_diff
(
ipynb_blob
.
path
,
nil
,
ipynb_blob
)
described_class
.
preprocess_before_diff
(
ipynb_blob
.
path
,
nil
,
ipynb_blob
)
expect
(
CustomDiffHelper
.
transformed_blob_language
(
ipynb_blob
)).
to
eq
(
'md'
)
expect
(
described_class
.
transformed_blob_language
(
ipynb_blob
)).
to
eq
(
'md'
)
end
end
it
'is nil for
.ipynb, but blob when i
t was not preprocessed'
do
it
'is nil for
a .ipynb blob tha
t was not preprocessed'
do
expect
(
CustomDiffHelper
.
transformed_blob_language
(
ipynb_blob
)).
to
be_nil
expect
(
described_class
.
transformed_blob_language
(
ipynb_blob
)).
to
be_nil
end
end
end
end
end
end
spec/presenters/blob_presenter_spec.rb
View file @
fba059da
...
@@ -161,7 +161,7 @@ RSpec.describe BlobPresenter do
...
@@ -161,7 +161,7 @@ RSpec.describe BlobPresenter do
let
(
:git_blob
)
{
blob
.
__getobj__
}
let
(
:git_blob
)
{
blob
.
__getobj__
}
before
do
before
do
allow
(
CustomDiffHelper
).
to
receive
(
:transformed_for_diff?
).
and_return
(
true
)
allow
(
Gitlab
::
Diff
::
CustomDiff
).
to
receive
(
:transformed_for_diff?
).
and_return
(
true
)
end
end
it
'uses md as the transformed language'
do
it
'uses md as the transformed language'
do
...
...
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