Sha256: 471679390401571acb2683886354d12451fda807ccd1542ff8d26eb8ab770cae

Contents?: true

Size: 1.23 KB

Versions: 9

Compression:

Stored size: 1.23 KB

Contents

require 'git'
require 'minimart/download/git_cache'

module Minimart
  module Download
    # GitRepository manages cloning, and checking out a given Git endpoint.
    class GitRepository

      # @return [String] any location that can be cloned by Git (Path, URL).
      attr_reader :location

      # @param [String] location Any location that can be cloned by Git (Path, URL).
      def initialize(location)
        @location = location
      end

      # Fetch the given commit, branch, or tag.
      # @param commitish The commit, branch, or tag to clone for the given location.
      # @yield [Dir] Tmp directory containing the repository. This directory will be removed when the block exits.
      def fetch(commitish, &block)
        Dir.mktmpdir do |tmpdir|
          result_repo = Git.clone(bare_repo_path, tmpdir)
          result_repo.fetch(bare_repo_path, tags: true)
          result_repo.reset_hard(bare_repo.revparse(commitish))
          block.call(tmpdir) if block
        end
      end

      private

      def bare_repo_path
        @bare_repo_path ||= Download::GitCache.instance.local_path_for(location)
      end

      def bare_repo
        @bare_repo ||= Download::GitCache.instance.get_repository(location)
      end

    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
minimart-1.2.5 lib/minimart/download/git_repository.rb
minimart-1.2.4 lib/minimart/download/git_repository.rb
minimart-1.2.3 lib/minimart/download/git_repository.rb
minimart-1.2.0 lib/minimart/download/git_repository.rb
minimart-1.1.6 lib/minimart/download/git_repository.rb
minimart-1.1.3 lib/minimart/download/git_repository.rb
minimart-1.0.2 lib/minimart/download/git_repository.rb
minimart-1.0.1 lib/minimart/download/git_repository.rb
minimart-0.0.1 lib/minimart/download/git_repository.rb