Skip to content

Commit

Permalink
Avoid keyword pattern matching
Browse files Browse the repository at this point in the history
When pattern matching using keywords the order matters and can cause
problems.
  • Loading branch information
qgadrian committed Oct 13, 2021
1 parent 3b7cb27 commit aa77359
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions lib/config/branch_config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ defmodule GitHooks.Config.BranchConfig do
@spec current_branch_allowed?(atom) :: boolean()
def current_branch_allowed?(git_hook_type) do
branch = current_branch()
[whitelist: whitelist, blacklist: blacklist] = branches(git_hook_type)

branches_config = branches(git_hook_type)

whitelist = Keyword.get(branches_config, :whitelist, [])
blacklist = Keyword.get(branches_config, :blacklist, [])

valid_branch?(branch, whitelist, blacklist)
end
Expand All @@ -50,19 +54,15 @@ defmodule GitHooks.Config.BranchConfig do
valid_branch?(branch, whitelist, []) or valid_branch?(branch, [], blacklist)
end

@empty_branches [whitelist: [], blacklist: []]
@spec branches() :: Keyword.t()
defp branches do
Keyword.merge(@empty_branches, Application.get_env(:git_hooks, :branches, []))
Application.get_env(:git_hooks, :branches, [])
end

@spec branches(atom) :: Keyword.t()
defp branches(git_hook_type) do
branches_config =
git_hook_type
|> Config.get_git_hook_type_config()
|> Keyword.get_lazy(:branches, fn -> branches() end)

Keyword.merge(@empty_branches, branches_config)
git_hook_type
|> Config.get_git_hook_type_config()
|> Keyword.get_lazy(:branches, fn -> branches() end)
end
end

0 comments on commit aa77359

Please sign in to comment.