Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
gitlab-foss
提交
e6e29f92
G
gitlab-foss
项目概览
李少辉-开发者
/
gitlab-foss
通知
15
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
gitlab-foss
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
e6e29f92
编写于
6月 06, 2017
作者:
D
Douwe Maan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Use Diff::File blob methods from diff highlighter
上级
1bc80c25
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
54 addition
and
54 deletion
+54
-54
app/controllers/projects/blob_controller.rb
app/controllers/projects/blob_controller.rb
+5
-3
lib/gitlab/diff/file.rb
lib/gitlab/diff/file.rb
+25
-13
lib/gitlab/diff/highlight.rb
lib/gitlab/diff/highlight.rb
+11
-6
lib/gitlab/highlight.rb
lib/gitlab/highlight.rb
+0
-8
spec/lib/gitlab/highlight_spec.rb
spec/lib/gitlab/highlight_spec.rb
+13
-24
未找到文件。
app/controllers/projects/blob_controller.rb
浏览文件 @
e6e29f92
...
...
@@ -93,9 +93,11 @@ class Projects::BlobController < Projects::ApplicationController
def
diff
apply_diff_view_cookie!
@form
=
UnfoldForm
.
new
(
params
)
@lines
=
Gitlab
::
Highlight
.
highlight_lines
(
repository
,
@ref
,
@path
)
@lines
=
@lines
[
@form
.
since
-
1
..
@form
.
to
-
1
]
@blob
.
load_all_data!
@lines
=
Gitlab
::
Highlight
.
highlight
(
@blob
.
path
,
@blob
.
data
,
repository:
@repository
).
lines
@form
=
UnfoldForm
.
new
(
params
)
@lines
=
@lines
[
@form
.
since
-
1
..
@form
.
to
-
1
].
map
(
&
:html_safe
)
if
@form
.
bottom?
@match_line
=
''
...
...
lib/gitlab/diff/file.rb
浏览文件 @
e6e29f92
...
...
@@ -58,19 +58,19 @@ module Gitlab
diff_refs
&
.
head_sha
end
def
content_sha
return
old_content_sha
if
deleted_file?
return
@
content_sha
if
defined?
(
@
content_sha
)
def
new_
content_sha
return
if
deleted_file?
return
@
new_content_sha
if
defined?
(
@new_
content_sha
)
refs
=
diff_refs
||
fallback_diff_refs
@content_sha
=
refs
&
.
head_sha
@
new_
content_sha
=
refs
&
.
head_sha
end
def
content_commit
return
@
content_commit
if
defined?
(
@
content_commit
)
def
new_
content_commit
return
@
new_content_commit
if
defined?
(
@new_
content_commit
)
sha
=
content_sha
@content_commit
=
repository
.
commit
(
sha
)
if
sha
sha
=
new_content_commit
@
new_
content_commit
=
repository
.
commit
(
sha
)
if
sha
end
def
old_content_sha
...
...
@@ -88,13 +88,13 @@ module Gitlab
@old_content_commit
=
repository
.
commit
(
sha
)
if
sha
end
def
blob
return
@
blob
if
defined?
(
@
blob
)
def
new_
blob
return
@
new_blob
if
defined?
(
@new_
blob
)
sha
=
content_sha
return
@blob
=
nil
unless
sha
sha
=
new_
content_sha
return
@
new_
blob
=
nil
unless
sha
repository
.
blob_at
(
sha
,
file_path
)
@new_blob
=
repository
.
blob_at
(
sha
,
file_path
)
end
def
old_blob
...
...
@@ -106,6 +106,18 @@ module Gitlab
@old_blob
=
repository
.
blob_at
(
sha
,
old_path
)
end
def
content_sha
new_content_sha
||
old_content_sha
end
def
content_commit
new_content_commit
||
old_content_commit
end
def
blob
new_blob
||
old_blob
end
attr_writer
:highlighted_diff_lines
# Array of Gitlab::Diff::Line objects
...
...
lib/gitlab/diff/highlight.rb
浏览文件 @
e6e29f92
...
...
@@ -42,9 +42,9 @@ module Gitlab
rich_line
=
if
diff_line
.
unchanged?
||
diff_line
.
added?
new_lines
[
diff_line
.
new_pos
-
1
]
new_lines
[
diff_line
.
new_pos
-
1
]
&
.
html_safe
elsif
diff_line
.
removed?
old_lines
[
diff_line
.
old_pos
-
1
]
old_lines
[
diff_line
.
old_pos
-
1
]
&
.
html_safe
end
# Only update text if line is found. This will prevent
...
...
@@ -60,13 +60,18 @@ module Gitlab
end
def
old_lines
return
unless
diff_file
@old_lines
||=
Gitlab
::
Highlight
.
highlight_lines
(
self
.
repository
,
diff_old_sha
,
diff_old_path
)
@old_lines
||=
highlighted_blob_lines
(
diff_file
.
old_blob
)
end
def
new_lines
return
unless
diff_file
@new_lines
||=
Gitlab
::
Highlight
.
highlight_lines
(
self
.
repository
,
diff_new_sha
,
diff_new_path
)
@new_lines
||=
highlighted_blob_lines
(
diff_file
.
new_blob
)
end
def
highlighted_blob_lines
(
blob
)
return
[]
unless
blob
blob
.
load_all_data!
Gitlab
::
Highlight
.
highlight
(
blob
.
path
,
blob
.
data
,
repository:
repository
).
lines
end
end
end
...
...
lib/gitlab/highlight.rb
浏览文件 @
e6e29f92
...
...
@@ -5,14 +5,6 @@ module Gitlab
highlight
(
blob_content
,
continue:
false
,
plain:
plain
)
end
def
self
.
highlight_lines
(
repository
,
ref
,
file_name
)
blob
=
repository
.
blob_at
(
ref
,
file_name
)
return
[]
unless
blob
blob
.
load_all_data!
highlight
(
file_name
,
blob
.
data
,
repository:
repository
).
lines
.
map!
(
&
:html_safe
)
end
attr_reader
:blob_name
def
initialize
(
blob_name
,
blob_content
,
repository:
nil
)
...
...
spec/lib/gitlab/highlight_spec.rb
浏览文件 @
e6e29f92
...
...
@@ -7,30 +7,6 @@ describe Gitlab::Highlight, lib: true do
let
(
:repository
)
{
project
.
repository
}
let
(
:commit
)
{
project
.
commit
(
sample_commit
.
id
)
}
describe
'.highlight_lines'
do
let
(
:lines
)
do
Gitlab
::
Highlight
.
highlight_lines
(
project
.
repository
,
commit
.
id
,
'files/ruby/popen.rb'
)
end
it
'highlights all the lines properly'
do
expect
(
lines
[
4
]).
to
eq
(
%Q{<span id="LC5" class="line" lang="ruby"> <span class="kp">extend</span> <span class="nb">self</span></span>
\n
}
)
expect
(
lines
[
21
]).
to
eq
(
%Q{<span id="LC22" class="line" lang="ruby"> <span class="k">unless</span> <span class="no">File</span><span class="p">.</span><span class="nf">directory?</span><span class="p">(</span><span class="n">path</span><span class="p">)</span></span>
\n
}
)
expect
(
lines
[
26
]).
to
eq
(
%Q{<span id="LC27" class="line" lang="ruby"> <span class="vi">@cmd_status</span> <span class="o">=</span> <span class="mi">0</span></span>
\n
}
)
end
describe
'with CRLF'
do
let
(
:branch
)
{
'crlf-diff'
}
let
(
:blob
)
{
repository
.
blob_at_branch
(
branch
,
path
)
}
let
(
:lines
)
do
Gitlab
::
Highlight
.
highlight_lines
(
project
.
repository
,
'crlf-diff'
,
'files/whitespace'
)
end
it
'strips extra LFs'
do
expect
(
lines
[
0
]).
to
eq
(
"<span id=
\"
LC1
\"
class=
\"
line
\"
lang=
\"
plaintext
\"
>test </span>"
)
end
end
end
describe
'custom highlighting from .gitattributes'
do
let
(
:branch
)
{
'gitattributes'
}
let
(
:blob
)
{
repository
.
blob_at_branch
(
branch
,
path
)
}
...
...
@@ -59,6 +35,19 @@ describe Gitlab::Highlight, lib: true do
end
describe
'#highlight'
do
describe
'with CRLF'
do
let
(
:branch
)
{
'crlf-diff'
}
let
(
:path
)
{
'files/whitespace'
}
let
(
:blob
)
{
repository
.
blob_at_branch
(
branch
,
path
)
}
let
(
:lines
)
do
Gitlab
::
Highlight
.
highlight
(
blob
.
path
,
blob
.
data
,
repository:
repository
).
lines
end
it
'strips extra LFs'
do
expect
(
lines
[
0
]).
to
eq
(
"<span id=
\"
LC1
\"
class=
\"
line
\"
lang=
\"
plaintext
\"
>test </span>"
)
end
end
it
'links dependencies via DependencyLinker'
do
expect
(
Gitlab
::
DependencyLinker
).
to
receive
(
:link
).
with
(
'file.name'
,
'Contents'
,
anything
).
and_call_original
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录