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

Modernize #6

Merged
merged 1 commit into from
Dec 15, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 7 additions & 14 deletions lib/gitable/scp_uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def self.parse(uri)
# @return [String] The same path passed in.
def path=(new_path)
super
if new_path[0..0] != '/' # addressable adds a / but scp-style uris are altered by this behavior
@path = path.sub(%r|^/+|,'')
if new_path[0] != '/' # addressable adds a / but scp-style uris are altered by this behavior
@path = path.delete_prefix("/")
@normalized_path = nil
validate
end
Expand All @@ -45,14 +45,7 @@ def path=(new_path)
#
# @return [String] The URI as a string.
def to_s
@uri_string ||=
begin
uri_string = "#{normalized_authority}:#{normalized_path}"
if uri_string.respond_to?(:force_encoding)
uri_string.force_encoding(Encoding::UTF_8)
end
uri_string
end
@uri_string ||= "#{normalized_authority}:#{normalized_path}".force_encoding(Encoding::UTF_8)
end
alias to_str to_s

Expand Down Expand Up @@ -82,19 +75,19 @@ def scp?
def validate
return if @validation_deferred

if host.to_s.empty?
if host.nil? || host.empty?
invalid! "Hostname segment missing"
end

if !scheme.to_s.empty?
if scheme && !scheme.empty?
invalid! "Scp style URI must not have a scheme"
end

if !port.to_s.empty?
if port
invalid! "Scp style URI cannot have a port"
end

if path.to_s.empty?
if path.nil? || path.empty?
invalid! "Absolute URI missing hierarchical segment"
end

Expand Down
48 changes: 30 additions & 18 deletions lib/gitable/uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class URI < Addressable::URI
# @raise [Gitable::URI::InvalidURIError] When the uri is *total* rubbish.
#
def self.parse(uri)
return nil if uri.nil?
return uri.dup if uri.kind_of?(self)
return unless uri
return uri.dup if uri.is_a?(self)

# Copied from Addressable to speed up our parsing.
#
Expand All @@ -34,7 +34,7 @@ def self.parse(uri)
uri = uri.to_str
rescue TypeError, NoMethodError
raise TypeError, "Can't convert #{uri.class} into String."
end if not uri.is_a? String
end

# This Regexp supplied as an example in RFC 3986, and it works great.
fragments = uri.scan(URIREGEX)[0]
Expand All @@ -52,14 +52,14 @@ def self.parse(uri)
end

if host.nil? && uri =~ SCP_REGEXP
Gitable::ScpURI.new(:authority => $1, :path => $2)
Gitable::ScpURI.new(authority: $1, path: $2)
else
new(
:scheme => scheme,
:authority => authority,
:path => path,
:query => query,
:fragment => fragment
scheme: scheme,
authority: authority,
path: path,
query: query,
fragment: fragment
)
end
end
Expand Down Expand Up @@ -92,7 +92,8 @@ def self.parse_when_valid(uri)
# @raise [Gitable::URI::InvalidURIError] When the uri is *total* rubbish.
#
def self.heuristic_parse(uri)
return uri if uri.nil? || uri.kind_of?(self)
return unless uri
return uri if uri.is_a?(self)

# Addressable::URI.heuristic_parse _does_ return the correct type :)
gitable = super # boo inconsistency
Expand All @@ -107,21 +108,25 @@ def self.heuristic_parse(uri)
#
# @return [Boolean] github.com is the host?
def github?
!!normalized_host.to_s.match(/\.?github.com$/)
host_match?("github.com")
end

# Is this uri a gitlab uri?
#
# @return [Boolean] gitlab.com is the host?
def gitlab?
!!normalized_host.to_s.match(/\.?gitlab.com$/)
host_match?("gitlab.com")
end

# Is this uri a bitbucket uri?
#
# @return [Boolean] bitbucket.org is the host?
def bitbucket?
!!normalized_host.to_s.match(/\.?bitbucket.org$/)
host_match?("bitbucket.org")
end

def host_match?(host)
normalized_host && normalized_host.include?(host)
end

# Create a web link uri for repositories that follow the github pattern.
Expand All @@ -141,11 +146,16 @@ def to_web_uri(uri_scheme='https')
#
# @return [String] Project name without .git
def project_name
basename.sub(/\.git\/?$/,'')
p = basename.delete_suffix("/")
p.delete_suffix!(".git")
p
end

def org_project
normalized_path.sub(/^\//,'').sub(/\.git\/?$/,'')
op = normalized_path.delete_prefix("/")
op.delete_suffix!("/")
op.delete_suffix!(".git")
op
end

# Detect local filesystem URIs.
Expand All @@ -159,7 +169,9 @@ def local?
#
# @return [Boolean] Is the URI local
def inferred_scheme
if normalized_scheme == 'file' || (normalized_scheme.to_s.empty? && normalized_host.to_s.empty?)
if normalized_scheme == 'file'
'file'
elsif (normalized_scheme.nil? || normalized_scheme.empty?) && (normalized_host.nil? || normalized_host.empty?)
'file'
else
normalized_scheme
Expand All @@ -170,7 +182,7 @@ def inferred_scheme
#
# @return [Boolean] true if the URI uses ssh?
def ssh?
!!normalized_scheme.to_s.match(/ssh/)
!normalized_scheme.nil? && normalized_scheme.include?("ssh")
end

# Is this an scp formatted uri? (No, always)
Expand Down Expand Up @@ -213,7 +225,7 @@ def equivalent?(other_uri)
org_project == other.org_project
else
# if the path is absolute, we can assume it's the same for all users (so the user doesn't have to match).
normalized_path.sub(/\/$/,'') == other.normalized_path.sub(/\/$/,'') &&
normalized_path.delete_suffix("/") == other.normalized_path.delete_suffix("/") &&
(path[0] == '/' || normalized_user == other.normalized_user)
end
end
Expand Down