Merge pull request #10245 from SeekingMeaning/git-repository-head
utils: add `git_repository`
This commit is contained in:
commit
7a086689ec
@ -20,6 +20,7 @@ Style/Documentation:
|
||||
- 'utils.rb'
|
||||
- 'utils/fork.rb'
|
||||
- 'utils/gems.rb'
|
||||
- 'utils/git_repository.rb'
|
||||
- 'utils/popen.rb'
|
||||
- 'utils/shell.rb'
|
||||
- 'version.rb'
|
||||
|
||||
@ -18,57 +18,59 @@ module GitRepositoryExtension
|
||||
# Gets the URL of the Git origin remote.
|
||||
sig { returns(T.nilable(String)) }
|
||||
def git_origin
|
||||
return unless git? && Utils::Git.available?
|
||||
return if !git? || !Utils::Git.available?
|
||||
|
||||
Utils.popen_read("git", "config", "--get", "remote.origin.url", chdir: self).chomp.presence
|
||||
Utils.popen_read(Utils::Git.git, "config", "--get", "remote.origin.url", chdir: self).chomp.presence
|
||||
end
|
||||
|
||||
# Sets the URL of the Git origin remote.
|
||||
sig { params(origin: String).returns(T.nilable(T::Boolean)) }
|
||||
def git_origin=(origin)
|
||||
return unless git? && Utils::Git.available?
|
||||
return if !git? || !Utils::Git.available?
|
||||
|
||||
safe_system "git", "remote", "set-url", "origin", origin, chdir: self
|
||||
safe_system Utils::Git.git, "remote", "set-url", "origin", origin, chdir: self
|
||||
end
|
||||
|
||||
# Gets the full commit hash of the HEAD commit.
|
||||
sig { returns(T.nilable(String)) }
|
||||
def git_head
|
||||
return unless git? && Utils::Git.available?
|
||||
return if !git? || !Utils::Git.available?
|
||||
|
||||
Utils.popen_read("git", "rev-parse", "--verify", "-q", "HEAD", chdir: self).chomp.presence
|
||||
Utils.popen_read(Utils::Git.git, "rev-parse", "--verify", "-q", "HEAD", chdir: self).chomp.presence
|
||||
end
|
||||
|
||||
# Gets a short commit hash of the HEAD commit.
|
||||
sig { returns(T.nilable(String)) }
|
||||
def git_short_head
|
||||
return unless git? && Utils::Git.available?
|
||||
sig { params(length: T.nilable(Integer)).returns(T.nilable(String)) }
|
||||
def git_short_head(length: nil)
|
||||
return if !git? || !Utils::Git.available?
|
||||
|
||||
Utils.popen_read("git", "rev-parse", "--short=4", "--verify", "-q", "HEAD", chdir: self).chomp.presence
|
||||
short_arg = length&.to_s&.prepend("=")
|
||||
Utils.popen_read(Utils::Git.git, "rev-parse", "--short#{short_arg}", "--verify", "-q", "HEAD", chdir: self)
|
||||
.chomp.presence
|
||||
end
|
||||
|
||||
# Gets the relative date of the last commit, e.g. "1 hour ago"
|
||||
sig { returns(T.nilable(String)) }
|
||||
def git_last_commit
|
||||
return unless git? && Utils::Git.available?
|
||||
return if !git? || !Utils::Git.available?
|
||||
|
||||
Utils.popen_read("git", "show", "-s", "--format=%cr", "HEAD", chdir: self).chomp.presence
|
||||
Utils.popen_read(Utils::Git.git, "show", "-s", "--format=%cr", "HEAD", chdir: self).chomp.presence
|
||||
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
|
||||
return unless git? && Utils::Git.available?
|
||||
return if !git? || !Utils::Git.available?
|
||||
|
||||
Utils.popen_read("git", "rev-parse", "--abbrev-ref", "HEAD", chdir: self).chomp.presence
|
||||
Utils.popen_read(Utils::Git.git, "rev-parse", "--abbrev-ref", "HEAD", chdir: self).chomp.presence
|
||||
end
|
||||
|
||||
# Gets the name of the default origin HEAD branch.
|
||||
sig { returns(T.nilable(String)) }
|
||||
def git_origin_branch
|
||||
return unless git? && Utils::Git.available?
|
||||
return if !git? || !Utils::Git.available?
|
||||
|
||||
Utils.popen_read("git", "symbolic-ref", "-q", "--short", "refs/remotes/origin/HEAD", chdir: self)
|
||||
Utils.popen_read(Utils::Git.git, "symbolic-ref", "-q", "--short", "refs/remotes/origin/HEAD", chdir: self)
|
||||
.chomp.presence&.split("/")&.last
|
||||
end
|
||||
|
||||
@ -81,16 +83,16 @@ module GitRepositoryExtension
|
||||
# Returns the date of the last commit, in YYYY-MM-DD format.
|
||||
sig { returns(T.nilable(String)) }
|
||||
def git_last_commit_date
|
||||
return unless git? && Utils::Git.available?
|
||||
return if !git? || !Utils::Git.available?
|
||||
|
||||
Utils.popen_read("git", "show", "-s", "--format=%cd", "--date=short", "HEAD", chdir: self).chomp.presence
|
||||
Utils.popen_read(Utils::Git.git, "show", "-s", "--format=%cd", "--date=short", "HEAD", chdir: self).chomp.presence
|
||||
end
|
||||
|
||||
# Gets the full commit message of the specified commit, or of the HEAD commit if unspecified.
|
||||
sig { params(commit: String).returns(T.nilable(String)) }
|
||||
def git_commit_message(commit = "HEAD")
|
||||
return unless git? && Utils::Git.available?
|
||||
return if !git? || !Utils::Git.available?
|
||||
|
||||
Utils.popen_read("git", "log", "-1", "--pretty=%B", commit, "--", chdir: self, err: :out).strip.presence
|
||||
Utils.popen_read(Utils::Git.git, "log", "-1", "--pretty=%B", commit, "--", chdir: self, err: :out).strip.presence
|
||||
end
|
||||
end
|
||||
|
||||
@ -172,7 +172,7 @@ class Tap
|
||||
def git_short_head
|
||||
raise TapUnavailableError, name unless installed?
|
||||
|
||||
path.git_short_head
|
||||
path.git_short_head(length: 4)
|
||||
end
|
||||
|
||||
# Time since last git commit for this {Tap}.
|
||||
|
||||
32
Library/Homebrew/test/utils/git_repository_spec.rb
Normal file
32
Library/Homebrew/test/utils/git_repository_spec.rb
Normal file
@ -0,0 +1,32 @@
|
||||
# typed: false
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "utils/git_repository"
|
||||
|
||||
describe Utils do
|
||||
before do
|
||||
HOMEBREW_CACHE.cd do
|
||||
system "git", "init"
|
||||
Pathname("README.md").write("README")
|
||||
system "git", "add", "README.md"
|
||||
system "git", "commit", "-m", "File added"
|
||||
end
|
||||
end
|
||||
|
||||
let(:head_revision) { HOMEBREW_CACHE.cd { `git rev-parse HEAD`.chomp } }
|
||||
let(:short_head_revision) { HOMEBREW_CACHE.cd { `git rev-parse --short HEAD`.chomp } }
|
||||
|
||||
describe ".git_head" do
|
||||
it "returns the revision at HEAD" do
|
||||
expect(described_class.git_head(HOMEBREW_CACHE)).to eq(head_revision)
|
||||
expect(described_class.git_head(HOMEBREW_CACHE, length: 5)).to eq(head_revision[0...5])
|
||||
end
|
||||
end
|
||||
|
||||
describe ".git_short_head" do
|
||||
it "returns the short revision at HEAD" do
|
||||
expect(described_class.git_short_head(HOMEBREW_CACHE)).to eq(short_head_revision)
|
||||
expect(described_class.git_short_head(HOMEBREW_CACHE, length: 5)).to eq(head_revision[0...5])
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -7,6 +7,7 @@ require "utils/fork"
|
||||
require "utils/formatter"
|
||||
require "utils/gems"
|
||||
require "utils/git"
|
||||
require "utils/git_repository"
|
||||
require "utils/github"
|
||||
require "utils/inreplace"
|
||||
require "utils/link"
|
||||
|
||||
20
Library/Homebrew/utils/git_repository.rb
Normal file
20
Library/Homebrew/utils/git_repository.rb
Normal file
@ -0,0 +1,20 @@
|
||||
# typed: strict
|
||||
# frozen_string_literal: true
|
||||
|
||||
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)
|
||||
return git_short_head(repo, length: length) if length.present?
|
||||
|
||||
repo = Pathname(repo).extend(GitRepositoryExtension)
|
||||
repo.git_head
|
||||
end
|
||||
|
||||
sig { params(repo: T.any(String, Pathname), length: T.nilable(Integer)).returns(T.nilable(String)) }
|
||||
def self.git_short_head(repo, length: nil)
|
||||
repo = Pathname(repo).extend(GitRepositoryExtension)
|
||||
repo.git_short_head(length: length)
|
||||
end
|
||||
end
|
||||
Loading…
x
Reference in New Issue
Block a user