class Envirobly::Git::Commit

def archive_and_upload(bucket:, credentials:)

def archive_and_upload(bucket:, credentials:)
  `GIT_WORK_TREE="#{@working_dir}" GIT_DIR="#{@working_dir}/.git" git archive --format=tar.gz #{ref} | #{credentials.as_inline_env_vars} aws s3 cp - #{archive_uri(bucket)}`
  $?.success?
end

def archive_uri(bucket)

def archive_uri(bucket)
  "s3://#{bucket}/#{ref}.tar.gz"
end

def exists?

def exists?
  run(%(cat-file -t #{@ref})).strip == "commit"
end

def file_content(path)

def file_content(path)
  run %(show #{@ref}:#{path})
end

def initialize(ref, working_dir: Dir.getwd)

def initialize(ref, working_dir: Dir.getwd)
  @ref = ref
  @working_dir = working_dir
end

def message

def message
  run(%(log #{@ref} -n1 --pretty=%B)).strip
end

def objects_with_checksum_at(path)

def objects_with_checksum_at(path)
  run(%{ls-tree #{@ref} --format='%(objectname) %(path)' #{path}}).lines.map(&:chomp).
    reject { _1.split(" ").last == Envirobly::Config::DIR }
end

def ref

def ref
  @normalized_ref ||= run(%(rev-parse #{@ref})).strip
end

def run(cmd)

def run(cmd)
  @stdout = @stderr = @exit_code = @success = nil
  full_cmd = %(GIT_WORK_TREE="#{@working_dir}" GIT_DIR="#{@working_dir}/.git" git #{cmd})
  Open3.popen3(full_cmd) do |stdin, stdout, stderr, thread|
    stdin.close
    @stdout = stdout.read
    @stderr = stderr.read
    @exit_code = thread.value.exitstatus
    @success = thread.value.success?
  end
  @stdout
end

def time

def time
  Time.parse run(%(log #{@ref} -n1 --date=iso --pretty=format:"%ad"))
end