Skip to content

Commit 040cbdd

Browse files
committed
single logic for known git providers
1 parent eafca30 commit 040cbdd

File tree

2 files changed

+13
-25
lines changed

2 files changed

+13
-25
lines changed

src/dependency_definition.cr

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,12 @@ module Shards
6060
source = value
6161
end
6262

63-
if value.starts_with?("https://github.com")
64-
resolver_key = "github"
65-
uri = URI.parse(value)
63+
uri = URI.parse(value)
64+
if uri.scheme != "file" && uri.host &&
65+
(resolver_key = GitResolver::KNOWN_PROVIDERS[uri.host]?)
6666
source = uri.path[1..-1].rchop(".git") # drop first "/""
6767
end
6868

69-
if value.starts_with?("https://gitlab.com")
70-
resolver_key = "gitlab"
71-
uri = URI.parse(value)
72-
source = uri.path[1..-1].rchop(".git") # drop first "/""
73-
end
74-
75-
if value.starts_with?("https://bitbucket.com")
76-
resolver_key = "bitbucket"
77-
uri = URI.parse(value)
78-
source = uri.path[1..-1] # drop first "/""
79-
end
80-
8169
if value.starts_with?("git://")
8270
resolver_key = "git"
8371
source = value

src/resolvers/git.cr

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,15 @@ module Shards
100100
"git"
101101
end
102102

103-
private KNOWN_PROVIDERS = {
104-
"www.github.com",
105-
"github.com",
106-
"www.bitbucket.com",
107-
"bitbucket.com",
108-
"www.gitlab.com",
109-
"gitlab.com",
110-
"www.codeberg.org",
111-
"codeberg.org",
103+
KNOWN_PROVIDERS = {
104+
"www.github.com" => "github",
105+
"github.com" => "github",
106+
"www.bitbucket.com" => "bitbucket",
107+
"bitbucket.com" => "bitbucket",
108+
"www.gitlab.com" => "gitlab",
109+
"gitlab.com" => "gitlab",
110+
"www.codeberg.org" => "codeberg",
111+
"codeberg.org" => "codeberg",
112112
}
113113

114114
def self.normalize_key_source(key : String, source : String) : {String, String}
@@ -117,7 +117,7 @@ module Shards
117117
uri = URI.parse(source)
118118
downcased_host = uri.host.try &.downcase
119119
scheme = uri.scheme.try &.downcase
120-
if scheme.in?("git", "http", "https") && downcased_host && downcased_host.in?(KNOWN_PROVIDERS)
120+
if scheme.in?("git", "http", "https") && downcased_host && downcased_host.in?(KNOWN_PROVIDERS.keys)
121121
# browsers are requested to enforce HTTP Strict Transport Security
122122
uri.scheme = "https"
123123
downcased_path = uri.path.downcase

0 commit comments

Comments
 (0)