Class: Gitlab::Diff::DiffRefs

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/diff/diff_refs.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_sha:, start_sha: base_sha, head_sha:) ⇒ DiffRefs

Returns a new instance of DiffRefs.



10
11
12
13
14
# File 'lib/gitlab/diff/diff_refs.rb', line 10

def initialize(base_sha:, start_sha: base_sha, head_sha:)
  @base_sha = base_sha
  @start_sha = start_sha
  @head_sha = head_sha
end

Instance Attribute Details

#base_shaObject (readonly)

Returns the value of attribute base_sha.



6
7
8
# File 'lib/gitlab/diff/diff_refs.rb', line 6

def base_sha
  @base_sha
end

#head_shaObject (readonly)

Returns the value of attribute head_sha.



8
9
10
# File 'lib/gitlab/diff/diff_refs.rb', line 8

def head_sha
  @head_sha
end

#start_shaObject (readonly)

Returns the value of attribute start_sha.



7
8
9
# File 'lib/gitlab/diff/diff_refs.rb', line 7

def start_sha
  @start_sha
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



16
17
18
19
20
21
# File 'lib/gitlab/diff/diff_refs.rb', line 16

def ==(other)
  other.is_a?(self.class) &&
    Git.shas_eql?(base_sha, other.base_sha) &&
    Git.shas_eql?(start_sha, other.start_sha) &&
    Git.shas_eql?(head_sha, other.head_sha)
end

#compare_in(project) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/gitlab/diff/diff_refs.rb', line 43

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

    CompareService.new(project, head_sha).execute(project,
                                                  start_sha,
                                                  base_sha: base_sha,
                                                  straight: straight)
  end
end

#complete?Boolean

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.

Returns:

  • (Boolean)


39
40
41
# File 'lib/gitlab/diff/diff_refs.rb', line 39

def complete?
  start_sha.present? && head_sha.present?
end

#hashObject



25
26
27
# File 'lib/gitlab/diff/diff_refs.rb', line 25

def hash
  [self.class, base_sha, start_sha, head_sha].hash
end