Sha256: 06ceba707ef116cc879fb72b96b14ac3fc980f7ef7726c3573abcfdfb954ac3f

Contents?: true

Size: 1.97 KB

Versions: 2

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

require 'git'
require 'logger'

module Unwrappr
  # Runs Git commands
  module GitCommandRunner
    class << self
      def create_branch!
        raise 'Not a git working dir' unless git_dir?
        raise 'failed to create branch' unless branch_created?
      end

      def commit_and_push_changes!
        raise 'failed to add git changes' unless git_added_changes?
        raise 'failed to commit changes' unless git_committed?
        raise 'failed to push changes' unless git_pushed?
      end

      def reset_client
        @git = nil
      end

      def show(revision, path)
        git.show(revision, path)
      rescue Git::GitExecuteError
        nil
      end

      def remote
        git.config('remote.origin.url')
      end

      def current_branch_name
        git.current_branch
      end

      def clone_repository(repo, directory)
        git_wrap { Git.clone(repo, directory) }
      end

      def file_exist?(filename)
        !git.ls_files(filename).empty?
      end

      private

      def git_dir?
        git_wrap { !current_branch_name.empty? }
      end

      def branch_created?
        timestamp = Time.now.strftime('%Y%m%d-%H%M').freeze
        git_wrap do
          git.checkout('origin/master')
          git.branch("auto_bundle_update_#{timestamp}").checkout
        end
      end

      def git_added_changes?
        git_wrap { git.add(all: true) }
      end

      def git_committed?
        git_wrap { git.commit('Automatic Bundle Update') }
      end

      def git_pushed?
        git_wrap { git.push('origin', current_branch_name) }
      end

      def git
        if Dir.pwd == @git&.dir&.path
          @git
        else
          Git.open(Dir.pwd, log_options)
        end
      end

      def log_options
        {}.tap do |opt|
          opt[:log] = Logger.new(STDOUT) if ENV['DEBUG']
        end
      end

      def git_wrap
        yield
        true
      rescue Git::GitExecuteError
        false
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
unwrappr-0.4.0 lib/unwrappr/git_command_runner.rb
unwrappr-0.3.5 lib/unwrappr/git_command_runner.rb