2019-07-05 07:03:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Diff
|
|
|
|
class PositionTracer
|
|
|
|
class ImageStrategy < BaseStrategy
|
|
|
|
def trace(position)
|
2020-06-19 17:08:32 -04:00
|
|
|
a_path = position.old_path
|
2019-07-05 07:03:47 -04:00
|
|
|
b_path = position.new_path
|
2020-06-19 17:08:32 -04:00
|
|
|
diff_file = diff_file(position)
|
|
|
|
a_mode = diff_file&.a_mode
|
|
|
|
b_mode = diff_file&.b_mode
|
2019-07-05 07:03:47 -04:00
|
|
|
|
|
|
|
# If file exists in B->D (e.g. updated, renamed, removed), let the
|
|
|
|
# note become outdated.
|
2020-06-19 17:08:32 -04:00
|
|
|
bd_diff = bd_diffs.diff_file_with_old_path(b_path, b_mode)
|
2019-07-05 07:03:47 -04:00
|
|
|
|
|
|
|
return { position: new_position(position, bd_diff), outdated: true } if bd_diff
|
|
|
|
|
|
|
|
# If file still exists in the new diff, update the position.
|
2020-06-19 17:08:32 -04:00
|
|
|
cd_diff = cd_diffs.diff_file_with_new_path(b_path, b_mode)
|
2019-07-05 07:03:47 -04:00
|
|
|
|
|
|
|
return { position: new_position(position, cd_diff), outdated: false } if cd_diff
|
|
|
|
|
|
|
|
# If file exists in A->C (e.g. rebased and same changes were present
|
|
|
|
# in target branch), let the note become outdated.
|
2020-06-19 17:08:32 -04:00
|
|
|
ac_diff = ac_diffs.diff_file_with_old_path(a_path, a_mode)
|
2019-07-05 07:03:47 -04:00
|
|
|
|
|
|
|
return { position: new_position(position, ac_diff), outdated: true } if ac_diff
|
|
|
|
|
|
|
|
# If ever there's a case that the file no longer exists in any diff,
|
|
|
|
# don't set a change position and let the note become outdated.
|
|
|
|
#
|
|
|
|
# This should never happen given the file should exist in one of the
|
|
|
|
# diffs above.
|
|
|
|
{ outdated: true }
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def new_position(position, diff_file)
|
|
|
|
Position.new(
|
|
|
|
diff_file: diff_file,
|
|
|
|
x: position.x,
|
|
|
|
y: position.y,
|
|
|
|
width: position.width,
|
|
|
|
height: position.height,
|
|
|
|
position_type: position.position_type
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|