Sha256: 9db31f07a2b5e5d7ef72222ea229bf52e6e0d611155bfed4465c13ca2673ff41

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 KB

Contents

class TerraspaceBundler::Mod::Fetcher
  class Git < Base
    extend Memoist

    def run
      setup_tmp
      org_folder = cache_path(@mod.org_folder)
      FileUtils.mkdir_p(org_folder)

      Dir.chdir(org_folder) do
        logger.debug "Current root dir: #{org_folder}"
        clone unless File.exist?(@mod.repo)

        Dir.chdir(@mod.repo) do
          logger.debug "Change dir: #{@mod.repo}"
          git "pull"
          git "submodule update --init"
          stage
        end
      end
    end
    memoize :run

    def clone
      command = ["git clone", ENV['TB_GIT_CLONE_ARGS'], @mod.url].compact.join(' ')
      sh command
    rescue TB::GitError => e
      logger.error "ERROR: #{e.message}".color(:red)
      exit 1
    end

    def stage
      copy_to_stage
      checkout = @mod.checkout_version || default_branch
      switch_version(checkout)
    end

    # Note: if not in a git repo, will get this error message in stderr
    #
    #    fatal: Not a git repository (or any of the parent directories): .git
    #
    def default_branch
      origin = `git remote show origin`.strip
      found = origin.split("\n").find { |l| l.include?("HEAD") }
      if found
        found.split(':').last.strip
      else
        ENV['TS_GIT_DEFAULT_BRANCH'] || 'master'
      end
    end

    def switch_version(version)
      stage_path = stage_path(rel_dest_dir)
      logger.debug "stage_path #{stage_path}"
      Dir.chdir(stage_path) do
        git "checkout #{version}"
        @sha = git("rev-parse HEAD").strip
      end
    end

    def copy_to_stage
      cache_path = cache_path(rel_dest_dir)
      stage_path = stage_path(rel_dest_dir)
      FileUtils.rm_rf(stage_path)
      FileUtils.mkdir_p(File.dirname(stage_path))
      FileUtils.cp_r(cache_path, stage_path)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
terraspace-bundler-0.5.0 lib/terraspace_bundler/mod/fetcher/git.rb