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

Update origin_changed? to ignore protocol differences #315

Merged
merged 9 commits into from
Feb 9, 2020
Merged
Show file tree
Hide file tree
Changes from 7 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
40 changes: 36 additions & 4 deletions src/resolvers/git.cr
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module Shards
refs = git_refs(version)

if file_exists?(refs, SPEC_FILENAME)
capture("git show #{refs}:#{SPEC_FILENAME}")
capture("git show #{refs}:#{SPEC_FILENAME}")
else
raise Error.new("Missing \"#{refs}:#{SPEC_FILENAME}\" for #{dependency.name.inspect}")
end
Expand Down Expand Up @@ -131,7 +131,7 @@ module Shards

def local_path
@local_path ||= begin
uri = URI.parse(git_url)
uri = parse_git_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
def origin_url
kalafut marked this conversation as resolved.
Show resolved Hide resolved
@origin_url ||= capture("git ls-remote --get-url origin").strip
straight-shoota marked this conversation as resolved.
Show resolved Hide resolved
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_git_uri(origin_url)
git_parsed = parse_git_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_git_uri(raw_uri)
kalafut marked this conversation as resolved.
Show resolved Hide resolved
# 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
43 changes: 43 additions & 0 deletions test/git_resolver_test.cr
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,49 @@ 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",
kalafut marked this conversation as resolved.
Show resolved Hide resolved
"",
]

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