Skip to content

Commit

Permalink
chore: rename git url to remote
Browse files Browse the repository at this point in the history
  • Loading branch information
elbywan committed Jan 27, 2024
1 parent 9b135d6 commit 75a2015
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
16 changes: 8 additions & 8 deletions src/commands/install/protocol/git/resolver.cr
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
require "../base"
require "../resolver"
require "../../../../utils/git/url"
require "../../../../utils/git/remote"

struct Zap::Commands::Install::Protocol::Git < Zap::Commands::Install::Protocol::Base
end

module Zap::Commands::Install::Protocol::Git::Resolver
private abstract struct Base < Zap::Commands::Install::Protocol::Resolver
getter git_url : Utils::Git::Url
getter git_remote : Utils::Git::Remote

def initialize(
state,
Expand All @@ -18,7 +18,7 @@ module Zap::Commands::Install::Protocol::Git::Resolver
skip_cache = false
)
super
@git_url = Utils::Git::Url.new(@specifier.to_s, @state.reporter)
@git_remote = Utils::Git::Remote.new(@specifier.to_s, @state.reporter)
end

def resolve(*, pinned_version : String? = nil) : Package
Expand All @@ -32,8 +32,8 @@ module Zap::Commands::Install::Protocol::Git::Resolver
end

def fetch_metadata : Package
commit_hash = @git_url.commitish_hash
cache_key = Digest::SHA1.hexdigest("#{@git_url.short_key}")
commit_hash = @git_remote.commitish_hash
cache_key = Digest::SHA1.hexdigest("#{@git_remote.short_key}")
metadata_cache_key = "#{@name}__git:#{cache_key}.package.json"
cloned_repo_path = Path.new(Dir.tempdir, cache_key)

Expand All @@ -45,7 +45,7 @@ module Zap::Commands::Install::Protocol::Git::Resolver
clone_to(cloned_repo_path) unless cloned || metadata_cached
Package.init(metadata_cached && metadata_path ? metadata_path : cloned_repo_path, append_filename: !metadata_cached).tap do |pkg|
@state.store.store_file(metadata_cache_key, pkg.to_json) unless metadata_cached
pkg.dist = Package::Dist::Git.new(commit_hash, specifier.to_s, @git_url.key, cache_key)
pkg.dist = Package::Dist::Git.new(commit_hash, specifier.to_s, @git_remote.key, cache_key)
end
end
end
Expand Down Expand Up @@ -89,7 +89,7 @@ module Zap::Commands::Install::Protocol::Git::Resolver
end

protected def clone_to(path : Path | String)
@git_url.clone(path)
@git_remote.clone(path)
end

private def prepare_package(
Expand Down Expand Up @@ -141,7 +141,7 @@ module Zap::Commands::Install::Protocol::Git::Resolver
end

protected def clone_to(path : Path | String)
api_url = "https://api.github.com/repos/#{@raw_version.to_s.split('#')[0]}/tarball/#{@git_url.commitish || ""}"
api_url = "https://api.github.com/repos/#{@raw_version.to_s.split('#')[0]}/tarball/#{@git_remote.commitish || ""}"
tarball_url = HTTP::Client.get(api_url).headers["Location"]?
raise "Failed to fetch package location from Github at #{api_url}" unless tarball_url && !tarball_url.empty?
Utils::TarGzip.download_and_unpack(tarball_url, path)
Expand Down
2 changes: 1 addition & 1 deletion src/utils/git/url.cr → src/utils/git/remote.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "../../reporter/*"

class Zap::Utils::Git::Url
class Zap::Utils::Git::Remote
# See: https://docs.npmjs.com/cli/v9/configuring-npm/package-json#git-urls-as-dependencies
# <protocol>://[<user>[:<password>]@]<hostname>[:<port>][:][/]<path>[#<commit-ish> | #semver:<semver>]
GIT_URL_REGEX = /(?:git\+)?(?<protocol>git|ssh|http|https|file):\/\/(?:(?<user>[^:@]+)?(:(?<password>[^@]+))?@)?(?<hostname>[^:\/]+)(:(?<port>\d+))?[\/:](?<path>[^#]+)((?:#semver:(?<semver>[^:]+))|(?:#(?<commitish>[^:]+)))?/
Expand Down
8 changes: 4 additions & 4 deletions src/utils/git/url_spec.cr → src/utils/git/remote_spec.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "spec"
require "./url"
require "./remote"

describe Zap::Utils::Git::Url do
describe Zap::Utils::Git::Remote do
it("should parse git urls") do
{
"git+ssh://[email protected]:sindresorhus/query-string.git#semver:6",
Expand All @@ -10,11 +10,11 @@ describe Zap::Utils::Git::Url do
"git+https://[email protected]/npm/cli.git",
"git://github.com/npm/cli.git#v1.0.27",
}.each do |url|
Zap::Utils::Git::Url.new(url)
Zap::Utils::Git::Remote.new(url)
end

expect_raises(Exception, "invalid git url: github.com/npm/cli.git") do
Zap::Utils::Git::Url.new("github.com/npm/cli.git")
Zap::Utils::Git::Remote.new("github.com/npm/cli.git")
end
end
end

0 comments on commit 75a2015

Please sign in to comment.