Credo: identity cases and withs return their value directly

3f25757 · Gabe Morell · 2026-11-04 21:22

19 files +12 -47

Files changed

modified lib/git_gud/git.ex
+1 −4
@@ -275,10 +275,7 @@ defmodule GitGud.Git do
275 275 @doc "Decode a 40-char hex string into a 20-byte SHA."
276 276 @spec from_hex(String.t()) :: {:ok, sha()} | :error
277 277 def from_hex(hex) when is_binary(hex) and byte_size(hex) == 40 do
278 − case Base.decode16(hex, case: :mixed) do
279 − {:ok, sha} -> {:ok, sha}
280 − :error -> :error
281 − end
278 + Base.decode16(hex, case: :mixed)
282 279 end
283 280
284 281 def from_hex(_), do: :error
modified lib/git_gud/git/cli.ex
+1 −4
@@ -361,10 +361,7 @@ defmodule GitGud.Git.Cli do
361 361 ["diff", "--no-color", "-U#{context}", Git.to_hex(bs), Git.to_hex(head_sha)]
362 362 end
363 363
364 − case git(path, args) do
365 − {:ok, out} -> {:ok, out}
366 − err -> err
367 − end
364 + git(path, args)
368 365 end
369 366
370 367 @impl true
modified lib/git_gud/packages/format.ex
+1 −6
@@ -195,12 +195,7 @@ defmodule GitGud.Packages.Format do
195 195 def adapters, do: Application.get_env(:git_gud, :package_formats, @default_adapters)
196 196
197 197 @doc "Resolve a format identifier to its adapter module."
198 − def adapter(format) when is_binary(format) do
199 − case Map.fetch(adapters(), format) do
200 − {:ok, mod} -> {:ok, mod}
201 − :error -> :error
202 − end
203 − end
198 + def adapter(format) when is_binary(format), do: Map.fetch(adapters(), format)
204 199
205 200 def adapter(_), do: :error
206 201
modified lib/git_gud/packages/signing/local.ex
+4 −2
@@ -20,8 +20,10 @@ defmodule GitGud.Packages.Signing.Local do
20 20
21 21 @impl true
22 22 def ensure_key(scope, format) do
23 − with {:ok, %{public_pem: pem}} <- Packages.signing_key(scope, format) do
24 − {:ok, %{public_pem: pem}}
23 + # Only the public half leaves: `signing_key/2` also hands back the
24 + # private key.
25 + with {:ok, key} <- Packages.signing_key(scope, format) do
26 + {:ok, Map.take(key, [:public_pem])}
25 27 end
26 28 end
27 29
modified lib/git_gud/pull_requests.ex
+0 −3
@@ -849,9 +849,6 @@ defmodule GitGud.PullRequests do
849 849 _ = GitGud.Federation.Publishing.publish_pr_merged(repo, updated)
850 850 {:ok, updated}
851 851 end
852 − else
853 − {:error, :conflict} -> {:error, :conflict}
854 − err -> err
855 852 end
856 853 end
857 854
modified lib/git_gud/repositories/workers/commit_backfill.ex
+1 −6
@@ -191,12 +191,7 @@ defmodule GitGud.Repositories.Workers.CommitBackfill do
191 191 :ok
192 192 end
193 193
194 − defp maybe_hex(hex) when is_binary(hex) and byte_size(hex) == 40 do
195 − case Git.from_hex(hex) do
196 − {:ok, sha} -> {:ok, sha}
197 − :error -> :error
198 − end
199 − end
194 + defp maybe_hex(hex) when is_binary(hex) and byte_size(hex) == 40, do: Git.from_hex(hex)
200 195
201 196 defp maybe_hex(_), do: {:ok, nil}
202 197
modified lib/git_gud/workflows/expr.ex
+1 −6
@@ -406,12 +406,7 @@ defmodule GitGud.Workflows.Expr do
406 406
407 407 # Top-level names are contexts; a name nobody supplied is an error, not
408 408 # null, because it means the expression reads something we don't model.
409 − defp lookup(contexts, name) do
410 − case find_key(contexts, name) do
411 − {:ok, value} -> {:ok, value}
412 − :error -> :error
413 − end
414 − end
409 + defp lookup(contexts, name), do: find_key(contexts, name)
415 410
416 411 defp property(%{} = map, key) when is_binary(key) do
417 412 case find_key(map, key) do
modified lib/git_gud_web/controllers/pre_receive_controller.ex
+2 −9
@@ -74,8 +74,8 @@ defmodule GitGudWeb.PreReceiveController do
74 74 defp parse_line(line) do
75 75 case String.split(line, " ", parts: 3) do
76 76 [old, new, ref] when byte_size(old) == 40 and byte_size(new) == 40 ->
77 − with {:ok, old_sha} <- decode_sha(old),
78 − {:ok, new_sha} <- decode_sha(new) do
77 + with {:ok, old_sha} <- Git.from_hex(old),
78 + {:ok, new_sha} <- Git.from_hex(new) do
79 79 {old_sha, new_sha, ref}
80 80 else
81 81 _ -> nil
@@ -86,13 +86,6 @@ defmodule GitGudWeb.PreReceiveController do
86 86 end
87 87 end
88 88
89 − defp decode_sha(hex) do
90 − case Git.from_hex(hex) do
91 − {:ok, sha} -> {:ok, sha}
92 − :error -> :error
93 − end
94 − end
95 −
96 89 defp check_one(repo, "refs/heads/" <> branch, old_sha, new_sha) do
97 90 BranchProtections.check_ref_update(repo, branch, old_sha, new_sha)
98 91 end
modified lib/git_gud_web/controllers/runner_controller.ex
+1 −7
@@ -129,13 +129,7 @@ defmodule GitGudWeb.RunnerController do
129 129 # `labels` field is what `claim_next_job/1` filters on, and we
130 130 # don't let the runner self-promote into a job pool it wasn't
131 131 # explicitly assigned to.
132 − agent_labels =
133 − case req.labels do
134 − [] -> []
135 − list -> list
136 − end
137 −
138 − attrs = %{version: req.version, agent_labels: %{"labels" => agent_labels}}
132 + attrs = %{version: req.version, agent_labels: %{"labels" => req.labels}}
139 133
140 134 case runner |> RunnerSchema.changeset(attrs) |> GitGud.Repo.update() do
141 135 {:ok, updated} ->

Parents: 4f53e42