<commit_before>require "open3"

module Magi
  class Repository
    attr_reader :job

    def initialize(job)
      @job = job
    end

    def clone
      validate_existence_of_git_url
      command("git clone #{job.git_url} #{path}")
    end

    def update
      command("cd #{path} && git pull")
    end

    def revision
      command("cd #{path} && git rev-parse HEAD").rstrip
    end

    def updated_since_last_finished_build?
      revision != job.last_finished_build.try(:revision)
    end

    def cloned?
      path.join(".git").exist?
    end

    private

    def command(script)
      Open3.capture3(script)[0]
    end

    def validate_existence_of_git_url
      raise GitUrlNotFound unless job.git_url
    end

    def path
      job.workspace.path + "repository"
    end

    class GitUrlNotFound < StandardError
      def message
        "You must set `git_url`"
      end
    end
  end
end

Job.class_eval do
  property(:git_url)

  before_enqueue do
    if git_url.present?
      repository.clone unless repository.cloned?
      repository.update
      repository.updated_since_last_finished_build?
    end
  end

  before_execute do
    if git_url.present?
      repository.clone unless repository.cloned?
    end
  end

  after_execute do
    if git_url.present?
      current_build.update_revision
    end
  end

  def repository
    @repository ||= Magi::Repository.new(self)
  end
end

Build.class_eval do
  property(:revision)

  def update_revision
    update_properties(revision: job.repository.revision)
  end
end
<commit_msg>Refactor git plugin<commit_after>require "open3"

module Magi
  class Repository
    attr_reader :job

    def initialize(job)
      @job = job
    end

    def before_enqueue
      if has_repository_url?
        clone unless cloned?
        update
        updated_since_last_finished_build?
      end
    end

    def before_execute
      if has_repository_url?
        clone
      end
    end

    def after_execute
      if has_repository_url?
        job.current_build.update_properties(revision: revision)
      end
    end

    def clone
      validate_existence_of_repository_url
      command("git clone #{job.repository_url} #{path}")
    end

    def update
      command("cd #{path} && git pull")
    end

    def revision
      command("cd #{path} && git rev-parse HEAD").rstrip
    end

    def updated_since_last_finished_build?
      revision != job.last_finished_build.try(:revision)
    end

    def cloned?
      path.join(".git").exist?
    end

    def command(script)
      Open3.capture3(script)[0]
    end

    def path
      job.workspace.path + "repository"
    end

    def has_repository_url?
      job.repository_url.present?
    end

    private

    def validate_existence_of_repository_url
      raise RepositoryUrlNotFound unless has_repository_url?
    end

    class RepositoryUrlNotFound < StandardError
      def message
        "You must set `repository_url`"
      end
    end
  end
end

Job.class_eval do
  property(:repository_url)

  before_enqueue { repository.before_enqueue }

  before_execute { repository.before_execute }

  after_execute { repository.after_execute }

  def repository
    @repository ||= Magi::Repository.new(self)
  end
end

Build.property(:revision)

{'fraction_non_alphanumeric': 0.07031488841332925, 'fraction_numerical': 0.0024457352491592784, 'mean_word_length': 2.583789704271632, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1311159', 'n_tokens_mistral': 1027, 'n_tokens_neox': 986, 'n_words': 243}