Create GitRepoPath

This commit is contained in:
Douglas Eichelberger 2023-04-15 15:06:58 -07:00
parent ad3d207172
commit 429f23dcc6
10 changed files with 50 additions and 27 deletions

View File

@ -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

View File

@ -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?

View File

@ -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

View File

@ -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

View File

@ -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"

View File

@ -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 (?<curl_version>[\d.]+)/ =~ out
"#{curl_version} => #{curl_path}"
match_data = /^curl (?<curl_version>[\d.]+)/.match(out)
if match_data
"#{match_data[:curl_version]} => #{curl_path}"
else
"N/A"
end

View File

@ -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}/")

View File

@ -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

View File

@ -4,7 +4,7 @@
module Utils
# Helper functions for querying Git information.
#
# @see GitRepositoryExtension
# @see GitRepoPath
# @api private
module Git
extend T::Sig

View File

@ -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