From 9add3e6eb56bb8d8a9b8e4e105f7beec27e685d2 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Sun, 21 Jun 2015 08:26:37 -0700 Subject: Extract the longest-matching ref from a commit path when multiple matches occur Closes #1839 --- lib/extracts_path.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'lib/extracts_path.rb') diff --git a/lib/extracts_path.rb b/lib/extracts_path.rb index 6e4ed01e079..3f420553d42 100644 --- a/lib/extracts_path.rb +++ b/lib/extracts_path.rb @@ -55,12 +55,16 @@ module ExtractsPath valid_refs = @project.repository.ref_names valid_refs.select! { |v| id.start_with?("#{v}/") } - if valid_refs.length != 1 + if valid_refs.length == 0 # No exact ref match, so just try our best pair = id.match(/([^\/]+)(.*)/).captures else + # There is a distinct possibility that multiple refs prefix the ID. + # Use the longest match to maximize the chance that we have the + # right ref. + best_match = valid_refs.max_by(&:length) # Partition the string into the ref and the path, ignoring the empty first value - pair = id.partition(valid_refs.first)[1..-1] + pair = id.partition(best_match)[1..-1] end end -- cgit v1.2.3