From 13115fee7decb71d453a93b7ec70c9542671e5b0 Mon Sep 17 00:00:00 2001 From: Carlo Cabrera <30379873+carlocab@users.noreply.github.com> Date: Sun, 25 Aug 2024 02:57:35 +0800 Subject: [PATCH 1/4] brew.sh: fix handling of dirty git repo We cache the output of `git describe`, but we don't check whether the cached value is valid. This fixes that. --- Library/Homebrew/brew.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/brew.sh b/Library/Homebrew/brew.sh index 314f683033..242e7eacea 100644 --- a/Library/Homebrew/brew.sh +++ b/Library/Homebrew/brew.sh @@ -457,7 +457,9 @@ fi if [[ -n "${GIT_REVISION}" ]] then GIT_DESCRIBE_CACHE_FILE="${GIT_DESCRIBE_CACHE}/${GIT_REVISION}" - if [[ -r "${GIT_DESCRIBE_CACHE_FILE}" ]] + # Pass `--` to guard against `git` being confused by a file named `HEAD`. + if "${HOMEBREW_GIT}" -C "${HOMEBREW_REPOSITORY}" diff-index --quiet --exit-code HEAD -- 2>/dev/null && + [[ -r "${GIT_DESCRIBE_CACHE_FILE}" ]] then GIT_DESCRIBE_CACHE_HOMEBREW_VERSION="$(cat "${GIT_DESCRIBE_CACHE_FILE}")" if [[ -n "${GIT_DESCRIBE_CACHE_HOMEBREW_VERSION}" && "${GIT_DESCRIBE_CACHE_HOMEBREW_VERSION}" != *"-dirty" ]] From 58ea96583fb7547d4dc87ca8faf0a1450a4cedcc Mon Sep 17 00:00:00 2001 From: Carlo Cabrera <30379873+carlocab@users.noreply.github.com> Date: Sun, 25 Aug 2024 03:54:40 +0800 Subject: [PATCH 2/4] brew.sh: use `read` instead of `cat` This will be slightly more efficient since it can be done in-process. --- Library/Homebrew/brew.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Library/Homebrew/brew.sh b/Library/Homebrew/brew.sh index 242e7eacea..9c0be7cbdc 100644 --- a/Library/Homebrew/brew.sh +++ b/Library/Homebrew/brew.sh @@ -387,7 +387,7 @@ fi # Docker image deprecation if [[ -f "${HOMEBREW_REPOSITORY}/.docker-deprecate" ]] then - DOCKER_DEPRECATION_MESSAGE="$(cat "${HOMEBREW_REPOSITORY}/.docker-deprecate")" + read -r DOCKER_DEPRECATION_MESSAGE <"${HOMEBREW_REPOSITORY}/.docker-deprecate" if [[ -n "${GITHUB_ACTIONS}" ]] then echo "::warning::${DOCKER_DEPRECATION_MESSAGE}" >&2 @@ -443,13 +443,13 @@ GIT_REVISION=$("${HOMEBREW_GIT}" -C "${HOMEBREW_REPOSITORY}" rev-parse HEAD 2>/d # safe fallback in case git rev-parse fails e.g. if this is not considered a safe git directory if [[ -z "${GIT_REVISION}" ]] then - GIT_HEAD="$(cat "${HOMEBREW_REPOSITORY}/.git/HEAD" 2>/dev/null)" + read -r GIT_HEAD <"${HOMEBREW_REPOSITORY}/.git/HEAD" 2>/dev/null if [[ "${GIT_HEAD}" == "ref: refs/heads/master" ]] then - GIT_REVISION="$(cat "${HOMEBREW_REPOSITORY}/.git/refs/heads/master" 2>/dev/null)" + read -r GIT_REVISION <"${HOMEBREW_REPOSITORY}/.git/refs/heads/master" 2>/dev/null elif [[ "${GIT_HEAD}" == "ref: refs/heads/stable" ]] then - GIT_REVISION="$(cat "${HOMEBREW_REPOSITORY}/.git/refs/heads/stable" 2>/dev/null)" + read -r GIT_REVISION <"${HOMEBREW_REPOSITORY}/.git/refs/heads/stable" 2>/dev/null fi unset GIT_HEAD fi @@ -461,7 +461,7 @@ then if "${HOMEBREW_GIT}" -C "${HOMEBREW_REPOSITORY}" diff-index --quiet --exit-code HEAD -- 2>/dev/null && [[ -r "${GIT_DESCRIBE_CACHE_FILE}" ]] then - GIT_DESCRIBE_CACHE_HOMEBREW_VERSION="$(cat "${GIT_DESCRIBE_CACHE_FILE}")" + read -r GIT_DESCRIBE_CACHE_HOMEBREW_VERSION <"${GIT_DESCRIBE_CACHE_FILE}" if [[ -n "${GIT_DESCRIBE_CACHE_HOMEBREW_VERSION}" && "${GIT_DESCRIBE_CACHE_HOMEBREW_VERSION}" != *"-dirty" ]] then HOMEBREW_VERSION="${GIT_DESCRIBE_CACHE_HOMEBREW_VERSION}" From a597a1eecbe4b8996be048ef73d7c267bcb4a51a Mon Sep 17 00:00:00 2001 From: Carlo Cabrera <30379873+carlocab@users.noreply.github.com> Date: Wed, 28 Aug 2024 18:58:27 +0800 Subject: [PATCH 3/4] Reverse order of `if` conditional Checking for readability is a cheaper operation that `git diff-index`. --- Library/Homebrew/brew.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/brew.sh b/Library/Homebrew/brew.sh index 9c0be7cbdc..cace4206ee 100644 --- a/Library/Homebrew/brew.sh +++ b/Library/Homebrew/brew.sh @@ -458,8 +458,8 @@ if [[ -n "${GIT_REVISION}" ]] then GIT_DESCRIBE_CACHE_FILE="${GIT_DESCRIBE_CACHE}/${GIT_REVISION}" # Pass `--` to guard against `git` being confused by a file named `HEAD`. - if "${HOMEBREW_GIT}" -C "${HOMEBREW_REPOSITORY}" diff-index --quiet --exit-code HEAD -- 2>/dev/null && - [[ -r "${GIT_DESCRIBE_CACHE_FILE}" ]] + if [[ -r "${GIT_DESCRIBE_CACHE_FILE}" ]] && + "${HOMEBREW_GIT}" -C "${HOMEBREW_REPOSITORY}" diff-index --quiet HEAD -- 2>/dev/null then read -r GIT_DESCRIBE_CACHE_HOMEBREW_VERSION <"${GIT_DESCRIBE_CACHE_FILE}" if [[ -n "${GIT_DESCRIBE_CACHE_HOMEBREW_VERSION}" && "${GIT_DESCRIBE_CACHE_HOMEBREW_VERSION}" != *"-dirty" ]] From b63ecff52c0048825144a38dd60adb7858c16666 Mon Sep 17 00:00:00 2001 From: Carlo Cabrera <30379873+carlocab@users.noreply.github.com> Date: Wed, 28 Aug 2024 19:21:50 +0800 Subject: [PATCH 4/4] brew.sh: use `git diff` instead of `git diff-index` --- Library/Homebrew/brew.sh | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Library/Homebrew/brew.sh b/Library/Homebrew/brew.sh index cace4206ee..5ac659e1fc 100644 --- a/Library/Homebrew/brew.sh +++ b/Library/Homebrew/brew.sh @@ -457,9 +457,7 @@ fi if [[ -n "${GIT_REVISION}" ]] then GIT_DESCRIBE_CACHE_FILE="${GIT_DESCRIBE_CACHE}/${GIT_REVISION}" - # Pass `--` to guard against `git` being confused by a file named `HEAD`. - if [[ -r "${GIT_DESCRIBE_CACHE_FILE}" ]] && - "${HOMEBREW_GIT}" -C "${HOMEBREW_REPOSITORY}" diff-index --quiet HEAD -- 2>/dev/null + if [[ -r "${GIT_DESCRIBE_CACHE_FILE}" ]] && "${HOMEBREW_GIT}" -C "${HOMEBREW_REPOSITORY}" diff --quiet --no-ext-diff 2>/dev/null then read -r GIT_DESCRIBE_CACHE_HOMEBREW_VERSION <"${GIT_DESCRIBE_CACHE_FILE}" if [[ -n "${GIT_DESCRIBE_CACHE_HOMEBREW_VERSION}" && "${GIT_DESCRIBE_CACHE_HOMEBREW_VERSION}" != *"-dirty" ]]