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
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
gitlab-ce
Commits
f5cc3f63
Commit
f5cc3f63
authored
Jul 11, 2016
by
Douwe Maan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Render inline diffs for multiple changed lines following eachother
parent
92772f85
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
57 additions
and
24 deletions
+57
-24
CHANGELOG
CHANGELOG
+1
-0
lib/gitlab/diff/inline_diff.rb
lib/gitlab/diff/inline_diff.rb
+38
-14
spec/lib/gitlab/diff/inline_diff_spec.rb
spec/lib/gitlab/diff/inline_diff_spec.rb
+18
-10
No files found.
CHANGELOG
View file @
f5cc3f63
...
@@ -25,6 +25,7 @@ v 8.10.0 (unreleased)
...
@@ -25,6 +25,7 @@ v 8.10.0 (unreleased)
- Updated layout for Projects, Groups, Users on Admin area !4424
- Updated layout for Projects, Groups, Users on Admin area !4424
- Fix changing issue state columns in milestone view
- Fix changing issue state columns in milestone view
- Add notification settings dropdown for groups
- Add notification settings dropdown for groups
- Render inline diffs for multiple changed lines following eachother
- Wildcards for protected branches. !4665
- Wildcards for protected branches. !4665
- Allow importing from Github using Personal Access Tokens. (Eric K Idema)
- Allow importing from Github using Personal Access Tokens. (Eric K Idema)
- API: Todos !3188 (Robert Schilling)
- API: Todos !3188 (Robert Schilling)
...
...
lib/gitlab/diff/inline_diff.rb
View file @
f5cc3f63
module
Gitlab
module
Gitlab
module
Diff
module
Diff
class
InlineDiff
class
InlineDiff
# Regex to find a run of deleted lines followed by the same number of added lines
REGEX
=
%r{
# Runs start at the beginning of the string (the first line) or after a space (for an unchanged line)
(?:
\A
| )
# This matches a number of `-`s followed by the same number of `+`s through recursion
(?<del_ins>
-
\g
<del_ins>?
\+
)
# Runs end at the end of the string (the last line) or before a space (for an unchanged line)
(?= |
\z
)
}x
.
freeze
attr_accessor
:old_line
,
:new_line
,
:offset
attr_accessor
:old_line
,
:new_line
,
:offset
def
self
.
for_lines
(
lines
)
def
self
.
for_lines
(
lines
)
local_edit_indexes
=
self
.
find_local_edit
s
(
lines
)
changed_line_pairs
=
self
.
find_changed_line_pair
s
(
lines
)
inline_diffs
=
[]
inline_diffs
=
[]
local_edit_indexes
.
each
do
|
index
|
changed_line_pairs
.
each
do
|
old_index
,
new_index
|
old_index
=
index
new_index
=
index
+
1
old_line
=
lines
[
old_index
]
old_line
=
lines
[
old_index
]
new_line
=
lines
[
new_index
]
new_line
=
lines
[
new_index
]
...
@@ -51,18 +65,28 @@ module Gitlab
...
@@ -51,18 +65,28 @@ module Gitlab
private
private
def
self
.
find_local_edits
(
lines
)
# Finds pairs of old/new line pairs that represent the same line that changed
line_prefixes
=
lines
.
map
{
|
line
|
line
.
match
(
/\A([+-])/
)
?
$1
:
' '
}
def
self
.
find_changed_line_pairs
(
lines
)
joined_line_prefixes
=
"
#{
line_prefixes
.
join
}
"
# Prefixes of all diff lines, indicating their types
# For example: `" - + -+ ---+++ --+ -++"`
line_prefixes
=
lines
.
each_with_object
(
""
)
{
|
line
,
s
|
s
<<
line
[
0
]
}.
gsub
(
/[^ +-]/
,
' '
)
changed_line_pairs
=
[]
line_prefixes
.
scan
(
REGEX
)
do
# For `"---+++"`, `begin_index == 0`, `end_index == 6`
begin_index
,
end_index
=
Regexp
.
last_match
.
offset
(
:del_ins
)
offset
=
0
# For `"---+++"`, `changed_line_count == 3`
local_edit_indexes
=
[]
changed_line_count
=
(
end_index
-
begin_index
)
/
2
while
index
=
joined_line_prefixes
.
index
(
" -+ "
,
offset
)
local_edit_indexes
<<
index
halfway_index
=
begin_index
+
changed_line_count
offset
=
index
+
1
(
begin_index
...
halfway_index
).
each
do
|
i
|
# For `"---+++"`, index 1 maps to 1 + 3 = 4
changed_line_pairs
<<
[
i
,
i
+
changed_line_count
]
end
end
end
local_edit_indexe
s
changed_line_pair
s
end
end
def
longest_common_prefix
(
a
,
b
)
def
longest_common_prefix
(
a
,
b
)
...
...
spec/lib/gitlab/diff/inline_diff_spec.rb
View file @
f5cc3f63
...
@@ -3,14 +3,19 @@ require 'spec_helper'
...
@@ -3,14 +3,19 @@ require 'spec_helper'
describe
Gitlab
::
Diff
::
InlineDiff
,
lib:
true
do
describe
Gitlab
::
Diff
::
InlineDiff
,
lib:
true
do
describe
'.for_lines'
do
describe
'.for_lines'
do
let
(
:diff
)
do
let
(
:diff
)
do
<<
eos
<<
-
EOF
.
strip_heredoc
class Test
class Test
- def initialize(test = true)
- def initialize(test = true)
+ def initialize(test = false)
+ def initialize(test = false)
@test = test
@test = test
- if true
- @foo = "bar"
+ unless false
+ @foo = "baz"
end
end
end
end
eos
end
EOF
end
end
let
(
:subject
)
{
described_class
.
for_lines
(
diff
.
lines
)
}
let
(
:subject
)
{
described_class
.
for_lines
(
diff
.
lines
)
}
...
@@ -20,8 +25,11 @@ eos
...
@@ -20,8 +25,11 @@ eos
expect
(
subject
[
1
]).
to
eq
([
25
..
27
])
expect
(
subject
[
1
]).
to
eq
([
25
..
27
])
expect
(
subject
[
2
]).
to
eq
([
25
..
28
])
expect
(
subject
[
2
]).
to
eq
([
25
..
28
])
expect
(
subject
[
3
]).
to
be_nil
expect
(
subject
[
3
]).
to
be_nil
expect
(
subject
[
4
]).
to
be_nil
expect
(
subject
[
4
]).
to
eq
([
5
..
10
])
expect
(
subject
[
5
]).
to
be_nil
expect
(
subject
[
5
]).
to
eq
([
17
..
17
])
expect
(
subject
[
6
]).
to
eq
([
5
..
15
])
expect
(
subject
[
7
]).
to
eq
([
17
..
17
])
expect
(
subject
[
8
]).
to
be_nil
end
end
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