From c9f6f14991bfe85ca6e62c055fef494474e25730 Mon Sep 17 00:00:00 2001 From: Sharon Azriel <150116802+sazriel26@users.noreply.github.com> Date: Mon, 4 Dec 2023 18:04:34 +0200 Subject: [PATCH 01/16] [update.sh] Parse local token in git remote.origin.url config --- Library/Homebrew/cmd/update.sh | 40 +++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/Library/Homebrew/cmd/update.sh b/Library/Homebrew/cmd/update.sh index 8730c8474ea53..894583da57832 100644 --- a/Library/Homebrew/cmd/update.sh +++ b/Library/Homebrew/cmd/update.sh @@ -513,12 +513,12 @@ EOS # HOMEBREW_GITHUB_API_TOKEN is optionally defined in the user environment. # shellcheck disable=SC2153 - if [[ -n "${HOMEBREW_GITHUB_API_TOKEN}" ]] - then - CURL_GITHUB_API_ARGS=("--header" "Authorization: token ${HOMEBREW_GITHUB_API_TOKEN}") - else - CURL_GITHUB_API_ARGS=() - fi + #if [[ -n "${HOMEBREW_GITHUB_API_TOKEN}" ]] + #then + # CURL_GITHUB_API_ARGS=("--header" "Authorization: token ${HOMEBREW_GITHUB_API_TOKEN}") + #else + # CURL_GITHUB_API_ARGS=() + #fi # only allow one instance of brew update lock update @@ -637,12 +637,36 @@ EOS # the refspec ensures that the default upstream branch gets updated ( UPSTREAM_REPOSITORY_URL="$(git config remote.origin.url)" + UPSTREAM_REPOSITORY_TOPARSE="${UPSTREAM_REPOSITORY_URL#*://}" + UPSTREAM_REPOSITORY_L1="${UPSTREAM_REPOSITORY_TOPARSE%%/*}" + UPSTREAM_REPOSITORY_DOMAIN="${UPSTREAM_REPOSITORY_L1#*@}" + [[ "${UPSTREAM_REPOSITORY_L1}" == "${UPSTREAM_REPOSITORY_DOMAIN}" ]] \ + && UPSTREAM_REPOSITORY_USERPASS="" \ + || UPSTREAM_REPOSITORY_USERPASS="${UPSTREAM_REPOSITORY_L1%@*}" + [[ -z "${UPSTREAM_REPOSITORY_USERPASS%:*}" ]] || [[ "${UPSTREAM_REPOSITORY_USERPASS%:*}" == "git" ]] \ + && UPSTREAM_REPOSITORY_TOKEN="" \ + || UPSTREAM_REPOSITORY_TOKEN="${UPSTREAM_REPOSITORY_USERPASS#*:}" + + # HOMEBREW_GITHUB_API_TOKEN is optionally defined in the user environment. + # UPSTREAM_REPOSITORY_TOKEN is optionally defined in the git configuration and will try to supersede + # shellcheck disable=SC2153 + if [[ -n "${UPSTREAM_REPOSITORY_TOKEN}" ]] + then + CURL_GITHUB_API_ARGS=("--header" "Authorization: token ${UPSTREAM_REPOSITORY_TOKEN}") + elif [[ -n "${HOMEBREW_GITHUB_API_TOKEN}" ]] + then + CURL_GITHUB_API_ARGS=("--header" "Authorization: token ${HOMEBREW_GITHUB_API_TOKEN}") + else + CURL_GITHUB_API_ARGS=() + fi # HOMEBREW_UPDATE_FORCE and HOMEBREW_UPDATE_AUTO aren't modified here so ignore subshell warning. # shellcheck disable=SC2030 - if [[ "${UPSTREAM_REPOSITORY_URL}" == "https://github.com/"* ]] + if [[ "${UPSTREAM_REPOSITORY_URL}" == "https://github.com/"* ]] \ + || [[ "${UPSTREAM_REPOSITORY_DOMAIN%:*}" == "github.com" ]] then - UPSTREAM_REPOSITORY="${UPSTREAM_REPOSITORY_URL#https://github.com/}" + #UPSTREAM_REPOSITORY="${UPSTREAM_REPOSITORY_URL#https://github.com/}" + UPSTREAM_REPOSITORY="${UPSTREAM_REPOSITORY_TOPARSE#*/}" UPSTREAM_REPOSITORY="${UPSTREAM_REPOSITORY%.git}" if [[ "${DIR}" == "${HOMEBREW_REPOSITORY}" && -n "${HOMEBREW_UPDATE_TO_TAG}" ]] From 9102492e2c669854499dbf9189a4c6c35b17fa2c Mon Sep 17 00:00:00 2001 From: Sharon Azriel <150116802+sazriel26@users.noreply.github.com> Date: Wed, 6 Dec 2023 14:20:54 +0200 Subject: [PATCH 02/16] Release Candidate 1 --- Library/Homebrew/cmd/update.sh | 32 +++++--------------- Library/Homebrew/utils/url.sh | 53 ++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 25 deletions(-) create mode 100755 Library/Homebrew/utils/url.sh diff --git a/Library/Homebrew/cmd/update.sh b/Library/Homebrew/cmd/update.sh index 894583da57832..7b14d5a80dfbb 100644 --- a/Library/Homebrew/cmd/update.sh +++ b/Library/Homebrew/cmd/update.sh @@ -18,6 +18,7 @@ # HOMEBREW_USER_AGENT_CURL are set by brew.sh # shellcheck disable=SC2154 source "${HOMEBREW_LIBRARY}/Homebrew/utils/lock.sh" +source "${HOMEBREW_LIBRARY}/Homebrew/utils/url.sh" # Replaces the function in Library/Homebrew/brew.sh to cache the Curl/Git executable to # provide speedup when using Curl/Git repeatedly (as update.sh does). @@ -511,15 +512,6 @@ EOS CURL_DISABLE_CURLRC_ARGS=() fi - # HOMEBREW_GITHUB_API_TOKEN is optionally defined in the user environment. - # shellcheck disable=SC2153 - #if [[ -n "${HOMEBREW_GITHUB_API_TOKEN}" ]] - #then - # CURL_GITHUB_API_ARGS=("--header" "Authorization: token ${HOMEBREW_GITHUB_API_TOKEN}") - #else - # CURL_GITHUB_API_ARGS=() - #fi - # only allow one instance of brew update lock update @@ -637,22 +629,14 @@ EOS # the refspec ensures that the default upstream branch gets updated ( UPSTREAM_REPOSITORY_URL="$(git config remote.origin.url)" - UPSTREAM_REPOSITORY_TOPARSE="${UPSTREAM_REPOSITORY_URL#*://}" - UPSTREAM_REPOSITORY_L1="${UPSTREAM_REPOSITORY_TOPARSE%%/*}" - UPSTREAM_REPOSITORY_DOMAIN="${UPSTREAM_REPOSITORY_L1#*@}" - [[ "${UPSTREAM_REPOSITORY_L1}" == "${UPSTREAM_REPOSITORY_DOMAIN}" ]] \ - && UPSTREAM_REPOSITORY_USERPASS="" \ - || UPSTREAM_REPOSITORY_USERPASS="${UPSTREAM_REPOSITORY_L1%@*}" - [[ -z "${UPSTREAM_REPOSITORY_USERPASS%:*}" ]] || [[ "${UPSTREAM_REPOSITORY_USERPASS%:*}" == "git" ]] \ - && UPSTREAM_REPOSITORY_TOKEN="" \ - || UPSTREAM_REPOSITORY_TOKEN="${UPSTREAM_REPOSITORY_USERPASS#*:}" + UPSTREAM_REPOSITORY_URL_PARSED=( $(url_get "${UPSTREAM_REPOSITORY_URL}" user pass host path) ) # HOMEBREW_GITHUB_API_TOKEN is optionally defined in the user environment. - # UPSTREAM_REPOSITORY_TOKEN is optionally defined in the git configuration and will try to supersede + # can be supersede by local repository URL # shellcheck disable=SC2153 - if [[ -n "${UPSTREAM_REPOSITORY_TOKEN}" ]] + if [[ -n "${UPSTREAM_REPOSITORY_URL_PARSED[1]}" ]] then - CURL_GITHUB_API_ARGS=("--header" "Authorization: token ${UPSTREAM_REPOSITORY_TOKEN}") + CURL_GITHUB_API_ARGS=("--header" "Authorization: token ${UPSTREAM_REPOSITORY_URL_PARSED[1]}") elif [[ -n "${HOMEBREW_GITHUB_API_TOKEN}" ]] then CURL_GITHUB_API_ARGS=("--header" "Authorization: token ${HOMEBREW_GITHUB_API_TOKEN}") @@ -663,11 +647,9 @@ EOS # HOMEBREW_UPDATE_FORCE and HOMEBREW_UPDATE_AUTO aren't modified here so ignore subshell warning. # shellcheck disable=SC2030 if [[ "${UPSTREAM_REPOSITORY_URL}" == "https://github.com/"* ]] \ - || [[ "${UPSTREAM_REPOSITORY_DOMAIN%:*}" == "github.com" ]] + || [[ "${UPSTREAM_REPOSITORY_URL_PARSED[2]}" == "github.com" ]] then - #UPSTREAM_REPOSITORY="${UPSTREAM_REPOSITORY_URL#https://github.com/}" - UPSTREAM_REPOSITORY="${UPSTREAM_REPOSITORY_TOPARSE#*/}" - UPSTREAM_REPOSITORY="${UPSTREAM_REPOSITORY%.git}" + UPSTREAM_REPOSITORY="${UPSTREAM_REPOSITORY_URL_PARSED[3]%.git}" if [[ "${DIR}" == "${HOMEBREW_REPOSITORY}" && -n "${HOMEBREW_UPDATE_TO_TAG}" ]] then diff --git a/Library/Homebrew/utils/url.sh b/Library/Homebrew/utils/url.sh new file mode 100755 index 0000000000000..b794e4ae1404d --- /dev/null +++ b/Library/Homebrew/utils/url.sh @@ -0,0 +1,53 @@ +# url_get [key] [key] ... +# where: +# URL ~ [scheme://][user[:pass]@]host[:port][path] +# key is one of the element composing the URL +url_get() { + [[ ${#} -gt 0 ]] || return + + local url="${1}" + [[ -z "${url}" ]] && return + shift + + local _uphpp="${url#*://}" + local _scheme="${url%%://*}" + if [[ "${url}" == "${_uphpp}" ]]; then + _scheme="" + fi + + local _hpp="${_uphpp#*@}" + local _up="${_uphpp%%@*}" + if [[ "${_uphpp}" == "${_hpp}" ]]; then + local _user="" + local _pass="" + else + local _user="${_up%:*}" + local _pass="${_up#*:}" + if [[ "${_user}" == "${_up}" ]]; then + _pass="" + fi + fi + + local _hp="${_hpp%%/*}" + # TODO: split path from arguments (path?arg=...&....) + local _path="${_hpp#*/}" + if [[ "${_hp}" == "${_hpp}" ]]; then + _path="" + elif [[ -z "${_path}" ]]; then + _path="/" + fi + + local _host="${_hp%:*}" + local _port="${_hp#*:}" + if [[ "${_hp}" == "${_host}" ]]; then + _port="" + fi + + while [[ ${#} -gt 0 ]]; do + echo -n "$(eval echo -n "\${_${1}}")" + [[ ${#} -gt 1 ]] && echo -n ${IFS} + shift + done +} + +# EoF From d1c2d5f0e2a027aba195b3a6fff60167552996c6 Mon Sep 17 00:00:00 2001 From: Sharon Azriel <150116802+sazriel26@users.noreply.github.com> Date: Wed, 6 Dec 2023 15:38:04 +0200 Subject: [PATCH 03/16] Release Candidate 2 --- Library/Homebrew/cmd/update.sh | 7 ++++++- Library/Homebrew/utils/url.sh | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Library/Homebrew/cmd/update.sh b/Library/Homebrew/cmd/update.sh index 7b14d5a80dfbb..c431862e90357 100644 --- a/Library/Homebrew/cmd/update.sh +++ b/Library/Homebrew/cmd/update.sh @@ -629,7 +629,12 @@ EOS # the refspec ensures that the default upstream branch gets updated ( UPSTREAM_REPOSITORY_URL="$(git config remote.origin.url)" - UPSTREAM_REPOSITORY_URL_PARSED=( $(url_get "${UPSTREAM_REPOSITORY_URL}" user pass host path) ) + eval UPSTREAM_REPOSITORY_URL_PARSED=( $(url_get "${UPSTREAM_REPOSITORY_URL}" user pass host path) ) + if [[ ${#UPSTREAM_REPOSITORY_URL_PARSED[@]} -ne 4 ]]; then + echo "Failed to parse repository URL=${UPSTREAM_REPOSITORY_URL} \!" >&2 + exit + fi + # HOMEBREW_GITHUB_API_TOKEN is optionally defined in the user environment. # can be supersede by local repository URL diff --git a/Library/Homebrew/utils/url.sh b/Library/Homebrew/utils/url.sh index b794e4ae1404d..a87744911088e 100755 --- a/Library/Homebrew/utils/url.sh +++ b/Library/Homebrew/utils/url.sh @@ -44,8 +44,8 @@ url_get() { fi while [[ ${#} -gt 0 ]]; do - echo -n "$(eval echo -n "\${_${1}}")" - [[ ${#} -gt 1 ]] && echo -n ${IFS} + echo -n \""$(eval echo -n "\${_${1}}")"\" + [[ ${#} -gt 1 ]] && echo -n "${IFS:- }" shift done } From e7c1575522e66218c17d5449e292fcbc93854292 Mon Sep 17 00:00:00 2001 From: sazriel26 <150116802+sazriel26@users.noreply.github.com> Date: Thu, 7 Dec 2023 09:39:56 +0200 Subject: [PATCH 04/16] Update Library/Homebrew/cmd/update.sh better wording Co-authored-by: Mike McQuaid --- Library/Homebrew/cmd/update.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/cmd/update.sh b/Library/Homebrew/cmd/update.sh index c431862e90357..dafb128dc5657 100644 --- a/Library/Homebrew/cmd/update.sh +++ b/Library/Homebrew/cmd/update.sh @@ -637,7 +637,7 @@ EOS # HOMEBREW_GITHUB_API_TOKEN is optionally defined in the user environment. - # can be supersede by local repository URL + # can be superseded a token from the local repository URL # shellcheck disable=SC2153 if [[ -n "${UPSTREAM_REPOSITORY_URL_PARSED[1]}" ]] then From 079e8a09be7bfa47f72062f7ca8ee37452f42bbd Mon Sep 17 00:00:00 2001 From: Sharon Azriel <150116802+sazriel26@users.noreply.github.com> Date: Sun, 7 Jan 2024 14:13:26 +0200 Subject: [PATCH 05/16] New implementation through BASH regexes match --- Library/Homebrew/cmd/update.sh | 38 ++++++++++-------------- Library/Homebrew/utils/url.sh | 53 ---------------------------------- 2 files changed, 15 insertions(+), 76 deletions(-) delete mode 100755 Library/Homebrew/utils/url.sh diff --git a/Library/Homebrew/cmd/update.sh b/Library/Homebrew/cmd/update.sh index 7630507e819b0..a83206aa66969 100644 --- a/Library/Homebrew/cmd/update.sh +++ b/Library/Homebrew/cmd/update.sh @@ -18,7 +18,6 @@ # HOMEBREW_USER_AGENT_CURL are set by brew.sh # shellcheck disable=SC2154 source "${HOMEBREW_LIBRARY}/Homebrew/utils/lock.sh" -source "${HOMEBREW_LIBRARY}/Homebrew/utils/url.sh" # Replaces the function in Library/Homebrew/brew.sh to cache the Curl/Git executable to # provide speedup when using Curl/Git repeatedly (as update.sh does). @@ -629,32 +628,25 @@ EOS # the refspec ensures that the default upstream branch gets updated ( UPSTREAM_REPOSITORY_URL="$(git config remote.origin.url)" - eval UPSTREAM_REPOSITORY_URL_PARSED=( $(url_get "${UPSTREAM_REPOSITORY_URL}" user pass host path) ) - if [[ ${#UPSTREAM_REPOSITORY_URL_PARSED[@]} -ne 4 ]]; then - echo "Failed to parse repository URL=${UPSTREAM_REPOSITORY_URL} \!" >&2 - exit - fi - - - # HOMEBREW_GITHUB_API_TOKEN is optionally defined in the user environment. - # can be superseded a token from the local repository URL - # shellcheck disable=SC2153 - if [[ -n "${UPSTREAM_REPOSITORY_URL_PARSED[1]}" ]] - then - CURL_GITHUB_API_ARGS=("--header" "Authorization: token ${UPSTREAM_REPOSITORY_URL_PARSED[1]}") - elif [[ -n "${HOMEBREW_GITHUB_API_TOKEN}" ]] - then - CURL_GITHUB_API_ARGS=("--header" "Authorization: token ${HOMEBREW_GITHUB_API_TOKEN}") - else - CURL_GITHUB_API_ARGS=() - fi # HOMEBREW_UPDATE_FORCE and HOMEBREW_UPDATE_AUTO aren't modified here so ignore subshell warning. # shellcheck disable=SC2030 - if [[ "${UPSTREAM_REPOSITORY_URL}" == "https://github.com/"* ]] \ - || [[ "${UPSTREAM_REPOSITORY_URL_PARSED[2]}" == "github.com" ]] + if [[ "${UPSTREAM_REPOSITORY_URL}" =~ https://(([^:@]+)(:([^@]+))?@)?github.com/(.*)$ ]] \ then - UPSTREAM_REPOSITORY="${UPSTREAM_REPOSITORY_URL_PARSED[3]%.git}" + UPSTREAM_REPOSITORY="${BASH_REMATCH[5]%.git}" + # HOMEBREW_GITHUB_API_TOKEN is optionally defined in the user environment. + # can be superseded a token from the local repository URL + # shellcheck disable=SC2153 + if [[ -n "${BASH_REMATCH[4]}" ]] + then + CURL_GITHUB_API_ARGS=("--header" "Authorization: token ${BASH_REMATCH[4]}") + elif [[ -n "${HOMEBREW_GITHUB_API_TOKEN}" ]] + then + CURL_GITHUB_API_ARGS=("--header" "Authorization: token ${HOMEBREW_GITHUB_API_TOKEN}") + else + CURL_GITHUB_API_ARGS=() + fi + if [[ "${DIR}" == "${HOMEBREW_REPOSITORY}" && -n "${HOMEBREW_UPDATE_TO_TAG}" ]] then diff --git a/Library/Homebrew/utils/url.sh b/Library/Homebrew/utils/url.sh deleted file mode 100755 index a87744911088e..0000000000000 --- a/Library/Homebrew/utils/url.sh +++ /dev/null @@ -1,53 +0,0 @@ -# url_get [key] [key] ... -# where: -# URL ~ [scheme://][user[:pass]@]host[:port][path] -# key is one of the element composing the URL -url_get() { - [[ ${#} -gt 0 ]] || return - - local url="${1}" - [[ -z "${url}" ]] && return - shift - - local _uphpp="${url#*://}" - local _scheme="${url%%://*}" - if [[ "${url}" == "${_uphpp}" ]]; then - _scheme="" - fi - - local _hpp="${_uphpp#*@}" - local _up="${_uphpp%%@*}" - if [[ "${_uphpp}" == "${_hpp}" ]]; then - local _user="" - local _pass="" - else - local _user="${_up%:*}" - local _pass="${_up#*:}" - if [[ "${_user}" == "${_up}" ]]; then - _pass="" - fi - fi - - local _hp="${_hpp%%/*}" - # TODO: split path from arguments (path?arg=...&....) - local _path="${_hpp#*/}" - if [[ "${_hp}" == "${_hpp}" ]]; then - _path="" - elif [[ -z "${_path}" ]]; then - _path="/" - fi - - local _host="${_hp%:*}" - local _port="${_hp#*:}" - if [[ "${_hp}" == "${_host}" ]]; then - _port="" - fi - - while [[ ${#} -gt 0 ]]; do - echo -n \""$(eval echo -n "\${_${1}}")"\" - [[ ${#} -gt 1 ]] && echo -n "${IFS:- }" - shift - done -} - -# EoF From 16fb0887d3517ff016dee07794880d62cdeffa06 Mon Sep 17 00:00:00 2001 From: Sharon Azriel <150116802+sazriel26@users.noreply.github.com> Date: Sun, 7 Jan 2024 14:14:48 +0200 Subject: [PATCH 06/16] Fix missing ; --- Library/Homebrew/cmd/update.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/cmd/update.sh b/Library/Homebrew/cmd/update.sh index a83206aa66969..fd32e8be59582 100644 --- a/Library/Homebrew/cmd/update.sh +++ b/Library/Homebrew/cmd/update.sh @@ -631,7 +631,7 @@ EOS # HOMEBREW_UPDATE_FORCE and HOMEBREW_UPDATE_AUTO aren't modified here so ignore subshell warning. # shellcheck disable=SC2030 - if [[ "${UPSTREAM_REPOSITORY_URL}" =~ https://(([^:@]+)(:([^@]+))?@)?github.com/(.*)$ ]] \ + if [[ "${UPSTREAM_REPOSITORY_URL}" =~ https://(([^:@]+)(:([^@]+))?@)?github.com/(.*)$ ]]; \ then UPSTREAM_REPOSITORY="${BASH_REMATCH[5]%.git}" # HOMEBREW_GITHUB_API_TOKEN is optionally defined in the user environment. From 2f3af888d4b1bba310595c9dd4f23a858a47d008 Mon Sep 17 00:00:00 2001 From: Sharon Azriel <150116802+sazriel26@users.noreply.github.com> Date: Sun, 7 Jan 2024 14:37:57 +0200 Subject: [PATCH 07/16] Fix brew style on update.sh --- Library/Homebrew/cmd/update.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/cmd/update.sh b/Library/Homebrew/cmd/update.sh index fd32e8be59582..ea4163acf77be 100644 --- a/Library/Homebrew/cmd/update.sh +++ b/Library/Homebrew/cmd/update.sh @@ -631,7 +631,7 @@ EOS # HOMEBREW_UPDATE_FORCE and HOMEBREW_UPDATE_AUTO aren't modified here so ignore subshell warning. # shellcheck disable=SC2030 - if [[ "${UPSTREAM_REPOSITORY_URL}" =~ https://(([^:@]+)(:([^@]+))?@)?github.com/(.*)$ ]]; \ + if [[ "${UPSTREAM_REPOSITORY_URL}" =~ https://(([^:@]+)(:([^@]+))?@)?github.com/(.*)$ ]] then UPSTREAM_REPOSITORY="${BASH_REMATCH[5]%.git}" # HOMEBREW_GITHUB_API_TOKEN is optionally defined in the user environment. From ed1c0fd140769f5dd2977d9c3037dfa9cb5c9e09 Mon Sep 17 00:00:00 2001 From: Sharon Azriel <150116802+sazriel26@users.noreply.github.com> Date: Sun, 7 Jan 2024 14:54:29 +0200 Subject: [PATCH 08/16] Fix brew style on update.sh (2) --- Library/Homebrew/cmd/update.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/Library/Homebrew/cmd/update.sh b/Library/Homebrew/cmd/update.sh index ea4163acf77be..3d777c9a59665 100644 --- a/Library/Homebrew/cmd/update.sh +++ b/Library/Homebrew/cmd/update.sh @@ -647,7 +647,6 @@ EOS CURL_GITHUB_API_ARGS=() fi - if [[ "${DIR}" == "${HOMEBREW_REPOSITORY}" && -n "${HOMEBREW_UPDATE_TO_TAG}" ]] then # Only try to `git fetch` when the upstream tags have changed From bced796ee9fa67e700522d607cfd3b914241f6a8 Mon Sep 17 00:00:00 2001 From: Sharon Azriel <150116802+sazriel26@users.noreply.github.com> Date: Wed, 10 Jan 2024 10:02:26 +0200 Subject: [PATCH 09/16] [Library/Homebrew/cmd/update.sh] Release Candidate for pull request --- Library/Homebrew/cmd/update.sh | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/Library/Homebrew/cmd/update.sh b/Library/Homebrew/cmd/update.sh index 3d777c9a59665..f60dcf813e567 100644 --- a/Library/Homebrew/cmd/update.sh +++ b/Library/Homebrew/cmd/update.sh @@ -631,18 +631,25 @@ EOS # HOMEBREW_UPDATE_FORCE and HOMEBREW_UPDATE_AUTO aren't modified here so ignore subshell warning. # shellcheck disable=SC2030 - if [[ "${UPSTREAM_REPOSITORY_URL}" =~ https://(([^:@]+)(:([^@]+))?@)?github.com/(.*)$ ]] + if [[ "${UPSTREAM_REPOSITORY_URL}" = "https://github.com/"* ]] || + [[ "${UPSTREAM_REPOSITORY_URL}" =~ https://(([^:@]+)(:([^@]+))?@)?github.com/(.*)$ ]] then - UPSTREAM_REPOSITORY="${BASH_REMATCH[5]%.git}" + if [[ -n "${BASH_REMATCH[5]}" ]] + then + UPSTREAM_REPOSITORY="${BASH_REMATCH[5]%.git}" + else + UPSTREAM_REPOSITORY="${UPSTREAM_REPOSITORY_URL#https://github.com/}" + UPSTREAM_REPOSITORY="${UPSTREAM_REPOSITORY%.git}" + fi # HOMEBREW_GITHUB_API_TOKEN is optionally defined in the user environment. - # can be superseded a token from the local repository URL + # Global token HOMEBREW_GITHUB_API_TOKEN supersedes local defined one # shellcheck disable=SC2153 - if [[ -n "${BASH_REMATCH[4]}" ]] - then - CURL_GITHUB_API_ARGS=("--header" "Authorization: token ${BASH_REMATCH[4]}") - elif [[ -n "${HOMEBREW_GITHUB_API_TOKEN}" ]] + if [[ -n "${HOMEBREW_GITHUB_API_TOKEN}" ]] then CURL_GITHUB_API_ARGS=("--header" "Authorization: token ${HOMEBREW_GITHUB_API_TOKEN}") + elif [[ -n "${BASH_REMATCH[4]}" ]] + then + CURL_GITHUB_API_ARGS=("--header" "Authorization: token ${BASH_REMATCH[4]}") else CURL_GITHUB_API_ARGS=() fi From 9c887df4de95a32cf1fc0eb2471dac669ecb0589 Mon Sep 17 00:00:00 2001 From: Sharon Azriel <150116802+sazriel26@users.noreply.github.com> Date: Thu, 11 Jan 2024 09:25:53 +0200 Subject: [PATCH 10/16] [Library/Homebrew/cmd/update.sh] Add support for token provided w/o user in URL --- Library/Homebrew/cmd/update.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Library/Homebrew/cmd/update.sh b/Library/Homebrew/cmd/update.sh index f60dcf813e567..23cd789d57ac7 100644 --- a/Library/Homebrew/cmd/update.sh +++ b/Library/Homebrew/cmd/update.sh @@ -650,6 +650,9 @@ EOS elif [[ -n "${BASH_REMATCH[4]}" ]] then CURL_GITHUB_API_ARGS=("--header" "Authorization: token ${BASH_REMATCH[4]}") + elif [[ -n "${BASH_REMATCH[2]}" ]] + then + CURL_GITHUB_API_ARGS=("--header" "Authorization: token ${BASH_REMATCH[2]}") else CURL_GITHUB_API_ARGS=() fi From 4a7018b83816c4c4b828b659c3fb6d06cf60442d Mon Sep 17 00:00:00 2001 From: Sharon Azriel <150116802+sazriel26@users.noreply.github.com> Date: Thu, 11 Jan 2024 12:03:45 +0200 Subject: [PATCH 11/16] Rewrite of branching tests in parsing URL --- Library/Homebrew/cmd/update.sh | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/Library/Homebrew/cmd/update.sh b/Library/Homebrew/cmd/update.sh index 23cd789d57ac7..b0d4236e1b01f 100644 --- a/Library/Homebrew/cmd/update.sh +++ b/Library/Homebrew/cmd/update.sh @@ -628,31 +628,32 @@ EOS # the refspec ensures that the default upstream branch gets updated ( UPSTREAM_REPOSITORY_URL="$(git config remote.origin.url)" + unset UPSTREAM_REPOSITORY + unset UPSTREAM_REPOSITORY_TOKEN # HOMEBREW_UPDATE_FORCE and HOMEBREW_UPDATE_AUTO aren't modified here so ignore subshell warning. # shellcheck disable=SC2030 - if [[ "${UPSTREAM_REPOSITORY_URL}" = "https://github.com/"* ]] || - [[ "${UPSTREAM_REPOSITORY_URL}" =~ https://(([^:@]+)(:([^@]+))?@)?github.com/(.*)$ ]] + + if [[ "${UPSTREAM_REPOSITORY_URL}" = "https://github.com/"* ]] then - if [[ -n "${BASH_REMATCH[5]}" ]] - then - UPSTREAM_REPOSITORY="${BASH_REMATCH[5]%.git}" - else UPSTREAM_REPOSITORY="${UPSTREAM_REPOSITORY_URL#https://github.com/}" UPSTREAM_REPOSITORY="${UPSTREAM_REPOSITORY%.git}" - fi + elif [[ "${UPSTREAM_REPOSITORY_URL}" =~ https://(([^:@]+)(:([^@]+))?@)?github.com/(.*)$ ]] + then + UPSTREAM_REPOSITORY="${BASH_REMATCH[5]%.git}" + UPSTREAM_REPOSITORY_TOKEN="${BASH_REMATCH[4]:-${BASH_REMATCH[2]}}" + fi + + if [[ -n "${UPSTREAM_REPOSITORY}" ]]; then # HOMEBREW_GITHUB_API_TOKEN is optionally defined in the user environment. # Global token HOMEBREW_GITHUB_API_TOKEN supersedes local defined one # shellcheck disable=SC2153 if [[ -n "${HOMEBREW_GITHUB_API_TOKEN}" ]] then CURL_GITHUB_API_ARGS=("--header" "Authorization: token ${HOMEBREW_GITHUB_API_TOKEN}") - elif [[ -n "${BASH_REMATCH[4]}" ]] - then - CURL_GITHUB_API_ARGS=("--header" "Authorization: token ${BASH_REMATCH[4]}") - elif [[ -n "${BASH_REMATCH[2]}" ]] + elif [[ -n "${UPSTREAM_REPOSITORY_TOKEN}" ]] then - CURL_GITHUB_API_ARGS=("--header" "Authorization: token ${BASH_REMATCH[2]}") + CURL_GITHUB_API_ARGS=("--header" "Authorization: token ${UPSTREAM_REPOSITORY_TOKEN}") else CURL_GITHUB_API_ARGS=() fi From f2e9b54162aa7abc8b338cb626be65a7c35d96da Mon Sep 17 00:00:00 2001 From: Sharon Azriel <150116802+sazriel26@users.noreply.github.com> Date: Thu, 11 Jan 2024 12:10:43 +0200 Subject: [PATCH 12/16] Brew style --fix (applied) --- Library/Homebrew/cmd/update.sh | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Library/Homebrew/cmd/update.sh b/Library/Homebrew/cmd/update.sh index b0d4236e1b01f..6e51988c476ff 100644 --- a/Library/Homebrew/cmd/update.sh +++ b/Library/Homebrew/cmd/update.sh @@ -636,15 +636,16 @@ EOS if [[ "${UPSTREAM_REPOSITORY_URL}" = "https://github.com/"* ]] then - UPSTREAM_REPOSITORY="${UPSTREAM_REPOSITORY_URL#https://github.com/}" - UPSTREAM_REPOSITORY="${UPSTREAM_REPOSITORY%.git}" + UPSTREAM_REPOSITORY="${UPSTREAM_REPOSITORY_URL#https://github.com/}" + UPSTREAM_REPOSITORY="${UPSTREAM_REPOSITORY%.git}" elif [[ "${UPSTREAM_REPOSITORY_URL}" =~ https://(([^:@]+)(:([^@]+))?@)?github.com/(.*)$ ]] then - UPSTREAM_REPOSITORY="${BASH_REMATCH[5]%.git}" - UPSTREAM_REPOSITORY_TOKEN="${BASH_REMATCH[4]:-${BASH_REMATCH[2]}}" + UPSTREAM_REPOSITORY="${BASH_REMATCH[5]%.git}" + UPSTREAM_REPOSITORY_TOKEN="${BASH_REMATCH[4]:-${BASH_REMATCH[2]}}" fi - if [[ -n "${UPSTREAM_REPOSITORY}" ]]; then + if [[ -n "${UPSTREAM_REPOSITORY}" ]] + then # HOMEBREW_GITHUB_API_TOKEN is optionally defined in the user environment. # Global token HOMEBREW_GITHUB_API_TOKEN supersedes local defined one # shellcheck disable=SC2153 From b21bfb1810063149bd60993020f2e6bb9e73eb1a Mon Sep 17 00:00:00 2001 From: sazriel26 <150116802+sazriel26@users.noreply.github.com> Date: Thu, 11 Jan 2024 12:25:23 +0200 Subject: [PATCH 13/16] Update Library/Homebrew/cmd/update.sh Remove one empty line? Co-authored-by: Mike McQuaid --- Library/Homebrew/cmd/update.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/Library/Homebrew/cmd/update.sh b/Library/Homebrew/cmd/update.sh index 6e51988c476ff..b3e22464633ca 100644 --- a/Library/Homebrew/cmd/update.sh +++ b/Library/Homebrew/cmd/update.sh @@ -633,7 +633,6 @@ EOS # HOMEBREW_UPDATE_FORCE and HOMEBREW_UPDATE_AUTO aren't modified here so ignore subshell warning. # shellcheck disable=SC2030 - if [[ "${UPSTREAM_REPOSITORY_URL}" = "https://github.com/"* ]] then UPSTREAM_REPOSITORY="${UPSTREAM_REPOSITORY_URL#https://github.com/}" From 226133c3ed287089680491fc89f2f0fc8854cd84 Mon Sep 17 00:00:00 2001 From: Sharon Azriel <150116802+sazriel26@users.noreply.github.com> Date: Thu, 11 Jan 2024 12:51:59 +0200 Subject: [PATCH 14/16] Ready for review? --- Library/Homebrew/cmd/update.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Library/Homebrew/cmd/update.sh b/Library/Homebrew/cmd/update.sh index b3e22464633ca..18c97239a6688 100644 --- a/Library/Homebrew/cmd/update.sh +++ b/Library/Homebrew/cmd/update.sh @@ -645,15 +645,15 @@ EOS if [[ -n "${UPSTREAM_REPOSITORY}" ]] then + # UPSTREAM_REPOSITORY_TOKEN is parsed (if exists) from UPSTREAM_REPOSITORY_URL # HOMEBREW_GITHUB_API_TOKEN is optionally defined in the user environment. - # Global token HOMEBREW_GITHUB_API_TOKEN supersedes local defined one # shellcheck disable=SC2153 - if [[ -n "${HOMEBREW_GITHUB_API_TOKEN}" ]] - then - CURL_GITHUB_API_ARGS=("--header" "Authorization: token ${HOMEBREW_GITHUB_API_TOKEN}") - elif [[ -n "${UPSTREAM_REPOSITORY_TOKEN}" ]] + if [[ -n "${UPSTREAM_REPOSITORY_TOKEN}" ]] then CURL_GITHUB_API_ARGS=("--header" "Authorization: token ${UPSTREAM_REPOSITORY_TOKEN}") + elif [[ -n "${HOMEBREW_GITHUB_API_TOKEN}" ]] + then + CURL_GITHUB_API_ARGS=("--header" "Authorization: token ${HOMEBREW_GITHUB_API_TOKEN}") else CURL_GITHUB_API_ARGS=() fi From 1d7191b76edfa3c1b0a1be9ef3c3eff9186e1ae1 Mon Sep 17 00:00:00 2001 From: sazriel26 <150116802+sazriel26@users.noreply.github.com> Date: Thu, 11 Jan 2024 14:17:29 +0200 Subject: [PATCH 15/16] Update Library/Homebrew/cmd/update.sh https://github.com/Homebrew/brew/pull/16289#discussion_r1448765696 Co-authored-by: Mike McQuaid --- Library/Homebrew/cmd/update.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/cmd/update.sh b/Library/Homebrew/cmd/update.sh index 18c97239a6688..22dc5c4b1c7fc 100644 --- a/Library/Homebrew/cmd/update.sh +++ b/Library/Homebrew/cmd/update.sh @@ -633,7 +633,7 @@ EOS # HOMEBREW_UPDATE_FORCE and HOMEBREW_UPDATE_AUTO aren't modified here so ignore subshell warning. # shellcheck disable=SC2030 - if [[ "${UPSTREAM_REPOSITORY_URL}" = "https://github.com/"* ]] + if [[ "${UPSTREAM_REPOSITORY_URL}" == "https://github.com/"* ]] then UPSTREAM_REPOSITORY="${UPSTREAM_REPOSITORY_URL#https://github.com/}" UPSTREAM_REPOSITORY="${UPSTREAM_REPOSITORY%.git}" From 5908569ec09e01db7cf8dd2b471a197f5ca8b177 Mon Sep 17 00:00:00 2001 From: Sharon Azriel <150116802+sazriel26@users.noreply.github.com> Date: Mon, 22 Jan 2024 09:47:47 +0200 Subject: [PATCH 16/16] [update.sh] New test with protection of Homebrew directory --- Library/Homebrew/cmd/update.sh | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Library/Homebrew/cmd/update.sh b/Library/Homebrew/cmd/update.sh index 18c97239a6688..732b0a99c1632 100644 --- a/Library/Homebrew/cmd/update.sh +++ b/Library/Homebrew/cmd/update.sh @@ -637,10 +637,11 @@ EOS then UPSTREAM_REPOSITORY="${UPSTREAM_REPOSITORY_URL#https://github.com/}" UPSTREAM_REPOSITORY="${UPSTREAM_REPOSITORY%.git}" - elif [[ "${UPSTREAM_REPOSITORY_URL}" =~ https://(([^:@]+)(:([^@]+))?@)?github.com/(.*)$ ]] + elif [[ "${DIR}" != "${HOMEBREW_REPOSITORY}" ]] && + [[ "${UPSTREAM_REPOSITORY_URL}" =~ https://([[:alnum:]_:]+)@github.com/(.*)$ ]] then - UPSTREAM_REPOSITORY="${BASH_REMATCH[5]%.git}" - UPSTREAM_REPOSITORY_TOKEN="${BASH_REMATCH[4]:-${BASH_REMATCH[2]}}" + UPSTREAM_REPOSITORY="${BASH_REMATCH[2]%.git}" + UPSTREAM_REPOSITORY_TOKEN="${BASH_REMATCH[1]#*:}" fi if [[ -n "${UPSTREAM_REPOSITORY}" ]]