Skip to content

Commit

Permalink
resolve the origin_url by parsing the git config file in the cach…
Browse files Browse the repository at this point in the history
…e dir
  • Loading branch information
GrantBirki committed Sep 3, 2024
1 parent 48b7295 commit a1269a1
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/resolvers/git.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "uri"
require "ini"
require "./resolver"
require "../versions"
require "../logger"
Expand Down Expand Up @@ -358,22 +359,30 @@ module Shards
end

private def origin_url
@origin_url ||= capture("git ls-remote --get-url origin").strip
@origin_url ||= begin
git_config_file = File.join(local_path, "config")

return nil unless File.exists?(git_config_file)

config = INI.parse(File.read(git_config_file))

return config.dig("remote \"origin\"", "url")
end
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 = parse_uri(origin_url.not_nil!)
git_parsed = parse_uri(git_url.not_nil!)

(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)
private def parse_uri(raw_uri : String)
# Need to check for file URIs early, otherwise generic parsing will fail on a colon.
if (path = raw_uri.lchop?("file://"))
return URI.new(scheme: "file", path: path)
Expand Down

0 comments on commit a1269a1

Please sign in to comment.