Remove delegation

This commit is contained in:
Douglas Eichelberger 2023-04-15 19:35:00 -07:00
parent b90897e280
commit 403f08db8b
6 changed files with 26 additions and 33 deletions

View File

@ -212,11 +212,11 @@ module Homebrew
trailers = [trailers + co_author_trailers].flatten.uniq.compact
# Apply the patch series but don't commit anything yet.
Utils::Git.cherry_pick!(git_repo, "--no-commit", *commits, verbose: verbose, resolve: resolve)
Utils::Git.cherry_pick!(git_repo.pathname, "--no-commit", *commits, verbose: verbose, resolve: resolve)
# Determine the bump subject by comparing the original state of the tree to its current state.
package_file = git_repo.pathname / file
old_package = Utils::Git.file_at_commit(git_repo, file, "#{commits.first}^")
old_package = Utils::Git.file_at_commit(git_repo.pathname, file, "#{commits.first}^")
new_package = package_file.read
bump_subject = determine_bump_subject(old_package, new_package, package_file, reason: reason)
@ -229,7 +229,8 @@ module Homebrew
# TODO: fix test in `test/dev-cmd/pr-pull_spec.rb` and assume `cherry_picked: false`.
def self.autosquash!(original_commit, tap:, reason: "", verbose: false, resolve: false, cherry_picked: true)
original_head = tap.git_repo.head_ref
git_repo = tap.git_repo
original_head = git_repo.head_ref
commits = Utils.safe_popen_read("git", "-C", tap.path, "rev-list",
"--reverse", "#{original_commit}..HEAD").lines.map(&:strip)
@ -269,15 +270,15 @@ module Homebrew
files = commits_to_files[commit]
if files.length == 1 && files_to_commits[files.first].length == 1
# If there's a 1:1 mapping of commits to files, just cherry pick and (maybe) reword.
reword_package_commit(commit, files.first, git_repo: tap.git_repo, reason: reason, verbose: verbose,
resolve: resolve)
reword_package_commit(
commit, files.first, git_repo: git_repo, reason: reason, verbose: verbose, resolve: resolve
)
processed_commits << commit
elsif files.length == 1 && files_to_commits[files.first].length > 1
# If multiple commits modify a single file, squash them down into a single commit.
file = files.first
commits = files_to_commits[file]
squash_package_commits(commits, file, git_repo: tap.git_repo, reason: reason, verbose: verbose,
resolve: resolve)
squash_package_commits(commits, file, git_repo: git_repo, reason: reason, verbose: verbose, resolve: resolve)
processed_commits += commits
else
# We can't split commits (yet) so just raise an error.
@ -483,7 +484,7 @@ module Homebrew
autosquash!(original_commit, tap: tap, cherry_picked: !args.no_cherry_pick?,
verbose: args.verbose?, resolve: args.resolve?, reason: args.message)
end
signoff!(tap.git_repo, pull_request: pr, dry_run: args.dry_run?) unless args.clean?
signoff!(git_repo, pull_request: pr, dry_run: args.dry_run?) unless args.clean?
end
unless formulae_need_bottles?(tap, original_commit, pr_labels, args: args)

View File

@ -1,10 +0,0 @@
# typed: strict
class GitRepository < 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
end

View File

@ -4,13 +4,19 @@
require "utils/git"
require "utils/popen"
# Extensions to {Pathname} for querying Git repository information.
# Given a {Pathname}, provides methods for querying Git repository information.
# @see Utils::Git
# @api private
class GitRepository < SimpleDelegator
class GitRepository
extend T::Sig
alias pathname __getobj__
sig { returns(Pathname) }
attr_reader :pathname
sig { params(pathname: Pathname).void }
def initialize(pathname)
@pathname = pathname
end
sig { returns(T::Boolean) }
def git_repo?

View File

@ -131,7 +131,7 @@ end
require "context"
require "extend/array"
require "extend/git_repository"
require "git_repository"
require "extend/pathname"
require "extend/predicable"
require "extend/module"

View File

@ -391,7 +391,7 @@ class Tap
$stderr.ohai "#{name}: changed remote from #{remote} to #{requested_remote}" unless quiet
end
current_upstream_head = git_repo.origin_branch_name
current_upstream_head = T.must(git_repo.origin_branch_name)
return if requested_remote.blank? && git_repo.origin_has_branch?(current_upstream_head)
args = %w[fetch]
@ -400,7 +400,7 @@ class Tap
safe_system "git", "-C", path, *args
git_repo.set_head_origin_auto
new_upstream_head = git_repo.origin_branch_name
new_upstream_head = T.must(git_repo.origin_branch_name)
return if new_upstream_head == current_upstream_head
git_repo.rename_branch old: current_upstream_head, new: new_upstream_head

View File

@ -13,10 +13,9 @@ module Utils
).returns(T.nilable(String))
}
def self.git_head(repo = Pathname.pwd, length: nil, safe: true)
return git_short_head(repo, length: length) if length.present?
return git_short_head(repo, length: length) if length
repo = GitRepository.new(Pathname(repo))
repo.head_ref(safe: safe)
GitRepository.new(Pathname(repo)).head_ref(safe: safe)
end
# Gets a short commit hash of the HEAD commit.
@ -28,8 +27,7 @@ module Utils
).returns(T.nilable(String))
}
def self.git_short_head(repo = Pathname.pwd, length: nil, safe: true)
repo = GitRepository.new(Pathname(repo))
repo.short_head_ref(length: length, safe: safe)
GitRepository.new(Pathname(repo)).short_head_ref(length: length, safe: safe)
end
# Gets the name of the currently checked-out branch, or HEAD if the repository is in a detached HEAD state.
@ -40,8 +38,7 @@ module Utils
).returns(T.nilable(String))
}
def self.git_branch(repo = Pathname.pwd, safe: true)
repo = GitRepository.new(Pathname(repo))
repo.branch_name(safe: safe)
GitRepository.new(Pathname(repo)).branch_name(safe: safe)
end
# Gets the full commit message of the specified commit, or of the HEAD commit if unspecified.
@ -53,7 +50,6 @@ module Utils
).returns(T.nilable(String))
}
def self.git_commit_message(repo = Pathname.pwd, commit: "HEAD", safe: true)
repo = GitRepository.new(Pathname(repo))
repo.commit_message(commit, safe: safe)
GitRepository.new(Pathname(repo)).commit_message(commit, safe: safe)
end
end