debian-mirror-gitlab/lib/gitlab/diff/file.rb

135 lines
3 KiB
Ruby
Raw Normal View History

2015-04-26 12:48:37 +05:30
module Gitlab
module Diff
class File
2016-08-24 12:49:21 +05:30
attr_reader :diff, :repository, :diff_refs
2015-04-26 12:48:37 +05:30
delegate :new_file, :deleted_file, :renamed_file,
2016-08-24 12:49:21 +05:30
:old_path, :new_path, :a_mode, :b_mode,
:submodule?, :too_large?, :collapsed?, to: :diff, prefix: false
2015-04-26 12:48:37 +05:30
2016-08-24 12:49:21 +05:30
def initialize(diff, repository:, diff_refs: nil)
2015-04-26 12:48:37 +05:30
@diff = diff
2016-08-24 12:49:21 +05:30
@repository = repository
2016-01-29 22:53:50 +05:30
@diff_refs = diff_refs
end
2016-08-24 12:49:21 +05:30
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,
diff_refs: diff_refs
)
end
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
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
def content_commit
return unless diff_refs
repository.commit(deleted_file ? old_ref : new_ref)
end
2016-01-29 22:53:50 +05:30
def old_ref
2016-08-24 12:49:21 +05:30
diff_refs.try(:base_sha)
2016-01-29 22:53:50 +05:30
end
def new_ref
2016-08-24 12:49:21 +05:30
diff_refs.try(:head_sha)
2015-04-26 12:48:37 +05:30
end
2016-09-13 17:45:13 +05:30
attr_writer :highlighted_diff_lines
2016-08-24 12:49:21 +05:30
# Array of Gitlab::Diff::Line objects
2015-04-26 12:48:37 +05:30
def diff_lines
2016-09-13 17:45:13 +05:30
@diff_lines ||= Gitlab::Diff::Parser.new.parse(raw_diff.each_line).to_a
2015-04-26 12:48:37 +05:30
end
2016-01-29 22:53:50 +05:30
def highlighted_diff_lines
2016-08-24 12:49:21 +05:30
@highlighted_diff_lines ||= Gitlab::Diff::Highlight.new(self, repository: self.repository).highlight
2016-01-29 22:53:50 +05:30
end
2016-09-13 17:45:13 +05:30
# Array[<Hash>] with right/left keys that contains Gitlab::Diff::Line objects which text is hightlighted
2016-01-29 22:53:50 +05:30
def parallel_diff_lines
2016-08-24 12:49:21 +05:30
@parallel_diff_lines ||= Gitlab::Diff::ParallelDiff.new(self).parallelize
2016-01-29 22:53:50 +05:30
end
2015-04-26 12:48:37 +05:30
def mode_changed?
2016-08-24 12:49:21 +05:30
a_mode && b_mode && a_mode != b_mode
2015-04-26 12:48:37 +05:30
end
def raw_diff
diff.diff.to_s
end
def next_line(index)
diff_lines[index + 1]
end
def prev_line(index)
2016-08-24 12:49:21 +05:30
diff_lines[index - 1] if index > 0
end
def paths
[old_path, new_path].compact
2015-04-26 12:48:37 +05:30
end
def file_path
2016-08-24 12:49:21 +05:30
new_path.presence || old_path
2015-04-26 12:48:37 +05:30
end
2015-10-24 18:46:33 +05:30
def added_lines
2015-12-23 02:04:40 +05:30
diff_lines.count(&:added?)
2015-10-24 18:46:33 +05:30
end
def removed_lines
2015-12-23 02:04:40 +05:30
diff_lines.count(&:removed?)
2015-10-24 18:46:33 +05:30
end
2016-08-24 12:49:21 +05:30
def old_blob(commit = content_commit)
return unless commit
parent_id = commit.parent_id
return unless parent_id
repository.blob_at(parent_id, old_path)
end
def blob(commit = content_commit)
return unless commit
repository.blob_at(commit.id, file_path)
end
2016-11-03 12:29:30 +05:30
def cache_key
"#{file_path}-#{new_file}-#{deleted_file}-#{renamed_file}"
end
2015-04-26 12:48:37 +05:30
end
end
end