2018-11-05 23:45:35 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-20 13:18:18 -04:00
|
|
|
# Finds the diff position in the new diff that corresponds to the same location
|
|
|
|
# specified by the provided position in the old diff.
|
|
|
|
module Gitlab
|
|
|
|
module Diff
|
|
|
|
class PositionTracer
|
2017-05-21 16:38:33 -04:00
|
|
|
attr_accessor :project
|
2016-06-20 13:18:18 -04:00
|
|
|
attr_accessor :old_diff_refs
|
|
|
|
attr_accessor :new_diff_refs
|
|
|
|
attr_accessor :paths
|
|
|
|
|
2017-05-21 16:38:33 -04:00
|
|
|
def initialize(project:, old_diff_refs:, new_diff_refs:, paths: nil)
|
|
|
|
@project = project
|
2016-06-20 13:18:18 -04:00
|
|
|
@old_diff_refs = old_diff_refs
|
|
|
|
@new_diff_refs = new_diff_refs
|
|
|
|
@paths = paths
|
|
|
|
end
|
|
|
|
|
2019-07-05 07:03:47 -04:00
|
|
|
def trace(old_position)
|
2017-05-18 16:48:42 -04:00
|
|
|
return unless old_diff_refs&.complete? && new_diff_refs&.complete?
|
2019-07-05 07:03:47 -04:00
|
|
|
return unless old_position.diff_refs == old_diff_refs
|
2016-06-20 13:18:18 -04:00
|
|
|
|
2019-07-05 07:03:47 -04:00
|
|
|
strategy = old_position.on_text? ? LineStrategy : ImageStrategy
|
2016-06-20 13:18:18 -04:00
|
|
|
|
2019-07-05 07:03:47 -04:00
|
|
|
strategy.new(self).trace(old_position)
|
2016-06-20 13:18:18 -04:00
|
|
|
end
|
|
|
|
|
2017-05-21 16:38:33 -04:00
|
|
|
def ac_diffs
|
|
|
|
@ac_diffs ||= compare(
|
|
|
|
old_diff_refs.base_sha || old_diff_refs.start_sha,
|
|
|
|
new_diff_refs.base_sha || new_diff_refs.start_sha,
|
|
|
|
straight: true
|
|
|
|
)
|
2016-06-20 13:18:18 -04:00
|
|
|
end
|
|
|
|
|
2017-05-21 16:38:33 -04:00
|
|
|
def bd_diffs
|
|
|
|
@bd_diffs ||= compare(old_diff_refs.head_sha, new_diff_refs.head_sha, straight: true)
|
2016-06-20 13:18:18 -04:00
|
|
|
end
|
|
|
|
|
2017-05-21 16:38:33 -04:00
|
|
|
def cd_diffs
|
|
|
|
@cd_diffs ||= compare(new_diff_refs.start_sha, new_diff_refs.head_sha)
|
2016-06-20 13:18:18 -04:00
|
|
|
end
|
|
|
|
|
2020-06-19 17:08:32 -04:00
|
|
|
def diff_file(position)
|
|
|
|
position.diff_file(project.repository)
|
|
|
|
end
|
|
|
|
|
2019-07-05 07:03:47 -04:00
|
|
|
private
|
|
|
|
|
2017-05-21 16:38:33 -04:00
|
|
|
def compare(start_sha, head_sha, straight: false)
|
|
|
|
compare = CompareService.new(project, head_sha).execute(project, start_sha, straight: straight)
|
2017-06-06 12:46:28 -04:00
|
|
|
compare.diffs(paths: paths, expanded: true)
|
2017-05-21 16:38:33 -04:00
|
|
|
end
|
2016-06-20 13:18:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|