livecheck: improve host checks to instead check domains

This commit is contained in:
Bo Anderson 2022-05-20 01:43:00 +01:00
parent 54b45c2c8b
commit 38ae98cbcd
No known key found for this signature in database
GPG Key ID: 3DB94E204E137D65

View File

@ -492,34 +492,35 @@ module Homebrew
sig { params(url: String).returns(String) }
def preprocess_url(url)
begin
uri = URI.parse url
rescue URI::InvalidURIError
uri = Addressable::URI.parse url
rescue Addressable::URI::InvalidURIError
return url
end
host = uri.host
domain = uri.domain
path = uri.path
return url if host.nil? || path.nil?
host = "github.com" if host == "github.s3.amazonaws.com"
domain = host = "github.com" if host == "github.s3.amazonaws.com"
path = path.delete_prefix("/").delete_suffix(".git")
scheme = uri.scheme
if host.end_with?("github.com")
if domain == "github.com"
return url if path.match? %r{/releases/latest/?$}
owner, repo = path.delete_prefix("downloads/").split("/")
url = "#{scheme}://#{host}/#{owner}/#{repo}.git"
elsif host.end_with?(*GITEA_INSTANCES)
elsif GITEA_INSTANCES.include?(domain)
return url if path.match? %r{/releases/latest/?$}
owner, repo = path.split("/")
url = "#{scheme}://#{host}/#{owner}/#{repo}.git"
elsif host.end_with?(*GOGS_INSTANCES)
elsif GOGS_INSTANCES.include?(domain)
owner, repo = path.split("/")
url = "#{scheme}://#{host}/#{owner}/#{repo}.git"
# sourcehut
elsif host.end_with?("git.sr.ht")
elsif host == "git.sr.ht"
owner, repo = path.split("/")
url = "#{scheme}://#{host}/#{owner}/#{repo}"
# GitLab (gitlab.com or self-hosted)