<commit_before># == Schema Information
#
# Table name: commits
#
#  id          :integer          not null, primary key
#  project_id  :integer
#  ref         :string(255)
#  sha         :string(255)
#  before_sha  :string(255)
#  push_data   :text
#  created_at  :datetime
#  updated_at  :datetime
#  tag         :boolean          default(FALSE)
#  yaml_errors :text
#

class Commit < ActiveRecord::Base
  belongs_to :project
  has_many :builds, dependent: :destroy

  serialize :push_data

  validates_presence_of :ref, :sha, :before_sha, :push_data
  validate :valid_commit_sha

  def self.truncate_sha(sha)
    sha[0...8]
  end

  def to_param
    sha
  end

  def last_build
    builds.order(:id).last
  end

  def retry
    builds_without_retry.each do |build|
      Build.retry(build)
    end
  end

  def valid_commit_sha
    if self.sha == Git::BLANK_SHA
      self.errors.add(:sha, " cant be 00000000 (branch removal)")
    end
  end

  def new_branch?
    before_sha == Git::BLANK_SHA
  end

  def compare?
    !new_branch?
  end

  def git_author_name
    commit_data[:author][:name] if commit_data && commit_data[:author]
  end

  def git_author_email
    commit_data[:author][:email] if commit_data && commit_data[:author]
  end

  def git_commit_message
    commit_data[:message] if commit_data && commit_data[:message]
  end

  def short_before_sha
    Commit.truncate_sha(before_sha)
  end

  def short_sha
    Commit.truncate_sha(sha)
  end

  def commit_data
    push_data[:commits].find do |commit|
      commit[:id] == sha
    end
  rescue
    nil
  end

  def project_recipients
    recipients = project.email_recipients.split(' ')

    if project.email_add_pusher? && push_data[:user_email].present?
      recipients << push_data[:user_email]
    end

    recipients.uniq
  end

  def stage
    return unless config_processor
    stages = builds_without_retry.select(&:active?).map(&:stage)
    config_processor.stages.find { |stage| stages.include? stage }
  end

  def create_builds_for_type(stage)
    return if skip_ci?
    return unless config_processor

    builds_attrs = config_processor.builds_for_stage_and_ref(stage, ref, tag)
    builds_attrs.map do |build_attrs|
      builds.create!({
        project: project,
        name: build_attrs[:name],
        commands: build_attrs[:script],
        tag_list: build_attrs[:tags],
        options: build_attrs[:options],
        allow_failure: build_attrs[:allow_failure],
        stage: build_attrs[:stage]
      })
    end
  end

  def create_next_builds
    return if skip_ci?
    return unless config_processor

    stages = builds.group_by(&:stage)

    config_processor.stages.any? do |stage|
      !stages.include?(stage) && create_builds_for_type(stage).present?
    end
  end

  def create_builds
    return if skip_ci?
    return unless config_processor

    config_processor.stages.any? do |stage|
      create_builds_for_type(stage).present?
    end<commit_msg>Rename create_builds_for_type to create_builds_for_stage in Commit model<commit_after># == Schema Information
#
# Table name: commits
#
#  id          :integer          not null, primary key
#  project_id  :integer
#  ref         :string(255)
#  sha         :string(255)
#  before_sha  :string(255)
#  push_data   :text
#  created_at  :datetime
#  updated_at  :datetime
#  tag         :boolean          default(FALSE)
#  yaml_errors :text
#

class Commit < ActiveRecord::Base
  belongs_to :project
  has_many :builds, dependent: :destroy

  serialize :push_data

  validates_presence_of :ref, :sha, :before_sha, :push_data
  validate :valid_commit_sha

  def self.truncate_sha(sha)
    sha[0...8]
  end

  def to_param
    sha
  end

  def last_build
    builds.order(:id).last
  end

  def retry
    builds_without_retry.each do |build|
      Build.retry(build)
    end
  end

  def valid_commit_sha
    if self.sha == Git::BLANK_SHA
      self.errors.add(:sha, " cant be 00000000 (branch removal)")
    end
  end

  def new_branch?
    before_sha == Git::BLANK_SHA
  end

  def compare?
    !new_branch?
  end

  def git_author_name
    commit_data[:author][:name] if commit_data && commit_data[:author]
  end

  def git_author_email
    commit_data[:author][:email] if commit_data && commit_data[:author]
  end

  def git_commit_message
    commit_data[:message] if commit_data && commit_data[:message]
  end

  def short_before_sha
    Commit.truncate_sha(before_sha)
  end

  def short_sha
    Commit.truncate_sha(sha)
  end

  def commit_data
    push_data[:commits].find do |commit|
      commit[:id] == sha
    end
  rescue
    nil
  end

  def project_recipients
    recipients = project.email_recipients.split(' ')

    if project.email_add_pusher? && push_data[:user_email].present?
      recipients << push_data[:user_email]
    end

    recipients.uniq
  end

  def stage
    return unless config_processor
    stages = builds_without_retry.select(&:active?).map(&:stage)
    config_processor.stages.find { |stage| stages.include? stage }
  end

  def create_builds_for_stage(stage)
    return if skip_ci?
    return unless config_processor

    builds_attrs = config_processor.builds_for_stage_and_ref(stage, ref, tag)
    builds_attrs.map do |build_attrs|
      builds.create!({
        project: project,
        name: build_attrs[:name],
        commands: build_attrs[:script],
        tag_list: build_attrs[:tags],
        options: build_attrs[:options],
        allow_failure: build_attrs[:allow_failure],
        stage: build_attrs[:stage]
      })
    end
  end

  def create_next_builds
    return if skip_ci?
    return unless config_processor

    stages = builds.group_by(&:stage)

    config_processor.stages.any? do |stage|
      !stages.include?(stage) && create_builds_for_stage(stage).present?
    end
  end

  def create_builds
    return if skip_ci?
    return unless config_processor

    config_processor.stages.any? do |stage|
      create_builds_for_stage(stage).present?
    end
{'fraction_non_alphanumeric': 0.09051290646999664, 'fraction_numerical': 0.006369426751592357, 'mean_word_length': 3.1408743927827896, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '6137405', 'n_tokens_mistral': 2040, 'n_tokens_neox': 1973, 'n_words': 439}