Sha256: de893a89b8661db9ec76cef2de9caa8c83fb204b73a2bf4e63271961bd76e472

Contents?: true

Size: 1.2 KB

Versions: 23

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

require_relative '../helpers/os_helper'

module Geet
  module Services
    # Open in the browser the current repository.
    #
    class OpenRepo
      include Helpers::OsHelper

      DEFAULT_GIT_CLIENT = Utils::GitClient.new

      def initialize(repository, out: $stdout, git_client: DEFAULT_GIT_CLIENT)
        @repository = repository
        @out = out
        @git_client = git_client
      end

      def execute(upstream: false)
        remote_options = upstream ? {name: Utils::GitClient::UPSTREAM_NAME} : {}

        repo_url = @git_client.remote(**remote_options)
        repo_url = convert_repo_url_to_http_protocol(repo_url)

        open_file_with_default_application(repo_url)

        repo_url
      end

      private

      # The repository URL may be in any of the git/http protocols.
      #
      def convert_repo_url_to_http_protocol(repo_url)
        case repo_url
        when /https:/
        when /git@/
        else
          # Minimal error, due to match guaranteed by GitClient#remote.
          raise
        end

        domain, _, path = repo_url.match(Utils::GitClient::REMOTE_URL_REGEX)[2..4]

        "https://#{domain}/#{path}"
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
geet-0.22.0 lib/geet/services/open_repo.rb
geet-0.21.0 lib/geet/services/open_repo.rb
geet-0.19.0 lib/geet/services/open_repo.rb
geet-0.18.0 lib/geet/services/open_repo.rb
geet-0.17.0 lib/geet/services/open_repo.rb
geet-0.16.0 lib/geet/services/open_repo.rb
geet-0.15.0 lib/geet/services/open_repo.rb
geet-0.14.0 lib/geet/services/open_repo.rb
geet-0.13.0 lib/geet/services/open_repo.rb
geet-0.12.0 lib/geet/services/open_repo.rb
geet-0.11.0 lib/geet/services/open_repo.rb
geet-0.10.0 lib/geet/services/open_repo.rb
geet-0.9.0 lib/geet/services/open_repo.rb
geet-0.8.0 lib/geet/services/open_repo.rb
geet-0.7.0 lib/geet/services/open_repo.rb
geet-0.6.0 lib/geet/services/open_repo.rb
geet-0.5.0 lib/geet/services/open_repo.rb
geet-0.4.4 lib/geet/services/open_repo.rb
geet-0.4.3 lib/geet/services/open_repo.rb
geet-0.4.2 lib/geet/services/open_repo.rb