summaryrefslogtreecommitdiff
path: root/app/finders
diff options
context:
space:
mode:
authorJason Rutherford <jason.rutherford@plansource.com>2018-10-11 14:25:30 +0000
committerDouglas Barbosa Alexandre <dbalexandre@gmail.com>2018-10-11 14:25:30 +0000
commitab5e8dc8800886ca206ee9e6ffe80026b319063c (patch)
treece697fc2969f57fea4455be6ccdf5a5f03b0ae0f /app/finders
parent5b6b2871dfc8f2159103df7f2803459b2cee1647 (diff)
downloadgitlab-ce-ab5e8dc8800886ca206ee9e6ffe80026b319063c.tar.gz
Feature improved branch filter sorting
Diffstat (limited to 'app/finders')
-rw-r--r--app/finders/branches_finder.rb41
1 files changed, 35 insertions, 6 deletions
diff --git a/app/finders/branches_finder.rb b/app/finders/branches_finder.rb
index 970efa79dfb..45d5591e81b 100644
--- a/app/finders/branches_finder.rb
+++ b/app/finders/branches_finder.rb
@@ -7,8 +7,9 @@ class BranchesFinder
end
def execute
- branches = @repository.branches_sorted_by(sort)
- filter_by_name(branches)
+ branches = repository.branches_sorted_by(sort)
+ branches = by_search(branches)
+ branches
end
private
@@ -23,11 +24,39 @@ class BranchesFinder
@params[:sort].presence || 'name'
end
- def filter_by_name(branches)
- if search
- branches.select { |branch| branch.name.upcase.include?(search.upcase) }
+ def by_search(branches)
+ return branches unless search
+
+ case search
+ when ->(v) { v.starts_with?('^') }
+ filter_branches_with_prefix(branches, search.slice(1..-1).upcase)
+ when ->(v) { v.ends_with?('$') }
+ filter_branches_with_suffix(branches, search.chop.upcase)
else
- branches
+ matches = filter_branches_by_name(branches, search.upcase)
+ set_exact_match_as_first_result(matches, search)
end
end
+
+ def filter_branches_with_prefix(branches, prefix)
+ branches.select { |branch| branch.name.upcase.starts_with?(prefix) }
+ end
+
+ def filter_branches_with_suffix(branches, suffix)
+ branches.select { |branch| branch.name.upcase.ends_with?(suffix) }
+ end
+
+ def filter_branches_by_name(branches, term)
+ branches.select { |branch| branch.name.upcase.include?(term) }
+ end
+
+ def set_exact_match_as_first_result(matches, term)
+ exact_match_index = find_exact_match_index(matches, term)
+ matches.insert(0, matches.delete_at(exact_match_index)) if exact_match_index
+ matches
+ end
+
+ def find_exact_match_index(matches, term)
+ matches.index { |branch| branch.name.casecmp(term) == 0 }
+ end
end