From 49d2e4cbabd2cfa878ce4d73400986e5a7d3ed3b Mon Sep 17 00:00:00 2001 From: Jonathan Chang <me@jonathanchang.org> Date: Wed, 16 Sep 2020 22:07:49 +1000 Subject: [PATCH] utils/git: modernize --- Library/Homebrew/utils/git.rb | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/Library/Homebrew/utils/git.rb b/Library/Homebrew/utils/git.rb index 4bb3eb2df5..4180abd88a 100644 --- a/Library/Homebrew/utils/git.rb +++ b/Library/Homebrew/utils/git.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require "open3" - module Utils # Helper functions for querying Git information. # @@ -16,7 +14,7 @@ module Utils def version return @version if defined?(@version) - stdout, _, status = system_command(HOMEBREW_SHIMS_PATH/"scm/git", args: ["--version"], print_stderr: false) + stdout, _, status = system_command(git, args: ["--version"], print_stderr: false) @version = status.success? ? stdout.chomp[/git version (\d+(?:\.\d+)*)/, 1] : nil end @@ -24,7 +22,13 @@ module Utils return unless available? return @path if defined?(@path) - @path = Utils.popen_read(HOMEBREW_SHIMS_PATH/"scm/git", "--homebrew=print-path").chomp.presence + @path = Utils.popen_read(git, "--homebrew=print-path").chomp.presence + end + + def git + return @git if defined?(@git) + + @git = HOMEBREW_SHIMS_PATH/"scm/git" end def remote_exists?(url) @@ -36,6 +40,7 @@ module Utils def clear_available_cache remove_instance_variable(:@version) if defined?(@version) remove_instance_variable(:@path) if defined?(@path) + remove_instance_variable(:@git) if defined?(@git) end def last_revision_commit_of_file(repo, file, before_commit: nil) @@ -45,12 +50,7 @@ module Utils [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 + Utils.popen_read(git, "-C", repo, "log", "--format=%h", "--abbrev=7", "--max-count=1", *args, "--", file).chomp end def last_revision_commit_of_files(repo, files, before_commit: nil) @@ -66,12 +66,11 @@ module Utils # <file_path2> # ... # return [<commit_hash>, [file_path1, file_path2, ...]] - out, = Open3.capture3( - HOMEBREW_SHIMS_PATH/"scm/git", "-C", repo, "log", + rev, *paths = Utils.popen_read( + 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?) + ).lines.map(&:chomp).reject(&:empty?) [rev, paths] end @@ -79,11 +78,7 @@ module Utils 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 + Utils.popen_read(git, "-C", repo, "show", "#{commit_hash}:#{relative_file}") end def ensure_installed! @@ -121,7 +116,7 @@ module Utils end def origin_branch(repo) - Utils.popen_read("git", "-C", repo, "symbolic-ref", "-q", "--short", + Utils.popen_read(git, "-C", repo, "symbolic-ref", "-q", "--short", "refs/remotes/origin/HEAD").chomp.presence end end -- GitLab