2014-09-08 09:25:50 -04:00
|
|
|
module Gitlab
|
|
|
|
module Diff
|
|
|
|
class File
|
2016-06-20 12:51:48 -04:00
|
|
|
attr_reader :diff, :repository, :diff_refs
|
2014-09-08 09:25:50 -04:00
|
|
|
|
|
|
|
delegate :new_file, :deleted_file, :renamed_file,
|
2016-06-20 12:57:10 -04:00
|
|
|
:old_path, :new_path, :a_mode, :b_mode,
|
2016-07-15 03:04:18 -04:00
|
|
|
:submodule?, :too_large?, :collapsed?, to: :diff, prefix: false
|
2014-09-08 09:25:50 -04:00
|
|
|
|
2016-06-20 12:51:48 -04:00
|
|
|
def initialize(diff, repository:, diff_refs: nil)
|
2014-09-08 09:25:50 -04:00
|
|
|
@diff = diff
|
2016-06-20 12:51:48 -04:00
|
|
|
@repository = repository
|
2016-01-20 12:44:27 -05:00
|
|
|
@diff_refs = diff_refs
|
|
|
|
end
|
|
|
|
|
2016-06-20 13:17:25 -04:00
|
|
|
def position(line)
|
|
|
|
return unless diff_refs
|
|
|
|
|
|
|
|
Position.new(
|
|
|
|
old_path: old_path,
|
|
|
|
new_path: new_path,
|
|
|
|
old_line: line.old_line,
|
|
|
|
new_line: line.new_line,
|
2016-07-06 19:29:41 -04:00
|
|
|
diff_refs: diff_refs
|
2016-06-20 13:17:25 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2016-06-20 13:15:44 -04:00
|
|
|
def line_code(line)
|
|
|
|
return if line.meta?
|
|
|
|
|
|
|
|
Gitlab::Diff::LineCode.generate(file_path, line.new_pos, line.old_pos)
|
|
|
|
end
|
|
|
|
|
|
|
|
def line_for_line_code(code)
|
|
|
|
diff_lines.find { |line| line_code(line) == code }
|
|
|
|
end
|
|
|
|
|
2016-06-20 13:17:25 -04:00
|
|
|
def line_for_position(pos)
|
|
|
|
diff_lines.find { |line| position(line) == pos }
|
|
|
|
end
|
|
|
|
|
|
|
|
def position_for_line_code(code)
|
|
|
|
line = line_for_line_code(code)
|
|
|
|
position(line) if line
|
|
|
|
end
|
|
|
|
|
|
|
|
def line_code_for_position(pos)
|
|
|
|
line = line_for_position(pos)
|
|
|
|
line_code(line) if line
|
|
|
|
end
|
|
|
|
|
2016-06-20 12:54:53 -04:00
|
|
|
def content_commit
|
|
|
|
return unless diff_refs
|
2016-07-06 19:29:41 -04:00
|
|
|
|
2016-06-20 12:54:53 -04:00
|
|
|
repository.commit(deleted_file ? old_ref : new_ref)
|
|
|
|
end
|
|
|
|
|
2016-08-27 23:59:48 -04:00
|
|
|
def old_content_commit
|
|
|
|
return unless diff_refs
|
|
|
|
|
|
|
|
repository.commit(old_ref)
|
|
|
|
end
|
|
|
|
|
2016-01-20 12:44:27 -05:00
|
|
|
def old_ref
|
2016-06-20 12:51:48 -04:00
|
|
|
diff_refs.try(:base_sha)
|
2016-01-20 12:44:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def new_ref
|
2016-06-20 12:51:48 -04:00
|
|
|
diff_refs.try(:head_sha)
|
2014-09-08 09:25:50 -04:00
|
|
|
end
|
|
|
|
|
2016-07-26 03:21:42 -04:00
|
|
|
attr_writer :highlighted_diff_lines
|
2016-07-20 12:25:36 -04:00
|
|
|
|
2016-06-20 12:51:48 -04:00
|
|
|
# Array of Gitlab::Diff::Line objects
|
2014-09-08 09:25:50 -04:00
|
|
|
def diff_lines
|
2016-07-20 12:25:36 -04:00
|
|
|
@diff_lines ||= Gitlab::Diff::Parser.new.parse(raw_diff.each_line).to_a
|
2016-03-11 11:40:59 -05:00
|
|
|
end
|
|
|
|
|
2015-12-30 00:52:50 -05:00
|
|
|
def highlighted_diff_lines
|
2016-06-20 12:51:48 -04:00
|
|
|
@highlighted_diff_lines ||= Gitlab::Diff::Highlight.new(self, repository: self.repository).highlight
|
2015-12-30 00:52:50 -05:00
|
|
|
end
|
|
|
|
|
2016-07-20 12:25:36 -04:00
|
|
|
# Array[<Hash>] with right/left keys that contains Gitlab::Diff::Line objects which text is hightlighted
|
2016-01-20 08:51:56 -05:00
|
|
|
def parallel_diff_lines
|
2016-06-20 12:51:48 -04:00
|
|
|
@parallel_diff_lines ||= Gitlab::Diff::ParallelDiff.new(self).parallelize
|
2016-01-20 08:51:56 -05:00
|
|
|
end
|
|
|
|
|
2014-09-08 09:25:50 -04:00
|
|
|
def mode_changed?
|
2016-06-20 12:57:10 -04:00
|
|
|
a_mode && b_mode && a_mode != b_mode
|
2014-09-08 09:25:50 -04:00
|
|
|
end
|
|
|
|
|
2014-09-08 14:54:52 -04:00
|
|
|
def raw_diff
|
2014-09-24 06:26:53 -04:00
|
|
|
diff.diff.to_s
|
2014-09-08 14:54:52 -04:00
|
|
|
end
|
|
|
|
|
2014-09-08 09:25:50 -04:00
|
|
|
def next_line(index)
|
|
|
|
diff_lines[index + 1]
|
|
|
|
end
|
|
|
|
|
|
|
|
def prev_line(index)
|
2016-06-20 12:57:10 -04:00
|
|
|
diff_lines[index - 1] if index > 0
|
2014-09-08 09:25:50 -04:00
|
|
|
end
|
2014-09-08 14:54:52 -04:00
|
|
|
|
2016-06-20 13:17:25 -04:00
|
|
|
def paths
|
|
|
|
[old_path, new_path].compact
|
|
|
|
end
|
|
|
|
|
2014-09-08 14:54:52 -04:00
|
|
|
def file_path
|
2016-06-20 12:57:10 -04:00
|
|
|
new_path.presence || old_path
|
2014-09-08 14:54:52 -04:00
|
|
|
end
|
2015-10-01 07:52:08 -04:00
|
|
|
|
|
|
|
def added_lines
|
2015-12-14 21:53:52 -05:00
|
|
|
diff_lines.count(&:added?)
|
2015-10-01 07:52:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def removed_lines
|
2015-12-14 21:53:52 -05:00
|
|
|
diff_lines.count(&:removed?)
|
2015-10-01 07:52:08 -04:00
|
|
|
end
|
2016-06-20 12:54:53 -04:00
|
|
|
|
2016-08-27 23:59:48 -04:00
|
|
|
def old_blob(commit = old_content_commit)
|
2016-06-20 12:54:53 -04:00
|
|
|
return unless commit
|
|
|
|
|
2016-08-27 23:59:48 -04:00
|
|
|
repository.blob_at(commit.id, old_path)
|
2016-06-20 12:54:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def blob(commit = content_commit)
|
|
|
|
return unless commit
|
2016-07-06 19:29:41 -04:00
|
|
|
|
2016-06-20 12:54:53 -04:00
|
|
|
repository.blob_at(commit.id, file_path)
|
|
|
|
end
|
2016-10-12 09:34:47 -04:00
|
|
|
|
2016-11-10 12:24:12 -05:00
|
|
|
def file_identifier
|
2016-10-12 09:34:47 -04:00
|
|
|
"#{file_path}-#{new_file}-#{deleted_file}-#{renamed_file}"
|
|
|
|
end
|
2014-09-08 09:25:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|