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: semver aware #6800

Merged
merged 1 commit into from Dec 4, 2019
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
20 changes: 15 additions & 5 deletions Library/Homebrew/dev-cmd/extract.rb
Expand Up @@ -125,24 +125,34 @@ def extract
if args.version
ohai "Searching repository history"
version = args.version
rev = "HEAD"
version_segments = Gem::Version.new(version).segments if Gem::Version.correct?(version)
rev = nil
MikeMcQuaid marked this conversation as resolved.
Show resolved Hide resolved
test_formula = nil
result = ""
loop do
rev, (path,) = Git.last_revision_commit_of_files(repo, pattern, before_commit: "#{rev}~1")
rev = rev.nil? ? "HEAD" : "#{rev}~1"
rev, (path,) = Git.last_revision_commit_of_files(repo, pattern, before_commit: rev)
odie "Could not find #{name}! The formula or version may not have existed." if rev.nil?

file = repo/path
result = Git.last_revision_of_file(repo, file, before_commit: rev)
if result.empty?
odebug "Skipping revision #{rev} - file is empty at this revision" if ARGV.debug?
odebug "Skipping revision #{rev} - file is empty at this revision"
next
end

test_formula = formula_at_revision(repo, name, file, rev)
break if test_formula.nil? || test_formula.version == version

odebug "Trying #{test_formula.version} from revision #{rev} against desired #{version}" if ARGV.debug?
if version_segments && Gem::Version.correct?(test_formula.version)
test_formula_version_segments = Gem::Version.new(test_formula.version).segments
if version_segments.length < test_formula_version_segments.length
odebug "Apply semantic versioning with #{test_formual_version_segments}"
break if version_segments == test_formula_version_segments.first(version_segments.length)
end
end

odebug "Trying #{test_formula.version} from revision #{rev} against desired #{version}"
end
odie "Could not find #{name}! The formula or version may not have existed." if test_formula.nil?
else
Expand Down Expand Up @@ -181,7 +191,7 @@ def extract
brew extract --force --version=#{version} #{name} #{destination_tap.name}
EOS
end
odebug "Overwriting existing formula at #{path}" if ARGV.debug?
odebug "Overwriting existing formula at #{path}"
path.delete
end
ohai "Writing formula for #{name} from revision #{rev} to #{path}"
Expand Down
23 changes: 19 additions & 4 deletions Library/Homebrew/test/dev-cmd/extract_spec.rb
Expand Up @@ -7,7 +7,7 @@
end

describe "brew extract", :integration_test do
it "retrieves the specified version of formula, defaulting to most recent" do
let!(:target) do
path = Tap::TAP_DIRECTORY/"homebrew/homebrew-foo"
(path/"Formula").mkpath
target = Tap.from_path(path)
Expand All @@ -23,12 +23,27 @@
system "git", "add", "--all"
system "git", "commit", "-m", "testball 0.2"
end
{ name: target.name, path: path }
end

expect { brew "extract", "testball", target.name, "--version=0.1" }
it "retrieves the most recent version of formula" do
expect { brew "extract", "testball", target[:name] }
.to be_a_success
expect(target[:path]/"Formula/testball@0.2.rb").to exist
expect(Formulary.factory(target[:path]/"Formula/testball@0.2.rb").version).to be == "0.2"
end

expect(path/"Formula/testball@0.1.rb").to exist
it "retrieves the specified version of formula" do
figroc marked this conversation as resolved.
Show resolved Hide resolved
expect { brew "extract", "testball", target[:name], "--version=0.1" }
.to be_a_success
expect(target[:path]/"Formula/testball@0.1.rb").to exist
expect(Formulary.factory(target[:path]/"Formula/testball@0.1.rb").version).to be == "0.1"
end

expect(Formulary.factory(path/"Formula/testball@0.1.rb").version).to be == "0.1"
it "retrieves the compatible version of formula" do
expect { brew "extract", "testball", target[:name], "--version=0", "--debug" }
.to be_a_success
expect(target[:path]/"Formula/testball@0.rb").to exist
expect(Formulary.factory(target[:path]/"Formula/testball@0.rb").version).to be == "0.2"
end
end