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

extract: Add the --git-revision argument #17588

Merged
merged 2 commits into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 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 "--git-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
start_rev = args.git_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? ? start_rev : "#{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 @@ -99,13 +102,17 @@ def run
odie "Could not find #{name}! The formula or version may not have existed." if test_formula.nil?
else
# Search in the root directory of `repository` as well as recursively in all of its subdirectories.
files = Dir[repo/"{,**/}"].filter_map do |dir|
Pathname.glob("#{dir}/#{name}.rb").find(&:file?)
files = if start_rev == "HEAD"
Dir[repo/"{,**/}"].filter_map do |dir|
Pathname.glob("#{dir}/#{name}.rb").find(&:file?)
end
else
[]
end

if files.empty?
ohai "Searching repository history"
rev, (path,) = Utils::Git.last_revision_commit_of_files(repo, pattern)
rev, (path,) = Utils::Git.last_revision_commit_of_files(repo, pattern, before_commit: start_rev)
odie "Could not find #{name}! The formula or version may not have existed." if rev.nil?
file = repo/path
version = T.must(formula_at_revision(repo, name, file, rev)).version
Expand Down
3 changes: 3 additions & 0 deletions Library/Homebrew/sorbet/rbi/dsl/homebrew/dev_cmd/extract.rbi

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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], "--git-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

MikeMcQuaid marked this conversation as resolved.
Show resolved Hide resolved
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