git_repository: add safe argument to git_head/git_short_head
This commit is contained in:
parent
88306d5f5d
commit
eefe5bb295
@ -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
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user