Skip to content

Commit

Permalink
use the git config file for reading origin_url
Browse files Browse the repository at this point in the history
  • Loading branch information
GrantBirki committed Sep 3, 2024
1 parent 5914fb9 commit 483bd94
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 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 @@ -369,8 +370,16 @@ module Shards
false
end

private def origin_url
@origin_url ||= capture("git -C #{local_path} config --get remote.origin.url").strip
private def origin_url : String?
@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.
Expand All @@ -387,14 +396,14 @@ module Shards
return true
end

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 483bd94

Please sign in to comment.