From 48e60f228350c3fb65f70123197225ba9434cfac Mon Sep 17 00:00:00 2001 From: Taylor Bockman Date: Sun, 31 May 2026 13:17:57 -0700 Subject: [PATCH] Update downloader for zshrc --- zshrc | 76 ++++++++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 43 insertions(+), 33 deletions(-) diff --git a/zshrc b/zshrc index 6b3ad2b..020d54d 100644 --- a/zshrc +++ b/zshrc @@ -63,6 +63,7 @@ fi # # Then paste the commit and hashes below. + ZSH_CACHE_DIR="$HOME/.zsh" mkdir -p "$ZSH_CACHE_DIR" @@ -88,23 +89,43 @@ _zshv2_sha256() { fi } -_zshv2_download_verified() { - local url="$1" - local dest="$2" - local expected_sha="$3" +_zshv2_file_has_sha256() { + local file="$1" + local expected_sha="$2" + local actual_sha + + [[ -f "$file" ]] || return 1 + + actual_sha="$(_zshv2_sha256 "$file")" || return 1 + [[ "$actual_sha" == "$expected_sha" ]] +} + +_zshv2_install_verified_file() { + local name="$1" + local url="$2" + local dest="$3" + local expected_sha="$4" local tmp_file local actual_sha - tmp_file="$(mktemp "${dest}.tmp.XXXXXX")" || return 1 + if [[ -z "$url" || -z "$dest" || -z "$expected_sha" ]]; then + echo "Git completion install misconfigured for $name." >&2 + return 1 + fi + + if _zshv2_file_has_sha256 "$dest" "$expected_sha"; then + return 0 + fi if ! command -v curl >/dev/null 2>&1; then - echo "curl is required to install Git completion files." >&2 - rm -f "$tmp_file" + echo "curl is required to install $name." >&2 return 1 fi + tmp_file="$(mktemp "${dest}.tmp.XXXXXX")" || return 1 + if ! curl --fail --location --silent --show-error --output "$tmp_file" "$url"; then - echo "Failed to download $url" >&2 + echo "Failed to download $name from $url" >&2 rm -f "$tmp_file" return 1 fi @@ -115,7 +136,7 @@ _zshv2_download_verified() { } if [[ "$actual_sha" != "$expected_sha" ]]; then - echo "SHA-256 mismatch for $url" >&2 + echo "SHA-256 mismatch for $name." >&2 echo "Expected: $expected_sha" >&2 echo "Actual: $actual_sha" >&2 rm -f "$tmp_file" @@ -125,32 +146,21 @@ _zshv2_download_verified() { mv "$tmp_file" "$dest" } -_zshv2_file_verified() { - local file="$1" - local expected_sha="$2" - local actual_sha - - [[ -f "$file" ]] || return 1 - - actual_sha="$(_zshv2_sha256 "$file")" || return 1 - - [[ "$actual_sha" == "$expected_sha" ]] +_zshv2_install_git_completion() { + _zshv2_install_verified_file \ + "git-completion.bash" \ + "$GIT_COMPLETION_BASH_URL" \ + "$GIT_COMPLETION_BASH_FILE" \ + "$GIT_COMPLETION_BASH_SHA256" + + _zshv2_install_verified_file \ + "git-completion.zsh" \ + "$GIT_COMPLETION_ZSH_URL" \ + "$GIT_COMPLETION_ZSH_FILE" \ + "$GIT_COMPLETION_ZSH_SHA256" } -if [[ "$GIT_COMPLETION_COMMIT" != "PUT_PINNED_GIT_COMMIT_HASH_HERE" ]] && - [[ "$GIT_COMPLETION_BASH_SHA256" != "PUT_SHA256_FOR_GIT_COMPLETION_BASH_HERE" ]] && - [[ "$GIT_COMPLETION_ZSH_SHA256" != "PUT_SHA256_FOR_GIT_COMPLETION_ZSH_HERE" ]]; then - - if ! _zshv2_file_verified "$GIT_COMPLETION_BASH_FILE" "$GIT_COMPLETION_BASH_SHA256"; then - _zshv2_download_verified "$GIT_COMPLETION_BASH_URL" "$GIT_COMPLETION_BASH_FILE" "$GIT_COMPLETION_BASH_SHA256" - fi - - if ! _zshv2_file_verified "$GIT_COMPLETION_ZSH_FILE" "$GIT_COMPLETION_ZSH_SHA256"; then - _zshv2_download_verified "$GIT_COMPLETION_ZSH_URL" "$GIT_COMPLETION_ZSH_FILE" "$GIT_COMPLETION_ZSH_SHA256" - fi -else - echo "Git completion auto-install skipped: pinned commit and SHA-256 values are not configured." >&2 -fi +_zshv2_install_git_completion if [[ -f "$GIT_COMPLETION_BASH_FILE" ]]; then zstyle ':completion:*:*:git:*' script "$GIT_COMPLETION_BASH_FILE"