diff --git a/Library/Homebrew/.rubocop_todo.yml b/Library/Homebrew/.rubocop_todo.yml index 5b30b296b4..d81dd0b498 100644 --- a/Library/Homebrew/.rubocop_todo.yml +++ b/Library/Homebrew/.rubocop_todo.yml @@ -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' diff --git a/Library/Homebrew/extend/git_repository.rb b/Library/Homebrew/extend/git_repository.rb index e69e6a1a29..3a99e1d4f4 100644 --- a/Library/Homebrew/extend/git_repository.rb +++ b/Library/Homebrew/extend/git_repository.rb @@ -40,11 +40,12 @@ module GitRepositoryExtension end # Gets a short commit hash of the HEAD commit. - sig { returns(T.nilable(String)) } - def git_short_head + sig { params(length: T.nilable(Integer)).returns(T.nilable(String)) } + def git_short_head(length: 4) 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#{length&.to_s&.prepend("=")}", + "--verify", "-q", "HEAD", chdir: self).chomp.presence end # Gets the relative date of the last commit, e.g. "1 hour ago" diff --git a/Library/Homebrew/test/utils/git_repository_spec.rb b/Library/Homebrew/test/utils/git_repository_spec.rb new file mode 100644 index 0000000000..2bd0508ce2 --- /dev/null +++ b/Library/Homebrew/test/utils/git_repository_spec.rb @@ -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 diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb index 59a5581c9a..c63352e562 100644 --- a/Library/Homebrew/utils.rb +++ b/Library/Homebrew/utils.rb @@ -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" diff --git a/Library/Homebrew/utils/git_repository.rb b/Library/Homebrew/utils/git_repository.rb new file mode 100644 index 0000000000..70602c3f7c --- /dev/null +++ b/Library/Homebrew/utils/git_repository.rb @@ -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