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

create: Fix getting name from GitHub archives #16238

Merged
merged 1 commit into from
Dec 5, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions Library/Homebrew/dev-cmd/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,12 @@
fetch: !args.no_fetch?,
head: args.HEAD?,
)
if fc.name.blank?
stem = Pathname.new(args.named.first).stem.rpartition("=").last
print "Formula name [#{stem}]: "
fc.name = __gets || stem
end

fc.parse_url
# ask for confirmation if name wasn't passed explicitly
if args.set_name.blank?
print "Formula name [#{fc.name}]: "
fc.name = __gets || fc.name

Check warning on line 179 in Library/Homebrew/dev-cmd/create.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/dev-cmd/create.rb#L179

Added line #L179 was not covered by tests
end

fc.verify

Expand Down
44 changes: 28 additions & 16 deletions Library/Homebrew/formula_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,36 @@
raise TapUnavailableError, @tap.name unless @tap.installed?
end

def parse_url
path = Pathname.new(@url)
if @name.nil?
case @url
when %r{github\.com/(\S+)/(\S+)\.git}
@user = Regexp.last_match(1)
@name = Regexp.last_match(2)
@head = true
@github = true
when %r{github\.com/(\S+)/(\S+)/(archive|releases)/}
@user = Regexp.last_match(1)
@name = Regexp.last_match(2)
@github = true
else
@name = path.basename.to_s[/(.*?)[-_.]?#{Regexp.escape(path.version.to_s)}/, 1]
end
def self.name_from_url(url)
stem = Pathname.new(url).stem
# special cases first
if stem.start_with? "index.cgi"
# gitweb URLs e.g. http://www.codesrc.com/gitweb/index.cgi?p=libzipper.git;a=summary
stem.rpartition("=").last
elsif url =~ %r{github\.com/\S+/(\S+)/(archive|releases)/}
# e.g. https://github.com/stella-emu/stella/releases/download/6.7/stella-6.7-src.tar.xz
Regexp.last_match(1)
abitrolly marked this conversation as resolved.
Show resolved Hide resolved
else
# e.g. http://digit-labs.org/files/tools/synscan/releases/synscan-5.02.tar.gz
pathver = Version.parse(stem).to_s
abitrolly marked this conversation as resolved.
Show resolved Hide resolved
stem.sub(/[-_.]?#{Regexp.escape(pathver)}$/, "")
end
end

def parse_url
@name = FormulaCreator.name_from_url(@url) if @name.blank?
abitrolly marked this conversation as resolved.
Show resolved Hide resolved
odebug "name_from_url: #{@name}"
@version = Version.detect(@url) if @version.nil?

case @url
when %r{github\.com/(\S+)/(\S+)\.git}
@user = Regexp.last_match(1)
@head = true
@github = true

Check warning on line 59 in Library/Homebrew/formula_creator.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/formula_creator.rb#L58-L59

Added lines #L58 - L59 were not covered by tests
when %r{github\.com/(\S+)/(\S+)/(archive|releases)/}
@user = Regexp.last_match(1)
@github = true

Check warning on line 62 in Library/Homebrew/formula_creator.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/formula_creator.rb#L62

Added line #L62 was not covered by tests
end
end

def write_formula!
Expand Down
30 changes: 30 additions & 0 deletions Library/Homebrew/test/formula_creator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

require "formula_creator"

describe Homebrew::FormulaCreator do
it "gets name from GitHub archive URL" do
t = described_class.name_from_url("https://github.com/abitrolly/lapce/archive/v0.3.0.tar.gz")
expect(t).to eq("lapce")
end

it "gets name from gitweb URL" do
t = described_class.name_from_url("http://www.codesrc.com/gitweb/index.cgi?p=libzipper.git;a=summary")
expect(t).to eq("libzipper")
end

it "gets name from GitHub repo URL" do
t = described_class.name_from_url("https://github.com/abitrolly/lapce.git")
expect(t).to eq("lapce")
end

it "gets name from GitHub download URL" do
t = described_class.name_from_url("https://github.com/stella-emu/stella/releases/download/6.7/stella-6.7-src.tar.xz")
expect(t).to eq("stella")
end

it "gets name from generic tarball URL" do
t = described_class.name_from_url("http://digit-labs.org/files/tools/synscan/releases/synscan-5.02.tar.gz")
expect(t).to eq("synscan")
end
end
Loading