<commit_before>require "logger"

require_relative "storage"
require_relative "uploadable"

module StaticSync
  class Processor

    class ConflictError < StandardError
    end

    def initialize(config, storage = nil)
      @config  = config
      @storage = storage || StaticSync::Storage.new(config)
    end

    def sync
      log.info("Synching #{@config.local_directory} to #{@config.remote_directory}.") if @config.log
      Dir.chdir(@config.local_directory) do
        local_filtered_files.each do |file|
          current_file = Uploadable.new(file, @config)

          unless @storage.has_version?(current_file.version)
            if @storage.has_file?(current_file.version)
              handle_conflict(file)
            else
              log.info("Uploading #{file}") if @config.log
            end
            begin
              @storage.create(current_file.headers)
            rescue => error
              log.error("Failed to upload #{file}")
              raise error
            end
          end
        end
      end
      log.info("Synching done.") if @config.log
    end

    def handle_conflict(file)
      log.info("Overwriting #{file}") if @config.log
      if @config.fail_on_conflict?
        raise ConflictError, "modifications to existing files are not allowed."
      elsif @config.fail_on_conflict_if_cached?
        if file.cached?
          raise ConflictError, "modifications to existing cached files are not allowed."
        end<commit_msg>Pass the correct file handle through to the conflict handler.<commit_after>require "logger"

require_relative "storage"
require_relative "uploadable"

module StaticSync
  class Processor

    class ConflictError < StandardError
    end

    def initialize(config, storage = nil)
      @config  = config
      @storage = storage || StaticSync::Storage.new(config)
    end

    def sync
      log.info("Synching #{@config.local_directory} to #{@config.remote_directory}.") if @config.log
      Dir.chdir(@config.local_directory) do
        local_filtered_files.each do |file|
          current_file = Uploadable.new(file, @config)

          unless @storage.has_version?(current_file.version)
            if @storage.has_file?(current_file.version)
              handle_conflict(current_file)
            else
              log.info("Uploading #{file}") if @config.log
            end
            begin
              @storage.create(current_file.headers)
            rescue => error
              log.error("Failed to upload #{file}")
              raise error
            end
          end
        end
      end
      log.info("Synching done.") if @config.log
    end

    def handle_conflict(file)
      log.info("Overwriting #{file}") if @config.log
      if @config.fail_on_conflict?
        raise ConflictError, "modifications to existing files are not allowed."
      elsif @config.fail_on_conflict_if_cached?
        if file.cached?
          raise ConflictError, "modifications to existing cached files are not allowed."
        end
{'fraction_non_alphanumeric': 0.08774834437086093, 'fraction_numerical': 0.0, 'mean_word_length': 2.308871851040526, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6787480', 'n_tokens_mistral': 828, 'n_tokens_neox': 813, 'n_words': 212}