utils/git: modernize
This commit is contained in:
parent
9dd5d42477
commit
49d2e4cbab
@ -1,7 +1,5 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require "open3"
|
|
||||||
|
|
||||||
module Utils
|
module Utils
|
||||||
# Helper functions for querying Git information.
|
# Helper functions for querying Git information.
|
||||||
#
|
#
|
||||||
@ -16,7 +14,7 @@ module Utils
|
|||||||
def version
|
def version
|
||||||
return @version if defined?(@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
|
@version = status.success? ? stdout.chomp[/git version (\d+(?:\.\d+)*)/, 1] : nil
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -24,7 +22,13 @@ module Utils
|
|||||||
return unless available?
|
return unless available?
|
||||||
return @path if defined?(@path)
|
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
|
end
|
||||||
|
|
||||||
def remote_exists?(url)
|
def remote_exists?(url)
|
||||||
@ -36,6 +40,7 @@ module Utils
|
|||||||
def clear_available_cache
|
def clear_available_cache
|
||||||
remove_instance_variable(:@version) if defined?(@version)
|
remove_instance_variable(:@version) if defined?(@version)
|
||||||
remove_instance_variable(:@path) if defined?(@path)
|
remove_instance_variable(:@path) if defined?(@path)
|
||||||
|
remove_instance_variable(:@git) if defined?(@git)
|
||||||
end
|
end
|
||||||
|
|
||||||
def last_revision_commit_of_file(repo, file, before_commit: nil)
|
def last_revision_commit_of_file(repo, file, before_commit: nil)
|
||||||
@ -45,12 +50,7 @@ module Utils
|
|||||||
[before_commit.split("..").first]
|
[before_commit.split("..").first]
|
||||||
end
|
end
|
||||||
|
|
||||||
out, = Open3.capture3(
|
Utils.popen_read(git, "-C", repo, "log", "--format=%h", "--abbrev=7", "--max-count=1", *args, "--", file).chomp
|
||||||
HOMEBREW_SHIMS_PATH/"scm/git", "-C", repo,
|
|
||||||
"log", "--format=%h", "--abbrev=7", "--max-count=1",
|
|
||||||
*args, "--", file
|
|
||||||
)
|
|
||||||
out.chomp
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def last_revision_commit_of_files(repo, files, before_commit: nil)
|
def last_revision_commit_of_files(repo, files, before_commit: nil)
|
||||||
@ -66,12 +66,11 @@ module Utils
|
|||||||
# <file_path2>
|
# <file_path2>
|
||||||
# ...
|
# ...
|
||||||
# return [<commit_hash>, [file_path1, file_path2, ...]]
|
# return [<commit_hash>, [file_path1, file_path2, ...]]
|
||||||
out, = Open3.capture3(
|
rev, *paths = Utils.popen_read(
|
||||||
HOMEBREW_SHIMS_PATH/"scm/git", "-C", repo, "log",
|
git, "-C", repo, "log",
|
||||||
"--pretty=format:%h", "--abbrev=7", "--max-count=1",
|
"--pretty=format:%h", "--abbrev=7", "--max-count=1",
|
||||||
"--diff-filter=d", "--name-only", *args, "--", *files
|
"--diff-filter=d", "--name-only", *args, "--", *files
|
||||||
)
|
).lines.map(&:chomp).reject(&:empty?)
|
||||||
rev, *paths = out.chomp.split(/\n/).reject(&:empty?)
|
|
||||||
[rev, paths]
|
[rev, paths]
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -79,11 +78,7 @@ module Utils
|
|||||||
relative_file = Pathname(file).relative_path_from(repo)
|
relative_file = Pathname(file).relative_path_from(repo)
|
||||||
|
|
||||||
commit_hash = last_revision_commit_of_file(repo, relative_file, before_commit: before_commit)
|
commit_hash = last_revision_commit_of_file(repo, relative_file, before_commit: before_commit)
|
||||||
out, = Open3.capture3(
|
Utils.popen_read(git, "-C", repo, "show", "#{commit_hash}:#{relative_file}")
|
||||||
HOMEBREW_SHIMS_PATH/"scm/git", "-C", repo,
|
|
||||||
"show", "#{commit_hash}:#{relative_file}"
|
|
||||||
)
|
|
||||||
out
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def ensure_installed!
|
def ensure_installed!
|
||||||
@ -121,7 +116,7 @@ module Utils
|
|||||||
end
|
end
|
||||||
|
|
||||||
def origin_branch(repo)
|
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
|
"refs/remotes/origin/HEAD").chomp.presence
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user