Commit 208b18c9 authored by Paco Guzman's avatar Paco Guzman

Unify check branch name exist

parent 74f8f260
...@@ -74,7 +74,7 @@ class Projects::ApplicationController < ApplicationController ...@@ -74,7 +74,7 @@ class Projects::ApplicationController < ApplicationController
end end
def require_branch_head def require_branch_head
unless @repository.branch_names.include?(@ref) unless @repository.branch_exists?(@ref)
redirect_to( redirect_to(
namespace_project_tree_path(@project.namespace, @project, @ref), namespace_project_tree_path(@project.namespace, @project, @ref),
notice: "This action is not allowed unless you are on a branch" notice: "This action is not allowed unless you are on a branch"
......
...@@ -116,7 +116,7 @@ module ApplicationHelper ...@@ -116,7 +116,7 @@ module ApplicationHelper
return false if project.merge_requests.where(source_branch: event.branch_name).opened.any? return false if project.merge_requests.where(source_branch: event.branch_name).opened.any?
# Skip if user removed branch right after that # Skip if user removed branch right after that
return false unless project.repository.branch_names.include?(event.branch_name) return false unless project.repository.branch_exists?(event.branch_name)
true true
end end
......
...@@ -10,7 +10,7 @@ module BranchesHelper ...@@ -10,7 +10,7 @@ module BranchesHelper
end end
def can_push_branch?(project, branch_name) def can_push_branch?(project, branch_name)
return false unless project.repository.branch_names.include?(branch_name) return false unless project.repository.branch_exists?(branch_name)
::Gitlab::GitAccess.new(current_user, project).can_push_to_branch?(branch_name) ::Gitlab::GitAccess.new(current_user, project).can_push_to_branch?(branch_name)
end end
......
...@@ -192,7 +192,7 @@ class Repository ...@@ -192,7 +192,7 @@ class Repository
end end
def branch_names def branch_names
cache.fetch(:branch_names) { branches.map(&:name) } @branch_names ||= cache.fetch(:branch_names) { branches.map(&:name) }
end end
def branch_exists?(branch_name) def branch_exists?(branch_name)
...@@ -267,6 +267,7 @@ class Repository ...@@ -267,6 +267,7 @@ class Repository
def expire_branches_cache def expire_branches_cache
cache.expire(:branch_names) cache.expire(:branch_names)
@branch_names = nil
@local_branches = nil @local_branches = nil
end end
...@@ -332,10 +333,6 @@ class Repository ...@@ -332,10 +333,6 @@ class Repository
@lookup_cache ||= {} @lookup_cache ||= {}
end end
def expire_branch_names
cache.expire(:branch_names)
end
def expire_avatar_cache(branch_name = nil, revision = nil) def expire_avatar_cache(branch_name = nil, revision = nil)
# Avatars are pulled from the default branch, thus if somebody pushes to a # Avatars are pulled from the default branch, thus if somebody pushes to a
# different branch there's no need to expire anything. # different branch there's no need to expire anything.
......
...@@ -487,9 +487,8 @@ class User < ActiveRecord::Base ...@@ -487,9 +487,8 @@ class User < ActiveRecord::Base
events.recent.find do |event| events.recent.find do |event|
project = Project.find_by_id(event.project_id) project = Project.find_by_id(event.project_id)
next unless project next unless project
repo = project.repository
if repo.branch_names.include?(event.branch_name) if project.repository.branch_exists?(event.branch_name)
merge_requests = MergeRequest.where("created_at >= ?", event.created_at). merge_requests = MergeRequest.where("created_at >= ?", event.created_at).
where(source_project_id: project.id, where(source_project_id: project.id,
source_branch: event.branch_name) source_branch: event.branch_name)
......
...@@ -9,7 +9,7 @@ describe MergeWorker do ...@@ -9,7 +9,7 @@ describe MergeWorker do
before do before do
source_project.team << [author, :master] source_project.team << [author, :master]
source_project.repository.expire_branch_names source_project.repository.expire_branches_cache
end end
it 'clears cache of source repo after removing source branch' do it 'clears cache of source repo after removing source branch' do
......
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