Skip to content

Commit dd07cd7

Browse files
committed
Use Hex.State to store cached repo identifier
1 parent 100e500 commit dd07cd7

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

lib/hex/state.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ defmodule Hex.State do
102102
env: ["HEX_REPOS_KEY"],
103103
config: [:repos_key]
104104
},
105+
repo_identifier: %{
106+
env: ["HEX_REPO_IDENTIFIER"],
107+
config: [:repo_identifier],
108+
},
105109
diff_command: %{
106110
env: ["HEX_DIFF_COMMAND"],
107111
config: [:diff_command],

lib/hex/utils.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,23 +338,23 @@ defmodule Hex.Utils do
338338
- The current directory isn't within a git repository
339339
"""
340340
def repo_identifier do
341-
case Process.get(:hex_repo_identifier) do
341+
case Hex.State.get(:repo_identifier) do
342342
cached when is_binary(cached) ->
343343
cached
344344

345345
nil ->
346-
cmd_opts = ~w(rev-list --max-parents=0 HEAD)
346+
cmd_args = ~w(rev-list --max-parents=0 HEAD)
347347

348348
with false <- Hex.State.get(:no_repo_identifier),
349349
path when is_binary(path) <- System.find_executable("git"),
350-
{output, 0} <- System.cmd("git", cmd_opts, stderr_to_stdout: true) do
350+
{output, 0} <- System.cmd("git", cmd_args, stderr_to_stdout: true) do
351351
identifier =
352352
output
353353
|> String.trim()
354354
|> then(&:crypto.hash(:sha256, &1))
355355
|> Base.encode16(case: :lower)
356356

357-
Process.put(:hex_repo_identifier, identifier)
357+
Hex.State.put(:repo_identifier, identifier)
358358

359359
identifier
360360
else

test/hex/utils_test.exs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
defmodule Hex.UtilsTest do
22
use ExUnit.Case
33

4+
setup do
5+
Hex.State.put(:repo_identifier, nil)
6+
7+
:ok
8+
end
9+
410
describe "repo_identifier/0" do
511
test "an identifier is included within a repository" do
612
assert Hex.Utils.repo_identifier() =~ ~r/^[a-f0-9]{64}$/
@@ -37,7 +43,7 @@ defmodule Hex.UtilsTest do
3743
test "the identifier is cached within the current process" do
3844
value = "cached-identifier"
3945

40-
Process.put(:hex_repo_identifier, value)
46+
Hex.State.put(:repo_identifier, value)
4147

4248
assert value == Hex.Utils.repo_identifier()
4349
end

0 commit comments

Comments
 (0)