Skip to content

Commit 100e500

Browse files
committed
Use Hex.State agent for identifier config
Switch from a raw `System.get_env` to using the normalized state agent. Also prevent STDERR from leaking when called outside of a git repository.
1 parent ffb5c02 commit 100e500

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

lib/hex/state.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ defmodule Hex.State do
112112
default: nil,
113113
config: [:cacerts_path]
114114
},
115+
no_repo_identifier: %{
116+
env: ["HEX_NO_REPO_IDENTIFIER"],
117+
default: false,
118+
config: [:no_repo_identifier],
119+
fun: {__MODULE__, :to_boolean}
120+
},
115121
no_short_urls: %{
116122
env: ["HEX_NO_SHORT_URLS"],
117123
config: [:no_short_urls],

lib/hex/utils.ex

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,14 +338,16 @@ 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, :unset) do
341+
case Process.get(:hex_repo_identifier) do
342342
cached when is_binary(cached) ->
343343
cached
344344

345-
_ ->
346-
with flag when flag in ["1", "true"] <- System.get_env("HEX_REPO_IDENTIFIER", "1"),
345+
nil ->
346+
cmd_opts = ~w(rev-list --max-parents=0 HEAD)
347+
348+
with false <- Hex.State.get(:no_repo_identifier),
347349
path when is_binary(path) <- System.find_executable("git"),
348-
{output, 0} <- System.cmd("git", ["rev-list", "--max-parents=0", "HEAD"]) do
350+
{output, 0} <- System.cmd("git", cmd_opts, stderr_to_stdout: true) do
349351
identifier =
350352
output
351353
|> String.trim()

test/hex/utils_test.exs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
defmodule Hex.UtilsTest do
22
use ExUnit.Case
33

4-
setup do
5-
on_exit(fn -> Process.delete(:hex_repo_identifier) end)
6-
end
7-
84
describe "repo_identifier/0" do
95
test "an identifier is included within a repository" do
106
assert Hex.Utils.repo_identifier() =~ ~r/^[a-f0-9]{64}$/
@@ -29,13 +25,13 @@ defmodule Hex.UtilsTest do
2925
end
3026

3127
test "identifier is nil when disabled by an environment variable" do
32-
System.put_env("HEX_REPO_IDENTIFIER", "0")
33-
refute Hex.Utils.repo_identifier()
28+
System.put_env("HEX_NO_REPO_IDENTIFIER", "1")
29+
Hex.State.refresh()
3430

35-
System.put_env("HEX_REPO_IDENTIFIER", "false")
3631
refute Hex.Utils.repo_identifier()
3732
after
38-
System.delete_env("HEX_REPO_IDENTIFIER")
33+
System.delete_env("HEX_NO_REPO_IDENTIFIER")
34+
Hex.State.refresh()
3935
end
4036

4137
test "the identifier is cached within the current process" do

0 commit comments

Comments
 (0)