Skip to content

Commit

Permalink
Update origin_changed? to ignore protocol differences (crystal-lang#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
kalafut authored and taylor committed Aug 11, 2020
1 parent a49abfb commit aa38cd3
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
38 changes: 35 additions & 3 deletions src/resolvers/git.cr
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ module Shards

def local_path
@local_path ||= begin
uri = URI.parse(git_url)
uri = parse_uri(git_url)

path = uri.path.to_s[1..-1]
path = path.gsub('/', File::SEPARATOR) unless File::SEPARATOR == '/'
Expand Down Expand Up @@ -242,8 +242,40 @@ module Shards
false
end

private def origin_changed?
(@origin_url ||= capture("git ls-remote --get-url origin").strip) != git_url
private def origin_url
@origin_url ||= capture("git ls-remote --get-url origin").strip
end

# Returns whether origin URLs have differing hosts and/or paths.
protected def origin_changed?
return false if origin_url == git_url
return true if origin_url.nil? || git_url.nil?

origin_parsed = parse_uri(origin_url)
git_parsed = parse_uri(git_url)

(origin_parsed.host != git_parsed.host) || (origin_parsed.path != git_parsed.path)
end

# Parses a URI string, with additional support for ssh+git URI schemes.
private def parse_uri(raw_uri)
# Try normal URI parsing first
uri = URI.parse(raw_uri)
return uri if uri.absolute? && !uri.opaque?

# Otherwise, assume and attempt to parse the scp-style ssh URIs
host, _, path = raw_uri.partition(':')

if host.includes?('@')
user, _, host = host.partition('@')
end

# Normalize leading slash, matching URI parsing
unless path.starts_with?('/')
path = '/' + path
end

URI.new(scheme: "ssh", host: host, path: path, user: user)
end

private def file_exists?(refs, path)
Expand Down
45 changes: 45 additions & 0 deletions test/git_resolver_test.cr
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,51 @@ module Shards
assert_equal "0.2.0", library.installed_spec.not_nil!.version
end

def test_origin_changed
dependency = Dependency.new("library", {"git" => git_url("library")})
library = GitResolver.new(dependency)
library.install("0.1.2")

# Change the origin in the cache repo to https://github.com/foo/bar
Dir.cd(library.local_path) do
run "git remote set-url origin https://github.com/foo/bar"
end

# All of these alternatives should not trigger origin as changed
same_origins = [
"https://github.com/foo/bar",
"https://github.com:1234/foo/bar",
"http://github.com/foo/bar",
"ssh://github.com/foo/bar",
"git://github.com/foo/bar",
"rsync://github.com/foo/bar",
"git@github.com:foo/bar",
"bob@github.com:foo/bar",
"github.com:foo/bar",
]

same_origins.each do |origin|
dependency["git"] = origin
refute library.origin_changed?, origin
end

# These alternatives should all trigger origin as changed
changed_origins = [
"https://github.com/foo/bar2",
"https://github.com/foos/bar",
"https://githubz.com/foo/bar",
"file:///github.com/foo/bar",
"git@github.com:foo/bar2",
"git@github2.com:foo/bar",
"",
]

changed_origins.each do |origin|
dependency["git"] = origin
assert library.origin_changed?, origin
end
end

def test_install_refs
skip "TODO: install commit (whatever the version)"
skip "TODO: install branch (whatever the version)"
Expand Down

0 comments on commit aa38cd3

Please sign in to comment.