diff --git a/Library/Homebrew/test/utils/git_spec.rb b/Library/Homebrew/test/utils/git_spec.rb index 09bac00fe2..5096700037 100644 --- a/Library/Homebrew/test/utils/git_spec.rb +++ b/Library/Homebrew/test/utils/git_spec.rb @@ -68,6 +68,17 @@ describe Utils::Git do end end + describe "#file_at_commit" do + it "returns file contents when file exists" do + expect(described_class.file_at_commit(HOMEBREW_CACHE, file, file_hash1)).to eq("README") + end + + it "returns empty when file doesn't exist" do + expect(described_class.file_at_commit(HOMEBREW_CACHE, "foo.txt", file_hash1)).to eq("") + expect(described_class.file_at_commit(HOMEBREW_CACHE, "LICENSE.txt", file_hash1)).to eq("") + end + end + describe "#last_revision_commit_of_files" do context "when before_commit is nil" do it "gives last revision commit" do diff --git a/Library/Homebrew/utils/git.rb b/Library/Homebrew/utils/git.rb index f6567a0f4b..93a7c2a30a 100644 --- a/Library/Homebrew/utils/git.rb +++ b/Library/Homebrew/utils/git.rb @@ -76,9 +76,14 @@ module Utils def last_revision_of_file(repo, file, before_commit: nil) relative_file = Pathname(file).relative_path_from(repo) - commit_hash = last_revision_commit_of_file(repo, relative_file, before_commit: before_commit) - Utils.popen_read(git, "-C", repo, "show", "#{commit_hash}:#{relative_file}") + file_at_commit(repo, file, commit_hash) + end + + def file_at_commit(repo, file, commit) + relative_file = Pathname(file) + relative_file = relative_file.relative_path_from(repo) if relative_file.absolute? + Utils.popen_read(git, "-C", repo, "show", "#{commit}:#{relative_file}") end def ensure_installed!