module GitService
  class Repo
    def initialize(repo)
      @repo = repo
    end

    def commit(sha)
      GitService::Commit.new(rugged_repo, sha)
    end

    def git_fetch
      require 'rugged'
      rugged_repo.remotes.each do |remote|
        fetch_options = {}

        username = uri_for_remote(remote.url).user
        fetch_options[:credentials] = Credentials.from_ssh_agent(username) if username

        rugged_repo.fetch(remote.name, fetch_options)
      end
    end

    private

    def rugged_repo
      @rugged_repo ||= Rugged::Repository.new(@repo.path.to_s)
    end

    def uri_for_remote(url)
      @remote_uris      ||= {}
      @remote_uris[url] ||= begin
                              if url.start_with?("http", "ssh://")
                                URI(url)
                              elsif url.match?(/\A[-\w:.]+@.*:/) # exp: git@github.com:org/repo
                                URI(url.sub(':', '/').prepend("ssh://"))
                              end
                            end
    end
  end
end

module GitService
  class Repo
    def initialize(repo)
      @repo = repo
    end

    def commit(sha)
      GitService::Commit.new(rugged_repo, sha)
    end

    def git_fetch
      require 'rugged'
      rugged_repo.remotes.each do |remote|
        fetch_options = {}
        username      = uri_for_remote(remote.url).user
        hostname      = uri_for_remote(remote.url).hostname
        credentials   = Credentials.find_for_user_and_host(username, hostname)

        fetch_options[:credentials] = credentials if credentials

        rugged_repo.fetch(remote.name, fetch_options)
      end
    end

    private

    def rugged_repo
      @rugged_repo ||= Rugged::Repository.new(@repo.path.to_s)
    end

    def uri_for_remote(url)
      @remote_uris      ||= {}
      @remote_uris[url] ||= begin
                              if url.start_with?("http", "ssh://")
                                URI(url)
                              elsif url.match?(/\A[-\w:.]+@.*:/) # exp: git@github.com:org/repo
                                URI(url.sub(':', '/').prepend("ssh://"))
                              end
                            end
    end
  end
end

Update .git_fetch to 'find' creds
[GitService::Repo] Update .git_fetch to 'find' creds

Uses GitService::Credentials.find_for_user_and_host when looking up
credentials, and uses .uri_for_remote to also find hostname for a given
remote url.
