From 429f23dcc605776a2e2660f3516b8d465e6dc0d7 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Sat, 15 Apr 2023 15:06:58 -0700 Subject: [PATCH] Create GitRepoPath --- Library/Homebrew/dev-cmd/extract.rb | 2 +- Library/Homebrew/diagnostic.rb | 1 + .../{git_repository.rb => git_repo_path.rb} | 12 +++++++----- Library/Homebrew/extend/git_repo_path.rbi | 17 +++++++++++++++++ Library/Homebrew/global.rb | 2 +- Library/Homebrew/system_config.rb | 13 +++++++------ Library/Homebrew/tap.rb | 18 ++++++++++-------- Library/Homebrew/test/dev-cmd/pr-pull_spec.rb | 2 +- Library/Homebrew/utils/git.rb | 2 +- Library/Homebrew/utils/git_repository.rb | 8 ++++---- 10 files changed, 50 insertions(+), 27 deletions(-) rename Library/Homebrew/extend/{git_repository.rb => git_repo_path.rb} (93%) create mode 100644 Library/Homebrew/extend/git_repo_path.rbi diff --git a/Library/Homebrew/dev-cmd/extract.rb b/Library/Homebrew/dev-cmd/extract.rb index ac002199cc..96542a3cdb 100644 --- a/Library/Homebrew/dev-cmd/extract.rb +++ b/Library/Homebrew/dev-cmd/extract.rb @@ -113,7 +113,7 @@ module Homebrew end destination_tap.install unless destination_tap.installed? - repo = source_tap.path + repo = source_tap.git_repo.pathname pattern = if source_tap.core_tap? [repo/"Formula/#{name}.rb"] else diff --git a/Library/Homebrew/diagnostic.rb b/Library/Homebrew/diagnostic.rb index 9b88e6ef7b..79f28088a3 100644 --- a/Library/Homebrew/diagnostic.rb +++ b/Library/Homebrew/diagnostic.rb @@ -126,6 +126,7 @@ module Homebrew EOS end + sig { params(repository_path: GitRepoPath, desired_origin: String).returns(T.nilable(String)) } def examine_git_origin(repository_path, desired_origin) return if !Utils::Git.available? || !repository_path.git? diff --git a/Library/Homebrew/extend/git_repository.rb b/Library/Homebrew/extend/git_repo_path.rb similarity index 93% rename from Library/Homebrew/extend/git_repository.rb rename to Library/Homebrew/extend/git_repo_path.rb index 6e21970e31..a02b108542 100644 --- a/Library/Homebrew/extend/git_repository.rb +++ b/Library/Homebrew/extend/git_repo_path.rb @@ -7,12 +7,14 @@ require "utils/popen" # Extensions to {Pathname} for querying Git repository information. # @see Utils::Git # @api private -module GitRepositoryExtension +class GitRepoPath < SimpleDelegator extend T::Sig + alias pathname __getobj__ + sig { returns(T::Boolean) } def git? - join(".git").exist? + pathname.join(".git").exist? end # Gets the URL of the Git origin remote. @@ -26,7 +28,7 @@ module GitRepositoryExtension def git_origin=(origin) return if !git? || !Utils::Git.available? - safe_system Utils::Git.git, "remote", "set-url", "origin", origin, chdir: self + safe_system Utils::Git.git, "remote", "set-url", "origin", origin, chdir: pathname end # Gets the full commit hash of the HEAD commit. @@ -108,7 +110,7 @@ module GitRepositoryExtension unless git? return unless safe - raise "Not a Git repository: #{self}" + raise "Not a Git repository: #{pathname}" end unless Utils::Git.available? @@ -117,6 +119,6 @@ module GitRepositoryExtension raise "Git is unavailable" end - Utils.popen_read(Utils::Git.git, *args, safe: safe, chdir: self, err: err).chomp.presence + Utils.popen_read(Utils::Git.git, *args, safe: safe, chdir: pathname, err: err).chomp.presence end end diff --git a/Library/Homebrew/extend/git_repo_path.rbi b/Library/Homebrew/extend/git_repo_path.rbi new file mode 100644 index 0000000000..b1fb6031dd --- /dev/null +++ b/Library/Homebrew/extend/git_repo_path.rbi @@ -0,0 +1,17 @@ +# typed: strict + +class GitRepoPath < SimpleDelegator + include Kernel + + # This is a workaround to enable `alias pathname __getobj__` + # @see https://github.com/sorbet/sorbet/issues/2378#issuecomment-569474238 + sig { returns(Pathname) } + def __getobj__; end + + def /(_arg0); end + def parent; end + def abv; end + def rmtree; end + def cd; end + def directory?; end +end diff --git a/Library/Homebrew/global.rb b/Library/Homebrew/global.rb index 3c3cecdb2d..7e451d952a 100644 --- a/Library/Homebrew/global.rb +++ b/Library/Homebrew/global.rb @@ -131,7 +131,7 @@ end require "context" require "extend/array" -require "extend/git_repository" +require "extend/git_repo_path" require "extend/pathname" require "extend/predicable" require "extend/module" diff --git a/Library/Homebrew/system_config.rb b/Library/Homebrew/system_config.rb index 349b1e1288..7b4c1f4018 100644 --- a/Library/Homebrew/system_config.rb +++ b/Library/Homebrew/system_config.rb @@ -1,4 +1,4 @@ -# typed: false +# typed: true # frozen_string_literal: true require "hardware" @@ -32,9 +32,9 @@ module SystemConfig end end - sig { returns(Pathname) } + sig { returns(GitRepoPath) } def homebrew_repo - HOMEBREW_REPOSITORY.dup.extend(GitRepositoryExtension) + GitRepoPath.new(HOMEBREW_REPOSITORY) end sig { returns(String) } @@ -69,7 +69,7 @@ module SystemConfig sig { returns(String) } def core_tap_origin - CoreTap.instance.remote || "(none)" + CoreTap.instance.remote end sig { returns(String) } @@ -132,8 +132,9 @@ module SystemConfig def describe_curl out, = system_command(curl_executable, args: ["--version"], verbose: false) - if /^curl (?[\d.]+)/ =~ out - "#{curl_version} => #{curl_path}" + match_data = /^curl (?[\d.]+)/.match(out) + if match_data + "#{match_data[:curl_version]} => #{curl_path}" else "N/A" end diff --git a/Library/Homebrew/tap.rb b/Library/Homebrew/tap.rb index f263af6053..45aa7a618f 100644 --- a/Library/Homebrew/tap.rb +++ b/Library/Homebrew/tap.rb @@ -93,16 +93,18 @@ class Tap # The local path to this {Tap}. # e.g. `/usr/local/Library/Taps/user/homebrew-repo` + sig { returns(GitRepoPath) } attr_reader :path + alias git_repo path + # @private def initialize(user, repo) @user = user @repo = repo @name = "#{@user}/#{@repo}".downcase @full_name = "#{@user}/homebrew-#{@repo}" - @path = TAP_DIRECTORY/@full_name.downcase - @path.extend(GitRepositoryExtension) + @path = GitRepoPath.new(TAP_DIRECTORY/@full_name.downcase) @alias_table = nil @alias_reverse_table = nil end @@ -315,7 +317,7 @@ class Tap ignore_interrupts do # wait for git to possibly cleanup the top directory when interrupt happens. sleep 0.1 - FileUtils.rm_rf path + FileUtils.rm_rf git_repo.pathname path.parent.rmdir_if_possible end raise @@ -386,7 +388,7 @@ class Tap $stderr.ohai "#{name}: changed remote from #{remote} to #{requested_remote}" unless quiet end - current_upstream_head = path.git_origin_branch + current_upstream_head = T.must(path.git_origin_branch) return if requested_remote.blank? && path.git_origin_has_branch?(current_upstream_head) args = %w[fetch] @@ -395,7 +397,7 @@ class Tap safe_system "git", "-C", path, *args path.git_origin_set_head_auto - new_upstream_head = path.git_origin_branch + new_upstream_head = T.must(path.git_origin_branch) return if new_upstream_head == current_upstream_head path.git_rename_branch old: current_upstream_head, new: new_upstream_head @@ -462,7 +464,7 @@ class Tap sig { returns(T::Array[Pathname]) } def potential_formula_dirs - @potential_formula_dirs ||= [path/"Formula", path/"HomebrewFormula", path].freeze + @potential_formula_dirs ||= [path/"Formula", path/"HomebrewFormula", git_repo.pathname].freeze end # Path to the directory of all {Cask} files for this {Tap}. @@ -567,7 +569,7 @@ class Tap sig { params(file: T.any(String, Pathname)).returns(T::Boolean) } def formula_file?(file) file = Pathname.new(file) unless file.is_a? Pathname - file = file.expand_path(path) + file = file.expand_path(git_repo.pathname) return false unless ruby_file?(file) return false if cask_file?(file) @@ -580,7 +582,7 @@ class Tap sig { params(file: T.any(String, Pathname)).returns(T::Boolean) } def cask_file?(file) file = Pathname.new(file) unless file.is_a? Pathname - file = file.expand_path(path) + file = file.expand_path(git_repo.pathname) return false unless ruby_file?(file) file.to_s.start_with?("#{cask_dir}/") diff --git a/Library/Homebrew/test/dev-cmd/pr-pull_spec.rb b/Library/Homebrew/test/dev-cmd/pr-pull_spec.rb index 9366befe34..058fa99b22 100644 --- a/Library/Homebrew/test/dev-cmd/pr-pull_spec.rb +++ b/Library/Homebrew/test/dev-cmd/pr-pull_spec.rb @@ -81,7 +81,7 @@ describe "brew pr-pull" do let(:tap) { Tap.fetch("Homebrew", "foo") } let(:formula_file) { tap.path/"Formula/foo.rb" } let(:cask_file) { tap.cask_dir/"food.rb" } - let(:path) { (Tap::TAP_DIRECTORY/"homebrew/homebrew-foo").extend(GitRepositoryExtension) } + let(:path) { GitRepoPath.new(Tap::TAP_DIRECTORY/"homebrew/homebrew-foo") } describe "#autosquash!" do it "squashes a formula or cask correctly" do diff --git a/Library/Homebrew/utils/git.rb b/Library/Homebrew/utils/git.rb index 54007e24b8..8b88654a12 100644 --- a/Library/Homebrew/utils/git.rb +++ b/Library/Homebrew/utils/git.rb @@ -4,7 +4,7 @@ module Utils # Helper functions for querying Git information. # - # @see GitRepositoryExtension + # @see GitRepoPath # @api private module Git extend T::Sig diff --git a/Library/Homebrew/utils/git_repository.rb b/Library/Homebrew/utils/git_repository.rb index 6c0e983a89..5ab0275ff1 100644 --- a/Library/Homebrew/utils/git_repository.rb +++ b/Library/Homebrew/utils/git_repository.rb @@ -15,7 +15,7 @@ module Utils def self.git_head(repo = Pathname.pwd, length: nil, safe: true) return git_short_head(repo, length: length) if length.present? - repo = Pathname(repo).extend(GitRepositoryExtension) + repo = GitRepoPath.new(Pathname(repo)) repo.git_head(safe: safe) end @@ -28,7 +28,7 @@ module Utils ).returns(T.nilable(String)) } def self.git_short_head(repo = Pathname.pwd, length: nil, safe: true) - repo = Pathname(repo).extend(GitRepositoryExtension) + repo = GitRepoPath.new(Pathname(repo)) repo.git_short_head(length: length, safe: safe) end @@ -40,7 +40,7 @@ module Utils ).returns(T.nilable(String)) } def self.git_branch(repo = Pathname.pwd, safe: true) - repo = Pathname(repo).extend(GitRepositoryExtension) + repo = GitRepoPath.new(Pathname(repo)) repo.git_branch(safe: safe) end @@ -53,7 +53,7 @@ module Utils ).returns(T.nilable(String)) } def self.git_commit_message(repo = Pathname.pwd, commit: "HEAD", safe: true) - repo = Pathname(repo).extend(GitRepositoryExtension) + repo = GitRepoPath.new(Pathname(repo)) repo.git_commit_message(commit, safe: safe) end end