1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
require "open3"
module Git
module_function
def last_revision_commit_of_file(repo, file, before_commit: nil)
args = [before_commit.nil? ? "--skip=1" : before_commit.split("..").first]
out, = Open3.capture3(
HOMEBREW_SHIMS_PATH/"scm/git", "-C", repo,
"log", "--oneline", "--max-count=1", *args, "--", file
)
out.split(" ").first
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 ||= 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"
).chuzzle
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 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.with_homebrew_gitconfig
with_env(HOME: HOMEBREW_LIBRARY/"Homebrew/gitconfig") do
yield if block_given?
end
end
def self.clear_git_available_cache
@git = 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
end
|