Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git_repository: raise error instead of returning nil if safe #10346

Merged
merged 1 commit into from Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 22 additions & 9 deletions Library/Homebrew/extend/git_repository.rb
Expand Up @@ -34,20 +34,14 @@ def git_origin=(origin)
# Gets the full commit hash of the HEAD commit.
sig { params(safe: T::Boolean).returns(T.nilable(String)) }
def git_head(safe: false)
return if !git? || !Utils::Git.available?

Utils.popen_read(Utils::Git.git, "rev-parse", "--verify", "-q", "HEAD", safe: safe, chdir: self).chomp.presence
popen_git("rev-parse", "--verify", "-q", "HEAD", safe: safe)
end

# Gets a short commit hash of the HEAD commit.
sig { params(length: T.nilable(Integer), safe: T::Boolean).returns(T.nilable(String)) }
def git_short_head(length: nil, safe: false)
return if !git? || !Utils::Git.available?

git = Utils::Git.git
short_arg = length&.to_s&.prepend("=")
Utils.popen_read(git, "rev-parse", "--short#{short_arg}", "--verify", "-q", "HEAD", safe: safe, chdir: self)
.chomp.presence
short_arg = length.present? ? "--short=#{length}" : "--short"
popen_git("rev-parse", short_arg, "--verify", "-q", "HEAD", safe: safe)
end

# Gets the relative date of the last commit, e.g. "1 hour ago"
Expand Down Expand Up @@ -96,4 +90,23 @@ def git_commit_message(commit = "HEAD")

Utils.popen_read(Utils::Git.git, "log", "-1", "--pretty=%B", commit, "--", chdir: self, err: :out).strip.presence
end

private

sig { params(args: T.untyped, safe: T::Boolean).returns(T.nilable(String)) }
def popen_git(*args, safe: false)
unless git?
return unless safe

raise "Not a Git repository: #{self}"
end

unless Utils::Git.available?
return unless safe

raise "Git is unavailable"
end

T.unsafe(Utils).popen_read(Utils::Git.git, *args, safe: safe, chdir: self).chomp.presence
end
end
62 changes: 60 additions & 2 deletions Library/Homebrew/test/utils/git_repository_spec.rb
Expand Up @@ -17,24 +17,82 @@
let(:short_head_revision) { HOMEBREW_CACHE.cd { `git rev-parse --short HEAD`.chomp } }

describe ".git_head" do
it "returns the revision at HEAD" do
it "returns the revision at HEAD if repo parameter is specified" 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

it "returns the revision at HEAD if repo parameter is omitted" do
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

context "when directory is not a Git repository" do
it "returns nil if `safe` parameter is `false`" do
expect(described_class.git_head(TEST_TMPDIR, safe: false)).to eq(nil)
end

it "raises an error if `safe` parameter is `true`" do
expect { described_class.git_head(TEST_TMPDIR, safe: true) }
.to raise_error("Not a Git repository: #{TEST_TMPDIR}")
end
end

context "when Git is unavailable" do
before do
allow(Utils::Git).to receive(:available?).and_return(false)
end

it "returns nil if `safe` parameter is `false`" do
expect(described_class.git_head(HOMEBREW_CACHE, safe: false)).to eq(nil)
end

it "raises an error if `safe` parameter is `true`" do
expect { described_class.git_head(HOMEBREW_CACHE, safe: true) }
.to raise_error("Git is unavailable")
end
end
end

describe ".git_short_head" do
it "returns the short revision at HEAD" do
it "returns the short revision at HEAD if repo parameter is specified" 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

it "returns the short revision at HEAD if repo parameter is omitted" do
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

context "when directory is not a Git repository" do
it "returns nil if `safe` parameter is `false`" do
expect(described_class.git_short_head(TEST_TMPDIR, safe: false)).to eq(nil)
end

it "raises an error if `safe` parameter is `true`" do
expect { described_class.git_short_head(TEST_TMPDIR, safe: true) }
.to raise_error("Not a Git repository: #{TEST_TMPDIR}")
end
end

context "when Git is unavailable" do
before do
allow(Utils::Git).to receive(:available?).and_return(false)
end

it "returns nil if `safe` parameter is `false`" do
expect(described_class.git_short_head(HOMEBREW_CACHE, safe: false)).to eq(nil)
end

it "raises an error if `safe` parameter is `true`" do
expect { described_class.git_short_head(HOMEBREW_CACHE, safe: true) }
.to raise_error("Git is unavailable")
end
end
end
end
12 changes: 10 additions & 2 deletions Library/Homebrew/utils/git_repository.rb
Expand Up @@ -5,7 +5,11 @@ module Utils
extend T::Sig

sig do
params(repo: T.any(String, Pathname), length: T.nilable(Integer), safe: T::Boolean).returns(T.nilable(String))
params(
repo: T.any(String, Pathname),
length: T.nilable(Integer),
safe: T::Boolean,
).returns(T.nilable(String))
end
def self.git_head(repo = Pathname.pwd, length: nil, safe: true)
return git_short_head(repo, length: length) if length.present?
Expand All @@ -15,7 +19,11 @@ def self.git_head(repo = Pathname.pwd, length: nil, safe: true)
end

sig do
params(repo: T.any(String, Pathname), length: T.nilable(Integer), safe: T::Boolean).returns(T.nilable(String))
params(
repo: T.any(String, Pathname),
length: T.nilable(Integer),
safe: T::Boolean,
).returns(T.nilable(String))
end
def self.git_short_head(repo = Pathname.pwd, length: nil, safe: true)
repo = Pathname(repo).extend(GitRepositoryExtension)
Expand Down