git_repository: add safe argument to git_head/git_short_head

This commit is contained in:
Seeker 2021-01-12 10:29:27 -08:00
parent 88306d5f5d
commit eefe5bb295
2 changed files with 17 additions and 12 deletions

View File

@ -32,20 +32,21 @@ module GitRepositoryExtension
end end
# Gets the full commit hash of the HEAD commit. # Gets the full commit hash of the HEAD commit.
sig { returns(T.nilable(String)) } sig { params(safe: T::Boolean).returns(T.nilable(String)) }
def git_head def git_head(safe: false)
return if !git? || !Utils::Git.available? 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 end
# Gets a short commit hash of the HEAD commit. # Gets a short commit hash of the HEAD commit.
sig { params(length: T.nilable(Integer)).returns(T.nilable(String)) } sig { params(length: T.nilable(Integer), safe: T::Boolean).returns(T.nilable(String)) }
def git_short_head(length: nil) def git_short_head(length: nil, safe: false)
return if !git? || !Utils::Git.available? return if !git? || !Utils::Git.available?
git = Utils::Git.git
short_arg = length&.to_s&.prepend("=") 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 .chomp.presence
end end

View File

@ -4,17 +4,21 @@
module Utils module Utils
extend T::Sig extend T::Sig
sig { params(repo: T.any(String, Pathname), length: T.nilable(Integer)).returns(T.nilable(String)) } sig do
def self.git_head(repo, length: nil) 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? return git_short_head(repo, length: length) if length.present?
repo = Pathname(repo).extend(GitRepositoryExtension) repo = Pathname(repo).extend(GitRepositoryExtension)
repo.git_head repo.git_head(safe: safe)
end end
sig { params(repo: T.any(String, Pathname), length: T.nilable(Integer)).returns(T.nilable(String)) } sig do
def self.git_short_head(repo, length: nil) 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 = Pathname(repo).extend(GitRepositoryExtension)
repo.git_short_head(length: length) repo.git_short_head(length: length, safe: safe)
end end
end end