module Berkshelf class GitLocation < Location::ScmLocation set_location_key :git set_valid_options :ref, :branch, :tag, :rel attr_accessor :uri attr_accessor :branch attr_accessor :rel attr_accessor :ref attr_reader :options alias_method :tag, :branch # @param [Dependency] dependency # @param [Hash] options # # @option options [String] :git # the Git URL to clone # @option options [String] :ref # the commit hash or an alias to a commit hash to clone # @option options [String] :branch # same as ref # @option options [String] :tag # same as tag # @option options [String] :rel # the path within the repository to find the cookbook def initialize(dependency, options = {}) super @uri = options[:git] @ref = options[:ref] @branch = options[:branch] || options[:tag] || "master" unless ref @sha = ref @rel = options[:rel] Git.validate_uri!(@uri) end def checkout_info if @sha kind, rev = "ref", @sha else kind, rev = "branch", branch end { :kind => kind, :rev => rev } end # @param [#to_s] destination # # @return [Berkshelf::CachedCookbook] def do_download destination = Berkshelf::CookbookStore.instance.storage_path if cached?(destination) @ref ||= Berkshelf::Git.rev_parse(revision_path(destination)) return local_revision(destination) end Berkshelf::Git.checkout(clone, ref || checkout_info[:rev]) @ref = Berkshelf::Git.rev_parse(clone) tmp_path = rel ? File.join(clone, rel) : clone unless File.chef_cookbook?(tmp_path) msg = "Cookbook '#{dependency.name}' not found at git: #{to_display}" msg << " at path '#{rel}'" if rel raise CookbookNotFound, msg end cb_path = revision_path(destination) FileUtils.rm_rf(cb_path) FileUtils.mv(tmp_path, cb_path) cached = CachedCookbook.from_store_path(cb_path) validate_cached(cached) cached end def to_hash super.tap do |h| h[:value] = self.uri h[:branch] = self.branch if branch end end def to_s "#{self.class.location_key}: #{to_display}" end private def to_display info = checkout_info s = "'#{uri}' with #{info[:kind]}: '#{info[:rev]}'" s << " at ref: '#{ref}'" if ref && (info[:kind] != "ref" || ref != info[:rev]) s end def git @git ||= Berkshelf::Git.new(uri) end def clone tmp_clone = File.join(self.class.tmpdir, uri.gsub(/[\/:]/,'-')) FileUtils.mkdir_p(File.join(File.split(tmp_clone).shift)) unless File.exists?(tmp_clone) Berkshelf::Git.clone(uri, tmp_clone) end tmp_clone end def cached?(destination) revision_path(destination) && File.exists?(revision_path(destination)) end def local_revision(destination) path = revision_path(destination) cached = Berkshelf::CachedCookbook.from_store_path(path) validate_cached(cached) return cached end def revision_path(destination) return unless ref File.join(destination, "#{dependency.name}-#{ref}") end end end