summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Andrew <mail@timothyandrew.net>2016-07-06 15:07:30 +0530
committerTimothy Andrew <mail@timothyandrew.net>2016-07-07 10:07:31 +0530
commitb1c81f849e5e5b03f56e89cdcefba029ed5c0543 (patch)
treeccfad1ee0b534e6a2239f8a51fe4d468d4a14896
parentd8d5424d25c1738b170d58657ef71d4dbc89ca5e (diff)
downloadgitlab-ce-18627-wildcard-branch-protection.tar.gz
Have `Project#open_branches` return branches that are matched by a wildcard protected branch.18627-wildcard-branch-protection
1. The `open_branches` method is used to provide a list of branches while creating a protected branch. 2. It makes sense to include branches which are matched by one or more wildcard protected branches, since the user might want to make exact protected branches from these as well. 3. This also provides a large performance improvement. On my machine, in a project with 5000 branches and 2000 protected branches, the `ProtectedBranches#index` page went from a 40 seconds load time to 4 seconds (10x speedup).
-rw-r--r--app/models/project.rb6
-rw-r--r--app/models/protected_branch.rb2
-rw-r--r--spec/models/project_spec.rb4
3 files changed, 8 insertions, 4 deletions
diff --git a/app/models/project.rb b/app/models/project.rb
index d5d57bafb98..087f4314565 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -802,8 +802,12 @@ class Project < ActiveRecord::Base
@repo_exists = false
end
+ # Branches that are not _exactly_ matched by a protected branch.
def open_branches
- repository.branches.reject { |branch| self.protected_branch?(branch.name) }
+ exact_protected_branch_names = protected_branches.reject(&:wildcard?).map(&:name)
+ branch_names = repository.branches.map(&:name)
+ non_open_branch_names = Set.new(exact_protected_branch_names).intersection(Set.new(branch_names))
+ repository.branches.reject { |branch| non_open_branch_names.include? branch.name }
end
def root_ref?(branch)
diff --git a/app/models/protected_branch.rb b/app/models/protected_branch.rb
index d3d5e1d98b2..b7011d7afdf 100644
--- a/app/models/protected_branch.rb
+++ b/app/models/protected_branch.rb
@@ -34,7 +34,7 @@ class ProtectedBranch < ActiveRecord::Base
# Checks if this protected branch contains a wildcard
def wildcard?
- self.name.include?('*')
+ self.name && self.name.include?('*')
end
protected
diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb
index 117ffd551e4..cd126027c95 100644
--- a/spec/models/project_spec.rb
+++ b/spec/models/project_spec.rb
@@ -438,12 +438,12 @@ describe Project, models: true do
it { expect(project.open_branches.map(&:name)).to include('feature') }
it { expect(project.open_branches.map(&:name)).not_to include('master') }
- it "does not include branches matching a protected branch wildcard" do
+ it "includes branches matching a protected branch wildcard" do
expect(project.open_branches.map(&:name)).to include('feature')
create(:protected_branch, name: 'feat*', project: project)
- expect(Project.find(project.id).open_branches.map(&:name)).not_to include('feature')
+ expect(Project.find(project.id).open_branches.map(&:name)).to include('feature')
end
end