Skip to content
GitLab
菜单
为什么选择 GitLab
定价
联系销售
探索
为什么选择 GitLab
定价
联系销售
探索
登录
获取免费试用
主导航
搜索或转到…
项目
GitLab
管理
动态
成员
标记
计划
议题
议题看板
里程碑
迭代
需求
代码
合并请求
仓库
分支
提交
标签
仓库图
比较修订版本
代码片段
锁定的文件
构建
流水线
作业
流水线计划
测试用例
产物
部署
发布
Package registry
容器镜像库
模型注册表
运维
环境
Terraform 模块
监控
事件
服务台
分析
价值流分析
贡献者分析
CI/CD 分析
仓库分析
代码评审分析
议题分析
洞察
模型实验
效能分析
帮助
帮助
支持
GitLab 文档
比较 GitLab 各版本
社区论坛
为极狐GitLab 提交贡献
提交反馈
隐私声明
快捷键
?
新增功能
4
代码片段
群组
项目
显示更多面包屑
gitlab-cn
GitLab
提交
f5cc3f63
提交
f5cc3f63
编辑于
8 years ago
作者:
Douwe Maan
浏览文件
操作
下载
补丁
差异文件
Render inline diffs for multiple changed lines following eachother
上级
92772f85
No related branches found
分支 包含提交
No related tags found
标签 包含提交
无相关合并请求
变更
3
隐藏空白变更内容
行内
左右并排
显示
3 个更改的文件
CHANGELOG
+1
-0
1 个添加, 0 个删除
CHANGELOG
lib/gitlab/diff/inline_diff.rb
+38
-14
38 个添加, 14 个删除
lib/gitlab/diff/inline_diff.rb
spec/lib/gitlab/diff/inline_diff_spec.rb
+18
-10
18 个添加, 10 个删除
spec/lib/gitlab/diff/inline_diff_spec.rb
有
57 个添加
和
24 个删除
CHANGELOG
+
1
−
0
浏览文件 @
f5cc3f63
...
...
@@ -25,6 +25,7 @@ v 8.10.0 (unreleased)
- Updated layout for Projects, Groups, Users on Admin area !4424
- Fix changing issue state columns in milestone view
- Add notification settings dropdown for groups
- Render inline diffs for multiple changed lines following eachother
- Wildcards for protected branches. !4665
- Allow importing from Github using Personal Access Tokens. (Eric K Idema)
- API: Todos !3188 (Robert Schilling)
...
...
此差异已折叠。
点击以展开。
lib/gitlab/diff/inline_diff.rb
+
38
−
14
浏览文件 @
f5cc3f63
module
Gitlab
module
Diff
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
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
=
[]
local_edit_indexes
.
each
do
|
index
|
old_index
=
index
new_index
=
index
+
1
changed_line_pairs
.
each
do
|
old_index
,
new_index
|
old_line
=
lines
[
old_index
]
new_line
=
lines
[
new_index
]
...
...
@@ -51,18 +65,28 @@ def inline_diffs
private
def
self
.
find_local_edits
(
lines
)
line_prefixes
=
lines
.
map
{
|
line
|
line
.
match
(
/\A([+-])/
)
?
$1
:
' '
}
joined_line_prefixes
=
"
#{
line_prefixes
.
join
}
"
offset
=
0
local_edit_indexes
=
[]
while
index
=
joined_line_prefixes
.
index
(
" -+ "
,
offset
)
local_edit_indexes
<<
index
offset
=
index
+
1
# Finds pairs of old/new line pairs that represent the same line that changed
def
self
.
find_changed_line_pairs
(
lines
)
# 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
)
# For `"---+++"`, `changed_line_count == 3`
changed_line_count
=
(
end_index
-
begin_index
)
/
2
halfway_index
=
begin_index
+
changed_line_count
(
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
local_edit_indexe
s
changed_line_pair
s
end
def
longest_common_prefix
(
a
,
b
)
...
...
此差异已折叠。
点击以展开。
spec/lib/gitlab/diff/inline_diff_spec.rb
+
18
−
10
浏览文件 @
f5cc3f63
...
...
@@ -3,14 +3,19 @@
describe
Gitlab
::
Diff
::
InlineDiff
,
lib:
true
do
describe
'.for_lines'
do
let
(
:diff
)
do
<<
eos
class Test
- def initialize(test = true)
+ def initialize(test = false)
@test = test
end
end
eos
<<-
EOF
.
strip_heredoc
class Test
- def initialize(test = true)
+ def initialize(test = false)
@test = test
- if true
- @foo = "bar"
+ unless false
+ @foo = "baz"
end
end
end
EOF
end
let
(
:subject
)
{
described_class
.
for_lines
(
diff
.
lines
)
}
...
...
@@ -20,8 +25,11 @@ class Test
expect
(
subject
[
1
]).
to
eq
([
25
..
27
])
expect
(
subject
[
2
]).
to
eq
([
25
..
28
])
expect
(
subject
[
3
]).
to
be_nil
expect
(
subject
[
4
]).
to
be_nil
expect
(
subject
[
5
]).
to
be_nil
expect
(
subject
[
4
]).
to
eq
([
5
..
10
])
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
...
...
此差异已折叠。
点击以展开。
预览
0%
加载中
请重试
或
添加新附件
.
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
保存评论
取消
想要评论请
注册
或
登录