From 0cdfd82bb7ea6b96b05acdedc71c6d85a53be3e4 Mon Sep 17 00:00:00 2001 From: Daniel Rinehart Date: Tue, 11 Mar 2025 15:59:41 +0000 Subject: [PATCH] Optimize `has_ambiguous_refs?` method With large numbers of branch or tag names the regular expression can get very expensive. This revised approach uses set intersection to check for overlap. --- app/models/repository.rb | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/app/models/repository.rb b/app/models/repository.rb index 1992f49bbbe396..e9555628d64210 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -238,16 +238,20 @@ def ambiguous_ref?(ref) def has_ambiguous_refs? return false unless branch_names.present? && tag_names.present? - with_slash, no_slash = (branch_names + tag_names).partition { |ref| ref.include?('/') } + with_slash = [] + no_slash = [] + (branch_names + tag_names).each do |ref| + slash_index = ref.index('/') + if slash_index.present? + with_slash << ref.first(slash_index) + else + no_slash << ref + end + end return false if with_slash.empty? - prefixes = no_slash.map { |ref| Regexp.escape(ref) }.join('|') - prefix_regex = %r{^(#{prefixes})/} - - with_slash.any? do |ref| - prefix_regex.match?(ref) - end + with_slash.intersect?(no_slash) end cache_method :has_ambiguous_refs? -- GitLab