<commit_before>require 'svn/client'
require 'svn/repos'

require 'brazil/version_control/svn_generic'

module Brazil::VersionControl
  class SVNRuby < SVNGeneric

    def checkout(checkout_path, vc_revision=nil)
      repos_path = make_revision_path(vc_revision)
      begin
        vc_client.checkout(repos_path, checkout_path, nil)
      rescue Svn::Error => svn_exception
        raise Brazil::VersionControlException, "Could not checkout repository: #{@repository_uri} to path: #{checkout_path} (#{svn_exception})", caller
      end
    end

    def export(export_path, vc_revision=nil)
      repos_path = make_revision_path(vc_revision)
      begin
        vc_client.export(repos_path, export_path, nil)
      rescue Svn::Error => svn_exception
        raise Brazil::VersionControlException, "Could not export repository: #{@repository_uri} to path: #{export_path} (#{svn_exception})", caller
      end
    end

    def update(update_path)
      begin
        vc_client.update(update_path)
      rescue Svn::Error => svn_exception
        raise Brazil::VersionControlException, "Could not update working copy path: #{update_path} (#{svn_exception})", caller
      end
    end

    def get_property(relative_repos_path, property_name, vc_revision=nil)
      repos_path = make_revision_path(vc_revision, relative_repos_path)
      begin
        return vc_client.revprop_get(property_name, repos_path)
      rescue Svn::Error => svn_exception
        raise Brazil::VersionControlException, "Could not get property: #{property_name} on url: #{repos_path}@#{vc_revision.to_s} (#{svn_exception})", caller
      end
    end

    def set_property(relative_repos_path, property_name, property_value, vc_revision=nil)
      repos_path = make_revision_path(vc_revision, relative_repos_path)
      begin
        vc_client.revprop_set(property_name, property_value, repos_path, nil, false)
      rescue Svn::Error => svn_exception
        raise Brazil::VersionControlException, "Could not set property: #{property_name} on url: #{repos_path}@HEAD (#{svn_exception})", caller
      end
    end

    # TODO: Test implementation
#    def mkdir(working_copy_path)
#      begin
#        vc_client.mkdir_p(working_copy_path)
#      rescue Svn::Error => svn_exception
#        raise Brazil::VersionControlException, "Could not create working copy dir: #{working_copy_path} (#{svn_exception})", caller
#      end
#    end

    def add(working_copy_path)
      begin
        vc_client.add(working_copy_path, true, true)
      rescue Svn::Error => svn_exception
        raise Brazil::VersionControlException, "Could not add working copy path: #{working_copy_path} (#{svn_exception})", caller
      end
    end

    def commit(working_copy_path, commit_message)
      begin
        set_commit_message(commit_message)
        vc_client.commit(working_copy_path, false)
      rescue Svn::Error => svn_exception
        raise Brazil::VersionControlException, "Failed to commit working copy path: #{working_copy_path} (#{svn_exception})", caller
      end
    end

    def valid_credentials?
      begin
        vc_client.ls(@repository_uri, nil)
      rescue Svn::Error::RA_NOT_AUTHORIZED
        return false
      rescue Svn::Error => svn_exception
        raise Brazil::VersionControlException, "Could not check credentials on url: #{@repository_uri} (#{svn_exception})", caller
      end

      return true
    end

    protected

    def vc_client
      ctx = Svn::Client::Context.new
      ctx.add_simple_prompt_provider(0) do |cred, realm, username, may_save|
        cred.username = @username
        cred.password = <PASSWORD>
        cred.may_save = false
      end
      return ctx
    end

    def set_commit_message(commit_message)
      begin
        vc_client.set_log_msg_func do |items|<commit_msg>Share subversion client between calls<commit_after>require 'svn/client'
require 'svn/repos'

require 'brazil/version_control/svn_generic'

module Brazil::VersionControl
  class SVNRuby < SVNGeneric

    def checkout(checkout_path, vc_revision=nil)
      repos_path = make_revision_path(vc_revision)
      begin
        vc_client.checkout(repos_path, checkout_path, nil)
      rescue Svn::Error => svn_exception
        raise Brazil::VersionControlException, "Could not checkout repository: #{@repository_uri} to path: #{checkout_path} (#{svn_exception})", caller
      end
    end

    def export(export_path, vc_revision=nil)
      repos_path = make_revision_path(vc_revision)
      begin
        vc_client.export(repos_path, export_path, nil)
      rescue Svn::Error => svn_exception
        raise Brazil::VersionControlException, "Could not export repository: #{@repository_uri} to path: #{export_path} (#{svn_exception})", caller
      end
    end

    def update(update_path)
      begin
        vc_client.update(update_path)
      rescue Svn::Error => svn_exception
        raise Brazil::VersionControlException, "Could not update working copy path: #{update_path} (#{svn_exception})", caller
      end
    end

    def get_property(relative_repos_path, property_name, vc_revision=nil)
      repos_path = make_revision_path(vc_revision, relative_repos_path)
      begin
        return vc_client.revprop_get(property_name, repos_path)
      rescue Svn::Error => svn_exception
        raise Brazil::VersionControlException, "Could not get property: #{property_name} on url: #{repos_path}@#{vc_revision.to_s} (#{svn_exception})", caller
      end
    end

    def set_property(relative_repos_path, property_name, property_value, vc_revision=nil)
      repos_path = make_revision_path(vc_revision, relative_repos_path)
      begin
        vc_client.revprop_set(property_name, property_value, repos_path, nil, false)
      rescue Svn::Error => svn_exception
        raise Brazil::VersionControlException, "Could not set property: #{property_name} on url: #{repos_path}@HEAD (#{svn_exception})", caller
      end
    end

    # TODO: Test implementation
#    def mkdir(working_copy_path)
#      begin
#        vc_client.mkdir_p(working_copy_path)
#      rescue Svn::Error => svn_exception
#        raise Brazil::VersionControlException, "Could not create working copy dir: #{working_copy_path} (#{svn_exception})", caller
#      end
#    end

    def add(working_copy_path)
      begin
        vc_client.add(working_copy_path, true, true)
      rescue Svn::Error => svn_exception
        raise Brazil::VersionControlException, "Could not add working copy path: #{working_copy_path} (#{svn_exception})", caller
      end
    end

    def commit(working_copy_path, commit_message)
      begin
        set_commit_message(commit_message)
        vc_client.commit(working_copy_path, false)
      rescue Svn::Error => svn_exception
        raise Brazil::VersionControlException, "Failed to commit working copy path: #{working_copy_path} (#{svn_exception})", caller
      end
    end

    def valid_credentials?
      begin
        vc_client.ls(@repository_uri, nil)
      rescue Svn::Error::RA_NOT_AUTHORIZED
        return false
      rescue Svn::Error => svn_exception
        raise Brazil::VersionControlException, "Could not check credentials on url: #{@repository_uri} (#{svn_exception})", caller
      end

      return true
    end

    protected

    def vc_client
      unless @vc_client
        ctx = Svn::Client::Context.new
        ctx.add_simple_prompt_provider(0) do |cred, realm, username, may_save|
          cred.username = @username
          cred.password = <PASSWORD>
          cred.may_save = false
        end
        @vc_client = ctx
      end

      return @vc_client
    end

    def set_commit_message(commit_message)
      begin
        vc_client.set_log_msg_func do |items|
{'fraction_non_alphanumeric': 0.08872121764017171, 'fraction_numerical': 0.0002601795238714713, 'mean_word_length': 3.5064478311840563, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5699969', 'n_tokens_mistral': 2372, 'n_tokens_neox': 2307, 'n_words': 536}