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
Kazuhiko Shiozaki
gitlab-ce
Commits
cecdb0b3
Commit
cecdb0b3
authored
Jul 02, 2012
by
Valeriy Sizov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
InlineDiff: refactoring
parent
a0955892
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
71 additions
and
51 deletions
+71
-51
app/helpers/commits_helper.rb
app/helpers/commits_helper.rb
+2
-51
lib/gitlab/inline_diff.rb
lib/gitlab/inline_diff.rb
+69
-0
No files found.
app/helpers/commits_helper.rb
View file @
cecdb0b3
...
@@ -36,8 +36,7 @@ module CommitsHelper
...
@@ -36,8 +36,7 @@ module CommitsHelper
line_new
=
1
line_new
=
1
type
=
nil
type
=
nil
lines_arr
=
inline_diff
diff_arr
lines_arr
=
::
Gitlab
::
InlineDiff
.
processing
diff_arr
#lines_arr = diff_arr
lines_arr
.
each
do
|
line
|
lines_arr
.
each
do
|
line
|
next
if
line
.
match
(
/^\-\-\- \/dev\/null/
)
next
if
line
.
match
(
/^\-\-\- \/dev\/null/
)
next
if
line
.
match
(
/^\+\+\+ \/dev\/null/
)
next
if
line
.
match
(
/^\+\+\+ \/dev\/null/
)
...
@@ -45,9 +44,7 @@ module CommitsHelper
...
@@ -45,9 +44,7 @@ module CommitsHelper
next
if
line
.
match
(
/^\+\+\+ b/
)
next
if
line
.
match
(
/^\+\+\+ b/
)
full_line
=
html_escape
(
line
.
gsub
(
/\n/
,
''
))
full_line
=
html_escape
(
line
.
gsub
(
/\n/
,
''
))
full_line
=
::
Gitlab
::
InlineDiff
.
replace_markers
full_line
full_line
.
gsub!
(
"#!idiff-start!#"
,
"<span class='idiff'>"
)
full_line
.
gsub!
(
"#!idiff-finish!#"
,
"</span>"
)
if
line
.
match
(
/^@@ -/
)
if
line
.
match
(
/^@@ -/
)
type
=
"match"
type
=
"match"
...
@@ -86,50 +83,4 @@ module CommitsHelper
...
@@ -86,50 +83,4 @@ module CommitsHelper
end
end
end
end
def
inline_diff
diff_arr
chain_of_first_symbols
=
""
diff_arr
.
each_with_index
do
|
line
,
i
|
chain_of_first_symbols
+=
line
[
0
]
end
chain_of_first_symbols
.
gsub!
(
/[^\-\+]/
,
"#"
)
offset
=
0
indexes
=
[]
while
index
=
chain_of_first_symbols
.
index
(
"#-+#"
,
offset
)
indexes
<<
index
offset
=
index
+
1
end
indexes
.
each
do
|
index
|
first_line
=
diff_arr
[
index
+
1
]
second_line
=
diff_arr
[
index
+
2
]
max_length
=
[
first_line
.
size
,
second_line
.
size
].
max
first_the_same_symbols
=
0
(
0
..
max_length
+
1
).
each
do
|
i
|
first_the_same_symbols
=
i
-
1
if
first_line
[
i
]
!=
second_line
[
i
]
&&
i
>
0
break
end
end
first_token
=
first_line
[
0
..
first_the_same_symbols
][
1
..-
1
]
diff_arr
[
index
+
1
].
sub!
(
first_token
,
first_token
+
"#!idiff-start!#"
)
diff_arr
[
index
+
2
].
sub!
(
first_token
,
first_token
+
"#!idiff-start!#"
)
last_the_same_symbols
=
0
(
1
..
max_length
+
1
).
each
do
|
i
|
last_the_same_symbols
=
-
i
if
first_line
[
-
i
]
!=
second_line
[
-
i
]
break
end
end
last_the_same_symbols
+=
1
last_token
=
first_line
[
last_the_same_symbols
..-
1
]
diff_arr
[
index
+
1
].
sub!
(
/
#{
last_token
}
$/
,
"#!idiff-finish!#"
+
last_token
)
diff_arr
[
index
+
2
].
sub!
(
/
#{
last_token
}
$/
,
"#!idiff-finish!#"
+
last_token
)
end
diff_arr
end
end
end
lib/gitlab/inline_diff.rb
0 → 100644
View file @
cecdb0b3
module
Gitlab
class
InlineDiff
class
<<
self
START
=
"#!idiff-start!#"
FINISH
=
"#!idiff-finish!#"
def
processing
diff_arr
indexes
=
_indexes_of_changed_lines
diff_arr
indexes
.
each
do
|
index
|
first_line
=
diff_arr
[
index
+
1
]
second_line
=
diff_arr
[
index
+
2
]
max_length
=
[
first_line
.
size
,
second_line
.
size
].
max
first_the_same_symbols
=
0
(
0
..
max_length
+
1
).
each
do
|
i
|
first_the_same_symbols
=
i
-
1
if
first_line
[
i
]
!=
second_line
[
i
]
&&
i
>
0
break
end
end
first_token
=
first_line
[
0
..
first_the_same_symbols
][
1
..-
1
]
diff_arr
[
index
+
1
].
sub!
(
first_token
,
first_token
+
START
)
diff_arr
[
index
+
2
].
sub!
(
first_token
,
first_token
+
START
)
last_the_same_symbols
=
0
(
1
..
max_length
+
1
).
each
do
|
i
|
last_the_same_symbols
=
-
i
if
first_line
[
-
i
]
!=
second_line
[
-
i
]
break
end
end
last_the_same_symbols
+=
1
last_token
=
first_line
[
last_the_same_symbols
..-
1
]
diff_arr
[
index
+
1
].
sub!
(
/
#{
last_token
}
$/
,
FINISH
+
last_token
)
diff_arr
[
index
+
2
].
sub!
(
/
#{
last_token
}
$/
,
FINISH
+
last_token
)
end
diff_arr
end
def
_indexes_of_changed_lines
diff_arr
chain_of_first_symbols
=
""
diff_arr
.
each_with_index
do
|
line
,
i
|
chain_of_first_symbols
+=
line
[
0
]
end
chain_of_first_symbols
.
gsub!
(
/[^\-\+]/
,
"#"
)
offset
=
0
indexes
=
[]
while
index
=
chain_of_first_symbols
.
index
(
"#-+#"
,
offset
)
indexes
<<
index
offset
=
index
+
1
end
indexes
end
def
replace_markers
line
line
.
gsub!
(
START
,
"<span class='idiff'>"
)
line
.
gsub!
(
FINISH
,
"</span>"
)
line
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