module Unwrappr::GitCommandRunner

Runs Git commands

Public Class Methods

clone_repository(repo, directory) click to toggle source
# File lib/unwrappr/git_command_runner.rb, line 39
def clone_repository(repo, directory)
  git_wrap { Git.clone(repo, directory) }
end
commit_and_push_changes!() click to toggle source
# File lib/unwrappr/git_command_runner.rb, line 15
def commit_and_push_changes!
  raise 'failed to add git changes' unless stage_all_changes
  raise 'failed to commit changes' unless commit_staged_changes
  raise 'failed to push changes' unless push_current_branch_to_origin
end
create_branch!(base_branch:) click to toggle source
# File lib/unwrappr/git_command_runner.rb, line 10
def create_branch!(base_branch:)
  raise 'Not a git working dir' unless git_dir?
  raise "failed to create branch from '#{base_branch}'" unless checkout_target_branch(base_branch: base_branch)
end
current_branch_name() click to toggle source
# File lib/unwrappr/git_command_runner.rb, line 35
def current_branch_name
  git.current_branch
end
file_exist?(filename) click to toggle source
# File lib/unwrappr/git_command_runner.rb, line 43
def file_exist?(filename)
  !git.ls_files(filename).empty?
end
remote() click to toggle source
# File lib/unwrappr/git_command_runner.rb, line 31
def remote
  git.config('remote.origin.url')
end
reset_client() click to toggle source
# File lib/unwrappr/git_command_runner.rb, line 21
def reset_client
  @git = nil
end
show(revision, path) click to toggle source
# File lib/unwrappr/git_command_runner.rb, line 25
def show(revision, path)
  git.show(revision, path)
rescue Git::GitExecuteError
  nil
end

Private Class Methods

checkout_target_branch(base_branch:) click to toggle source
# File lib/unwrappr/git_command_runner.rb, line 53
def checkout_target_branch(base_branch:)
  timestamp = Time.now.strftime('%Y%m%d-%H%M').freeze
  git_wrap do
    git.checkout(base_branch) unless base_branch.nil?
    git.branch("auto_bundle_update_#{timestamp}").checkout
  end
end
commit_staged_changes() click to toggle source
# File lib/unwrappr/git_command_runner.rb, line 65
def commit_staged_changes
  git_wrap { git.commit('Automatic Bundle Update') }
end
git() click to toggle source
# File lib/unwrappr/git_command_runner.rb, line 73
def git
  if Dir.pwd == @git&.dir&.path
    @git
  else
    Git.open(Dir.pwd, log_options)
  end
end
git_dir?() click to toggle source
# File lib/unwrappr/git_command_runner.rb, line 49
def git_dir?
  git_wrap { !current_branch_name.empty? }
end
git_wrap() { || ... } click to toggle source
# File lib/unwrappr/git_command_runner.rb, line 87
def git_wrap
  yield
  true
rescue Git::GitExecuteError
  false
end
log_options() click to toggle source
# File lib/unwrappr/git_command_runner.rb, line 81
def log_options
  {}.tap do |opt|
    opt[:log] = Logger.new($stdout) if ENV['DEBUG']
  end
end
push_current_branch_to_origin() click to toggle source
# File lib/unwrappr/git_command_runner.rb, line 69
def push_current_branch_to_origin
  git_wrap { git.push('origin', current_branch_name) }
end
stage_all_changes() click to toggle source
# File lib/unwrappr/git_command_runner.rb, line 61
def stage_all_changes
  git_wrap { git.add(all: true) }
end