Sha256: 264044119679c57278040ec576068104ccf9a45c4c6a6fbfb1bfe89b42658aac

Contents?: true

Size: 1.53 KB

Versions: 10

Compression:

Stored size: 1.53 KB

Contents

module Berkshelf
  class Git
    class << self
      def git(*command)
        out = quietly {
          %x{ #{git_cmd} #{command.join(' ')} }
        }
        error_check
        
        out.chomp
      end

      def clone(uri, destination = Dir.mktmpdir)
        git("clone", uri, destination.to_s)

        error_check

        destination
      end

      def checkout(repo_path, ref)
        Dir.chdir repo_path do
          git("checkout", "-q", ref)
        end
      end

      def rev_parse(repo_path)
        Dir.chdir repo_path do
          git("rev-parse", "HEAD")
        end
      end

      #
      # This is to defeat aliases/shell functions called 'git' and a number of
      # other problems.
      #
      def find_git
        git_path = nil
        ENV["PATH"].split(File::PATH_SEPARATOR).each do |path|
          potential_path = File.join(path, 'git')
          if File.executable?(potential_path)
            git_path = potential_path
            break
          end
          potential_path = File.join(path, 'git.exe')
          if File.executable?(potential_path)
            git_path = potential_path
            break
          end
        end

        unless git_path
          raise "Could not find git. Please ensure it is in your path."
        end

        return git_path
      end

      private

        def git_cmd
          @git_cmd ||= find_git
        end

        def error_check
          raise Berkshelf::GitError, "Did not succeed executing git; check the output above." unless $?.success?
        end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
berkshelf-0.3.3 lib/berkshelf/git.rb
berkshelf-0.3.2 lib/berkshelf/git.rb
berkshelf-0.3.1 lib/berkshelf/git.rb
berkshelf-0.3.0 lib/berkshelf/git.rb
berkshelf-0.2.0 lib/berkshelf/git.rb
berkshelf-0.1.5 lib/berkshelf/git.rb
berkshelf-0.1.4 lib/berkshelf/git.rb
berkshelf-0.1.3 lib/berkshelf/git.rb
berkshelf-0.1.2 lib/berkshelf/git.rb
berkshelf-0.1.1 lib/berkshelf/git.rb