 2c3b2f68cd
			
		
	
	
		2c3b2f68cd
		
			
		
	
	
	
	
		
			
			- My refactoring of #7933 went wrong in that the tests passed for `brew extract` and my manual testing, but both forgot about third-party taps exist, so that functionality broke as follows (courtesy of Misty). Before: ``` # Git.last_revision_commit_of_files("/usr/local/Homebrew", ["LICENSE.txt", "README.md"]) => ["ac0665d", ["README.md"]] ``` After: ``` # Git.last_revision_commit_of_files("/usr/local/Homebrew", ["LICENSE.txt", "README.md"]) => [nil, []] ``` - While we think about how to do splats in Sorbet, revert this so that users are able to `brew extract` from third party taps again. - A TODO for later in a separate PR is to write a test for `brew extract` that tests the behaviour of third-party taps. --- - Reverted this manually because the GitHub UI couldn't do it. - Arguably I didn't need to remove the RBI file, but it's easier to have everything gone for now and then revert this revert in future once we have a strategy for dealing with splats in Sorbet than have inconsistency.
		
			
				
	
	
		
			118 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
			
		
		
	
	
			118 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
| # frozen_string_literal: true
 | |
| 
 | |
| require "open3"
 | |
| 
 | |
| module Git
 | |
|   module_function
 | |
| 
 | |
|   def last_revision_commit_of_file(repo, file, before_commit: nil)
 | |
|     args = if before_commit.nil?
 | |
|       ["--skip=1"]
 | |
|     else
 | |
|       [before_commit.split("..").first]
 | |
|     end
 | |
| 
 | |
|     out, = Open3.capture3(
 | |
|       HOMEBREW_SHIMS_PATH/"scm/git", "-C", repo,
 | |
|       "log", "--format=%h", "--abbrev=7", "--max-count=1",
 | |
|       *args, "--", file
 | |
|     )
 | |
|     out.chomp
 | |
|   end
 | |
| 
 | |
|   def last_revision_commit_of_files(repo, files, before_commit: nil)
 | |
|     args = if before_commit.nil?
 | |
|       ["--skip=1"]
 | |
|     else
 | |
|       [before_commit.split("..").first]
 | |
|     end
 | |
| 
 | |
|     # git log output format:
 | |
|     #   <commit_hash>
 | |
|     #   <file_path1>
 | |
|     #   <file_path2>
 | |
|     #   ...
 | |
|     # return [<commit_hash>, [file_path1, file_path2, ...]]
 | |
|     out, = Open3.capture3(
 | |
|       HOMEBREW_SHIMS_PATH/"scm/git", "-C", repo, "log",
 | |
|       "--pretty=format:%h", "--abbrev=7", "--max-count=1",
 | |
|       "--diff-filter=d", "--name-only", *args, "--", *files
 | |
|     )
 | |
|     rev, *paths = out.chomp.split(/\n/).reject(&:empty?)
 | |
|     [rev, paths]
 | |
|   end
 | |
| 
 | |
|   def last_revision_of_file(repo, file, before_commit: nil)
 | |
|     relative_file = Pathname(file).relative_path_from(repo)
 | |
| 
 | |
|     commit_hash = last_revision_commit_of_file(repo, relative_file, before_commit: before_commit)
 | |
|     out, = Open3.capture3(
 | |
|       HOMEBREW_SHIMS_PATH/"scm/git", "-C", repo,
 | |
|       "show", "#{commit_hash}:#{relative_file}"
 | |
|     )
 | |
|     out
 | |
|   end
 | |
| end
 | |
| 
 | |
| module Utils
 | |
|   def self.git_available?
 | |
|     @git_available ||= quiet_system HOMEBREW_SHIMS_PATH/"scm/git", "--version"
 | |
|   end
 | |
| 
 | |
|   def self.git_path
 | |
|     return unless git_available?
 | |
| 
 | |
|     @git_path ||= Utils.popen_read(
 | |
|       HOMEBREW_SHIMS_PATH/"scm/git", "--homebrew=print-path"
 | |
|     ).chomp.presence
 | |
|   end
 | |
| 
 | |
|   def self.git_version
 | |
|     return unless git_available?
 | |
| 
 | |
|     @git_version ||= Utils.popen_read(
 | |
|       HOMEBREW_SHIMS_PATH/"scm/git", "--version"
 | |
|     ).chomp[/git version (\d+(?:\.\d+)*)/, 1]
 | |
|   end
 | |
| 
 | |
|   def self.ensure_git_installed!
 | |
|     return if git_available?
 | |
| 
 | |
|     # we cannot install brewed git if homebrew/core is unavailable.
 | |
|     if CoreTap.instance.installed?
 | |
|       begin
 | |
|         oh1 "Installing #{Formatter.identifier("git")}"
 | |
|         safe_system HOMEBREW_BREW_FILE, "install", "git"
 | |
|       rescue
 | |
|         raise "Git is unavailable"
 | |
|       end
 | |
|     end
 | |
| 
 | |
|     raise "Git is unavailable" unless git_available?
 | |
|   end
 | |
| 
 | |
|   def self.clear_git_available_cache
 | |
|     @git_available = nil
 | |
|     @git_path = nil
 | |
|     @git_version = nil
 | |
|   end
 | |
| 
 | |
|   def self.git_remote_exists?(url)
 | |
|     return true unless git_available?
 | |
| 
 | |
|     quiet_system "git", "ls-remote", url
 | |
|   end
 | |
| 
 | |
|   def self.set_git_name_email!(author: true, committer: true)
 | |
|     if Homebrew::EnvConfig.git_name
 | |
|       ENV["GIT_AUTHOR_NAME"] = Homebrew::EnvConfig.git_name if author
 | |
|       ENV["GIT_COMMITTER_NAME"] = Homebrew::EnvConfig.git_name if committer
 | |
|     end
 | |
| 
 | |
|     return unless Homebrew::EnvConfig.git_email
 | |
| 
 | |
|     ENV["GIT_AUTHOR_EMAIL"] = Homebrew::EnvConfig.git_email if author
 | |
|     ENV["GIT_COMMITTER_EMAIL"] = Homebrew::EnvConfig.git_email if committer
 | |
|   end
 | |
| end
 |