summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAhmad Sherif <me@ahmadsherif.com>2017-02-13 17:42:38 +0200
committerAhmad Sherif <me@ahmadsherif.com>2017-02-13 17:42:38 +0200
commit12a08e9d8d00fa1c8d9c36dc2f40247a62ed5572 (patch)
treec0ad1cb4f8f4002b2b6f97d5ae324c99bf7a0399
parent0fddece769352b650c01d3a0920eea05f378b940 (diff)
downloadgitlab-ce-fix/optimize-commit-for-sha-only-access.tar.gz
Eliminate git access from MergeRequestsController#ci_statusfix/optimize-commit-for-sha-only-access
-rw-r--r--app/controllers/projects/merge_requests_controller.rb2
-rw-r--r--app/models/merge_request.rb28
-rw-r--r--lib/gitlab/git/ref.rb12
-rw-r--r--lib/gitlab/git/repository.rb2
4 files changed, 23 insertions, 21 deletions
diff --git a/app/controllers/projects/merge_requests_controller.rb b/app/controllers/projects/merge_requests_controller.rb
index fbad66c5c40..cced7054258 100644
--- a/app/controllers/projects/merge_requests_controller.rb
+++ b/app/controllers/projects/merge_requests_controller.rb
@@ -441,7 +441,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController
response = {
title: merge_request.title,
- sha: (merge_request.diff_head_commit.short_id if merge_request.diff_head_sha),
+ sha: (Commit.truncate_sha(merge_request.diff_head_sha) if merge_request.diff_head_sha),
status: status,
coverage: coverage,
pipeline: pipeline.try(:id)
diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb
index c0d4dd0197f..2cb07af72fa 100644
--- a/app/models/merge_request.rb
+++ b/app/models/merge_request.rb
@@ -230,23 +230,19 @@ class MergeRequest < ActiveRecord::Base
end
def diff_start_commit
- if persisted?
- merge_request_diff.start_commit
- else
- target_branch_head
- end
+ project.commit(diff_start_sha)
end
def diff_head_commit
- if persisted?
- merge_request_diff.head_commit
- else
- source_branch_head
- end
+ project.commit(diff_head_sha)
end
def diff_start_sha
- diff_start_commit.try(:sha)
+ if persisted?
+ merge_request_diff.start_commit_sha
+ else
+ target_branch_sha
+ end
end
def diff_base_sha
@@ -254,7 +250,11 @@ class MergeRequest < ActiveRecord::Base
end
def diff_head_sha
- diff_head_commit.try(:sha)
+ if persisted?
+ merge_request_diff.head_commit_sha
+ else
+ source_branch_sha
+ end
end
# When importing a pull request from GitHub, the old and new branches may no
@@ -283,11 +283,11 @@ class MergeRequest < ActiveRecord::Base
end
def target_branch_sha
- @target_branch_sha || target_branch_head.try(:sha)
+ @target_branch_sha || project.repository.find_branch(target_branch).try(:target)
end
def source_branch_sha
- @source_branch_sha || source_branch_head.try(:sha)
+ @source_branch_sha || project.repository.find_branch(source_branch).try(:target)
end
def diff_refs
diff --git a/lib/gitlab/git/ref.rb b/lib/gitlab/git/ref.rb
index 37ef6836742..1bec44b4645 100644
--- a/lib/gitlab/git/ref.rb
+++ b/lib/gitlab/git/ref.rb
@@ -12,10 +12,6 @@ module Gitlab
# when tag reference on other tag it can be tag sha
attr_reader :target
- # Dereferenced target
- # Commit object to which the Ref points to
- attr_reader :dereferenced_target
-
# Extract branch name from full ref path
#
# Ex.
@@ -33,7 +29,7 @@ module Gitlab
def initialize(repository, name, target)
encode! name
@name = name.gsub(/\Arefs\/(tags|heads)\//, '')
- @dereferenced_target = Gitlab::Git::Commit.find(repository, target)
+ @repository = repository
@target = if target.respond_to?(:oid)
target.oid
elsif target.respond_to?(:name)
@@ -44,6 +40,12 @@ module Gitlab
nil
end
end
+
+ # Dereferenced target
+ # Commit object to which the Ref points to
+ def dereferenced_target
+ @dereferenced_target ||= Gitlab::Git::Commit.find(@repository, target)
+ end
end
end
end
diff --git a/lib/gitlab/git/repository.rb b/lib/gitlab/git/repository.rb
index 7068e68a855..7483b37c2bb 100644
--- a/lib/gitlab/git/repository.rb
+++ b/lib/gitlab/git/repository.rb
@@ -81,7 +81,7 @@ module Gitlab
reload_rugged if force_reload
rugged_ref = rugged.branches[name]
- Gitlab::Git::Branch.new(self, rugged_ref.name, rugged_ref.target) if rugged_ref
+ Gitlab::Git::Branch.new(self, rugged_ref.name, rugged_ref.target_id) if rugged_ref
end
def local_branches