<commit_before>require 'securerandom'

class CommitService
  class PreReceiveError < StandardError; end
  class CommitError < StandardError; end

  def self.transaction(project, current_user, ref)
    repository = project.repository
    path_to_repo = repository.path_to_repo
    empty_repo = repository.empty?

    # Create temporary ref
    random_string = SecureRandom.hex
    tmp_ref = "refs/tmp/#{random_string}/head"

    unless empty_repo
      target = repository.find_branch(ref).target
      repository.rugged.references.create(tmp_ref, target)
    end

    # Make commit in tmp ref
    sha = yield(tmp_ref)

    unless sha
      raise CommitError.new('Failed to create commit')
    end

    # Run GitLab pre-receive hook
    status = PreCommitService.new(project, current_user).execute(sha, ref)

    if status
      if empty_repo
        # Create branch
        repository.rugged.references.create(Gitlab::Git::BRANCH_REF_PREFIX + ref, sha)
      else
        # Update head
        repository.rugged.references.update(Gitlab::Git::BRANCH_REF_PREFIX + ref, sha)
      end

      # Run GitLab post receive hook
      PostCommitService.new(project, current_user).execute(sha, ref)
    else
      # Remove tmp ref and return error to user
      repository.rugged.references.delete(tmp_ref)
<commit_msg>Make sure target has not changed during pre-receive hook<commit_after>require 'securerandom'

class CommitService
  class PreReceiveError < StandardError; end
  class CommitError < StandardError; end

  def self.transaction(project, current_user, ref)
    repository = project.repository
    path_to_repo = repository.path_to_repo
    empty_repo = repository.empty?

    # Create temporary ref
    random_string = SecureRandom.hex
    tmp_ref = "refs/tmp/#{random_string}/head"

    unless empty_repo
      target = repository.find_branch(ref).target
      repository.rugged.references.create(tmp_ref, target)
    end

    # Make commit in tmp ref
    sha = yield(tmp_ref)

    unless sha
      raise CommitError.new('Failed to create commit')
    end

    # Run GitLab pre-receive hook
    status = PreCommitService.new(project, current_user).execute(sha, ref)

    if status
      if empty_repo
        # Create branch
        repository.rugged.references.create(Gitlab::Git::BRANCH_REF_PREFIX + ref, sha)
      else
        # Update head
        current_target = repository.find_branch(ref).target

        # Make sure target branch was not changed during pre-receive hook
        if current_target == target
          repository.rugged.references.update(Gitlab::Git::BRANCH_REF_PREFIX + ref, sha)
        else
          raise CommitError.new('Commit was rejected because branch received new push')
        end
      end

      # Run GitLab post receive hook
      PostCommitService.new(project, current_user).execute(sha, ref)
    else
      # Remove tmp ref and return error to user
      repository.rugged.references.delete(tmp_ref)

{'fraction_non_alphanumeric': 0.07494065785011868, 'fraction_numerical': 0.0, 'mean_word_length': 3.1086350974930363, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '1530017', 'n_tokens_mistral': 863, 'n_tokens_neox': 818, 'n_words': 220}