Sha256: c2202c914d2719d7634a8264806d8f883053707f9ce5531359566011024e1a41

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
jive-0.7.0 lib/jive/repo.rb
jive-0.6.0 lib/jive/repo.rb
jive-0.5.0 lib/jive/repo.rb