<commit_before>class GitlabMerge
  attr_accessor :project, :merge_request, :user

  def initialize(merge_request, user)
    self.user = user
    self.merge_request = merge_request
    self.project = merge_request.project
  end

  def can_be_merged?
    process do |repo, output|
      !(output =~ /Automatic merge failed/)
    end
  end

  def merge
    process do |repo, output|
      if output =~ /Automatic merge failed/
        false  
      else 
        repo.git.push({}, "origin", merge_request.target_branch)
        true
      end
    end
  end

  def process
    Grit::Git.with_timeout(30.seconds) do 
      File.open(File.join(Rails.root, "tmp", "merge_repo", "#{project.path}.lock"), "w+") do |f|
        f.flock(File::LOCK_EX)
        
        unless project.satellite.exists?
          raise "You should run: rake gitlab:app:enable_automerge"
        end

        project.satellite.clear

        Dir.chdir(project.satellite.path) do
          merge_repo = Grit::Repo.new('.')
          merge_repo.git.sh "git fetch origin"
          merge_repo.git.sh "git config user.name \"#{user.name}\""
          merge_repo.git.sh "git config user.email \"#{user.email}\""
          merge_repo.git.sh "git checkout -b #{merge_request.target_branch} origin/#{merge_request.target_branch}"
          output = merge_repo.git.pull({}, "--no-ff", "origin", merge_request.source_branch)
          yield(merge_repo, output)
        end
      end
    end<commit_msg>Make sure tmp/merge_repo is available to write lock files for auto-merge.<commit_after>class GitlabMerge
  attr_accessor :project, :merge_request, :user

  def initialize(merge_request, user)
    self.user = user
    self.merge_request = merge_request
    self.project = merge_request.project
  end

  def can_be_merged?
    process do |repo, output|
      !(output =~ /Automatic merge failed/)
    end
  end

  def merge
    process do |repo, output|
      if output =~ /Automatic merge failed/
        false  
      else 
        repo.git.push({}, "origin", merge_request.target_branch)
        true
      end
    end
  end

  def process
    Grit::Git.with_timeout(30.seconds) do
      # Make sure tmp/merge_repo exists
      lock_path = File.join(Rails.root, "tmp", "merge_repo")
      FileUtils.mkdir_p(lock_path) unless File.exists?(File.join(Rails.root, "tmp", "merge_repo"))

      File.open(File.join(lock_path, "#{project.path}.lock"), "w+") do |f|
        f.flock(File::LOCK_EX)
        
        unless project.satellite.exists?
          raise "You should run: rake gitlab:app:enable_automerge"
        end

        project.satellite.clear

        Dir.chdir(project.satellite.path) do
          merge_repo = Grit::Repo.new('.')
          merge_repo.git.sh "git fetch origin"
          merge_repo.git.sh "git config user.name \"#{user.name}\""
          merge_repo.git.sh "git config user.email \"#{user.email}\""
          merge_repo.git.sh "git checkout -b #{merge_request.target_branch} origin/#{merge_request.target_branch}"
          output = merge_repo.git.pull({}, "--no-ff", "origin", merge_request.source_branch)
          yield(merge_repo, output)
        end
      end
    end
{'fraction_non_alphanumeric': 0.11835443037974684, 'fraction_numerical': 0.0012658227848101266, 'mean_word_length': 2.52396878483835, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4781144', 'n_tokens_mistral': 1020, 'n_tokens_neox': 991, 'n_words': 228}