Skip to content

Commit

Permalink
extract: Add the --revision argument
Browse files Browse the repository at this point in the history
When pinning formula alongside its dependencies it's important to limit
the search scope.
  • Loading branch information
Kentzo committed Jun 28, 2024
1 parent a40f327 commit 5eb9b66
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
7 changes: 5 additions & 2 deletions Library/Homebrew/dev-cmd/extract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class Extract < AbstractCommand
a formula from a tap that is not `homebrew/core` use its fully-qualified form of
<user>`/`<repo>`/`<formula>.
EOS
flag "--revision=",
description: "Search for the specified <version> of <formula> starting at <revision> instead of HEAD."
flag "--version=",
description: "Extract the specified <version> of <formula> instead of the most recent."
switch "-f", "--force",
Expand Down Expand Up @@ -49,6 +51,7 @@ def run
destination_tap.install unless destination_tap.installed?

repo = source_tap.path
default_revision = args.revision || "HEAD"
pattern = if source_tap.core_tap?
[source_tap.new_formula_path(name), repo/"Formula/#{name}.rb"].uniq
else
Expand All @@ -64,7 +67,7 @@ def run
test_formula = T.let(nil, T.nilable(Formula))
result = ""
loop do
rev = rev.nil? ? "HEAD" : "#{rev}~1"
rev = rev.nil? ? default_revision : "#{rev}~1"
rev, (path,) = Utils::Git.last_revision_commit_of_files(repo, pattern, before_commit: rev)
if rev.nil? && source_tap.shallow?
odie <<~EOS
Expand Down Expand Up @@ -112,7 +115,7 @@ def run
result = Utils::Git.last_revision_of_file(repo, file)
else
file = files.fetch(0).realpath
rev = T.let("HEAD", T.nilable(String))
rev = T.let(default_revision, T.nilable(String))
version = Formulary.factory(file).version
result = File.read(file)
end
Expand Down
10 changes: 10 additions & 0 deletions Library/Homebrew/test/dev-cmd/extract_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@
expect(Formulary.factory(path).version).to eq "0.2"
end

it "retrieves the most recent version of formula starting at the specified revision", :integration_test do
path = target[:path]/"Formula/[email protected]"
expect { brew "extract", "testball", target[:name], "--revision=HEAD~1" }
.to output(/^#{path}$/).to_stdout
.and not_to_output.to_stderr
.and be_a_success
expect(path).to exist
expect(Formulary.factory(path).version).to eq "0.1"
end

it "retrieves the specified version of formula", :integration_test do
path = target[:path]/"Formula/[email protected]"
expect { brew "extract", "testball", target[:name], "--version=0.1" }
Expand Down

0 comments on commit 5eb9b66

Please sign in to comment.