Skip to content
Snippets Groups Projects
Commit 49d2e4cb authored by Jonathan Chang's avatar Jonathan Chang
Browse files

utils/git: modernize

parent 9dd5d424
No related branches found
No related tags found
No related merge requests found
# 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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment