module RepositoryHelper
  module_function

  def create_repository
    @tmp_git_dir = Dir.mktmpdir
    Rugged::Repository.init_at(@tmp_git_dir)
  end

  def delete_repository
    FileUtils.rm_r(@tmp_git_dir)
  end

  def repository_dir
    File.realpath(@tmp_git_dir) + '/'
  end
end

module RepositoryHelper
  module_function

  def create_repository
    @tmp_git_dir = Dir.mktmpdir
    @repo = Rugged::Repository.init_at(@tmp_git_dir)
  end

  def delete_repository
    FileUtils.rm_r(@tmp_git_dir)
  end

  def repository_dir
    File.realpath(@tmp_git_dir) + '/'
  end

  def add_to_index(file_name, blob_content)
    object_id = @repo.write(blob_content, :blob)
    @repo.index.add(path: file_name, oid: object_id, mode: 0100644)
  end

  def create_commit(author_name, author_email, time, subject, body = nil)
    author = { email: author_email, name: author_name, time: time }

    tree = @repo.index.write_tree(@repo)

    Rugged::Commit.create(@repo,
                          author: author,
                          message: "#{subject}\n\n#{body}".strip,
                          committer: author,
                          parents: @repo.empty? ? [] : [@repo.head.target].compact,
                          tree: tree,
                          update_ref: 'HEAD')

    @repo.checkout('master', {strategy: [:force]})
  end
end

Add create_commit and add_to_index methods to RepositoryHelper
Add create_commit and add_to_index methods to RepositoryHelper

#add_to_index provides a way to add files to the repository's index

#create_commit creates a commit off the state of the repository's index

The flow of using the RepositoryHelper is:

 create_repository

 add_to_index  // This can happen many times per commit
 create_commit // Add many commits by repeating #add_to_index and this

 delete_repository
