# frozen_string_literal: true module Jive class Repo SSH_REGEX = %r{\Agit@(?.+):(?.+)/(?.+)\z}.freeze def initialize(path) @repo = Rugged::Repository.new(path.to_s) end def uri @uri ||= URI.parse(canonical_url) end def canonical_url remote = @repo.remotes.find { |x| x.name == "origin" } return if remote.nil? ssh?(remote) ? url_for_ssh(remote) : url_for(remote) end def nwo segments = uri.path.split("/") "#{segments[1]}/#{segments[2].gsub(".git", "")}" end def branch uri.path[1..-1] end class << self def current @current ||= new(Pathname.pwd) end end private def ssh?(remote) remote.url.match?(SSH_REGEX) end def url_for_ssh(remote) match = remote.url.match(SSH_REGEX) [ "https:/", match[:host], match[:account], match[:repo], @repo.head.name ].join("/") end def url_for(remote) uri = URI.parse(remote.url) [uri.host, uri.path, @repo.head.name].join("/") end end end