summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Speicher <robert@gitlab.com>2016-04-29 21:45:15 +0000
committerRobert Speicher <robert@gitlab.com>2016-04-29 21:45:15 +0000
commit11773f3fc9ecdf125f453d40f4158b0cd3d2b970 (patch)
treee6d0b85c8f9eb7e25aa438e5d28130e93b6411a2
parent65e845bad9bfe987f301b1100ce8262e4b724ccc (diff)
parentb6a18f1093c88e82d2320d4d3f3c15c549b861be (diff)
downloadgitlab-ce-11773f3fc9ecdf125f453d40f4158b0cd3d2b970.tar.gz
Merge branch 'check-protected-branches' into 'master'
Cleaned up/tweaked Project#open_branches See commit c825404572040a3a45cb9e2b3d3e7d64900f66c9 for the details of the changes and https://gitlab.com/gitlab-org/gitlab-ce/issues/14280#note_4973648 for more information. See merge request !3985
-rw-r--r--CHANGELOG1
-rw-r--r--app/models/project.rb18
-rw-r--r--spec/models/project_spec.rb14
3 files changed, 23 insertions, 10 deletions
diff --git a/CHANGELOG b/CHANGELOG
index 5bd897126b2..121b03be320 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,7 @@
Please view this file on the master branch, on stable branches it's out of date.
v 8.8.0 (unreleased)
+ - Project#open_branches has been cleaned up and no longer loads entire records into memory.
- Make build status canceled if any of the jobs was canceled and none failed
- Remove future dates from contribution calendar graph.
- Use ActionDispatch Remote IP for Akismet checking
diff --git a/app/models/project.rb b/app/models/project.rb
index 5c6c36e6b31..af62e8ecd90 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -735,19 +735,17 @@ class Project < ActiveRecord::Base
end
def open_branches
- all_branches = repository.branches
+ # We're using a Set here as checking values in a large Set is faster than
+ # checking values in a large Array.
+ protected_set = Set.new(protected_branch_names)
- if protected_branches.present?
- all_branches.reject! do |branch|
- protected_branches_names.include?(branch.name)
- end
+ repository.branches.reject do |branch|
+ protected_set.include?(branch.name)
end
-
- all_branches
end
- def protected_branches_names
- @protected_branches_names ||= protected_branches.map(&:name)
+ def protected_branch_names
+ @protected_branch_names ||= protected_branches.pluck(:name)
end
def root_ref?(branch)
@@ -764,7 +762,7 @@ class Project < ActiveRecord::Base
# Check if current branch name is marked as protected in the system
def protected_branch?(branch_name)
- protected_branches_names.include?(branch_name)
+ protected_branches.where(name: branch_name).any?
end
def developers_can_push_to_protected_branch?(branch_name)
diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb
index e33c7d62ff4..5b1cf71337e 100644
--- a/spec/models/project_spec.rb
+++ b/spec/models/project_spec.rb
@@ -798,4 +798,18 @@ describe Project, models: true do
end
end
end
+
+ describe '#protected_branch?' do
+ let(:project) { create(:empty_project) }
+
+ it 'returns true when a branch is a protected branch' do
+ project.protected_branches.create!(name: 'foo')
+
+ expect(project.protected_branch?('foo')).to eq(true)
+ end
+
+ it 'returns false when a branch is not a protected branch' do
+ expect(project.protected_branch?('foo')).to eq(false)
+ end
+ end
end