Skip to content
Snippets Groups Projects
Commit eefe5bb2 authored by Seeker's avatar Seeker
Browse files

git_repository: add `safe` argument to `git_head`/`git_short_head`

parent 88306d5f
No related branches found
No related tags found
No related merge requests found
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment