diff --git a/Library/Homebrew/extend/git_repository.rb b/Library/Homebrew/extend/git_repository.rb index 2c366db0bf..2b281f37f0 100644 --- a/Library/Homebrew/extend/git_repository.rb +++ b/Library/Homebrew/extend/git_repository.rb @@ -32,20 +32,21 @@ module GitRepositoryExtension end # Gets the full commit hash of the HEAD commit. - sig { returns(T.nilable(String)) } - def git_head + sig { params(safe: T::Boolean).returns(T.nilable(String)) } + def git_head(safe: false) return if !git? || !Utils::Git.available? - Utils.popen_read(Utils::Git.git, "rev-parse", "--verify", "-q", "HEAD", chdir: self).chomp.presence + Utils.popen_read(Utils::Git.git, "rev-parse", "--verify", "-q", "HEAD", safe: safe, chdir: self).chomp.presence end # Gets a short commit hash of the HEAD commit. - sig { params(length: T.nilable(Integer)).returns(T.nilable(String)) } - def git_short_head(length: nil) + sig { params(length: T.nilable(Integer), safe: T::Boolean).returns(T.nilable(String)) } + def git_short_head(length: nil, safe: false) return if !git? || !Utils::Git.available? + git = Utils::Git.git short_arg = length&.to_s&.prepend("=") - Utils.popen_read(Utils::Git.git, "rev-parse", "--short#{short_arg}", "--verify", "-q", "HEAD", chdir: self) + Utils.popen_read(git, "rev-parse", "--short#{short_arg}", "--verify", "-q", "HEAD", safe: safe, chdir: self) .chomp.presence end diff --git a/Library/Homebrew/utils/git_repository.rb b/Library/Homebrew/utils/git_repository.rb index 70602c3f7c..ce3142fb8c 100644 --- a/Library/Homebrew/utils/git_repository.rb +++ b/Library/Homebrew/utils/git_repository.rb @@ -4,17 +4,21 @@ module Utils extend T::Sig - sig { params(repo: T.any(String, Pathname), length: T.nilable(Integer)).returns(T.nilable(String)) } - def self.git_head(repo, length: nil) + sig do + params(repo: T.any(String, Pathname), length: T.nilable(Integer), safe: T::Boolean).returns(T.nilable(String)) + end + def self.git_head(repo, length: nil, safe: true) return git_short_head(repo, length: length) if length.present? repo = Pathname(repo).extend(GitRepositoryExtension) - repo.git_head + repo.git_head(safe: safe) end - sig { params(repo: T.any(String, Pathname), length: T.nilable(Integer)).returns(T.nilable(String)) } - def self.git_short_head(repo, length: nil) + sig do + params(repo: T.any(String, Pathname), length: T.nilable(Integer), safe: T::Boolean).returns(T.nilable(String)) + end + def self.git_short_head(repo, length: nil, safe: true) repo = Pathname(repo).extend(GitRepositoryExtension) - repo.git_short_head(length: length) + repo.git_short_head(length: length, safe: safe) end end