From 4f02c1c8a8addcc61d0a3f82ccef59429450a55c Mon Sep 17 00:00:00 2001 From: Seeker Date: Tue, 12 Jan 2021 11:04:49 -0800 Subject: [PATCH] utils/git_repository: use `Pathname.pwd` if no `repo` specified --- Library/Homebrew/test/utils/git_repository_spec.rb | 8 ++++++++ Library/Homebrew/utils/git_repository.rb | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/test/utils/git_repository_spec.rb b/Library/Homebrew/test/utils/git_repository_spec.rb index 2bd0508ce2..3d54ce9b85 100644 --- a/Library/Homebrew/test/utils/git_repository_spec.rb +++ b/Library/Homebrew/test/utils/git_repository_spec.rb @@ -20,6 +20,10 @@ describe Utils 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]) + HOMEBREW_CACHE.cd do + expect(described_class.git_head).to eq(head_revision) + expect(described_class.git_head(length: 5)).to eq(head_revision[0...5]) + end end end @@ -27,6 +31,10 @@ describe Utils 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]) + HOMEBREW_CACHE.cd do + expect(described_class.git_short_head).to eq(short_head_revision) + expect(described_class.git_short_head(length: 5)).to eq(head_revision[0...5]) + end end end end diff --git a/Library/Homebrew/utils/git_repository.rb b/Library/Homebrew/utils/git_repository.rb index ce3142fb8c..b22c7dfae8 100644 --- a/Library/Homebrew/utils/git_repository.rb +++ b/Library/Homebrew/utils/git_repository.rb @@ -7,7 +7,7 @@ module Utils 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) + def self.git_head(repo = Pathname.pwd, length: nil, safe: true) return git_short_head(repo, length: length) if length.present? repo = Pathname(repo).extend(GitRepositoryExtension) @@ -17,7 +17,7 @@ module Utils 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) + def self.git_short_head(repo = Pathname.pwd, length: nil, safe: true) repo = Pathname(repo).extend(GitRepositoryExtension) repo.git_short_head(length: length, safe: safe) end