cmd/*env-sync: add HOMEBREW_ENV_SYNC_STRICT mode.

If this variable is set, `brew *env-sync` will only sync the exact
installed versions of formulae rather than all the patch (or, for node,
minor and patch) versions.
This commit is contained in:
Mike McQuaid 2025-03-24 15:00:16 +00:00
parent f104184214
commit 61bb2f6225
No known key found for this signature in database
5 changed files with 50 additions and 10 deletions

View File

@ -55,14 +55,24 @@ module Homebrew
minor_version = version.minor.to_i || 0 minor_version = version.minor.to_i || 0
patch_version = version.patch.to_i || 0 patch_version = version.patch.to_i || 0
(0..minor_version).each do |minor| minor_version_range, patch_version_range = if Homebrew::EnvConfig.env_sync_strict?
(0..patch_version).each do |patch| # Only create symlinks for the exact installed patch version.
# e.g. 23.9.0 => 23.9.0
[[minor_version], [patch_version]]
else
# Create folder symlinks for all patch versions to the latest patch version
# e.g. 23.9.0 => 23.10.1
[0..minor_version, 0..patch_version]
end
minor_version_range.each do |minor|
patch_version_range.each do |patch|
link_path = nodenv_versions/"#{major_version}.#{minor}.#{patch}" link_path = nodenv_versions/"#{major_version}.#{minor}.#{patch}"
# Don't clobber existing user installations. # Don't clobber existing user installations.
next if link_path.exist? && !link_path.symlink? next if link_path.exist? && !link_path.symlink?
FileUtils.rm_f link_path FileUtils.rm_f link_path
FileUtils.ln_sf path, link_path FileUtils.ln_s path, link_path
end end
end end
end end

View File

@ -54,22 +54,29 @@ module Homebrew
minor_version = version.minor.to_i minor_version = version.minor.to_i
patch_version = version.patch.to_i patch_version = version.patch.to_i
(0..patch_version).each do |patch| patch_version_range = if Homebrew::EnvConfig.env_sync_strict?
# Only create symlinks for the exact installed patch version.
# e.g. 3.11.0 => 3.11.0
[patch_version]
else
# Create folder symlinks for all patch versions to the latest patch version # Create folder symlinks for all patch versions to the latest patch version
# (eg. 3.11.0 -> 3.11.3). # e.g. 3.11.0 => 3.11.3
0..patch_version
end
patch_version_range.each do |patch|
link_path = pyenv_versions/"#{major_version}.#{minor_version}.#{patch}" link_path = pyenv_versions/"#{major_version}.#{minor_version}.#{patch}"
# Don't clobber existing user installations. # Don't clobber existing user installations.
next if link_path.exist? && !link_path.symlink? next if link_path.exist? && !link_path.symlink?
FileUtils.rm_f link_path FileUtils.rm_f link_path
FileUtils.ln_sf path, link_path FileUtils.ln_s path, link_path
# Create an unversioned symlinks # Create an unversioned symlinks
# This is what pyenv expects to find in ~/.pyenv/versions/___/bin'. # This is what pyenv expects to find in ~/.pyenv/versions/___/bin'.
# Without this, `python3`, `pip3` do not exist and pyenv falls back to system Python. # Without this, `python3`, `pip3` do not exist and pyenv falls back to system Python.
# (eg. python3 -> python3.11, pip3 -> pip3.11) # (eg. python3 -> python3.11, pip3 -> pip3.11)
executables = %w[python3 pip3 wheel3 idle3 pydoc3] executables = %w[python3 pip3 wheel3 idle3 pydoc3]
executables.each do |executable| executables.each do |executable|
major_link_path = link_path/"bin/#{executable}" major_link_path = link_path/"bin/#{executable}"
@ -77,8 +84,14 @@ module Homebrew
# Don't clobber existing user installations. # Don't clobber existing user installations.
next if major_link_path.exist? && !major_link_path.symlink? next if major_link_path.exist? && !major_link_path.symlink?
executable_link_path = link_path/"bin/#{executable}.#{minor_version}"
FileUtils.rm_f major_link_path FileUtils.rm_f major_link_path
FileUtils.ln_s link_path/"bin/#{executable}.#{minor_version}", major_link_path
begin
FileUtils.ln_s executable_link_path, major_link_path
rescue => e
opoo "Failed to link #{executable_link_path} to #{major_link_path}: #{e}"
end
end end
end end
end end

View File

@ -55,13 +55,23 @@ module Homebrew
minor_version = version.minor.to_i minor_version = version.minor.to_i
patch_version = version.patch.to_i || 0 patch_version = version.patch.to_i || 0
(0..patch_version).each do |patch| patch_version_range = if Homebrew::EnvConfig.env_sync_strict?
# Only create symlinks for the exact installed patch version.
# e.g. 3.4.0 => 3.4.0
[patch_version]
else
# Create folder symlinks for all patch versions to the latest patch version
# e.g. 3.4.0 => 3.4.2
0..patch_version
end
patch_version_range.each do |patch|
link_path = rbenv_versions/"#{major_version}.#{minor_version}.#{patch}" link_path = rbenv_versions/"#{major_version}.#{minor_version}.#{patch}"
# Don't clobber existing user installations. # Don't clobber existing user installations.
next if link_path.exist? && !link_path.symlink? next if link_path.exist? && !link_path.symlink?
FileUtils.rm_f link_path FileUtils.rm_f link_path
FileUtils.ln_sf path, link_path FileUtils.ln_s path, link_path
end end
end end
end end

View File

@ -195,6 +195,10 @@ module Homebrew
"editors will do strange things in this case.", "editors will do strange things in this case.",
default_text: "`$EDITOR` or `$VISUAL`.", default_text: "`$EDITOR` or `$VISUAL`.",
}, },
HOMEBREW_ENV_SYNC_STRICT: {
description: "If set, `brew *env-sync` will only sync the exact installed versions of formulae.",
boolean: true,
},
HOMEBREW_EVAL_ALL: { HOMEBREW_EVAL_ALL: {
description: "If set, `brew` commands evaluate all formulae and casks, executing their arbitrary code, by " \ description: "If set, `brew` commands evaluate all formulae and casks, executing their arbitrary code, by " \
"default without requiring `--eval-all`. Required to cache formula and cask descriptions.", "default without requiring `--eval-all`. Required to cache formula and cask descriptions.",

View File

@ -115,6 +115,9 @@ module Homebrew::EnvConfig
sig { returns(T.nilable(::String)) } sig { returns(T.nilable(::String)) }
def editor; end def editor; end
sig { returns(T::Boolean) }
def env_sync_strict?; end
sig { returns(T::Boolean) } sig { returns(T::Boolean) }
def eval_all?; end def eval_all?; end