utils/git: new file_at_commit function

This commit is contained in:
Jonathan Chang 2020-09-19 15:21:14 +10:00
parent 04687aef93
commit d09ebf428f
2 changed files with 18 additions and 2 deletions

View File

@ -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

View File

@ -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!