diff options
author | Oswaldo Ferreira <oswaldo@gitlab.com> | 2018-03-07 22:29:12 -0300 |
---|---|---|
committer | Oswaldo Ferreira <oswaldo@gitlab.com> | 2018-03-07 22:46:29 -0300 |
commit | c6273ec50c019a115b11a8ef1032a64710f0a46a (patch) | |
tree | ce1a808bafc98f2181a621b75e293c4c27d1899e /app/models/compare.rb | |
parent | 7734e85bc6592c5ad3330c611c5f83a051b680b0 (diff) | |
download | gitlab-ce-c6273ec50c019a115b11a8ef1032a64710f0a46a.tar.gz |
Avoid re-fetching merge-base SHA from Gitaly unnecessarily
Diffstat (limited to 'app/models/compare.rb')
-rw-r--r-- | app/models/compare.rb | 36 |
1 files changed, 20 insertions, 16 deletions
diff --git a/app/models/compare.rb b/app/models/compare.rb index 3a8bbcb1acd..19ea2bc3065 100644 --- a/app/models/compare.rb +++ b/app/models/compare.rb @@ -1,4 +1,6 @@ class Compare + include Gitlab::Utils::StrongMemoize + delegate :same, :head, :base, to: :@compare attr_reader :project @@ -11,9 +13,10 @@ class Compare end end - def initialize(compare, project, straight: false) + def initialize(compare, project, base_sha: nil, straight: false) @compare = compare @project = project + @base_sha = base_sha @straight = straight end @@ -22,40 +25,41 @@ class Compare end def start_commit - return @start_commit if defined?(@start_commit) + strong_memoize(:start_commit) do + commit = @compare.base - commit = @compare.base - @start_commit = commit ? ::Commit.new(commit, project) : nil + ::Commit.new(commit, project) if commit + end end def head_commit - return @head_commit if defined?(@head_commit) + strong_memoize(:head_commit) do + commit = @compare.head - commit = @compare.head - @head_commit = commit ? ::Commit.new(commit, project) : nil + ::Commit.new(commit, project) if commit + end end alias_method :commit, :head_commit def base_commit - return @base_commit if defined?(@base_commit) + strong_memoize(:base_commit) do + return unless start_commit && head_commit + return OpenStruct.new(sha: @base_sha) if @base_sha - @base_commit = if start_commit && head_commit - project.merge_base_commit(start_commit.id, head_commit.id) - else - nil - end + project.merge_base_commit(start_commit.id, head_commit.id) + end end def start_commit_sha - start_commit.try(:sha) + start_commit&.sha end def base_commit_sha - base_commit.try(:sha) + base_commit&.sha end def head_commit_sha - commit.try(:sha) + commit&.sha end def raw_diffs(*args) |