From 3f3a0b5469e174ced73ba31044779644aaa0f928 Mon Sep 17 00:00:00 2001 From: hyuraku <32809703+hyuraku@users.noreply.github.com> Date: Sun, 6 Nov 2022 18:12:43 +0900 Subject: [PATCH 1/7] move two methods from download_storategy to utils/github --- Library/Homebrew/download_strategy.rb | 35 +++------------------------ Library/Homebrew/utils/github.rb | 28 +++++++++++++++++++++ 2 files changed, 31 insertions(+), 32 deletions(-) diff --git a/Library/Homebrew/download_strategy.rb b/Library/Homebrew/download_strategy.rb index 3ce7303d47..c10858a353 100644 --- a/Library/Homebrew/download_strategy.rb +++ b/Library/Homebrew/download_strategy.rb @@ -12,6 +12,7 @@ require "mechanize/version" require "mechanize/http/content_disposition_parser" require "utils/curl" +require "utils/github" require "github_packages" @@ -1069,43 +1070,13 @@ class GitHubGitDownloadStrategy < GitDownloadStrategy @repo = repo end - def github_last_commit - # TODO: move to Utils::GitHub - return if Homebrew::EnvConfig.no_github_api? - - output, _, status = curl_output( - "--silent", "--head", "--location", - "-H", "Accept: application/vnd.github.sha", - "https://api.github.com/repos/#{@user}/#{@repo}/commits/#{@ref}" - ) - - return unless status.success? - - commit = output[/^ETag: "(\h+)"/, 1] - version.update_commit(commit) if commit - commit - end - - def multiple_short_commits_exist?(commit) - # TODO: move to Utils::GitHub - return if Homebrew::EnvConfig.no_github_api? - - output, _, status = curl_output( - "--silent", "--head", "--location", - "-H", "Accept: application/vnd.github.sha", - "https://api.github.com/repos/#{@user}/#{@repo}/commits/#{commit}" - ) - - !(status.success? && output && output[/^Status: (200)/, 1] == "200") - end - def commit_outdated?(commit) - @last_commit ||= github_last_commit + @last_commit ||= GitHub.last_commit(@user, @repo, @ref) if @last_commit return true unless commit return true unless @last_commit.start_with?(commit) - if multiple_short_commits_exist?(commit) + if GitHub.multiple_short_commits_exist?(@user, @repo, commit) true else version.update_commit(commit) diff --git a/Library/Homebrew/utils/github.rb b/Library/Homebrew/utils/github.rb index 0d7472784b..1769f1cb43 100644 --- a/Library/Homebrew/utils/github.rb +++ b/Library/Homebrew/utils/github.rb @@ -666,4 +666,32 @@ module GitHub pr_data = API.open_rest(url_to("repos", user, repo, "pulls", pr)) pr_data["labels"].map { |label| label["name"] } end + + def last_commit(user, repo, ref) + return if Homebrew::EnvConfig.no_github_api? + + output, _, status = curl_output( + "--silent", "--head", "--location", + "-H", "Accept: application/vnd.github.sha", + "https://api.github.com/repos/#{user}/#{repo}/commits/#{ref}" + ) + + return unless status.success? + + commit = output[/^ETag: "(\h+)"/, 1] + version.update_commit(commit) if commit + commit + end + + def multiple_short_commits_exist?(user, repo, commit) + return if Homebrew::EnvConfig.no_github_api? + + output, _, status = curl_output( + "--silent", "--head", "--location", + "-H", "Accept: application/vnd.github.sha", + "https://api.github.com/repos/#{user}/#{repo}/commits/#{commit}" + ) + + !(status.success? && output && output[/^Status: (200)/, 1] == "200") + end end From 80592f6013db20c254d5c4ba1bb6d72db4cc45ce Mon Sep 17 00:00:00 2001 From: hyuraku <32809703+hyuraku@users.noreply.github.com> Date: Sun, 6 Nov 2022 23:32:46 +0900 Subject: [PATCH 2/7] move require-utils/github location --- Library/Homebrew/download_strategy.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/download_strategy.rb b/Library/Homebrew/download_strategy.rb index c10858a353..7a0fb1d3ce 100644 --- a/Library/Homebrew/download_strategy.rb +++ b/Library/Homebrew/download_strategy.rb @@ -12,7 +12,6 @@ require "mechanize/version" require "mechanize/http/content_disposition_parser" require "utils/curl" -require "utils/github" require "github_packages" @@ -1071,6 +1070,7 @@ class GitHubGitDownloadStrategy < GitDownloadStrategy end def commit_outdated?(commit) + require "utils/github" @last_commit ||= GitHub.last_commit(@user, @repo, @ref) if @last_commit return true unless commit From 3e092e5e9024db01329fa59ee05b816e0b847748 Mon Sep 17 00:00:00 2001 From: hyuraku <32809703+hyuraku@users.noreply.github.com> Date: Mon, 7 Nov 2022 23:12:07 +0900 Subject: [PATCH 3/7] refactor option style --- Library/Homebrew/utils/github.rb | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Library/Homebrew/utils/github.rb b/Library/Homebrew/utils/github.rb index 1769f1cb43..1aacf3099a 100644 --- a/Library/Homebrew/utils/github.rb +++ b/Library/Homebrew/utils/github.rb @@ -672,14 +672,16 @@ module GitHub output, _, status = curl_output( "--silent", "--head", "--location", - "-H", "Accept: application/vnd.github.sha", + "--header", "Accept: application/vnd.github.sha", "https://api.github.com/repos/#{user}/#{repo}/commits/#{ref}" ) return unless status.success? commit = output[/^ETag: "(\h+)"/, 1] - version.update_commit(commit) if commit + return if commit.blank? + + version.update_commit(commit) commit end @@ -688,10 +690,14 @@ module GitHub output, _, status = curl_output( "--silent", "--head", "--location", - "-H", "Accept: application/vnd.github.sha", + "--header", "Accept: application/vnd.github.sha", "https://api.github.com/repos/#{user}/#{repo}/commits/#{commit}" ) - !(status.success? && output && output[/^Status: (200)/, 1] == "200") + return true if status.success? + return true unless output + return true if output[/^Status: (200)/, 1] != "200" + + false end end From 43eca95ce4a8d3ba7acb377fa0760cd62378a4f8 Mon Sep 17 00:00:00 2001 From: hyuraku <32809703+hyuraku@users.noreply.github.com> Date: Mon, 7 Nov 2022 23:12:27 +0900 Subject: [PATCH 4/7] reset require-utils/github location --- Library/Homebrew/download_strategy.rb | 2 +- Library/Homebrew/utils/github/api.rb | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/download_strategy.rb b/Library/Homebrew/download_strategy.rb index 7a0fb1d3ce..c10858a353 100644 --- a/Library/Homebrew/download_strategy.rb +++ b/Library/Homebrew/download_strategy.rb @@ -12,6 +12,7 @@ require "mechanize/version" require "mechanize/http/content_disposition_parser" require "utils/curl" +require "utils/github" require "github_packages" @@ -1070,7 +1071,6 @@ class GitHubGitDownloadStrategy < GitDownloadStrategy end def commit_outdated?(commit) - require "utils/github" @last_commit ||= GitHub.last_commit(@user, @repo, @ref) if @last_commit return true unless commit diff --git a/Library/Homebrew/utils/github/api.rb b/Library/Homebrew/utils/github/api.rb index b802a4af24..86d1cdc2c7 100644 --- a/Library/Homebrew/utils/github/api.rb +++ b/Library/Homebrew/utils/github/api.rb @@ -3,6 +3,7 @@ require "tempfile" require "utils/shell" +require "utils/formatter" module GitHub API_URL = "https://api.github.com" From 903cac3651a9e388f223a5d59a8ce5621cc4c86d Mon Sep 17 00:00:00 2001 From: hyuraku <32809703+hyuraku@users.noreply.github.com> Date: Mon, 7 Nov 2022 23:44:42 +0900 Subject: [PATCH 5/7] refactor GitHub.multiple_short_commits_exist? --- Library/Homebrew/utils/github.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Library/Homebrew/utils/github.rb b/Library/Homebrew/utils/github.rb index 1aacf3099a..78581ee724 100644 --- a/Library/Homebrew/utils/github.rb +++ b/Library/Homebrew/utils/github.rb @@ -696,8 +696,7 @@ module GitHub return true if status.success? return true unless output - return true if output[/^Status: (200)/, 1] != "200" - false + output[/^Status: (200)/, 1] != "200" end end From 5522cd106f57238c43e6a33a251f1b14f123cbb3 Mon Sep 17 00:00:00 2001 From: hyuraku <32809703+hyuraku@users.noreply.github.com> Date: Tue, 8 Nov 2022 18:54:18 +0900 Subject: [PATCH 6/7] use url_to instead of url string --- Library/Homebrew/utils/github.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/utils/github.rb b/Library/Homebrew/utils/github.rb index 78581ee724..712ffda7b3 100644 --- a/Library/Homebrew/utils/github.rb +++ b/Library/Homebrew/utils/github.rb @@ -673,7 +673,7 @@ module GitHub output, _, status = curl_output( "--silent", "--head", "--location", "--header", "Accept: application/vnd.github.sha", - "https://api.github.com/repos/#{user}/#{repo}/commits/#{ref}" + url_to("repos", user, repo, "commits", ref).to_s ) return unless status.success? @@ -691,7 +691,7 @@ module GitHub output, _, status = curl_output( "--silent", "--head", "--location", "--header", "Accept: application/vnd.github.sha", - "https://api.github.com/repos/#{user}/#{repo}/commits/#{commit}" + url_to("repos", user, repo, "commits", commit).to_s ) return true if status.success? From 6423c27e8b6824378a1cce4563a06a91f0108cd4 Mon Sep 17 00:00:00 2001 From: hyuraku <32809703+hyuraku@users.noreply.github.com> Date: Tue, 8 Nov 2022 21:02:11 +0900 Subject: [PATCH 7/7] change the condition on GitHub.multiple_short_commits_exist? --- Library/Homebrew/utils/github.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/utils/github.rb b/Library/Homebrew/utils/github.rb index 712ffda7b3..a72fa5ab68 100644 --- a/Library/Homebrew/utils/github.rb +++ b/Library/Homebrew/utils/github.rb @@ -694,8 +694,8 @@ module GitHub url_to("repos", user, repo, "commits", commit).to_s ) - return true if status.success? - return true unless output + return true unless status.success? + return true if output.blank? output[/^Status: (200)/, 1] != "200" end