2018-11-05 23:45:35 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-20 12:51:48 -04:00
|
|
|
module Gitlab
|
|
|
|
module Diff
|
|
|
|
class DiffRefs
|
|
|
|
attr_reader :base_sha
|
|
|
|
attr_reader :start_sha
|
|
|
|
attr_reader :head_sha
|
|
|
|
|
|
|
|
def initialize(base_sha:, start_sha: base_sha, head_sha:)
|
|
|
|
@base_sha = base_sha
|
|
|
|
@start_sha = start_sha
|
|
|
|
@head_sha = head_sha
|
|
|
|
end
|
|
|
|
|
|
|
|
def ==(other)
|
|
|
|
other.is_a?(self.class) &&
|
2017-11-22 09:48:09 -05:00
|
|
|
Git.shas_eql?(base_sha, other.base_sha) &&
|
|
|
|
Git.shas_eql?(start_sha, other.start_sha) &&
|
|
|
|
Git.shas_eql?(head_sha, other.head_sha)
|
2016-06-20 12:51:48 -04:00
|
|
|
end
|
|
|
|
|
2017-03-31 19:49:43 -04:00
|
|
|
alias_method :eql?, :==
|
|
|
|
|
|
|
|
def hash
|
2018-11-23 06:27:08 -05:00
|
|
|
[self.class, base_sha, start_sha, head_sha].hash
|
2017-03-31 19:49:43 -04:00
|
|
|
end
|
|
|
|
|
2016-07-06 19:28:13 -04:00
|
|
|
# There is only one case in which we will have `start_sha` and `head_sha`,
|
|
|
|
# but not `base_sha`, which is when a diff is generated between an
|
|
|
|
# orphaned branch and another branch, which means there _is_ no base, but
|
|
|
|
# we're still able to highlight it, and to create diff notes, which are
|
|
|
|
# the primary things `DiffRefs` are used for.
|
|
|
|
# `DiffRefs` are "complete" when they have `start_sha` and `head_sha`,
|
|
|
|
# because `base_sha` can always be derived from this, to return an actual
|
|
|
|
# sha, or `nil`.
|
|
|
|
# We have `base_sha` directly available on `DiffRefs` because it's faster#
|
|
|
|
# than having to look it up in the repo every time.
|
2016-06-20 12:51:48 -04:00
|
|
|
def complete?
|
2020-02-17 13:09:00 -05:00
|
|
|
start_sha.present? && head_sha.present?
|
2016-06-20 12:51:48 -04:00
|
|
|
end
|
2017-06-06 12:46:28 -04:00
|
|
|
|
|
|
|
def compare_in(project)
|
|
|
|
# We're at the initial commit, so just get that as we can't compare to anything.
|
|
|
|
if Gitlab::Git.blank_ref?(start_sha)
|
|
|
|
project.commit(head_sha)
|
|
|
|
else
|
|
|
|
straight = start_sha == base_sha
|
2018-03-07 20:29:12 -05:00
|
|
|
|
|
|
|
CompareService.new(project, head_sha).execute(project,
|
|
|
|
start_sha,
|
|
|
|
base_sha: base_sha,
|
|
|
|
straight: straight)
|
2017-06-06 12:46:28 -04:00
|
|
|
end
|
|
|
|
end
|
2016-06-20 12:51:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|