Module: Ace::Git::Atoms::ServerUrl

Defined in:
lib/ace/git/atoms/server_url.rb

Overview

Normalize forge server URLs and remote URLs to a canonical comparable form. Purely textual: no network access, no hostname allow-lists, and no assumptions about any particular forge host.

Supported input shapes:

Canonical form: lowercase "host/path" with any credentials, scheme, trailing slash, and ".git" suffix removed. A colon segment is treated as a port (not an scp separator) only when it is all digits and followed by a slash; everything else is scp-style "host:path".

Class Method Summary collapse

Class Method Details

.match?(server_url, remote_url) ⇒ Boolean

Compare a configured server URL against a remote URL for identity.

Parameters:

  • server_url (String) —

    configured server base URL

  • remote_url (String) —

    git remote URL

Returns:

  • (Boolean) —

    true when both normalize to the same form



46
47
48
49
50
51
52
# File 'lib/ace/git/atoms/server_url.rb', line 46

def self.match?(server_url, remote_url)
  normalized_server = normalize(server_url)
  normalized_remote = normalize(remote_url)
  return false if normalized_server.empty? || normalized_remote.empty?

  normalized_server == normalized_remote
end

.normalize(url) ⇒ String

Normalize a server or remote URL to the canonical comparable form.

Parameters:

  • url (String) —

    server or remote URL

Returns:

  • (String) —

    canonical "host/path" form



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ace/git/atoms/server_url.rb', line 26

def self.normalize(url)
  text = url.to_s.strip
  text = text.sub(%r{\A[a-z][a-z0-9+.\-]*://}i, "")
  text = text.sub(%r{\A[^/@]+@}, "")

  if (port_match = text.match(%r{\A([^/:]+):(\d+)(/.+)\z}))
    text = "#{port_match[1]}:#{port_match[2]}#{port_match[3]}"
  elsif (scp_match = text.match(%r{\A([^/:]+):(.+)\z}))
    text = "#{scp_match[1]}/#{scp_match[2]}"
  end

  text = text.sub(%r{/?\.git/?\z}i, "")
  text.chomp("/").downcase
end