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
0
Merge Requests
0
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
Tatuya Kamada
gitlab-ce
Commits
985af1a6
Commit
985af1a6
authored
Mar 13, 2017
by
mhasbini
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
take nonewline context into account in diff parser
parent
9ed3db91
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
59 additions
and
7 deletions
+59
-7
app/views/projects/diffs/_line.html.haml
app/views/projects/diffs/_line.html.haml
+1
-1
app/views/projects/diffs/_parallel_view.html.haml
app/views/projects/diffs/_parallel_view.html.haml
+2
-2
lib/gitlab/diff/line.rb
lib/gitlab/diff/line.rb
+3
-3
lib/gitlab/diff/parser.rb
lib/gitlab/diff/parser.rb
+5
-1
spec/lib/gitlab/diff/parser_spec.rb
spec/lib/gitlab/diff/parser_spec.rb
+48
-0
No files found.
app/views/projects/diffs/_line.html.haml
View file @
985af1a6
...
@@ -9,7 +9,7 @@
...
@@ -9,7 +9,7 @@
-
case
type
-
case
type
-
when
'match'
-
when
'match'
=
diff_match_line
line
.
old_pos
,
line
.
new_pos
,
text:
line
.
text
=
diff_match_line
line
.
old_pos
,
line
.
new_pos
,
text:
line
.
text
-
when
'nonewline'
-
when
'
old-nonewline'
,
'new-
nonewline'
%td
.old_line.diff-line-num
%td
.old_line.diff-line-num
%td
.new_line.diff-line-num
%td
.new_line.diff-line-num
%td
.line_content.match
=
line
.
text
%td
.line_content.match
=
line
.
text
...
...
app/views/projects/diffs/_parallel_view.html.haml
View file @
985af1a6
...
@@ -12,7 +12,7 @@
...
@@ -12,7 +12,7 @@
-
case
left
.
type
-
case
left
.
type
-
when
'match'
-
when
'match'
=
diff_match_line
left
.
old_pos
,
nil
,
text:
left
.
text
,
view: :parallel
=
diff_match_line
left
.
old_pos
,
nil
,
text:
left
.
text
,
view: :parallel
-
when
'nonewline'
-
when
'
old-nonewline'
,
'new-
nonewline'
%td
.old_line.diff-line-num
%td
.old_line.diff-line-num
%td
.line_content.match
=
left
.
text
%td
.line_content.match
=
left
.
text
-
else
-
else
...
@@ -31,7 +31,7 @@
...
@@ -31,7 +31,7 @@
-
case
right
.
type
-
case
right
.
type
-
when
'match'
-
when
'match'
=
diff_match_line
nil
,
right
.
new_pos
,
text:
left
.
text
,
view: :parallel
=
diff_match_line
nil
,
right
.
new_pos
,
text:
left
.
text
,
view: :parallel
-
when
'nonewline'
-
when
'
old-nonewline'
,
'new-
nonewline'
%td
.new_line.diff-line-num
%td
.new_line.diff-line-num
%td
.line_content.match
=
right
.
text
%td
.line_content.match
=
right
.
text
-
else
-
else
...
...
lib/gitlab/diff/line.rb
View file @
985af1a6
...
@@ -38,11 +38,11 @@ module Gitlab
...
@@ -38,11 +38,11 @@ module Gitlab
end
end
def
added?
def
added?
type
==
'new'
type
==
'new'
||
type
==
'new-nonewline'
end
end
def
removed?
def
removed?
type
==
'old'
type
==
'old'
||
type
==
'old-nonewline'
end
end
def
rich_text
def
rich_text
...
@@ -52,7 +52,7 @@ module Gitlab
...
@@ -52,7 +52,7 @@ module Gitlab
end
end
def
meta?
def
meta?
type
==
'match'
||
type
==
'nonewline'
type
==
'match'
end
end
def
as_json
(
opts
=
nil
)
def
as_json
(
opts
=
nil
)
...
...
lib/gitlab/diff/parser.rb
View file @
985af1a6
...
@@ -11,6 +11,7 @@ module Gitlab
...
@@ -11,6 +11,7 @@ module Gitlab
line_old
=
1
line_old
=
1
line_new
=
1
line_new
=
1
type
=
nil
type
=
nil
context
=
nil
# By returning an Enumerator we make it possible to search for a single line (with #find)
# By returning an Enumerator we make it possible to search for a single line (with #find)
# without having to instantiate all the others that come after it.
# without having to instantiate all the others that come after it.
...
@@ -31,7 +32,8 @@ module Gitlab
...
@@ -31,7 +32,8 @@ module Gitlab
line_obj_index
+=
1
line_obj_index
+=
1
next
next
elsif
line
[
0
]
==
'\\'
elsif
line
[
0
]
==
'\\'
type
=
'nonewline'
type
=
"
#{
context
}
-nonewline"
yielder
<<
Gitlab
::
Diff
::
Line
.
new
(
full_line
,
type
,
line_obj_index
,
line_old
,
line_new
)
yielder
<<
Gitlab
::
Diff
::
Line
.
new
(
full_line
,
type
,
line_obj_index
,
line_old
,
line_new
)
line_obj_index
+=
1
line_obj_index
+=
1
else
else
...
@@ -43,8 +45,10 @@ module Gitlab
...
@@ -43,8 +45,10 @@ module Gitlab
case
line
[
0
]
case
line
[
0
]
when
"+"
when
"+"
line_new
+=
1
line_new
+=
1
context
=
:new
when
"-"
when
"-"
line_old
+=
1
line_old
+=
1
context
=
:old
when
"
\\
"
# rubocop:disable Lint/EmptyWhen
when
"
\\
"
# rubocop:disable Lint/EmptyWhen
# No increment
# No increment
else
else
...
...
spec/lib/gitlab/diff/parser_spec.rb
View file @
985af1a6
...
@@ -91,6 +91,54 @@ eos
...
@@ -91,6 +91,54 @@ eos
end
end
end
end
describe
'\ No newline at end of file'
do
it
"parses nonewline in one file correctly"
do
first_nonewline_diff
=
<<~
END
--- a/test
+++ b/test
@@ -1,2 +1,2 @@
+ipsum
lorem
-ipsum
\\
No newline at end of file
END
lines
=
parser
.
parse
(
first_nonewline_diff
.
lines
).
to_a
expect
(
lines
[
0
].
type
).
to
eq
(
'new'
)
expect
(
lines
[
0
].
text
).
to
eq
(
'+ipsum'
)
expect
(
lines
[
2
].
type
).
to
eq
(
'old'
)
expect
(
lines
[
3
].
type
).
to
eq
(
'old-nonewline'
)
expect
(
lines
[
1
].
old_pos
).
to
eq
(
1
)
expect
(
lines
[
1
].
new_pos
).
to
eq
(
2
)
end
it
"parses nonewline in two files correctly"
do
both_nonewline_diff
=
<<~
END
--- a/test
+++ b/test
@@ -1,2 +1,2 @@
-lorem
-ipsum
\\
No newline at end of file
+ipsum
+lorem
\\
No newline at end of file
END
lines
=
parser
.
parse
(
both_nonewline_diff
.
lines
).
to_a
expect
(
lines
[
0
].
type
).
to
eq
(
'old'
)
expect
(
lines
[
1
].
type
).
to
eq
(
'old'
)
expect
(
lines
[
2
].
type
).
to
eq
(
'old-nonewline'
)
expect
(
lines
[
5
].
type
).
to
eq
(
'new-nonewline'
)
expect
(
lines
[
3
].
text
).
to
eq
(
'+ipsum'
)
expect
(
lines
[
3
].
old_pos
).
to
eq
(
3
)
expect
(
lines
[
3
].
new_pos
).
to
eq
(
1
)
expect
(
lines
[
4
].
text
).
to
eq
(
'+lorem'
)
expect
(
lines
[
4
].
old_pos
).
to
eq
(
3
)
expect
(
lines
[
4
].
new_pos
).
to
eq
(
2
)
end
end
context
'when lines is empty'
do
context
'when lines is empty'
do
it
{
expect
(
parser
.
parse
([])).
to
eq
([])
}
it
{
expect
(
parser
.
parse
([])).
to
eq
([])
}
it
{
expect
(
parser
.
parse
(
nil
)).
to
eq
([])
}
it
{
expect
(
parser
.
parse
(
nil
)).
to
eq
([])
}
...
...
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