module Avmtrf1::Git::PushLarge::LfsCommit::PickSourceRevision

def add_cacheinfo_fail?(result)

def add_cacheinfo_fail?(result)
  !result.fetch(:exit_code).zero? && result.fetch(:stderr).include?('add_cacheinfo')
end

def add_file(path)

def add_file(path)
  infov message('Adding file'), path
  git.execute!('add', path)
end

def bash_editor

def bash_editor
  if source_commit.subject.present?
    'true'
  else
    'bash -c "printf \\"Mensagem original em branco.\\n\\" > $1"'
  end
end

def check_cherry_continue

def check_cherry_continue
  infom message('Continuing cherry pick')
  git.execute!('-c', "core.editor=#{bash_editor}", 'cherry-pick', '--continue')
  raise 'Still in cherry-pick conflict' if cherry_pick_conflict?
  infom message('Cherry pick continue successful')
end

def check_cherry_pick_result(result)

def check_cherry_pick_result(result)
  return if result.fetch(:exit_code).zero?
  return on_cherry_pick_conflict if cherry_pick_conflict?
  return on_add_cacheinfo_fail if add_cacheinfo_fail?(result)
  raise "Cherry pick failed: #{result}"
end

def check_dirty_files

def check_dirty_files
  infom message('Checking dirty files...')
  df = dirty_files
  infov message('Dirty files?'), df.any?
  return unless df.any?
  df.each do |f|
    infov message(f.path, 1), "|#{f.stage}|#{f.dirty}|"
  end
  raise 'There are dirty files'
end

def cherry_pick_conflict?

def cherry_pick_conflict?
  ::File.exist?(::File.join(git, '.git', 'CHERRY_PICK_HEAD'))
end

def cherry_pick_source_revision_uncached

def cherry_pick_source_revision_uncached
  checkout_base
  return if previous_revision.blank?
  infom message("Cherry pick #{source_commit.sha1}")
  result = git.execute('cherry-pick', '--allow-empty', '--allow-empty-message',
                       '-Xtheirs', source_commit.sha1)
  check_cherry_pick_result(result)
end

def dirty_files

def dirty_files
  git.status_files.select { |scf| scf.dirty.present? }
end

def on_add_cacheinfo_fail

def on_add_cacheinfo_fail
  warn message('"add_cacheinfo" fail')
end

def on_cherry_pick_conflict

def on_cherry_pick_conflict
  warn message('Cherry pick failed with conflict')
  resolve_cherry_pick_conflicts
  check_dirty_files
  check_cherry_continue
end

def remove_file(path)

def remove_file(path)
  infov message('Removing file'), path
  git.execute!('rm', path)
end

def resolve_cherry_pick_conflicts

def resolve_cherry_pick_conflicts
  git.status_files.each do |scf|
    if scf.dirty == 'D'
      remove_file(scf.path)
    elsif scf.dirty.present?
      add_file(scf.path)
    end
  end
end