<commit_before>module GitRepository::Commit
  # depends on GitRepository
  extend ActiveSupport::Concern

  # delete a single file and commit the change
  def delete_file(userinfo, target_path, &block)
    commit_file(userinfo, nil, target_path, "Delete file #{target_path}", &block)
  end

  # add a single file and commit the change
  def add_file(userinfo, tmp_path, target_path, message, &block)
    commit_file(userinfo, File.open(tmp_path, 'rb').read, target_path, message, &block)
  end

  # change a single file and commit the change
  def commit_file(userinfo, file_contents, target_path, message, &block)
    # save current head oid in case of an emergency
    old_head = head_oid unless @repo.empty?

    # Entry
    entry = nil
    if file_contents
      entry = {
        type: :blob,
        name: nil,
        oid: Rugged::Blob.create(@repo, file_contents),
        content: file_contents,
        filemode: 33188
      }
    end

    # TreeBuilder
    if @repo.empty?
      old_tree = nil
    else
      old_tree = head.tree
    end

    tree = build_tree(entry, old_tree, target_path.split('/'))

    userinfo.reverse_merge! time: Time.now
    # Commit Sha
    commit_oid = Rugged::Commit.create(@repo, author: userinfo,
      message: message, committer: userinfo, parents: commit_parents, tree: tree)
    rugged_commit = @repo.lookup(commit_oid)

    if @repo.empty?
      ref = Rugged::Reference.create(@repo, 'refs/heads/master', commit_oid)
    else
      @repo.head.target = commit_oid
    end

    # TODO: Use those model operations in the correct place:
    #touch
    #push = build_push(userinfo)
    #commit = build_commit(userinfo, push, rugged_commit)
    #commit.save
    block.call(commit_oid) if block_given?
    commit_oid
  rescue => e
    if old_head
      @repo.head.target = old_head
    else
      @repo.head.delete!
    end

    raise e
  end


  protected

  def build_tree(entry, tree, path_parts)
    builder = Rugged::Tree::Builder.new

    if tree
      old_entry = nil

      tree.each do |e|
        builder.insert(e)
        old_entry = e if e[:name] == path_parts.first<commit_msg>add newline before return value<commit_after>module GitRepository::Commit
  # depends on GitRepository
  extend ActiveSupport::Concern

  # delete a single file and commit the change
  def delete_file(userinfo, target_path, &block)
    commit_file(userinfo, nil, target_path, "Delete file #{target_path}", &block)
  end

  # add a single file and commit the change
  def add_file(userinfo, tmp_path, target_path, message, &block)
    commit_file(userinfo, File.open(tmp_path, 'rb').read, target_path, message, &block)
  end

  # change a single file and commit the change
  def commit_file(userinfo, file_contents, target_path, message, &block)
    # save current head oid in case of an emergency
    old_head = head_oid unless @repo.empty?

    # Entry
    entry = nil
    if file_contents
      entry = {
        type: :blob,
        name: nil,
        oid: Rugged::Blob.create(@repo, file_contents),
        content: file_contents,
        filemode: 33188
      }
    end

    # TreeBuilder
    if @repo.empty?
      old_tree = nil
    else
      old_tree = head.tree
    end

    tree = build_tree(entry, old_tree, target_path.split('/'))

    userinfo.reverse_merge! time: Time.now
    # Commit Sha
    commit_oid = Rugged::Commit.create(@repo, author: userinfo,
      message: message, committer: userinfo, parents: commit_parents, tree: tree)
    rugged_commit = @repo.lookup(commit_oid)

    if @repo.empty?
      ref = Rugged::Reference.create(@repo, 'refs/heads/master', commit_oid)
    else
      @repo.head.target = commit_oid
    end

    # TODO: Use those model operations in the correct place:
    #touch
    #push = build_push(userinfo)
    #commit = build_commit(userinfo, push, rugged_commit)
    #commit.save
    block.call(commit_oid) if block_given?

    commit_oid
  rescue => e
    if old_head
      @repo.head.target = old_head
    else
      @repo.head.delete!
    end

    raise e
  end


  protected

  def build_tree(entry, tree, path_parts)
    builder = Rugged::Tree::Builder.new

    if tree
      old_entry = nil

      tree.each do |e|
        builder.insert(e)
        old_entry = e if e[:name] == path_parts.first
{'fraction_non_alphanumeric': 0.0944587327566051, 'fraction_numerical': 0.002338087444470423, 'mean_word_length': 2.931985294117647, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5143782', 'n_tokens_mistral': 1383, 'n_tokens_neox': 1337, 'n_words': 379}