Commit b1c81f84 authored by Timothy Andrew's avatar Timothy Andrew

Have `Project#open_branches` return branches that are matched by a wildcard protected branch.

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).
parent d8d5424d
...@@ -802,8 +802,12 @@ class Project < ActiveRecord::Base ...@@ -802,8 +802,12 @@ class Project < ActiveRecord::Base
@repo_exists = false @repo_exists = false
end end
# Branches that are not _exactly_ matched by a protected branch.
def open_branches 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 end
def root_ref?(branch) def root_ref?(branch)
......
...@@ -34,7 +34,7 @@ class ProtectedBranch < ActiveRecord::Base ...@@ -34,7 +34,7 @@ class ProtectedBranch < ActiveRecord::Base
# Checks if this protected branch contains a wildcard # Checks if this protected branch contains a wildcard
def wildcard? def wildcard?
self.name.include?('*') self.name && self.name.include?('*')
end end
protected protected
......
...@@ -438,12 +438,12 @@ describe Project, models: true do ...@@ -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)).to include('feature') }
it { expect(project.open_branches.map(&:name)).not_to include('master') } 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') expect(project.open_branches.map(&:name)).to include('feature')
create(:protected_branch, name: 'feat*', project: project) 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
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment