git_repository: add type signatures and comments
This commit is contained in:
parent
596b4f6278
commit
75be0ca4d0
@ -1,50 +1,70 @@
|
|||||||
# typed: false
|
# typed: strict
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require "utils/git"
|
require "utils/git"
|
||||||
require "utils/popen"
|
require "utils/popen"
|
||||||
|
|
||||||
|
# Extensions to {Pathname} for querying Git repository information.
|
||||||
|
# @see Utils::Git
|
||||||
|
# @api private
|
||||||
module GitRepositoryExtension
|
module GitRepositoryExtension
|
||||||
|
extend T::Sig
|
||||||
|
|
||||||
|
sig { returns(T::Boolean) }
|
||||||
def git?
|
def git?
|
||||||
join(".git").exist?
|
join(".git").exist?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Gets the URL of the Git origin remote.
|
||||||
|
sig { returns(T.nilable(String)) }
|
||||||
def git_origin
|
def git_origin
|
||||||
return unless git? && Utils::Git.available?
|
return unless git? && Utils::Git.available?
|
||||||
|
|
||||||
Utils.popen_read("git", "config", "--get", "remote.origin.url", chdir: self).chomp.presence
|
Utils.popen_read("git", "config", "--get", "remote.origin.url", chdir: self).chomp.presence
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Sets the URL of the Git origin remote.
|
||||||
|
sig { params(origin: String).returns(T.nilable(T::Boolean)) }
|
||||||
def git_origin=(origin)
|
def git_origin=(origin)
|
||||||
return unless git? && Utils::Git.available?
|
return unless git? && Utils::Git.available?
|
||||||
|
|
||||||
safe_system "git", "remote", "set-url", "origin", origin, chdir: self
|
safe_system "git", "remote", "set-url", "origin", origin, chdir: self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Gets the full commit hash of the HEAD commit.
|
||||||
|
sig { returns(T.nilable(String)) }
|
||||||
def git_head
|
def git_head
|
||||||
return unless git? && Utils::Git.available?
|
return unless git? && Utils::Git.available?
|
||||||
|
|
||||||
Utils.popen_read("git", "rev-parse", "--verify", "-q", "HEAD", chdir: self).chomp.presence
|
Utils.popen_read("git", "rev-parse", "--verify", "-q", "HEAD", chdir: self).chomp.presence
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Gets a short commit hash of the HEAD commit.
|
||||||
|
sig { returns(T.nilable(String)) }
|
||||||
def git_short_head
|
def git_short_head
|
||||||
return unless git? && Utils::Git.available?
|
return unless git? && Utils::Git.available?
|
||||||
|
|
||||||
Utils.popen_read("git", "rev-parse", "--short=4", "--verify", "-q", "HEAD", chdir: self).chomp.presence
|
Utils.popen_read("git", "rev-parse", "--short=4", "--verify", "-q", "HEAD", chdir: self).chomp.presence
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Gets the relative date of the last commit, e.g. "1 hour ago"
|
||||||
|
sig { returns(T.nilable(String)) }
|
||||||
def git_last_commit
|
def git_last_commit
|
||||||
return unless git? && Utils::Git.available?
|
return unless git? && Utils::Git.available?
|
||||||
|
|
||||||
Utils.popen_read("git", "show", "-s", "--format=%cr", "HEAD", chdir: self).chomp.presence
|
Utils.popen_read("git", "show", "-s", "--format=%cr", "HEAD", chdir: self).chomp.presence
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Gets the name of the currently checked-out branch, or HEAD if the repository is in a detached HEAD state.
|
||||||
|
sig { returns(T.nilable(String)) }
|
||||||
def git_branch
|
def git_branch
|
||||||
return unless git? && Utils::Git.available?
|
return unless git? && Utils::Git.available?
|
||||||
|
|
||||||
Utils.popen_read("git", "rev-parse", "--abbrev-ref", "HEAD", chdir: self).chomp.presence
|
Utils.popen_read("git", "rev-parse", "--abbrev-ref", "HEAD", chdir: self).chomp.presence
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Returns the date of the last commit, in YYYY-MM-DD format.
|
||||||
|
sig { returns(T.nilable(String)) }
|
||||||
def git_last_commit_date
|
def git_last_commit_date
|
||||||
return unless git? && Utils::Git.available?
|
return unless git? && Utils::Git.available?
|
||||||
|
|
||||||
|
8
Library/Homebrew/extend/git_repository.rbi
Normal file
8
Library/Homebrew/extend/git_repository.rbi
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
# typed: strict
|
||||||
|
|
||||||
|
module GitRepositoryExtension
|
||||||
|
include Kernel
|
||||||
|
|
||||||
|
sig { params(args: T.any(String, Pathname)).returns(Pathname) }
|
||||||
|
def join(*args); end
|
||||||
|
end
|
@ -4,6 +4,7 @@
|
|||||||
module Utils
|
module Utils
|
||||||
# Helper functions for querying Git information.
|
# Helper functions for querying Git information.
|
||||||
#
|
#
|
||||||
|
# @see GitRepositoryExtension
|
||||||
# @api private
|
# @api private
|
||||||
module Git
|
module Git
|
||||||
module_function
|
module_function
|
||||||
|
Loading…
x
Reference in New Issue
Block a user