Class: Gitlab::Conflict::File
- Inherits:
-
Object
- Object
- Gitlab::Conflict::File
show all
- Includes:
- Routing, Utils::StrongMemoize, IconsHelper
- Defined in:
- lib/gitlab/conflict/file.rb
Constant Summary
collapse
- CONTEXT_LINES =
3
- CONFLICT_MARKER_OUR =
'conflict_marker_our'
- CONFLICT_MARKER_THEIR =
'conflict_marker_their'
- CONFLICT_MARKER_SEPARATOR =
'conflict_marker'
- CONFLICT_TYPES =
{
"old" => "conflict_their",
"new" => "conflict_our"
}.freeze
Constants included
from IconsHelper
IconsHelper::DEFAULT_ICON_SIZE
Instance Attribute Summary collapse
Instance Method Summary
collapse
#audit_icon, #boolean_to_icon, #custom_icon, #external_snippet_icon, #file_type_icon_class, #gl_loading_icon, #sprite_file_icons_path, #sprite_icon, #sprite_icon_path, #visibility_level_icon
Methods included from Routing
includes_helpers, redirect_legacy_paths, url_helpers
Constructor Details
#initialize(raw, merge_request:) ⇒ File
Returns a new instance of File.
28
29
30
31
32
|
# File 'lib/gitlab/conflict/file.rb', line 28
def initialize(raw, merge_request:)
@raw = raw
@merge_request = merge_request
@match_line_headers = {}
end
|
Instance Attribute Details
#merge_request ⇒ Object
Returns the value of attribute merge_request.
21
22
23
|
# File 'lib/gitlab/conflict/file.rb', line 21
def merge_request
@merge_request
end
|
#raw ⇒ Object
‘raw’ holds the Gitlab::Git::Conflict::File that this instance wraps
24
25
26
|
# File 'lib/gitlab/conflict/file.rb', line 24
def raw
@raw
end
|
Instance Method Details
#as_json(opts = {}) ⇒ Object
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
# File 'lib/gitlab/conflict/file.rb', line 204
def as_json(opts = {})
json_hash = {
old_path: their_path,
new_path: our_path,
blob_icon: file_type_icon_class('file', our_mode, our_path),
blob_path: project_blob_path(merge_request.project, ::File.join(merge_request.diff_refs.head_sha, our_path))
}
json_hash.tap do |json_hash|
if opts[:full_content]
json_hash[:content] = content
else
json_hash[:sections] = sections if type.text?
json_hash[:type] = type
json_hash[:content_path] = content_path
end
end
end
|
#conflict_type(when_renamed: false) ⇒ Object
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
# File 'lib/gitlab/conflict/file.rb', line 230
def conflict_type(when_renamed: false)
if ancestor_path.present?
if our_path.present? && their_path.present?
:both_modified
elsif their_path.blank?
:modified_source_removed_target
else
:modified_target_removed_source
end
elsif our_path.present? && their_path.present?
:both_added
elsif their_path.blank?
when_renamed ? :renamed_same_file : :removed_target_renamed_source
else
:removed_source_renamed_target
end
end
|
#content_path ⇒ Object
223
224
225
226
227
228
|
# File 'lib/gitlab/conflict/file.rb', line 223
def content_path
conflict_for_path_project_merge_request_path(merge_request.project,
merge_request,
old_path: their_path,
new_path: our_path)
end
|
#create_match_line(line) ⇒ Object
166
167
168
|
# File 'lib/gitlab/conflict/file.rb', line 166
def create_match_line(line)
Gitlab::Diff::Line.new('', 'match', line.index, line.old_pos, line.new_pos)
end
|
#create_separator_line(line, type) ⇒ Object
170
171
172
|
# File 'lib/gitlab/conflict/file.rb', line 170
def create_separator_line(line, type)
Gitlab::Diff::Line.new('', type, line.index, nil, nil)
end
|
#diff_lines_for_serializer ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/gitlab/conflict/file.rb', line 58
def diff_lines_for_serializer
sections && highlight_lines!
sections.flat_map do |section|
if section[:conflict]
lines = []
lines << create_separator_line(section[:lines].first, CONFLICT_MARKER_OUR)
current_type = section[:lines].first.type
section[:lines].each do |line|
if line.type != current_type lines << create_separator_line(line, CONFLICT_MARKER_SEPARATOR)
current_type = line.type
end
line.type = CONFLICT_TYPES[line.type]
line.old_pos, line.new_pos = line.new_pos, line.old_pos
lines << line
end
lines << create_separator_line(lines.last, CONFLICT_MARKER_THEIR)
lines
else
section[:lines]
end
end
end
|
Any line beginning with a letter, an underscore, or a dollar can be used in a match line header. Only context sections can contain match lines, as match lines have to exist in both versions of the file.
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
# File 'lib/gitlab/conflict/file.rb', line 177
def (index)
return @match_line_headers[index] if @match_line_headers.key?(index)
@match_line_headers[index] = begin
if index >= 0
line = lines[index]
if line.type.nil? && line.text.match(/\A[A-Za-z$_]/)
" #{line.text}"
else
(index - 1)
end
end
end
end
|
#highlight_lines! ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/gitlab/conflict/file.rb', line 44
def highlight_lines!
their_highlight = Gitlab::Highlight.highlight(their_path, their_lines, language: their_language).lines
our_highlight = Gitlab::Highlight.highlight(our_path, our_lines, language: our_language).lines
lines.each do |line|
line.rich_text =
if line.type == 'old'
their_highlight[line.old_line - 1].try(:html_safe)
else
our_highlight[line.new_line - 1].try(:html_safe)
end
end
end
|
#line_code(line) ⇒ Object
162
163
164
|
# File 'lib/gitlab/conflict/file.rb', line 162
def line_code(line)
Gitlab::Git.diff_line_code(our_path, line.new_pos, line.old_pos)
end
|
#lines ⇒ Object
34
35
36
37
38
|
# File 'lib/gitlab/conflict/file.rb', line 34
def lines
return @lines if defined?(@lines)
@lines = raw.lines.nil? ? nil : map_raw_lines(raw.lines)
end
|
#resolve_lines(resolution) ⇒ Object
40
41
42
|
# File 'lib/gitlab/conflict/file.rb', line 40
def resolve_lines(resolution)
map_raw_lines(raw.resolve_lines(resolution))
end
|
#sections ⇒ Object
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
# File 'lib/gitlab/conflict/file.rb', line 93
def sections
return @sections if @sections
chunked_lines = lines.chunk { |line| line.type.nil? }.to_a
match_line = nil
sections_count = chunked_lines.size
@sections = chunked_lines.flat_map.with_index do |(no_conflict, lines), i|
section = nil
if no_conflict
conflict_before = i > 0
conflict_after = (sections_count - i) > 1
if conflict_before && conflict_after
if lines.length > CONTEXT_LINES * 2
head_lines = lines.first(CONTEXT_LINES)
tail_lines = lines.last(CONTEXT_LINES)
update_match_line_text(match_line, head_lines.last)
match_line = create_match_line(tail_lines.first)
section = [
{ conflict: false, lines: head_lines },
{ conflict: false, lines: tail_lines.unshift(match_line) }
]
end
elsif conflict_after
tail_lines = lines.last(CONTEXT_LINES)
if lines.length > tail_lines.length
match_line = create_match_line(tail_lines.first)
tail_lines.unshift(match_line)
end
lines = tail_lines
elsif conflict_before
number_of_trailing_lines = lines.size
lines = lines.first(CONTEXT_LINES)
if number_of_trailing_lines > CONTEXT_LINES
lines << create_match_line(lines.last)
end
end
end
update_match_line_text(match_line, lines.last) unless section
section ||= { conflict: !no_conflict, lines: lines }
section[:id] = line_code(lines.first) unless no_conflict
section
end
end
|
#update_match_line_text(match_line, line) ⇒ Object
Set the match line’s text for the current line. A match line takes its start position and context header (where present) from itself, and its end position from the line passed in.
196
197
198
199
200
201
202
|
# File 'lib/gitlab/conflict/file.rb', line 196
def update_match_line_text(match_line, line)
return unless match_line
= (match_line.index - 1)
match_line.text = "@@ -#{match_line.old_pos},#{line.old_pos} +#{match_line.new_pos},#{line.new_pos} @@#{}"
end
|