diff --git a/Library/Homebrew/cmd/update-report.rb b/Library/Homebrew/cmd/update-report.rb index a951eafb38808..f700039468b0b 100644 --- a/Library/Homebrew/cmd/update-report.rb +++ b/Library/Homebrew/cmd/update-report.rb @@ -428,11 +428,11 @@ def initialize(tap, api_names_txt: nil, api_names_before_txt: nil, api_dir_prefi @api_names_before_txt = api_names_before_txt @api_dir_prefix = api_dir_prefix else - initial_revision_var = "HOMEBREW_UPDATE_BEFORE#{tap.repo_var}" + initial_revision_var = "HOMEBREW_UPDATE_BEFORE#{tap.repo_var_suffix}" @initial_revision = ENV[initial_revision_var].to_s raise ReporterRevisionUnsetError, initial_revision_var if @initial_revision.empty? - current_revision_var = "HOMEBREW_UPDATE_AFTER#{tap.repo_var}" + current_revision_var = "HOMEBREW_UPDATE_AFTER#{tap.repo_var_suffix}" @current_revision = ENV[current_revision_var].to_s raise ReporterRevisionUnsetError, current_revision_var if @current_revision.empty? end diff --git a/Library/Homebrew/cmd/update.sh b/Library/Homebrew/cmd/update.sh index 8f38b12131f24..4df690c542eae 100644 --- a/Library/Homebrew/cmd/update.sh +++ b/Library/Homebrew/cmd/update.sh @@ -92,18 +92,18 @@ git_init_if_necessary() { fi } -repo_var() { - local repo_var +repo_var_suffix() { + local repo_dir="${1}" + local repo_var_suffix - repo_var="$1" - if [[ "${repo_var}" == "${HOMEBREW_REPOSITORY}" ]] + if [[ "${repo_dir}" == "${HOMEBREW_REPOSITORY}" ]] then - repo_var="" + repo_var_suffix="" else - repo_var="${repo_var#"${HOMEBREW_LIBRARY}/Taps"}" - repo_var="$(echo -n "${repo_var}" | tr -C "A-Za-z0-9" "_" | tr "[:lower:]" "[:upper:]")" + repo_var_suffix="${repo_dir#"${HOMEBREW_LIBRARY}/Taps"}" + repo_var_suffix="$(echo -n "${repo_var_suffix}" | tr -C "A-Za-z0-9" "_" | tr "[:lower:]" "[:upper:]")" fi - echo "${repo_var}" + echo "${repo_var_suffix}" } upstream_branch() { @@ -600,7 +600,7 @@ EOS echo "Checking if we need to fetch ${DIR}..." fi - TAP_VAR="$(repo_var "${DIR}")" + TAP_VAR="$(repo_var_suffix "${DIR}")" UPSTREAM_BRANCH_DIR="$(upstream_branch)" declare UPSTREAM_BRANCH"${TAP_VAR}"="${UPSTREAM_BRANCH_DIR}" declare PREFETCH_REVISION"${TAP_VAR}"="$(git rev-parse -q --verify refs/remotes/origin/"${UPSTREAM_BRANCH_DIR}")" @@ -774,7 +774,7 @@ EOS continue fi - TAP_VAR="$(repo_var "${DIR}")" + TAP_VAR="$(repo_var_suffix "${DIR}")" UPSTREAM_BRANCH_VAR="UPSTREAM_BRANCH${TAP_VAR}" UPSTREAM_BRANCH="${!UPSTREAM_BRANCH_VAR}" CURRENT_REVISION="$(read_current_revision)" diff --git a/Library/Homebrew/tap.rb b/Library/Homebrew/tap.rb index 17ecf569e000c..60df907f123fa 100644 --- a/Library/Homebrew/tap.rb +++ b/Library/Homebrew/tap.rb @@ -122,7 +122,7 @@ def initialize(user, repo) # Clear internal cache. def clear_cache @remote = nil - @repo_var = nil + @repo_var_suffix = nil @formula_dir = nil @cask_dir = nil @command_dir = nil @@ -175,11 +175,13 @@ def default_remote "https://github.com/#{full_name}" end - def repo_var - @repo_var ||= path.to_s - .delete_prefix(TAP_DIRECTORY.to_s) - .tr("^A-Za-z0-9", "_") - .upcase + # @private + sig { returns(String) } + def repo_var_suffix + @repo_var_suffix ||= path.to_s + .delete_prefix(TAP_DIRECTORY.to_s) + .tr("^A-Za-z0-9", "_") + .upcase end # True if this {Tap} is a Git repository. diff --git a/Library/Homebrew/test/cmd/update-report_spec.rb b/Library/Homebrew/test/cmd/update-report_spec.rb index 4a3037ee576a1..bb8b7cc28bfec 100644 --- a/Library/Homebrew/test/cmd/update-report_spec.rb +++ b/Library/Homebrew/test/cmd/update-report_spec.rb @@ -15,8 +15,8 @@ def initialize(tap) @tap = tap - ENV["HOMEBREW_UPDATE_BEFORE#{tap.repo_var}"] = "12345678" - ENV["HOMEBREW_UPDATE_AFTER#{tap.repo_var}"] = "abcdef00" + ENV["HOMEBREW_UPDATE_BEFORE#{tap.repo_var_suffix}"] = "12345678" + ENV["HOMEBREW_UPDATE_AFTER#{tap.repo_var_suffix}"] = "abcdef00" super(tap) end diff --git a/Library/Homebrew/test/tap_spec.rb b/Library/Homebrew/test/tap_spec.rb index c4d9ea8bc4e67..2b735c9d44ae6 100644 --- a/Library/Homebrew/test/tap_spec.rb +++ b/Library/Homebrew/test/tap_spec.rb @@ -588,4 +588,15 @@ class Foo < Formula expect(core_tap.pypi_formula_mappings).to eq formula_list_file_contents end end + + describe "#repo_var_suffix" do + it "converts the repo directory to an environment variable suffix" do + expect(CoreTap.instance.repo_var_suffix).to eq "_HOMEBREW_HOMEBREW_CORE" + end + + it "converts non-alphanumeric characters to underscores" do + expect(described_class.fetch("my", "tap-with-dashes").repo_var_suffix).to eq "_MY_HOMEBREW_TAP_WITH_DASHES" + expect(described_class.fetch("my", "tap-with-@-symbol").repo_var_suffix).to eq "_MY_HOMEBREW_TAP_WITH___SYMBOL" + end + end end