Sha256: 0ecf5752a37af51c0412cf2b108b28fa6664b2bd1c9a99e9a94f96e0df91e1b2

Contents?: true

Size: 912 Bytes

Versions: 1

Compression:

Stored size: 912 Bytes

Contents

# frozen_string_literal: true

module Jive
  class Repo
    SSH_REGEX = %r{\Agit@(?<host>.+):(?<account>.+)/(?<repo>.+)\z}.freeze

    def initialize(path)
      @repo = Rugged::Repository.new(path.to_s)
    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

    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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jive-0.4.4 lib/jive/repo.rb