Skip to content

Commit

Permalink
fix(self-update): use brew --repository instead of --prefix (#936)
Browse files Browse the repository at this point in the history
The brew repository is where the taps will be located, and can be
different than the brew prefix. This thus changes to use `brew
--repository` instead of `brew --prefix` to locate and update the tap
before triggering the `brew upgrade` for omni.
  • Loading branch information
xaf authored Feb 4, 2025
1 parent fbff613 commit bdb42db
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 26 deletions.
86 changes: 62 additions & 24 deletions src/internal/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,30 +70,10 @@ lazy_static! {
};

#[derive(Debug)]
static ref HOMEBREW_PREFIX: Option<String> = {
// Get the homebrew prefix, either through the HOMEBREW_PREFIX env var
// if available, or by calling `brew --prefix`; if both fail, we're not
// installed with homebrew since... probably no homebrew.
if let Ok(prefix) = std::env::var("HOMEBREW_PREFIX") {
if !prefix.is_empty() {
return Some(prefix);
}
}
let output = std::process::Command::new("brew")
.arg("--prefix")
.output();
if let Ok(output) = output {
if output.status.success() {
if let Ok(prefix) = String::from_utf8(output.stdout) {
let prefix = prefix.trim();
if !prefix.is_empty() {
return Some(prefix.to_string());
}
}
}
}
None
};
static ref HOMEBREW_PREFIX: Option<String> = resolve_homebrew_prefix();

#[derive(Debug)]
static ref HOMEBREW_REPOSITORY: Option<String> = resolve_homebrew_repository();

#[derive(Debug)]
static ref TMPDIR_CLEANUP_PREFIX: String = {
Expand Down Expand Up @@ -639,10 +619,68 @@ pub fn current_dir() -> PathBuf {
std::env::current_dir().expect("failed to get current dir")
}

/// Get the homebrew prefix, if available.
#[inline]
fn resolve_homebrew_prefix() -> Option<String> {
// Get the homebrew prefix, either through the HOMEBREW_PREFIX env var
// if available, or by calling `brew --prefix`; if both fail, we're not
// installed with homebrew since... probably no homebrew.
if let Ok(prefix) = std::env::var("HOMEBREW_PREFIX") {
if !prefix.is_empty() {
return Some(prefix);
}
}
let output = std::process::Command::new("brew").arg("--prefix").output();
if let Ok(output) = output {
if output.status.success() {
if let Ok(prefix) = String::from_utf8(output.stdout) {
let prefix = prefix.trim();
if !prefix.is_empty() {
return Some(prefix.to_string());
}
}
}
}
None
}

/// Returns the homebrew prefix, if available.
pub fn homebrew_prefix() -> Option<String> {
(*HOMEBREW_PREFIX).clone()
}

/// Get the homebrew repository, if available.
#[inline]
fn resolve_homebrew_repository() -> Option<String> {
// Get the homebrew repository, either through the HOMEBREW_REPOSITORY env var
// if available, or by calling `brew --repository`; if both fail, we're not
// installed with homebrew since... probably no homebrew.
if let Ok(repository) = std::env::var("HOMEBREW_REPOSITORY") {
if !repository.is_empty() {
return Some(repository);
}
}
let output = std::process::Command::new("brew")
.arg("--repository")
.output();
if let Ok(output) = output {
if output.status.success() {
if let Ok(repository) = String::from_utf8(output.stdout) {
let repository = repository.trim();
if !repository.is_empty() {
return Some(repository.to_string());
}
}
}
}
None
}

/// Returns the homebrew repository, if available.
pub fn homebrew_repository() -> Option<String> {
(*HOMEBREW_REPOSITORY).clone()
}

#[derive(Debug, Clone)]
pub struct GitRepoEnvByPath {
env_by_path: HashMap<String, GitRepoEnv>,
Expand Down
5 changes: 3 additions & 2 deletions src/internal/self_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use crate::internal::config::up::utils::RunConfig;
use crate::internal::config::up::utils::SpinnerProgressHandler;
use crate::internal::env::current_exe;
use crate::internal::env::homebrew_prefix;
use crate::internal::env::homebrew_repository;
use crate::internal::env::shell_is_interactive;
use crate::internal::user_interface::colors::StringColor;
use crate::internal::ConfigLoader;
Expand Down Expand Up @@ -485,8 +486,8 @@ impl OmniRelease {
let mut git_pull = TokioCommand::new("git");
git_pull.arg("pull");
git_pull.current_dir(
Path::new(&homebrew_prefix().ok_or_else(|| {
io::Error::new(io::ErrorKind::Other, "failed to get homebrew prefix")
Path::new(&homebrew_repository().ok_or_else(|| {
io::Error::new(io::ErrorKind::Other, "failed to get homebrew repository")
})?)
.join("Library")
.join("Taps")
Expand Down

0 comments on commit bdb42db

Please sign in to comment.