Sha256: 5fcfb40c4c04392be335a3731f4a20a28f75891d2b6e552e279d137deb838f90

Contents?: true

Size: 1.09 KB

Versions: 6

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true

module Geet
  module Services
    # Add the upstream repository to the current repository (configuration).
    #
    class AddUpstreamRepo
      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
        raise "Upstream remote already existing!" if @git_client.remote_defined?(Utils::GitClient::UPSTREAM_NAME)

        parent_path = @repository.remote.parent_path

        if parent_path
          parent_url = compose_parent_url(parent_path)

          @git_client.add_remote(Utils::GitClient::UPSTREAM_NAME, parent_url)
        else
          raise "The repository has no upstream!"
        end
      end

      private

      # Use the same protocol as the main repository.
      #
      def compose_parent_url(parent_path)
        protocol, domain, separator, _, suffix = @git_client.remote_components

        "#{protocol}#{domain}#{separator}#{parent_path}#{suffix}"
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
geet-0.22.0 lib/geet/services/add_upstream_repo.rb
geet-0.21.0 lib/geet/services/add_upstream_repo.rb
geet-0.19.0 lib/geet/services/add_upstream_repo.rb
geet-0.18.0 lib/geet/services/add_upstream_repo.rb
geet-0.17.0 lib/geet/services/add_upstream_repo.rb
geet-0.16.0 lib/geet/services/add_upstream_repo.rb