Skip to content

Commit 019b82d

Browse files
authored
Update origin_changed? to ignore protocol differences (#315)
1 parent a314835 commit 019b82d

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed

src/resolvers/git.cr

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ module Shards
131131

132132
def local_path
133133
@local_path ||= begin
134-
uri = URI.parse(git_url)
134+
uri = parse_uri(git_url)
135135

136136
path = uri.path.to_s[1..-1]
137137
path = path.gsub('/', File::SEPARATOR) unless File::SEPARATOR == '/'
@@ -242,8 +242,40 @@ module Shards
242242
false
243243
end
244244

245-
private def origin_changed?
246-
(@origin_url ||= capture("git ls-remote --get-url origin").strip) != git_url
245+
private def origin_url
246+
@origin_url ||= capture("git ls-remote --get-url origin").strip
247+
end
248+
249+
# Returns whether origin URLs have differing hosts and/or paths.
250+
protected def origin_changed?
251+
return false if origin_url == git_url
252+
return true if origin_url.nil? || git_url.nil?
253+
254+
origin_parsed = parse_uri(origin_url)
255+
git_parsed = parse_uri(git_url)
256+
257+
(origin_parsed.host != git_parsed.host) || (origin_parsed.path != git_parsed.path)
258+
end
259+
260+
# Parses a URI string, with additional support for ssh+git URI schemes.
261+
private def parse_uri(raw_uri)
262+
# Try normal URI parsing first
263+
uri = URI.parse(raw_uri)
264+
return uri if uri.absolute? && !uri.opaque?
265+
266+
# Otherwise, assume and attempt to parse the scp-style ssh URIs
267+
host, _, path = raw_uri.partition(':')
268+
269+
if host.includes?('@')
270+
user, _, host = host.partition('@')
271+
end
272+
273+
# Normalize leading slash, matching URI parsing
274+
unless path.starts_with?('/')
275+
path = '/' + path
276+
end
277+
278+
URI.new(scheme: "ssh", host: host, path: path, user: user)
247279
end
248280

249281
private def file_exists?(refs, path)

test/git_resolver_test.cr

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,51 @@ module Shards
3838
assert_equal "0.2.0", library.installed_spec.not_nil!.version
3939
end
4040

41+
def test_origin_changed
42+
dependency = Dependency.new("library", {"git" => git_url("library")})
43+
library = GitResolver.new(dependency)
44+
library.install("0.1.2")
45+
46+
# Change the origin in the cache repo to https://github.com/foo/bar
47+
Dir.cd(library.local_path) do
48+
run "git remote set-url origin https://github.com/foo/bar"
49+
end
50+
51+
# All of these alternatives should not trigger origin as changed
52+
same_origins = [
53+
"https://github.com/foo/bar",
54+
"https://github.com:1234/foo/bar",
55+
"http://github.com/foo/bar",
56+
"ssh://github.com/foo/bar",
57+
"git://github.com/foo/bar",
58+
"rsync://github.com/foo/bar",
59+
"[email protected]:foo/bar",
60+
"[email protected]:foo/bar",
61+
"github.com:foo/bar",
62+
]
63+
64+
same_origins.each do |origin|
65+
dependency["git"] = origin
66+
refute library.origin_changed?, origin
67+
end
68+
69+
# These alternatives should all trigger origin as changed
70+
changed_origins = [
71+
"https://github.com/foo/bar2",
72+
"https://github.com/foos/bar",
73+
"https://githubz.com/foo/bar",
74+
"file:///github.com/foo/bar",
75+
"[email protected]:foo/bar2",
76+
"[email protected]:foo/bar",
77+
"",
78+
]
79+
80+
changed_origins.each do |origin|
81+
dependency["git"] = origin
82+
assert library.origin_changed?, origin
83+
end
84+
end
85+
4186
def test_install_refs
4287
skip "TODO: install commit (whatever the version)"
4388
skip "TODO: install branch (whatever the version)"

0 commit comments

Comments
 (0)